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