Skip to content

Commit

Permalink
[chip-cert] Added Print Certification Declaration (CD) Command to the…
Browse files Browse the repository at this point in the history
… Tool. (#22605)
  • Loading branch information
emargolis authored and pull[bot] committed Nov 14, 2023
1 parent 51ac7d6 commit 4349208
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/lib/core/CHIPTLVDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ static void DumpHandler(DumpWriter aWriter, const char * aIndent, const TLVReade
temp.Init(aReader);
tagControl = static_cast<TLVTagControl>(temp.GetControlByte() & kTLVTagControlMask);

aWriter("%zd ", aDepth);
aWriter("0x%02X, ", temp.GetLengthRead());

for (size_t i = 0; i < aDepth; i++)
aWriter("%s", aIndent);

aWriter("%p, ", temp.GetReadPoint());

if (IsProfileTag(tag))
{
aWriter("tag[%s]: 0x%x::0x%x::0x%x, ", DecodeTagControl(tagControl), VendorIdFromTag(tag), ProfileNumFromTag(tag),
Expand Down Expand Up @@ -147,7 +145,11 @@ static void DumpHandler(DumpWriter aWriter, const char * aIndent, const TLVReade
case kTLVType_ByteString:
err = temp.GetDataPtr(strbuf);
VerifyOrExit(err == CHIP_NO_ERROR, aWriter("Error in kTLVType_ByteString"));
aWriter("%p\n", strbuf);
aWriter("hex:");
for (uint32_t i = 0; i < len; i++)
{
aWriter("%02X", strbuf[i]);
}
break;

case kTLVType_Null:
Expand Down Expand Up @@ -267,7 +269,7 @@ const char * DecodeType(const TLVType aType)
break;

case kTLVType_ByteString:
retval = "Data";
retval = "Octet String";
break;

case kTLVType_Null:
Expand Down
54 changes: 53 additions & 1 deletion src/lib/core/tests/TestCHIPTLV.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2020-2022 Project CHIP Authors
* Copyright (c) 2013-2017 Nest Labs, Inc.
* All rights reserved.
*
Expand Down Expand Up @@ -37,6 +37,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/ScopedBuffer.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestExtendedAssertions.h>
#include <lib/support/UnitTestRegistration.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/logging/Constants.h>
Expand Down Expand Up @@ -1676,6 +1677,56 @@ void CheckPrettyPrinter(nlTestSuite * inSuite, void * inContext)
chip::TLV::Debug::Dump(reader, SimpleDumpWriter);
}

static char gStringDumpWriterBuf[128] = { 0 };
static size_t gStringDumpWriterLengthWritten = 0;

/**
* Log the specified message in the form of @a aFormat.
*
* @param[in] aFormat A pointer to a NULL-terminated C string with
* C Standard Library-style format specifiers
* containing the log message to be formatted and
* logged.
* @param[in] ... An argument list whose elements should correspond
* to the format specifiers in @a aFormat.
*
*/
void ENFORCE_FORMAT(1, 2) StringDumpWriter(const char * aFormat, ...)
{
va_list args;

va_start(args, aFormat);

gStringDumpWriterLengthWritten +=
static_cast<size_t>(vsprintf(&gStringDumpWriterBuf[gStringDumpWriterLengthWritten], aFormat, args));

va_end(args);
}

/**
* Test Octet String Pretty Printer
*/
void CheckOctetStringPrettyPrinter(nlTestSuite * inSuite, void * inContext)
{
const uint8_t testOctetString[] = { 0x62, 0xFA, 0x82, 0x33, 0x59, 0xAC, 0xFA, 0xA9 };
const char expectedPrint[] =
"0x04, tag[Common Profile (2 Bytes)]: 0x0::0x0::0x0, type: Octet String (0x10), length: 8, value: hex:62FA823359ACFAA9\n";
uint8_t encodedBuf[128] = { 0 };

TLVWriter writer;
writer.Init(encodedBuf);
NL_TEST_ASSERT_SUCCESS(inSuite, writer.PutBytes(CommonTag(0), testOctetString, sizeof(testOctetString)));
NL_TEST_ASSERT_SUCCESS(inSuite, writer.Finalize());

TLVReader reader;
reader.Init(encodedBuf, writer.GetLengthWritten());

chip::TLV::Debug::Dump(reader, StringDumpWriter);

NL_TEST_ASSERT(inSuite, strlen(expectedPrint) == strlen(gStringDumpWriterBuf));
NL_TEST_ASSERT(inSuite, strcmp(expectedPrint, gStringDumpWriterBuf) == 0);
}

/**
* Test Data Macros
*/
Expand Down Expand Up @@ -4397,6 +4448,7 @@ static const nlTest sTests[] =
NL_TEST_DEF("Inet Buffer Test", CheckPacketBuffer),
NL_TEST_DEF("Buffer Overflow Test", CheckBufferOverflow),
NL_TEST_DEF("Pretty Print Test", CheckPrettyPrinter),
NL_TEST_DEF("Pretty Octet String Print Test", CheckOctetStringPrettyPrinter),
NL_TEST_DEF("Data Macro Test", CheckDataMacro),
NL_TEST_DEF("Strict Aliasing Test", CheckStrictAliasing),
NL_TEST_DEF("CHIP TLV Basics", CheckCHIPTLVBasics),
Expand Down
1 change: 1 addition & 0 deletions src/tools/chip-cert/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ executable("chip-cert") {
"Cmd_GenAttCert.cpp",
"Cmd_GenCD.cpp",
"Cmd_GenCert.cpp",
"Cmd_PrintCD.cpp",
"Cmd_PrintCert.cpp",
"Cmd_ResignCert.cpp",
"Cmd_ValidateAttCert.cpp",
Expand Down
Loading

0 comments on commit 4349208

Please sign in to comment.