blob: 2c6573b1933c17fd3c87c9c146b6212b1be47357 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 "net/third_party/quiche/src/quic/platform/api/quic_hostname_utils.h"
6
vasilvv872e7a32019-03-12 16:42:44 -07007#include <string>
8
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
11
12namespace quic {
13namespace test {
14namespace {
15
16class QuicHostnameUtilsTest : public QuicTest {};
17
18TEST_F(QuicHostnameUtilsTest, IsValidSNI) {
19 // IP as SNI.
20 EXPECT_FALSE(QuicHostnameUtils::IsValidSNI("192.168.0.1"));
21 // SNI without any dot.
22 EXPECT_FALSE(QuicHostnameUtils::IsValidSNI("somedomain"));
23 // Invalid by RFC2396 but unfortunately domains of this form exist.
24 EXPECT_TRUE(QuicHostnameUtils::IsValidSNI("some_domain.com"));
25 // An empty string must be invalid otherwise the QUIC client will try sending
26 // it.
27 EXPECT_FALSE(QuicHostnameUtils::IsValidSNI(""));
28
29 // Valid SNI
30 EXPECT_TRUE(QuicHostnameUtils::IsValidSNI("test.google.com"));
31}
32
33TEST_F(QuicHostnameUtilsTest, NormalizeHostname) {
34 // clang-format off
35 struct {
36 const char *input, *expected;
37 } tests[] = {
38 {
39 "www.google.com",
40 "www.google.com",
41 },
42 {
43 "WWW.GOOGLE.COM",
44 "www.google.com",
45 },
46 {
47 "www.google.com.",
48 "www.google.com",
49 },
50 {
51 "www.google.COM.",
52 "www.google.com",
53 },
54 {
55 "www.google.com..",
56 "www.google.com",
57 },
58 {
59 "www.google.com........",
60 "www.google.com",
61 },
62 {
63 "",
64 "",
65 },
66 {
67 ".",
68 "",
69 },
70 {
71 "........",
72 "",
73 },
74 {
75 "\xe5\x85\x89.google.com",
76 "xn--54q.google.com",
77 },
78 };
79 // clang-format on
80
81 for (size_t i = 0; i < QUIC_ARRAYSIZE(tests); ++i) {
vasilvvc48c8712019-03-11 13:38:16 -070082 EXPECT_EQ(std::string(tests[i].expected),
QUICHE teama6ef0a62019-03-07 20:34:33 -050083 QuicHostnameUtils::NormalizeHostname(tests[i].input));
84 }
85}
86
87} // namespace
88} // namespace test
89} // namespace quic