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
3 // found in the LICENSE file.
5 #include "net/base/dns_util.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 class DNSUtilTest
: public testing::Test
{
13 // IncludeNUL converts a char* to a std::string and includes the terminating
15 static std::string
IncludeNUL(const char* in
) {
16 return std::string(in
, strlen(in
) + 1);
19 TEST_F(DNSUtilTest
, DNSDomainFromDot
) {
22 EXPECT_FALSE(DNSDomainFromDot("", &out
));
23 EXPECT_FALSE(DNSDomainFromDot(".", &out
));
24 EXPECT_FALSE(DNSDomainFromDot("..", &out
));
26 EXPECT_TRUE(DNSDomainFromDot("com", &out
));
27 EXPECT_EQ(out
, IncludeNUL("\003com"));
28 EXPECT_TRUE(DNSDomainFromDot("google.com", &out
));
29 EXPECT_EQ(out
, IncludeNUL("\x006google\003com"));
30 EXPECT_TRUE(DNSDomainFromDot("www.google.com", &out
));
31 EXPECT_EQ(out
, IncludeNUL("\003www\006google\003com"));
33 // Label is 63 chars: still valid
34 EXPECT_TRUE(DNSDomainFromDot("z23456789a123456789a123456789a123456789a123456789a123456789a123", &out
));
35 EXPECT_EQ(out
, IncludeNUL("\077z23456789a123456789a123456789a123456789a123456789a123456789a123"));
37 // Label is too long: invalid
38 EXPECT_FALSE(DNSDomainFromDot("123456789a123456789a123456789a123456789a123456789a123456789a1234", &out
));
40 // 253 characters in the name: still valid
41 EXPECT_TRUE(DNSDomainFromDot("abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abcdefghi.abc", &out
));
42 EXPECT_EQ(out
, IncludeNUL("\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\011abcdefghi\003abc"));
44 // 254 characters in the name: invalid
45 EXPECT_FALSE(DNSDomainFromDot("123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.1234", &out
));
47 // Zero length labels should fail, except that one trailing dot is allowed
48 // (to disable suffix search):
49 EXPECT_FALSE(DNSDomainFromDot(".google.com", &out
));
50 EXPECT_FALSE(DNSDomainFromDot("www..google.com", &out
));
52 EXPECT_TRUE(DNSDomainFromDot("www.google.com.", &out
));
53 EXPECT_EQ(out
, IncludeNUL("\003www\006google\003com"));
56 TEST_F(DNSUtilTest
, DNSDomainToString
) {
57 EXPECT_EQ("", DNSDomainToString(IncludeNUL("")));
58 EXPECT_EQ("foo", DNSDomainToString(IncludeNUL("\003foo")));
59 EXPECT_EQ("foo.bar", DNSDomainToString(IncludeNUL("\003foo\003bar")));
60 EXPECT_EQ("foo.bar.uk",
61 DNSDomainToString(IncludeNUL("\003foo\003bar\002uk")));
63 // It should cope with a lack of root label.
64 EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar"));
66 // Invalid inputs should return an empty string.
67 EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80")));
68 EXPECT_EQ("", DNSDomainToString("\x06"));