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