1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/quic/crypto/crypto_utils.h"
6
7#include "net/quic/test_tools/quic_test_utils.h"
8#include "testing/gtest/include/gtest/gtest.h"
9
10namespace net {
11namespace test {
12namespace {
13
14TEST(CryptoUtilsTest, IsValidSNI) {
15  // IP as SNI.
16  EXPECT_FALSE(CryptoUtils::IsValidSNI("192.168.0.1"));
17  // SNI without any dot.
18  EXPECT_FALSE(CryptoUtils::IsValidSNI("somedomain"));
19  // Invalid RFC2396 hostname
20  // TODO(rtenneti): Support RFC2396 hostname.
21  // EXPECT_FALSE(CryptoUtils::IsValidSNI("some_domain.com"));
22  // An empty string must be invalid otherwise the QUIC client will try sending
23  // it.
24  EXPECT_FALSE(CryptoUtils::IsValidSNI(""));
25
26  // Valid SNI
27  EXPECT_TRUE(CryptoUtils::IsValidSNI("test.google.com"));
28}
29
30TEST(CryptoUtilsTest, NormalizeHostname) {
31  struct {
32    const char *input, *expected;
33  } tests[] = {
34    { "www.google.com", "www.google.com", },
35    { "WWW.GOOGLE.COM", "www.google.com", },
36    { "www.google.com.", "www.google.com", },
37    { "www.google.COM.", "www.google.com", },
38    { "www.google.com..", "www.google.com", },
39    { "www.google.com........", "www.google.com", },
40  };
41
42  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
43    EXPECT_EQ(std::string(tests[i].expected),
44              CryptoUtils::NormalizeHostname(tests[i].input));
45  }
46}
47
48TEST(CryptoUtilsTest, TestExportKeyingMaterial) {
49  const struct TestVector {
50    // Input (strings of hexadecimal digits):
51    const char* subkey_secret;
52    const char* label;
53    const char* context;
54    size_t result_len;
55
56    // Expected output (string of hexadecimal digits):
57    const char* expected;  // Null if it should fail.
58  } test_vector[] = {
59    // Try a typical input
60    { "4823c1189ecc40fce888fbb4cf9ae6254f19ba12e6d9af54788f195a6f509ca3",
61      "e934f78d7a71dd85420fceeb8cea0317",
62      "b8d766b5d3c8aba0009c7ed3de553eba53b4de1030ea91383dcdf724cd8b7217",
63      32,
64      "a9979da0d5f1c1387d7cbe68f5c4163ddb445a03c4ad6ee72cb49d56726d679e"
65    },
66    // Don't let the label contain nulls
67    { "14fe51e082ffee7d1b4d8d4ab41f8c55",
68      "3132333435363700",
69      "58585858585858585858585858585858",
70      16,
71      NULL
72    },
73    // Make sure nulls in the context are fine
74    { "d862c2e36b0a42f7827c67ebc8d44df7",
75      "7a5b95e4e8378123",
76      "4142434445464700",
77      16,
78      "12d418c6d0738a2e4d85b2d0170f76e1"
79    },
80    // ... and give a different result than without
81    { "d862c2e36b0a42f7827c67ebc8d44df7",
82      "7a5b95e4e8378123",
83      "41424344454647",
84      16,
85      "abfa1c479a6e3ffb98a11dee7d196408"
86    },
87    // Try weird lengths
88    { "d0ec8a34f6cc9a8c96",
89      "49711798cc6251",
90      "933d4a2f30d22f089cfba842791116adc121e0",
91      23,
92      "c9a46ed0757bd1812f1f21b4d41e62125fec8364a21db7"
93    },
94  };
95
96  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_vector); i++) {
97    // Decode the test vector.
98    string subkey_secret;
99    string label;
100    string context;
101    ASSERT_TRUE(DecodeHexString(test_vector[i].subkey_secret, &subkey_secret));
102    ASSERT_TRUE(DecodeHexString(test_vector[i].label, &label));
103    ASSERT_TRUE(DecodeHexString(test_vector[i].context, &context));
104    size_t result_len = test_vector[i].result_len;
105    bool expect_ok = test_vector[i].expected != NULL;
106    string expected;
107    if (expect_ok) {
108      ASSERT_TRUE(DecodeHexString(test_vector[i].expected, &expected));
109    }
110
111    string result;
112    bool ok = CryptoUtils::ExportKeyingMaterial(subkey_secret,
113                                                label,
114                                                context,
115                                                result_len,
116                                                &result);
117    EXPECT_EQ(expect_ok, ok);
118    if (expect_ok) {
119      EXPECT_EQ(result_len, result.length());
120      test::CompareCharArraysWithHexError("HKDF output",
121                                          result.data(),
122                                          result.length(),
123                                          expected.data(),
124                                          expected.length());
125    }
126  }
127}
128
129}  // namespace
130}  // namespace test
131}  // namespace net
132