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/base/dns_util.h" | 5 #include "net/base/dns_util.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace net { | 8 namespace net { |
9 | 9 |
10 class DNSUtilTest : public testing::Test { | 10 class DNSUtilTest : public testing::Test {}; |
11 }; | |
12 | 11 |
13 // IncludeNUL converts a char* to a std::string and includes the terminating | 12 // IncludeNUL converts a char* to a std::string and includes the terminating |
14 // NUL in the result. | 13 // NUL in the result. |
15 static std::string IncludeNUL(const char* in) { | 14 static std::string IncludeNUL(const char* in) { |
16 return std::string(in, strlen(in) + 1); | 15 return std::string(in, strlen(in) + 1); |
17 } | 16 } |
18 | 17 |
19 TEST_F(DNSUtilTest, DNSDomainFromDot) { | 18 TEST_F(DNSUtilTest, DNSDomainFromDot) { |
20 std::string out; | 19 std::string out; |
21 | 20 |
22 EXPECT_FALSE(DNSDomainFromDot("", &out)); | 21 EXPECT_FALSE(DNSDomainFromDot("", &out)); |
23 EXPECT_FALSE(DNSDomainFromDot(".", &out)); | 22 EXPECT_FALSE(DNSDomainFromDot(".", &out)); |
24 EXPECT_FALSE(DNSDomainFromDot("..", &out)); | 23 EXPECT_FALSE(DNSDomainFromDot("..", &out)); |
25 | 24 |
26 EXPECT_TRUE(DNSDomainFromDot("com", &out)); | 25 EXPECT_TRUE(DNSDomainFromDot("com", &out)); |
27 EXPECT_EQ(out, IncludeNUL("\003com")); | 26 EXPECT_EQ(out, IncludeNUL("\003com")); |
28 EXPECT_TRUE(DNSDomainFromDot("google.com", &out)); | 27 EXPECT_TRUE(DNSDomainFromDot("google.com", &out)); |
29 EXPECT_EQ(out, IncludeNUL("\x006google\003com")); | 28 EXPECT_EQ(out, IncludeNUL("\x006google\003com")); |
30 EXPECT_TRUE(DNSDomainFromDot("www.google.com", &out)); | 29 EXPECT_TRUE(DNSDomainFromDot("www.google.com", &out)); |
31 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); | 30 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); |
32 | 31 |
33 // Label is 63 chars: still valid | 32 // Label is 63 chars: still valid |
34 EXPECT_TRUE(DNSDomainFromDot("123456789a123456789a123456789a123456789a12345678
9a123456789a123", &out)); | 33 EXPECT_TRUE(DNSDomainFromDot( |
35 EXPECT_EQ(out, IncludeNUL("\077123456789a123456789a123456789a123456789a1234567
89a123456789a123")); | 34 "123456789a123456789a123456789a123456789a123456789a123456789a123", &out)); |
| 35 EXPECT_EQ(out, |
| 36 IncludeNUL( |
| 37 "\077123456789a123456789a123456789a123456789a123456789a12345678" |
| 38 "9a123")); |
36 | 39 |
37 // Label is too long: invalid | 40 // Label is too long: invalid |
38 EXPECT_FALSE(DNSDomainFromDot("123456789a123456789a123456789a123456789a1234567
89a123456789a1234", &out)); | 41 EXPECT_FALSE(DNSDomainFromDot( |
| 42 "123456789a123456789a123456789a123456789a123456789a123456789a1234", |
| 43 &out)); |
39 | 44 |
40 // 253 characters in the name: still valid | 45 // 253 characters in the name: still valid |
41 EXPECT_TRUE(DNSDomainFromDot("123456789.123456789.123456789.123456789.12345678
9.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345678
9.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345678
9.123456789.123456789.123456789.123456789.123", &out)); | 46 EXPECT_TRUE(DNSDomainFromDot( |
42 EXPECT_EQ(out, IncludeNUL("\011123456789\011123456789\011123456789\01112345678
9\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\
011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\01
1123456789\011123456789\011123456789\011123456789\011123456789\011123456789\0111
23456789\011123456789\011123456789\003123")); | 47 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 48 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 49 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 50 "123456789.123456789.123456789.123456789.123", |
| 51 &out)); |
| 52 EXPECT_EQ(out, |
| 53 IncludeNUL( |
| 54 "\011123456789\011123456789\011123456789\011123456789\011123456" |
| 55 "789\011123456789\011123456789\011123456789\011123456789\011123" |
| 56 "456789\011123456789\011123456789\011123456789\011123456789\011" |
| 57 "123456789\011123456789\011123456789\011123456789\011123456789" |
| 58 "\011123456789\011123456789\011123456789\011123456789\011123456" |
| 59 "789\011123456789\003123")); |
43 | 60 |
44 // 254 characters in the name: invalid | 61 // 254 characters in the name: invalid |
45 EXPECT_FALSE(DNSDomainFromDot("123456789.123456789.123456789.123456789.1234567
89.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234567
89.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234567
89.123456789.123456789.123456789.123456789.1234", &out)); | 62 EXPECT_FALSE(DNSDomainFromDot( |
| 63 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 64 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 65 "123456789.123456789.123456789.123456789.123456789.123456789.123456789." |
| 66 "123456789.123456789.123456789.123456789.1234", |
| 67 &out)); |
46 | 68 |
47 // Zero length labels should be dropped. | 69 // Zero length labels should be dropped. |
48 EXPECT_TRUE(DNSDomainFromDot("www.google.com.", &out)); | 70 EXPECT_TRUE(DNSDomainFromDot("www.google.com.", &out)); |
49 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); | 71 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); |
50 | 72 |
51 EXPECT_TRUE(DNSDomainFromDot(".google.com", &out)); | 73 EXPECT_TRUE(DNSDomainFromDot(".google.com", &out)); |
52 EXPECT_EQ(out, IncludeNUL("\006google\003com")); | 74 EXPECT_EQ(out, IncludeNUL("\006google\003com")); |
53 | 75 |
54 EXPECT_TRUE(DNSDomainFromDot("www..google.com", &out)); | 76 EXPECT_TRUE(DNSDomainFromDot("www..google.com", &out)); |
55 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); | 77 EXPECT_EQ(out, IncludeNUL("\003www\006google\003com")); |
56 } | 78 } |
57 | 79 |
58 TEST_F(DNSUtilTest, DNSDomainToString) { | 80 TEST_F(DNSUtilTest, DNSDomainToString) { |
59 EXPECT_EQ("", DNSDomainToString(IncludeNUL(""))); | 81 EXPECT_EQ("", DNSDomainToString(IncludeNUL(""))); |
60 EXPECT_EQ("foo", DNSDomainToString(IncludeNUL("\003foo"))); | 82 EXPECT_EQ("foo", DNSDomainToString(IncludeNUL("\003foo"))); |
61 EXPECT_EQ("foo.bar", DNSDomainToString(IncludeNUL("\003foo\003bar"))); | 83 EXPECT_EQ("foo.bar", DNSDomainToString(IncludeNUL("\003foo\003bar"))); |
62 EXPECT_EQ("foo.bar.uk", | 84 EXPECT_EQ("foo.bar.uk", |
63 DNSDomainToString(IncludeNUL("\003foo\003bar\002uk"))); | 85 DNSDomainToString(IncludeNUL("\003foo\003bar\002uk"))); |
64 | 86 |
65 // It should cope with a lack of root label. | 87 // It should cope with a lack of root label. |
66 EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar")); | 88 EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar")); |
67 | 89 |
68 // Invalid inputs should return an empty string. | 90 // Invalid inputs should return an empty string. |
69 EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80"))); | 91 EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80"))); |
70 EXPECT_EQ("", DNSDomainToString("\x06")); | 92 EXPECT_EQ("", DNSDomainToString("\x06")); |
71 } | 93 } |
72 | 94 |
73 } // namespace net | 95 } // namespace net |
OLD | NEW |