Skip to content

Commit

Permalink
Fix CtapParseClientPin GetPinToken test (implement EXPECT_SAME_BYTES)
Browse files Browse the repository at this point in the history
  • Loading branch information
pokusew committed Jan 18, 2025
1 parent 60755d3 commit 92da7cf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
9 changes: 9 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ FetchContent_Declare(
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# https://fmt.dev/11.1/get-started/#cmake
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 8303d140a1a11f19b982a9f664bbe59a1ccda3f4 # 11.1.2
)
FetchContent_MakeAvailable(fmt)

# TESTS

add_executable(
Expand Down Expand Up @@ -123,6 +131,7 @@ add_executable(
target_link_libraries(
test_ctap_parse_client_pin
GTest::gtest_main
fmt::fmt
core_ctap_parse
)

Expand Down
47 changes: 41 additions & 6 deletions test/src/ctap_parse_client_pin_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
#include <gtest/gtest.h>
#include <fmt/format.h>
#include <fmt/ranges.h>
extern "C" {
#include "ctap_parse.h"
}

namespace {

testing::AssertionResult SameBytes(
const char *size_expr,
const char *actual_expr,
const char *expected_expr,
size_t size,
const uint8_t actual[size],
const uint8_t expected[size]
) {
for (int i = 0; i < size; ++i) {
if (actual[i] != expected[i]) {
size_t field_width = std::max(strlen(actual_expr), strlen(expected_expr));
return testing::AssertionFailure()
<< fmt::format(
"bytes differ when comparing {0} bytes ({1}):"
"\n {3:>{2}}: {4:02X}"
"\n {5:>{2}}: {6:02X}"
"\n {8:>{9}} ^^ first diff at index = {7}",
size, size_expr, field_width,
actual_expr, fmt::join(actual, actual + size, " "),
expected_expr, fmt::join(expected, expected + size, " "),
i, "", field_width + (i * 3)
);
}
}
return testing::AssertionSuccess();
}

#define EXPECT_SAME_BYTES(actual, expected) EXPECT_PRED_FORMAT3(SameBytes, sizeof((actual)), (actual), (expected))

TEST(CtapParseClientPin, InvalidCbor) {
const uint8_t request[] = {0xFF};
CTAP_clientPIN cp;
Expand Down Expand Up @@ -64,12 +95,16 @@ TEST(CtapParseClientPin, GetPinToken) {
EXPECT_EQ(cp.keyAgreementPresent, true);
EXPECT_EQ(cp.keyAgreement.kty, 2);
EXPECT_EQ(cp.keyAgreement.crv, 1);
const uint8_t x[] = "\x39\x09\xe3\x89\xb5\x45\x66\x2b\xfb\xee\x67\x90\x5d\xd4\x32\xe7" \
"\x01\xa1\xc1\x46\xac\xab\x6a\x5b\x42\x0b\x75\xdd\x35\x10\x5e\x5d";
const uint8_t y[] = "\xf0\xb7\xcb\x3e\xbf\x62\x59\x4b\x9e\x8d\x98\x70\xb1\xa9\x15\x4a" \
"\xb5\x0c\xdd\x2c\x1e\x6e\x86\x14\x90\xb8\x2d\x92\xf0\x9e\x19\x84";
EXPECT_EQ(cp.keyAgreement.pubkey.x, x);
EXPECT_EQ(cp.keyAgreement.pubkey.y, y);
const uint8_t expected_x[] = "\x39\x09\xe3\x89\xb5\x45\x66\x2b\xfb\xee\x67\x90\x5d\xd4\x32\xe7\x01\xa1\xc1\x46\xac\xab\x6a\x5b\x42\x0b\x75\xdd\x35\x10\x5e\x5d";
const uint8_t expected_y[] = "\xf0\xb7\xcb\x3f\xbf\x62\x59\x4b\x9e\x8d\x98\x70\xb1\xa9\x15\x4a\xb5\x0c\xdd\x2c\x1e\x6e\x86\x14\x90\xb8\x2d\x92\xf0\x9e\x19\x84";
EXPECT_SAME_BYTES(
cp.keyAgreement.pubkey.x,
expected_x
);
EXPECT_SAME_BYTES(
cp.keyAgreement.pubkey.y,
expected_y
);
EXPECT_EQ(cp.pinUvAuthParamPresent, false);
EXPECT_EQ(cp.newPinEncSize, 0);
EXPECT_EQ(cp.pinHashEncPresent, true);
Expand Down

0 comments on commit 92da7cf

Please sign in to comment.