Skip to content

Commit

Permalink
Fix x509 attestation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drwill-ms committed Oct 18, 2022
1 parent 443f475 commit 0a385a5
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions provisioning/service/tests/Config/X509AttestationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ private string MakeX509AttestationJson(string certName, bool primaryOnly = false
}


/* SRS_X509_ATTESTATION_21_001: [The factory shall throws ArgumentException if the primary certificate is null or empty.] */
[TestMethod]
public void X509AttestationCreateFromClientCertificatesThrowsOnNullPrimaryCertificate()
{
Expand Down Expand Up @@ -114,8 +113,6 @@ public void X509AttestationCreateFromRootCertificatesThrowsOnNullPrimaryCertific
TestAssert.Throws<ArgumentException>(() => X509Attestation.CreateFromRootCertificates(primaryStr, secondaryStr));
}

/* SRS_X509_ATTESTATION_21_002: [The factory shall create a new instance of the X509Certificates with the provided primary and secondary certificates.] */
/* SRS_X509_ATTESTATION_21_003: [The factory shall create a new instance of the X509Attestation with the created X509Certificates as the ClientCertificates.] */
[TestMethod]
public void X509AttestationCreateFromClientCertificatesSucceedOnPrimaryCertificate()
{
Expand Down Expand Up @@ -220,7 +217,6 @@ public void X509AttestationCreateFromClientCertificatesSucceedOnPrimaryAndSecond
Assert.IsNull(attestation.CaReferences);
}

/* SRS_X509_ATTESTATION_21_004: [The factory shall create a new instance of the X509Attestation with the created X509Certificates as the RootCertificates.] */
[TestMethod]
public void X509AttestationCreateFromRootCertificatesSucceedOnPrimaryCertificate()
{
Expand Down Expand Up @@ -325,7 +321,6 @@ public void X509AttestationCreateFromRootCertificatesSucceedOnPrimaryAndSecondar
Assert.IsNull(attestation.CaReferences);
}

