[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / quic / crypto / crypto_utils_test.cc
blob1395cc313729cfbacd2c1103b3ab2f5dba2c079a
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.
5 #include "net/quic/crypto/crypto_utils.h"
7 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using std::string;
12 namespace net {
13 namespace test {
14 namespace {
16 TEST(CryptoUtilsTest, IsValidSNI) {
17 // IP as SNI.
18 EXPECT_FALSE(CryptoUtils::IsValidSNI("192.168.0.1"));
19 // SNI without any dot.
20 EXPECT_FALSE(CryptoUtils::IsValidSNI("somedomain"));
21 // Invalid RFC2396 hostname
22 // TODO(rtenneti): Support RFC2396 hostname.
23 // EXPECT_FALSE(CryptoUtils::IsValidSNI("some_domain.com"));
24 // An empty string must be invalid otherwise the QUIC client will try sending
25 // it.
26 EXPECT_FALSE(CryptoUtils::IsValidSNI(""));
28 // Valid SNI
29 EXPECT_TRUE(CryptoUtils::IsValidSNI("test.google.com"));
32 TEST(CryptoUtilsTest, NormalizeHostname) {
33 struct {
34 const char *input, *expected;
35 } tests[] = {
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 { "www.google.com..", "www.google.com", },
41 { "www.google.com........", "www.google.com", },
44 for (size_t i = 0; i < arraysize(tests); ++i) {
45 EXPECT_EQ(std::string(tests[i].expected),
46 CryptoUtils::NormalizeHostname(tests[i].input));
50 TEST(CryptoUtilsTest, TestExportKeyingMaterial) {
51 const struct TestVector {
52 // Input (strings of hexadecimal digits):
53 const char* subkey_secret;
54 const char* label;
55 const char* context;
56 size_t result_len;
58 // Expected output (string of hexadecimal digits):
59 const char* expected; // Null if it should fail.
60 } test_vector[] = {
61 // Try a typical input
62 { "4823c1189ecc40fce888fbb4cf9ae6254f19ba12e6d9af54788f195a6f509ca3",
63 "e934f78d7a71dd85420fceeb8cea0317",
64 "b8d766b5d3c8aba0009c7ed3de553eba53b4de1030ea91383dcdf724cd8b7217",
65 32,
66 "a9979da0d5f1c1387d7cbe68f5c4163ddb445a03c4ad6ee72cb49d56726d679e"
68 // Don't let the label contain nulls
69 { "14fe51e082ffee7d1b4d8d4ab41f8c55",
70 "3132333435363700",
71 "58585858585858585858585858585858",
72 16,
73 nullptr
75 // Make sure nulls in the context are fine
76 { "d862c2e36b0a42f7827c67ebc8d44df7",
77 "7a5b95e4e8378123",
78 "4142434445464700",
79 16,
80 "12d418c6d0738a2e4d85b2d0170f76e1"
82 // ... and give a different result than without
83 { "d862c2e36b0a42f7827c67ebc8d44df7",
84 "7a5b95e4e8378123",
85 "41424344454647",
86 16,
87 "abfa1c479a6e3ffb98a11dee7d196408"
89 // Try weird lengths
90 { "d0ec8a34f6cc9a8c96",
91 "49711798cc6251",
92 "933d4a2f30d22f089cfba842791116adc121e0",
93 23,
94 "c9a46ed0757bd1812f1f21b4d41e62125fec8364a21db7"
98 for (size_t i = 0; i < arraysize(test_vector); i++) {
99 // Decode the test vector.
100 string subkey_secret;
101 string label;
102 string context;
103 ASSERT_TRUE(DecodeHexString(test_vector[i].subkey_secret, &subkey_secret));
104 ASSERT_TRUE(DecodeHexString(test_vector[i].label, &label));
105 ASSERT_TRUE(DecodeHexString(test_vector[i].context, &context));
106 size_t result_len = test_vector[i].result_len;
107 bool expect_ok = test_vector[i].expected != nullptr;
108 string expected;
109 if (expect_ok) {
110 ASSERT_TRUE(DecodeHexString(test_vector[i].expected, &expected));
113 string result;
114 bool ok = CryptoUtils::ExportKeyingMaterial(subkey_secret,
115 label,
116 context,
117 result_len,
118 &result);
119 EXPECT_EQ(expect_ok, ok);
120 if (expect_ok) {
121 EXPECT_EQ(result_len, result.length());
122 test::CompareCharArraysWithHexError("HKDF output",
123 result.data(),
124 result.length(),
125 expected.data(),
126 expected.length());
131 } // namespace
132 } // namespace test
133 } // namespace net