Skip to content

Commit

Permalink
Fix SAMKeyEntryComparer and add related unit test
Browse files Browse the repository at this point in the history
Fix new Codacy warnings
  • Loading branch information
Maxhy committed Oct 19, 2023
1 parent 88031ef commit 5e0a426
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 10 deletions.
2 changes: 1 addition & 1 deletion KeyManager.Library.KeyStore.File/FileKeyStoreArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Leosac.KeyManager.Library.KeyStore.File
{
public class FileKeyStoreArchive
public static class FileKeyStoreArchive
{
public static void Import(string fileName, FileKeyStore keyStore)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public PKCS11KeyStoreProperties? PKCS11Properties

public ObservableCollection<CKU> UserTypes { get; private set; }

public RelayCommand BrowseCommand;
public RelayCommand BrowseCommand { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<RootNamespace>Leosac.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\KeyManager.Library.KeyStore.NXP_SAM\KeyManager.Library.KeyStore.NXP_SAM.csproj" />
<ProjectReference Include="..\KeyManager.Library\KeyManager.Library.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Leosac.KeyManager.Library.KeyStore.NXP_SAM.Tests
{
[TestClass]
public class SAMKeyEntryComparerTests
{
[TestMethod]
public void Test_ListOrder()
{
var comparer = new SAMKeyEntryComparer(new SAMKeyStoreProperties
{
AuthenticateKeyEntryIdentifier = 0,
AuthenticateKeyVersion = 0
});

var list = new List<SAMSymmetricKeyEntry>(new[]
{
new SAMSymmetricKeyEntry { Identifier = new KeyEntryId("0") },
new SAMSymmetricKeyEntry { Identifier = new KeyEntryId("1") },
new SAMSymmetricKeyEntry { Identifier = new KeyEntryId("2") }
});
var orderedList = list.Order(comparer);

Assert.AreEqual("1", orderedList.ElementAt(0).Identifier.Id);
Assert.AreEqual("2", orderedList.ElementAt(1).Identifier.Id);
Assert.AreEqual("0", orderedList.ElementAt(2).Identifier.Id);
}
}
}
1 change: 1 addition & 0 deletions KeyManager.Library.KeyStore.NXP_SAM.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
2 changes: 1 addition & 1 deletion KeyManager.Library.KeyStore.NXP_SAM/ISLOG/ISLOGKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public override async Task Open()
// We create LLA objects to be able to use Key Entry helpers for SET/ExtSET decoding
var llaKeInfo = new LibLogicalAccess.Card.KeyEntryAV2Information
{
set = new byte[]
set = new[]
{
byte.Parse(setEl.Attribute("set0")?.Value ?? "0"),
byte.Parse(setEl.Attribute("set1")?.Value ?? "0")
Expand Down
5 changes: 5 additions & 0 deletions KeyManager.Library.KeyStore.NXP_SAM/SAMKeyEntryComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public int Compare(IChangeKeyEntry? x, IChangeKeyEntry? y)

if (y is SAMSymmetricKeyEntry ky)
{
if (ky.Identifier.Id == _properties.AuthenticateKeyEntryIdentifier.ToString())
{
return -1;
}

try
{
return int.Parse(kx.Identifier.Id ?? "0") - int.Parse(ky.Identifier.Id ?? "0");
Expand Down
11 changes: 6 additions & 5 deletions KeyManager.Library/Key.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Leosac.KeyManager.Library.Policy;
using System.Collections.ObjectModel;
using System.Text;

namespace Leosac.KeyManager.Library
{
Expand Down Expand Up @@ -127,27 +128,27 @@ public void ValidatePolicies(string value)

public string? GetAggregatedValueString(KeyValueStringFormat format, string? delimiter)
{
string? ret = null;
StringBuilder? ret = null;
foreach (var m in Materials)
{
var v = m.GetValueString(format);
if (ret != null)
{
if (delimiter != null)
{
ret += delimiter;
ret.Append(delimiter);
}
if (v != null)
{
ret += v;
ret.Append(v);
}
}
else
{
ret = v ?? string.Empty;
ret = new StringBuilder(v ?? string.Empty);
}
}
return ret;
return ret?.ToString();
}

public byte[]? GetAggregatedValueBinary()
Expand Down
22 changes: 20 additions & 2 deletions KeyManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyManager.Library.Plugin",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyManager.Library.Plugin.UI", "KeyManager.Library.Plugin.UI\KeyManager.Library.Plugin.UI.csproj", "{DA74A075-D143-43FE-A977-EDDBD3741F26}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyManager.Library.KeyStore.LCP", "KeyManager.Library.KeyStore.LCP\KeyManager.Library.KeyStore.LCP.csproj", "{B5738CC1-8AD1-4744-B6F0-7D292A0918F5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyManager.Library.KeyStore.LCP", "KeyManager.Library.KeyStore.LCP\KeyManager.Library.KeyStore.LCP.csproj", "{B5738CC1-8AD1-4744-B6F0-7D292A0918F5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyManager.Library.KeyStore.LCP.UI", "KeyManager.Library.KeyStore.LCP.UI\KeyManager.Library.KeyStore.LCP.UI.csproj", "{F41FA968-E867-4681-87A6-08F1D6F395E8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyManager.Library.KeyStore.LCP.UI", "KeyManager.Library.KeyStore.LCP.UI\KeyManager.Library.KeyStore.LCP.UI.csproj", "{F41FA968-E867-4681-87A6-08F1D6F395E8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{B0AC74E9-CFE3-43F5-A798-E851FAF21B83}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyManager.Library.KeyStore.NXP_SAM.Tests", "KeyManager.Library.KeyStore.NXP_SAM.Tests\KeyManager.Library.KeyStore.NXP_SAM.Tests.csproj", "{20065C3D-F98E-4234-B835-19BD67DC60D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -251,6 +255,18 @@ Global
{F41FA968-E867-4681-87A6-08F1D6F395E8}.Release|x64.Build.0 = Release|Any CPU
{F41FA968-E867-4681-87A6-08F1D6F395E8}.Release|x86.ActiveCfg = Release|Any CPU
{F41FA968-E867-4681-87A6-08F1D6F395E8}.Release|x86.Build.0 = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|x64.ActiveCfg = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|x64.Build.0 = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|x86.ActiveCfg = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Debug|x86.Build.0 = Debug|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|Any CPU.Build.0 = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|x64.ActiveCfg = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|x64.Build.0 = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|x86.ActiveCfg = Release|Any CPU
{20065C3D-F98E-4234-B835-19BD67DC60D9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -264,8 +280,10 @@ Global
{2E826BDF-4186-4725-9380-9AD18E4BB5F8} = {E13D01DB-9ADA-4C08-BD35-BE0D1D071896}
{AFB28B07-2FBE-4C6C-809C-1FC51AA18905} = {E13D01DB-9ADA-4C08-BD35-BE0D1D071896}
{AD05527B-B52D-41C9-A5A8-983516AAC84E} = {E13D01DB-9ADA-4C08-BD35-BE0D1D071896}
{0A32D255-4377-4427-8571-A88D451048BB} = {B0AC74E9-CFE3-43F5-A798-E851FAF21B83}
{B5738CC1-8AD1-4744-B6F0-7D292A0918F5} = {E13D01DB-9ADA-4C08-BD35-BE0D1D071896}
{F41FA968-E867-4681-87A6-08F1D6F395E8} = {E13D01DB-9ADA-4C08-BD35-BE0D1D071896}
{20065C3D-F98E-4234-B835-19BD67DC60D9} = {B0AC74E9-CFE3-43F5-A798-E851FAF21B83}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {991DF8DB-0844-4CCB-B0FE-97C38EAC223C}
Expand Down

0 comments on commit 5e0a426

Please sign in to comment.