/* SRS_X509_ATTESTATION_21_005: [The factory shall throws ArgumentException if the primary CA reference is null or empty.] */
[TestMethod]
public void X509AttestationCreateFromCAReferencesThrowsOnNullPrimaryCertificate()
{
Expand All @@ -338,8 +333,6 @@ public void X509AttestationCreateFromCAReferencesThrowsOnNullPrimaryCertificate(
TestAssert.Throws<ArgumentException>(() => X509Attestation.CreateFromCaReferences(primaryStr, secondaryStr));
}

/* SRS_X509_ATTESTATION_21_006: [The factory shall create a new instance of the X509Certificates with the provided primary and secondary CA reference.] */
/* SRS_X509_ATTESTATION_21_007: [The factory shall create a new instance of the X509Attestation with the created X509Certificates as the caReference.] */
[TestMethod]
public void X509AttestationCreateFromCAReferencesSucceedOnPrimaryString()
{
Expand Down Expand Up @@ -390,9 +383,6 @@ public void X509AttestationCreateFromCAReferencesSucceedOnPrimaryAndSecondaryNul
Assert.IsNull(attestation.RootCertificates);
}

/* SRS_X509_ATTESTATION_21_008: [If the ClientCertificates is not null, the GetPrimaryX509CertificateInfo shall return the info in the Primary key of the ClientCertificates.] */
/* SRS_X509_ATTESTATION_21_012: [If the ClientCertificates is not null, and it contains Secondary key, the GetSecondaryX509CertificateInfo shall return the info in the Secondary key of the ClientCertificates.] */
/* SRS_X509_ATTESTATION_21_016: [The constructor shall store the provided `clientCertificates`, `rootCertificates`, and `caReferences`.] */
[TestMethod]
public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryAndSecondaryClientCertificates()
{
Expand All @@ -417,8 +407,6 @@ public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryOnlyClientCerti
Assert.IsNull(attestation.GetSecondaryX509CertificateInfo());
}

/* SRS_X509_ATTESTATION_21_009: [If the RootCertificates is not null, the GetPrimaryX509CertificateInfo shall return the info in the Primary key of the RootCertificates.] */
/* SRS_X509_ATTESTATION_21_013: [If the RootCertificates is not null, and it contains Secondary key, the GetSecondaryX509CertificateInfo shall return the info in the Secondary key of the RootCertificates.] */
[TestMethod]
public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryAndSecondaryRoottCertificates()
{
Expand All @@ -431,7 +419,6 @@ public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryAndSecondaryRoo
Assert.IsNotNull(attestation.GetSecondaryX509CertificateInfo());
}

/* SRS_X509_ATTESTATION_21_010: [If the CAReferences is not null, the GetPrimaryX509CertificateInfo shall return null.] */
[TestMethod]
public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryAndSecondaryCAReferences()
{
Expand All @@ -450,28 +437,21 @@ public void X509AttestationGetX509CertificateInfoSucceedOnPrimaryAndSecondaryCAR
Assert.IsNull(attestation.GetSecondaryX509CertificateInfo());
}

/* SRS_X509_ATTESTATION_21_011: [If ClientCertificates, RootCertificates, and CAReferences are null, the GetPrimaryX509CertificateInfo shall throw ArgumentException.] */
/*
* Not testable!
*/

/* SRS_X509_ATTESTATION_21_014: [The constructor shall throws ArgumentException if `clientCertificates`, `rootCertificates`, and `caReferences` are null.] */
[TestMethod]
public void X509AttestationJsonConstructorTrowsOnNoCert()
public void X509AttestationJsonConstructorThrowsOnNoCert()
{
// arrange
string json = "{}";

// act - assert
// act
Action act = () => JsonConvert.DeserializeObject<X509Attestation>(json);
var error = act.Should().Throw<DeviceProvisioningServiceException>();
error.And.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.And.IsTransient.Should().BeFalse();

// assert
act.Should().Throw<InvalidOperationException>();
}

/* SRS_X509_ATTESTATION_21_015: [The constructor shall throws ArgumentException if more than one certificate type are not null.] */
[TestMethod]
public void X509AttestationJsonConstructorTrowsOnClientAndRootCertificates()
public void X509AttestationJsonConstructorThrowsOnClientAndRootCertificates()
{
// arrange
string json =
Expand All @@ -486,15 +466,14 @@ public void X509AttestationJsonConstructorTrowsOnClientAndRootCertificates()
" }" +
"}";

// act - assert
Action act = () => JsonConvert.DeserializeObject<X509Attestation>(json);
var error = act.Should().Throw<DeviceProvisioningServiceException>();
error.And.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.And.IsTransient.Should().BeFalse();

// assert
act.Should().Throw<InvalidOperationException>();
}

[TestMethod]
public void X509AttestationJsonConstructorTrowsOnClientCertificatesAndCAReferences()
public void X509AttestationJsonConstructorThrowsOnClientCertificatesAndCaReferences()
{
// arrange
string json =
Expand All @@ -508,15 +487,15 @@ public void X509AttestationJsonConstructorTrowsOnClientCertificatesAndCAReferenc
" }" +
"}";

// act - assert
// act
Action act = () => JsonConvert.DeserializeObject<X509Attestation>(json);
var error = act.Should().Throw<DeviceProvisioningServiceException>();
error.And.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.And.IsTransient.Should().BeFalse();

// assert
act.Should().Throw<InvalidOperationException>();
}

[TestMethod]
public void X509AttestationJsonConstructorTrowsOnRootCertificatesAndCAReferences()
public void X509AttestationJsonConstructorThrowsOnRootCertificatesAndCaReferences()
{
// arrange
string json =
Expand All @@ -532,9 +511,9 @@ public void X509AttestationJsonConstructorTrowsOnRootCertificatesAndCAReferences

// act - assert
Action act = () => JsonConvert.DeserializeObject<X509Attestation>(json);
var error = act.Should().Throw<DeviceProvisioningServiceException>();
error.And.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.And.IsTransient.Should().BeFalse();

// assert
act.Should().Throw<InvalidOperationException>();
}
}
}

0 comments on commit 0a385a5

Please sign in to comment.