Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/dns_util.h" | 5 #include "net/dns/dns_util.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace { | |
| 10 | |
| 11 bool IsValidDNSDomainName(const char* name) { | |
| 12 size_t length = strlen(name); | |
| 13 for (size_t i = 0; i < length; ++i) { | |
| 14 if (name[i] == '.') { | |
| 15 if (i == 0 || name[i - 1] == '.') { | |
| 16 return false; | |
| 17 } else { | |
|
Ryan Sleevi
2017/03/22 00:16:07
https://chromium.googlesource.com/chromium/src/+/m
palmer
2017/03/22 01:03:34
Done.
| |
| 18 continue; | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 if (!net::IsValidLabelCharacter(name[i], i == 0 || name[i - 1] == '.')) { | |
| 23 return false; | |
| 24 } | |
| 25 } | |
| 26 return true; | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 9 namespace net { | 31 namespace net { |
| 10 | 32 |
| 11 class DNSUtilTest : public testing::Test { | 33 class DNSUtilTest : public testing::Test { |
| 12 }; | 34 }; |
| 13 | 35 |
| 14 // IncludeNUL converts a char* to a std::string and includes the terminating | 36 // IncludeNUL converts a char* to a std::string and includes the terminating |
| 15 // NUL in the result. | 37 // NUL in the result. |
| 16 static std::string IncludeNUL(const char* in) { | 38 static std::string IncludeNUL(const char* in) { |
| 17 return std::string(in, strlen(in) + 1); | 39 return std::string(in, strlen(in) + 1); |
| 18 } | 40 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 DNSDomainToString(IncludeNUL("\003foo\003bar\002uk"))); | 84 DNSDomainToString(IncludeNUL("\003foo\003bar\002uk"))); |
| 63 | 85 |
| 64 // It should cope with a lack of root label. | 86 // It should cope with a lack of root label. |
| 65 EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar")); | 87 EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar")); |
| 66 | 88 |
| 67 // Invalid inputs should return an empty string. | 89 // Invalid inputs should return an empty string. |
| 68 EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80"))); | 90 EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80"))); |
| 69 EXPECT_EQ("", DNSDomainToString("\x06")); | 91 EXPECT_EQ("", DNSDomainToString("\x06")); |
| 70 } | 92 } |
| 71 | 93 |
| 94 TEST_F(DNSUtilTest, IsValidDNSDomain) { | |
| 95 const char* const bad_hostnames[] = { | |
| 96 "%20%20noodles.blorg", "noo dles.blorg ", "noo dles.blorg. ", | |
| 97 "^noodles.blorg", "noodles^.blorg", "noo&dles.blorg", | |
| 98 "noodles.blorg`", "www.-noodles.blorg", | |
| 99 }; | |
| 100 | |
| 101 // TODO(palmer): In the future, when we can remove support for invalid names, | |
| 102 // change the calls to from |IsValidDNSDomainName| to |IsValidDNSDomain|, and | |
| 103 // remove |IsValidDNSDomainName| (defined above). | |
| 104 | |
| 105 for (size_t i = 0; i < arraysize(bad_hostnames); ++i) { | |
| 106 EXPECT_FALSE(IsValidDNSDomainName(bad_hostnames[i])); | |
| 107 } | |
| 108 | |
| 109 const char* const good_hostnames[] = { | |
| 110 "www.noodles.blorg", "1www.noodles.blorg", "www.2noodles.blorg", | |
| 111 "www.n--oodles.blorg", "www.noodl_es.blorg", "www.no-_odles.blorg", | |
| 112 "www_.noodles.blorg", "www.noodles.blorg.", "_privet._tcp.local", | |
| 113 }; | |
| 114 | |
| 115 for (size_t i = 0; i < arraysize(good_hostnames); ++i) { | |
| 116 EXPECT_TRUE(IsValidDNSDomainName(good_hostnames[i])); | |
| 117 } | |
| 118 } | |
| 119 | |
| 72 } // namespace net | 120 } // namespace net |
| OLD | NEW |