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