Skip to content

Commit

Permalink
credentials to pigweed (project-chip#33044)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlatusek authored Apr 25, 2024
1 parent 72eab93 commit 22188b6
Show file tree
Hide file tree
Showing 11 changed files with 2,573 additions and 3,155 deletions.
5 changes: 1 addition & 4 deletions src/credentials/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/build/chip/fuzz_test.gni")
Expand Down Expand Up @@ -42,7 +41,7 @@ static_library("cert_test_vectors") {
]
}

chip_test_suite_using_nltest("tests") {
chip_test_suite("tests") {
output_name = "libCredentialsTest"
output_dir = "${root_out_dir}/lib"

Expand Down Expand Up @@ -71,8 +70,6 @@ chip_test_suite_using_nltest("tests") {
"${chip_root}/src/credentials:default_attestation_verifier",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support:testing_nlunit",
"${nlunit_test_root}:nlunit-test",
]
}

Expand Down
275 changes: 119 additions & 156 deletions src/credentials/tests/TestCertificationDeclaration.cpp

Large diffs are not rendered by default.

935 changes: 422 additions & 513 deletions src/credentials/tests/TestChipCert.cpp

Large diffs are not rendered by default.

89 changes: 21 additions & 68 deletions src/credentials/tests/TestCommissionerDUTVectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPMem.h>
#include <lib/support/Span.h>
#include <lib/support/UnitTestExtendedAssertions.h>
#include <lib/support/UnitTestRegistration.h>

#include <nlunit-test.h>
#include <gtest/gtest.h>

#include <dirent.h>
#include <stdio.h>
Expand All @@ -49,10 +47,17 @@ static void OnAttestationInformationVerificationCallback(void * context, const D
*pResult = result;
}

static void TestCommissionerDUTVectors(nlTestSuite * inSuite, void * inContext)
struct TestCommissionerDUTVectors : public ::testing::Test
{
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }

static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};

TEST_F(TestCommissionerDUTVectors, TestCommissionerDUTVectors)
{
DeviceAttestationVerifier * example_dac_verifier = GetDefaultDACVerifier(GetTestAttestationTrustStore());
NL_TEST_ASSERT(inSuite, example_dac_verifier != nullptr);
ASSERT_NE(example_dac_verifier, nullptr);

std::string dirPath("../../../../../credentials/development/commissioner_dut/");
DIR * dir = opendir(dirPath.c_str());
Expand Down Expand Up @@ -105,14 +110,14 @@ static void TestCommissionerDUTVectors(nlTestSuite * inSuite, void * inContext)
VendorId vid = TestVendor1;
uint16_t pid = strstr(entry->d_name, "_vidpid_fallback_encoding_") ? 0x00B1 : 0x8000;

NL_TEST_ASSERT_SUCCESS(inSuite, dacProvider.GetCertificationDeclaration(certDeclSpan));
NL_TEST_ASSERT_SUCCESS(inSuite, dacProvider.GetDeviceAttestationCert(dacCertSpan));
NL_TEST_ASSERT_SUCCESS(inSuite, dacProvider.GetProductAttestationIntermediateCert(paiCertSpan));
EXPECT_EQ(dacProvider.GetCertificationDeclaration(certDeclSpan), CHIP_NO_ERROR);
EXPECT_EQ(dacProvider.GetDeviceAttestationCert(dacCertSpan), CHIP_NO_ERROR);
EXPECT_EQ(dacProvider.GetProductAttestationIntermediateCert(paiCertSpan), CHIP_NO_ERROR);

size_t attestationElementsLen =
TLV::EstimateStructOverhead(certDeclSpan.size(), attestationNonceSpan.size(), sizeof(uint64_t) * 8);
Platform::ScopedMemoryBuffer<uint8_t> attestationElements;
NL_TEST_ASSERT(inSuite, attestationElements.Alloc(attestationElementsLen + attestationChallengeSpan.size()));
EXPECT_TRUE(attestationElements.Alloc(attestationElementsLen + attestationChallengeSpan.size()));
MutableByteSpan attestationElementsSpan(attestationElements.Get(), attestationElementsLen);

// Construct attestation elements
Expand All @@ -121,10 +126,9 @@ static void TestCommissionerDUTVectors(nlTestSuite * inSuite, void * inContext)
Credentials::DeviceAttestationVendorReservedConstructor emptyVendorReserved(nullptr, 0);
const ByteSpan kEmptyFirmwareInfo;

NL_TEST_ASSERT_SUCCESS(inSuite,
Credentials::ConstructAttestationElements(certDeclSpan, attestationNonceSpan, timestamp,
kEmptyFirmwareInfo, emptyVendorReserved,
attestationElementsSpan));
EXPECT_EQ(Credentials::ConstructAttestationElements(certDeclSpan, attestationNonceSpan, timestamp, kEmptyFirmwareInfo,
emptyVendorReserved, attestationElementsSpan),
CHIP_NO_ERROR);
}

