2 #include "gtest/gtest.h"
4 #include "ScopedNSSTypes.h"
5 #include "mozilla/gtest/MozAssertions.h"
6 #include "mozilla/Span.h"
11 const unsigned char kTestKey
[] = "Jefe";
12 const unsigned char kTestInput
[] = "what do ya want for nothing?";
16 std::string expectedOutput
;
19 #define EXPECTED_RESULT(val) std::string(val, sizeof(val) - 1)
21 MOZ_RUNINIT
static const HMACTestCase HMACTestCases
[] = {
25 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"),
30 "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
31 "\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43"),
35 #undef EXPECTED_RESULT
37 class psm_HMAC
: public ::testing::Test
,
38 public ::testing::WithParamInterface
<HMACTestCase
> {
40 void SetUp() override
{ NSS_NoDB_Init(nullptr); }
43 TEST_P(psm_HMAC
, Test
) {
45 const HMACTestCase
& testCase(GetParam());
46 nsresult rv
= hmac
.Begin(testCase
.hashAlg
,
47 mozilla::Span(kTestKey
, sizeof(kTestKey
) - 1));
48 ASSERT_NS_SUCCEEDED(rv
);
49 rv
= hmac
.Update(reinterpret_cast<const unsigned char*>(kTestInput
),
50 sizeof(kTestInput
) - 1);
51 ASSERT_NS_SUCCEEDED(rv
);
52 nsTArray
<uint8_t> output
;
53 rv
= hmac
.End(output
);
54 ASSERT_NS_SUCCEEDED(rv
);
55 EXPECT_EQ(output
.Length(), testCase
.expectedOutput
.length());
56 for (size_t i
= 0; i
< output
.Length(); i
++) {
57 EXPECT_EQ(char(output
[i
]), testCase
.expectedOutput
[i
]);
61 INSTANTIATE_TEST_SUITE_P(psm_HMAC
, psm_HMAC
,
62 ::testing::ValuesIn(HMACTestCases
));