Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "net/base/dns_util.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace net {
      9 
     10 class DNSUtilTest : public testing::Test {
     11 };
     12 
     13 // IncludeNUL converts a char* to a std::string and includes the terminating
     14 // NUL in the result.
     15 static std::string IncludeNUL(const char* in) {
     16   return std::string(in, strlen(in) + 1);
     17 }
     18 
     19 TEST_F(DNSUtilTest, DNSDomainFromDot) {
     20   std::string out;
     21 
     22   EXPECT_TRUE(DNSDomainFromDot("", &out));
     23   EXPECT_EQ(out, IncludeNUL(""));
     24   EXPECT_TRUE(DNSDomainFromDot("com", &out));
     25   EXPECT_EQ(out, IncludeNUL("\003com"));
     26   EXPECT_TRUE(DNSDomainFromDot("google.com", &out));
     27   EXPECT_EQ(out, IncludeNUL("\x006google\003com"));
     28   EXPECT_TRUE(DNSDomainFromDot("www.google.com", &out));
     29   EXPECT_EQ(out, IncludeNUL("\003www\006google\003com"));
     30 
     31   // Label is 63 chars: still valid
     32   EXPECT_TRUE(DNSDomainFromDot("123456789a123456789a123456789a123456789a123456789a123456789a123", &out));
     33   EXPECT_EQ(out, IncludeNUL("\077123456789a123456789a123456789a123456789a123456789a123456789a123"));
     34 
     35   // Label is too long: invalid
     36   EXPECT_FALSE(DNSDomainFromDot("123456789a123456789a123456789a123456789a123456789a123456789a1234", &out));
     37 
     38   // 253 characters in the name: still valid
     39   EXPECT_TRUE(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.123", &out));
     40   EXPECT_EQ(out, IncludeNUL("\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\011123456789\003123"));
     41 
     42   // 254 characters in the name: invalid
     43   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));
     44 
     45   // Zero length labels should be dropped.
     46   EXPECT_TRUE(DNSDomainFromDot("www.google.com.", &out));
     47   EXPECT_EQ(out, IncludeNUL("\003www\006google\003com"));
     48 
     49   EXPECT_TRUE(DNSDomainFromDot(".google.com", &out));
     50   EXPECT_EQ(out, IncludeNUL("\006google\003com"));
     51 
     52   EXPECT_TRUE(DNSDomainFromDot("www..google.com", &out));
     53   EXPECT_EQ(out, IncludeNUL("\003www\006google\003com"));
     54 }
     55 
     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")));
     62 
     63   // It should cope with a lack of root label.
     64   EXPECT_EQ("foo.bar", DNSDomainToString("\003foo\003bar"));
     65 
     66   // Invalid inputs should return an empty string.
     67   EXPECT_EQ("", DNSDomainToString(IncludeNUL("\x80")));
     68   EXPECT_EQ("", DNSDomainToString("\x06"));
     69 }
     70 
     71 TEST_F(DNSUtilTest, STD3ASCII) {
     72   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('a'));
     73   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('b'));
     74   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('c'));
     75   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('1'));
     76   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('2'));
     77   EXPECT_TRUE(IsSTD3ASCIIValidCharacter('3'));
     78 
     79   EXPECT_FALSE(IsSTD3ASCIIValidCharacter('.'));
     80   EXPECT_FALSE(IsSTD3ASCIIValidCharacter('/'));
     81   EXPECT_FALSE(IsSTD3ASCIIValidCharacter('\x00'));
     82 }
     83 
     84 }  // namespace net
     85