1 // Copyright (c) 2017 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 "quiche/common/platform/api/quiche_hostname_utils.h"
6 
7 #include <string>
8 
9 #include "absl/base/macros.h"
10 #include "quiche/common/platform/api/quiche_test.h"
11 #include "quiche/common/test_tools/quiche_test_utils.h"
12 
13 namespace quiche {
14 namespace test {
15 namespace {
16 
17 class QuicheHostnameUtilsTest : public QuicheTest {};
18 
TEST_F(QuicheHostnameUtilsTest,IsValidSNI)19 TEST_F(QuicheHostnameUtilsTest, IsValidSNI) {
20   // IP as SNI.
21   EXPECT_FALSE(QuicheHostnameUtils::IsValidSNI("192.168.0.1"));
22   // SNI without any dot.
23   EXPECT_TRUE(QuicheHostnameUtils::IsValidSNI("somedomain"));
24   // Invalid by RFC2396 but unfortunately domains of this form exist.
25   EXPECT_TRUE(QuicheHostnameUtils::IsValidSNI("some_domain.com"));
26   // An empty string must be invalid otherwise the QUIC client will try sending
27   // it.
28   EXPECT_FALSE(QuicheHostnameUtils::IsValidSNI(""));
29 
30   // Valid SNI
31   EXPECT_TRUE(QuicheHostnameUtils::IsValidSNI("test.google.com"));
32 }
33 
TEST_F(QuicheHostnameUtilsTest,NormalizeHostname)34 TEST_F(QuicheHostnameUtilsTest, NormalizeHostname) {
35   // clang-format off
36   struct {
37     const char *input, *expected;
38   } tests[] = {
39       {
40           "www.google.com",
41           "www.google.com",
42       },
43       {
44           "WWW.GOOGLE.COM",
45           "www.google.com",
46       },
47       {
48           "www.google.com.",
49           "www.google.com",
50       },
51       {
52           "www.google.COM.",
53           "www.google.com",
54       },
55       {
56           "www.google.com..",
57           "www.google.com",
58       },
59       {
60           "www.google.com........",
61           "www.google.com",
62       },
63       {
64           "",
65           "",
66       },
67       {
68           ".",
69           "",
70       },
71       {
72           "........",
73           "",
74       },
75   };
76   // clang-format on
77 
78   for (size_t i = 0; i < ABSL_ARRAYSIZE(tests); ++i) {
79     EXPECT_EQ(std::string(tests[i].expected),
80               QuicheHostnameUtils::NormalizeHostname(tests[i].input));
81   }
82 
83   if (GoogleUrlSupportsIdnaForTest()) {
84     EXPECT_EQ("xn--54q.google.com", QuicheHostnameUtils::NormalizeHostname(
85                                         "\xe5\x85\x89.google.com"));
86   } else {
87     EXPECT_EQ(
88         "", QuicheHostnameUtils::NormalizeHostname("\xe5\x85\x89.google.com"));
89   }
90 }
91 
92 }  // namespace
93 }  // namespace test
94 }  // namespace quiche
95