Skip to content

Commit

Permalink
.NET 8 trying to fix (#39)
Browse files Browse the repository at this point in the history
* Ignore CA1416 platform OS incompability
added GITHUB link

* Ignoring CA1416
Changing lots of Unit tests from Assert.True to proper ones

* Loads of updates
https://xunit.net/xunit.analyzers/rules/xUnit2012
  • Loading branch information
virot authored Nov 3, 2024
1 parent be1bc51 commit 386877c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 145 deletions.
119 changes: 55 additions & 64 deletions TameMyCerts.Tests/CertificateContentValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using TameMyCerts.Enums;
using TameMyCerts.Models;
Expand Down Expand Up @@ -185,8 +186,8 @@ public void Does_add_static_RDN_when_not_present()

PrintResult(result);

Assert.True(result.CertificateProperties.Any(x =>
x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization]) && x.Value.Equals("ADCS Labor")));
Assert.Contains("ADCS Labor", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization])).Select(x => x.Value));

}

[Fact]
Expand Down Expand Up @@ -236,8 +237,7 @@ public void Does_not_add_static_RDN_when_present_and_not_forced()

PrintResult(result);

Assert.False(result.CertificateProperties.Any(x =>
x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization]) && x.Value.Equals("ADCS Labor")));
Assert.DoesNotContain("ADCS Labor", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -288,8 +288,7 @@ public void Does_add_static_RDN_when_present_and_forced()

PrintResult(result);

Assert.True(result.CertificateProperties.Any(x =>
x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization]) && x.Value.Equals("ADCS Labor")));
Assert.Contains("ADCS Labor", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization])).Select(x => x.Value));
}

[Fact]
Expand All @@ -307,7 +306,7 @@ public void Does_return_if_already_denied()
PrintResult(result);

Assert.True(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.NTE_FAIL));
Assert.Equal(WinError.NTE_FAIL, result.StatusCode);
}

[Fact]
Expand Down Expand Up @@ -335,7 +334,7 @@ public void Does_not_add_static_RDN_when_length_constraint_was_violated()
PrintResult(result);

Assert.True(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.CERTSRV_E_TEMPLATE_DENIED));
Assert.Equal(WinError.CERTSRV_E_TEMPLATE_DENIED, result.StatusCode);
}


Expand Down Expand Up @@ -364,7 +363,7 @@ public void Does_add_static_RDN_when_length_constraint_was_not_violated()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
}

[Fact]
Expand Down Expand Up @@ -392,10 +391,8 @@ public void Does_transfer_RDN_to_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization]))
.Any(x => x.Value.Equals("intranet.adcslabor.de")));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.Contains("intranet.adcslabor.de", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -430,13 +427,20 @@ public void Does_transfer_RDN_to_RDN_and_clears_original_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(result.CertificateProperties.ContainsKey(RdnTypes.NameProperty[RdnTypes.CommonName]) &&
result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.CommonName]]
.Equals(string.Empty));
Assert.True(result.CertificateProperties.ContainsKey(RdnTypes.NameProperty[RdnTypes.Organization]) &&
result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.Organization]]
.Equals("intranet.adcslabor.de"));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);

Assert.Multiple(() =>
{
Assert.Contains(RdnTypes.NameProperty[RdnTypes.CommonName], result.CertificateProperties.Keys.ToList());
Assert.Empty(result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.CommonName]]);
}
);
Assert.Multiple(() =>
{
Assert.Contains(RdnTypes.NameProperty[RdnTypes.Organization], result.CertificateProperties.Keys.ToList());
Assert.Equal("intranet.adcslabor.de", result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.Organization]]);
}
);
}

[Fact]
Expand Down Expand Up @@ -464,11 +468,13 @@ public void Does_transfer_RDN_to_SAN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(
result.CertificateExtensions.ContainsKey(WinCrypt.szOID_SUBJECT_ALT_NAME2) &&
Convert.ToBase64String(result.CertificateExtensions[WinCrypt.szOID_SUBJECT_ALT_NAME2])
.Equals("MBeCFWludHJhbmV0LmFkY3NsYWJvci5kZQ=="));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.Multiple(() =>
{
Assert.Contains(WinCrypt.szOID_SUBJECT_ALT_NAME2, result.CertificateExtensions.Keys.ToList());
Assert.Equal("MBeCFWludHJhbmV0LmFkY3NsYWJvci5kZQ==", Convert.ToBase64String(result.CertificateExtensions[WinCrypt.szOID_SUBJECT_ALT_NAME2]));
}
);
}

