no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / security / manager / ssl / tests / gtest / HMACTest.cpp
blobefa264b6943d2653fb80e9213b2582595eaf50df
1 #include <string>
2 #include "gtest/gtest.h"
4 #include "ScopedNSSTypes.h"
5 #include "mozilla/gtest/MozAssertions.h"
6 #include "mozilla/Span.h"
7 #include "nss.h"
8 #include "secoidt.h"
10 // From RFC 2202
11 const unsigned char kTestKey[] = "Jefe";
12 const unsigned char kTestInput[] = "what do ya want for nothing?";
14 struct HMACTestCase {
15 SECOidTag hashAlg;
16 std::string expectedOutput;
19 #define EXPECTED_RESULT(val) std::string(val, sizeof(val) - 1)
21 MOZ_RUNINIT static const HMACTestCase HMACTestCases[] = {
23 SEC_OID_MD5,
24 EXPECTED_RESULT(
25 "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"),
28 SEC_OID_SHA256,
29 EXPECTED_RESULT(
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> {
39 public:
40 void SetUp() override { NSS_NoDB_Init(nullptr); }
43 TEST_P(psm_HMAC, Test) {
44 mozilla::HMAC hmac;
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));