| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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 "base/memory/scoped_ptr.h" | |
| 6 #include "net/base/net_util.h" | |
| 7 #include "net/dns/dns_response.h" | |
| 8 #include "net/dns/record_rdata.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace net { | |
| 12 | |
| 13 base::StringPiece MakeStringPiece(const uint8* data, unsigned size) { | |
| 14 const char* data_cc = reinterpret_cast<const char*>(data); | |
| 15 return base::StringPiece(data_cc, size); | |
| 16 } | |
| 17 | |
| 18 TEST(RecordRdataTest, ParseSrvRecord) { | |
| 19 scoped_ptr<SrvRecordRdata> record1_obj; | |
| 20 scoped_ptr<SrvRecordRdata> record2_obj; | |
| 21 | |
| 22 // These are just the rdata portions of the DNS records, rather than complete | |
| 23 // records, but it works well enough for this test. | |
| 24 | |
| 25 const uint8 record[] = { | |
| 26 0x00, 0x01, | |
| 27 0x00, 0x02, | |
| 28 0x00, 0x50, | |
| 29 0x03, 'w', 'w', 'w', | |
| 30 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 31 0x03, 'c', 'o', 'm', | |
| 32 0x00, | |
| 33 0x01, 0x01, | |
| 34 0x01, 0x02, | |
| 35 0x01, 0x03, | |
| 36 0x04, 'w', 'w', 'w', '2', | |
| 37 0xc0, 0x0a, // Pointer to "google.com" | |
| 38 }; | |
| 39 | |
| 40 DnsRecordParser parser(record, sizeof(record), 0); | |
| 41 const unsigned first_record_len = 22; | |
| 42 base::StringPiece record1_strpiece = MakeStringPiece( | |
| 43 record, first_record_len); | |
| 44 base::StringPiece record2_strpiece = MakeStringPiece( | |
| 45 record + first_record_len, sizeof(record) - first_record_len); | |
| 46 | |
| 47 record1_obj = SrvRecordRdata::Create(record1_strpiece, parser); | |
| 48 ASSERT_TRUE(record1_obj != NULL); | |
| 49 ASSERT_EQ(1, record1_obj->priority()); | |
| 50 ASSERT_EQ(2, record1_obj->weight()); | |
| 51 ASSERT_EQ(80, record1_obj->port()); | |
| 52 | |
| 53 ASSERT_EQ("www.google.com", record1_obj->target()); | |
| 54 | |
| 55 record2_obj = SrvRecordRdata::Create(record2_strpiece, parser); | |
| 56 ASSERT_TRUE(record2_obj != NULL); | |
| 57 ASSERT_EQ(257, record2_obj->priority()); | |
| 58 ASSERT_EQ(258, record2_obj->weight()); | |
| 59 ASSERT_EQ(259, record2_obj->port()); | |
| 60 | |
| 61 ASSERT_EQ("www2.google.com", record2_obj->target()); | |
| 62 | |
| 63 ASSERT_TRUE(record1_obj->IsEqual(record1_obj.get())); | |
| 64 ASSERT_FALSE(record1_obj->IsEqual(record2_obj.get())); | |
| 65 } | |
| 66 | |
| 67 TEST(RecordRdataTest, ParseARecord) { | |
| 68 scoped_ptr<ARecordRdata> record_obj; | |
| 69 | |
| 70 // These are just the rdata portions of the DNS records, rather than complete | |
| 71 // records, but it works well enough for this test. | |
| 72 | |
| 73 const uint8 record[] = { | |
| 74 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1 | |
| 75 }; | |
| 76 | |
| 77 DnsRecordParser parser(record, sizeof(record), 0); | |
| 78 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 79 | |
| 80 record_obj = ARecordRdata::Create(record_strpiece, parser); | |
| 81 ASSERT_TRUE(record_obj != NULL); | |
| 82 | |
| 83 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address())); | |
| 84 | |
| 85 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 86 } | |
| 87 | |
| 88 TEST(RecordRdataTest, ParseAAAARecord) { | |
| 89 scoped_ptr<AAAARecordRdata> record_obj; | |
| 90 | |
| 91 // These are just the rdata portions of the DNS records, rather than complete | |
| 92 // records, but it works well enough for this test. | |
| 93 | |
| 94 const uint8 record[] = { | |
| 95 0x12, 0x34, 0x56, 0x78, | |
| 96 0x00, 0x00, 0x00, 0x00, | |
| 97 0x00, 0x00, 0x00, 0x00, | |
| 98 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A | |
| 99 }; | |
| 100 | |
| 101 DnsRecordParser parser(record, sizeof(record), 0); | |
| 102 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 103 | |
| 104 record_obj = AAAARecordRdata::Create(record_strpiece, parser); | |
| 105 ASSERT_TRUE(record_obj != NULL); | |
| 106 | |
| 107 ASSERT_EQ("1234:5678::9", | |
| 108 IPAddressToString(record_obj->address())); | |
| 109 | |
| 110 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 111 } | |
| 112 | |
| 113 TEST(RecordRdataTest, ParseCnameRecord) { | |
| 114 scoped_ptr<CnameRecordRdata> record_obj; | |
| 115 | |
| 116 // These are just the rdata portions of the DNS records, rather than complete | |
| 117 // records, but it works well enough for this test. | |
| 118 | |
| 119 const uint8 record[] = { | |
| 120 0x03, 'w', 'w', 'w', | |
| 121 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 122 0x03, 'c', 'o', 'm', | |
| 123 0x00 | |
| 124 }; | |
| 125 | |
| 126 DnsRecordParser parser(record, sizeof(record), 0); | |
| 127 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 128 | |
| 129 record_obj = CnameRecordRdata::Create(record_strpiece, parser); | |
| 130 ASSERT_TRUE(record_obj != NULL); | |
| 131 | |
| 132 ASSERT_EQ("www.google.com", record_obj->cname()); | |
| 133 | |
| 134 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 135 } | |
| 136 | |
| 137 TEST(RecordRdataTest, ParsePtrRecord) { | |
| 138 scoped_ptr<PtrRecordRdata> record_obj; | |
| 139 | |
| 140 // These are just the rdata portions of the DNS records, rather than complete | |
| 141 // records, but it works well enough for this test. | |
| 142 | |
| 143 const uint8 record[] = { | |
| 144 0x03, 'w', 'w', 'w', | |
| 145 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 146 0x03, 'c', 'o', 'm', | |
| 147 0x00 | |
| 148 }; | |
| 149 | |
| 150 DnsRecordParser parser(record, sizeof(record), 0); | |
| 151 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 152 | |
| 153 record_obj = PtrRecordRdata::Create(record_strpiece, parser); | |
| 154 ASSERT_TRUE(record_obj != NULL); | |
| 155 | |
| 156 ASSERT_EQ("www.google.com", record_obj->ptrdomain()); | |
| 157 | |
| 158 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 159 } | |
| 160 | |
| 161 TEST(RecordRdataTest, ParseTxtRecord) { | |
| 162 scoped_ptr<TxtRecordRdata> record_obj; | |
| 163 | |
| 164 // These are just the rdata portions of the DNS records, rather than complete | |
| 165 // records, but it works well enough for this test. | |
| 166 | |
| 167 const uint8 record[] = { | |
| 168 0x03, 'w', 'w', 'w', | |
| 169 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 170 0x03, 'c', 'o', 'm' | |
| 171 }; | |
| 172 | |
| 173 DnsRecordParser parser(record, sizeof(record), 0); | |
| 174 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 175 | |
| 176 record_obj = TxtRecordRdata::Create(record_strpiece, parser); | |
| 177 ASSERT_TRUE(record_obj != NULL); | |
| 178 | |
| 179 std::vector<std::string> expected; | |
| 180 expected.push_back("www"); | |
| 181 expected.push_back("google"); | |
| 182 expected.push_back("com"); | |
| 183 | |
| 184 ASSERT_EQ(expected, record_obj->texts()); | |
| 185 | |
| 186 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 187 } | |
| 188 | |
| 189 TEST(RecordRdataTest, ParseNsecRecord) { | |
| 190 scoped_ptr<NsecRecordRdata> record_obj; | |
| 191 | |
| 192 // These are just the rdata portions of the DNS records, rather than complete | |
| 193 // records, but it works well enough for this test. | |
| 194 | |
| 195 const uint8 record[] = { | |
| 196 0x03, 'w', 'w', 'w', | |
| 197 0x06, 'g', 'o', 'o', 'g', 'l', 'e', | |
| 198 0x03, 'c', 'o', 'm', | |
| 199 0x00, | |
| 200 0x00, 0x02, 0x40, 0x01 | |
| 201 }; | |
| 202 | |
| 203 DnsRecordParser parser(record, sizeof(record), 0); | |
| 204 base::StringPiece record_strpiece = MakeStringPiece(record, sizeof(record)); | |
| 205 | |
| 206 record_obj = NsecRecordRdata::Create(record_strpiece, parser); | |
| 207 ASSERT_TRUE(record_obj != NULL); | |
| 208 | |
| 209 ASSERT_EQ(16u, record_obj->bitmap_length()); | |
| 210 | |
| 211 EXPECT_FALSE(record_obj->GetBit(0)); | |
| 212 EXPECT_TRUE(record_obj->GetBit(1)); | |
| 213 for (int i = 2; i < 15; i++) { | |
| 214 EXPECT_FALSE(record_obj->GetBit(i)); | |
| 215 } | |
| 216 EXPECT_TRUE(record_obj->GetBit(15)); | |
| 217 | |
| 218 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); | |
| 219 } | |
| 220 | |
| 221 | |
| 222 } // namespace net | |
| OLD | NEW |