[Fact]
Expand Down Expand Up @@ -497,10 +503,8 @@ public void Does_transfer_inline_RDN_to_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization]))
.Any(x => x.Value.Equals("intranet.adcslabor.de")));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.Contains("intranet.adcslabor.de", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.Organization])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -559,10 +563,8 @@ public void Does_transfer_inline_custom_RDN_to_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("test")));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.Contains("test", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -591,7 +593,7 @@ public void Does_transfer_inline_RDN_to_SAN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.True(
result.CertificateExtensions.ContainsKey(WinCrypt.szOID_SUBJECT_ALT_NAME2) &&
Convert.ToBase64String(result.CertificateExtensions[WinCrypt.szOID_SUBJECT_ALT_NAME2])
Expand Down Expand Up @@ -648,10 +650,8 @@ public void Does_transfer_SAN_to_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("intranet.adcslabor.de")));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.Contains("intranet.adcslabor.de", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -679,7 +679,7 @@ public void Does_not_add_static_RDN_when_field_is_invalid()
PrintResult(result);

Assert.True(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.CERTSRV_E_TEMPLATE_DENIED));
Assert.Equal(WinError.CERTSRV_E_TEMPLATE_DENIED, result.StatusCode);
}

[Fact]
Expand Down Expand Up @@ -858,10 +858,13 @@ public void Does_add_static_SAN_when_present_and_forced()

PrintResult(result);

Assert.True(
result.CertificateExtensions.ContainsKey(WinCrypt.szOID_SUBJECT_ALT_NAME2) &&
Convert.ToBase64String(result.CertificateExtensions[WinCrypt.szOID_SUBJECT_ALT_NAME2])
.Equals(expectedResult));
Assert.Multiple(() =>
{
Assert.Contains(WinCrypt.szOID_SUBJECT_ALT_NAME2, result.CertificateExtensions.Keys.ToList());
Assert.Equal(expectedResult, Convert.ToBase64String(result.CertificateExtensions[WinCrypt.szOID_SUBJECT_ALT_NAME2]));
}
);

}


Expand Down Expand Up @@ -943,10 +946,7 @@ public void Allow_and_add_one_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("[email protected]"))
);
Assert.Contains("[email protected]", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -975,10 +975,7 @@ public void Allow_and_add_one_RDN_CI()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("[email protected]"))
);
Assert.Contains("[email protected]", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -1007,8 +1004,8 @@ public void Does_recognize_and_deny_valid_name_for_unknown_attribute()
PrintResult(result);

Assert.True(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.CERTSRV_E_TEMPLATE_DENIED));
Assert.True(string.Join("\n", result.Description.ToList()).Contains("test-attribute"));
Assert.Equal(WinError.CERTSRV_E_TEMPLATE_DENIED, result.StatusCode);
Assert.Contains("test-attribute", string.Join("\n", result.Description.ToList()));
}

[Fact]
Expand Down Expand Up @@ -1065,10 +1062,7 @@ public void Allow_and_add_one_combined_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("Ratlos, Rudi"))
);
Assert.Contains("Ratlos, Rudi", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -1097,10 +1091,7 @@ public void Allow_and_add_one_combined_RDN_with_twice_the_same_value()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.CertificateProperties
.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName]))
.Any(x => x.Value.Equals("Rudi is Rudi"))
);
Assert.Contains("Rudi is Rudi", result.CertificateProperties.Where(x => x.Key.Equals(RdnTypes.NameProperty[RdnTypes.CommonName])).Select(x => x.Value));
}

[Fact]
Expand Down Expand Up @@ -1321,7 +1312,7 @@ public void Does_clear_existing_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.True(result.CertificateProperties.ContainsKey(RdnTypes.NameProperty[RdnTypes.CommonName]) &&
result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.CommonName]]
.Equals(string.Empty));
Expand Down Expand Up @@ -1351,7 +1342,7 @@ public void Does_clear_nonexisting_RDN()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.True(result.CertificateProperties.ContainsKey(RdnTypes.NameProperty[RdnTypes.State]) &&
result.CertificateProperties[RdnTypes.NameProperty[RdnTypes.State]].Equals(string.Empty));
}
Expand Down Expand Up @@ -1380,7 +1371,7 @@ public void Does_not_clear_existing_RDN_if_not_mandatory()
PrintResult(result);

Assert.False(result.DeniedForIssuance);
Assert.True(result.StatusCode.Equals(WinError.ERROR_SUCCESS));
Assert.Equal(WinError.ERROR_SUCCESS, result.StatusCode);
Assert.False(result.CertificateProperties.ContainsKey(RdnTypes.NameProperty[RdnTypes.CommonName]));
}

Expand Down
Loading

0 comments on commit 386877c

Please sign in to comment.