// Generate attestation signature
Expand All @@ -134,8 +138,8 @@ static void TestCommissionerDUTVectors(nlTestSuite * inSuite, void * inContext)
attestationChallengeSpan.size());
ByteSpan tbsSpan(attestationElementsSpan.data(), attestationElementsSpan.size() + attestationChallengeSpan.size());

NL_TEST_ASSERT_SUCCESS(inSuite, dacProvider.SignWithDeviceAttestationKey(tbsSpan, attestationSignatureSpan));
NL_TEST_ASSERT(inSuite, attestationSignatureSpan.size() == signature.Capacity());
EXPECT_EQ(dacProvider.SignWithDeviceAttestationKey(tbsSpan, attestationSignatureSpan), CHIP_NO_ERROR);
EXPECT_EQ(attestationSignatureSpan.size(), signature.Capacity());
}

AttestationVerificationResult attestationResult = AttestationVerificationResult::kNotImplemented;
Expand Down Expand Up @@ -167,63 +171,12 @@ static void TestCommissionerDUTVectors(nlTestSuite * inSuite, void * inContext)

if (isSuccessCase)
{
NL_TEST_ASSERT(inSuite, attestationResult == AttestationVerificationResult::kSuccess);
EXPECT_EQ(attestationResult, AttestationVerificationResult::kSuccess);
}
else
{
NL_TEST_ASSERT(inSuite, attestationResult != AttestationVerificationResult::kSuccess);
EXPECT_NE(attestationResult, AttestationVerificationResult::kSuccess);
}
}
closedir(dir);
}

/**
* Set up the test suite.
*/
int TestCommissionerDUT_Setup(void * inContext)
{
CHIP_ERROR error = chip::Platform::MemoryInit();

if (error != CHIP_NO_ERROR)
{
return FAILURE;
}

return SUCCESS;
}

/**
* Tear down the test suite.
*/
int TestCommissionerDUT_Teardown(void * inContext)
{
chip::Platform::MemoryShutdown();
return SUCCESS;
}

/**
* Test Suite. It lists all the test functions.
*/
// clang-format off
static const nlTest sTests[] = {
NL_TEST_DEF("Test Device Attestation Credentials Vectors", TestCommissionerDUTVectors),
NL_TEST_SENTINEL()
};
// clang-format on

int TestCommissionerDUT()
{
// clang-format off
nlTestSuite theSuite =
{
"Device Attestation Credentials Test Vectors",
&sTests[0],
TestCommissionerDUT_Setup,
TestCommissionerDUT_Teardown
};
// clang-format on
nlTestRunner(&theSuite, nullptr);
return (nlTestRunnerStats(&theSuite));
}

CHIP_REGISTER_TEST_SUITE(TestCommissionerDUT);
Loading

0 comments on commit 22188b6

Please sign in to comment.