Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: net/dns/record_rdata_unittest.cc

Issue 21534003: avoid char+'\xHH' as it's too easy to use values > 127 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 TEST(RecordRdataTest, ParseSrvRecord) { 13 TEST(RecordRdataTest, ParseSrvRecord) {
14 scoped_ptr<SrvRecordRdata> record1_obj; 14 scoped_ptr<SrvRecordRdata> record1_obj;
15 scoped_ptr<SrvRecordRdata> record2_obj; 15 scoped_ptr<SrvRecordRdata> record2_obj;
16 16
17 // These are just the rdata portions of the DNS records, rather than complete 17 // These are just the rdata portions of the DNS records, rather than complete
18 // records, but it works well enough for this test. 18 // records, but it works well enough for this test.
19 19
20 const char record[] = { 20 const uint8 record[] = {
21 '\x00', '\x01', 21 0x00, 0x01,
22 '\x00', '\x02', 22 0x00, 0x02,
23 '\x00', '\x50', 23 0x00, 0x50,
24 '\x03', 'w', 'w', 'w', 24 0x03, 'w', 'w', 'w',
25 '\x06', 'g', 'o', 'o', 'g', 'l', 'e', 25 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
26 '\x03', 'c', 'o', 'm', 26 0x03, 'c', 'o', 'm',
27 '\x00', 27 0x00,
28 '\x01', '\x01', 28 0x01, 0x01,
29 '\x01', '\x02', 29 0x01, 0x02,
30 '\x01', '\x03', 30 0x01, 0x03,
31 '\x04', 'w', 'w', 'w', '2', 31 0x04, 'w', 'w', 'w', '2',
32 '\xc0', '\x0a', // Pointer to "google.com" 32 0xc0, 0x0a, // Pointer to "google.com"
33 }; 33 };
34 34
35 DnsRecordParser parser(record, sizeof(record), 0); 35 DnsRecordParser parser(record, sizeof(record), 0);
36 const unsigned first_record_len = 22; 36 const unsigned first_record_len = 22;
37 base::StringPiece record1_strpiece(record, first_record_len); 37 const char* record_cc = reinterpret_cast<const char*>(record);
38 base::StringPiece record1_strpiece(record_cc, first_record_len);
szym 2013/08/01 14:28:27 This is done 8 times in this file. Suggest helper
Mostyn Bramley-Moore 2013/08/01 14:56:24 Done.
38 base::StringPiece record2_strpiece( 39 base::StringPiece record2_strpiece(
39 record + first_record_len, sizeof(record) - first_record_len); 40 record_cc + first_record_len, sizeof(record) - first_record_len);
40 41
41 record1_obj = SrvRecordRdata::Create(record1_strpiece, parser); 42 record1_obj = SrvRecordRdata::Create(record1_strpiece, parser);
42 ASSERT_TRUE(record1_obj != NULL); 43 ASSERT_TRUE(record1_obj != NULL);
43 ASSERT_EQ(1, record1_obj->priority()); 44 ASSERT_EQ(1, record1_obj->priority());
44 ASSERT_EQ(2, record1_obj->weight()); 45 ASSERT_EQ(2, record1_obj->weight());
45 ASSERT_EQ(80, record1_obj->port()); 46 ASSERT_EQ(80, record1_obj->port());
46 47
47 ASSERT_EQ("www.google.com", record1_obj->target()); 48 ASSERT_EQ("www.google.com", record1_obj->target());
48 49
49 record2_obj = SrvRecordRdata::Create(record2_strpiece, parser); 50 record2_obj = SrvRecordRdata::Create(record2_strpiece, parser);
50 ASSERT_TRUE(record2_obj != NULL); 51 ASSERT_TRUE(record2_obj != NULL);
51 ASSERT_EQ(257, record2_obj->priority()); 52 ASSERT_EQ(257, record2_obj->priority());
52 ASSERT_EQ(258, record2_obj->weight()); 53 ASSERT_EQ(258, record2_obj->weight());
53 ASSERT_EQ(259, record2_obj->port()); 54 ASSERT_EQ(259, record2_obj->port());
54 55
55 ASSERT_EQ("www2.google.com", record2_obj->target()); 56 ASSERT_EQ("www2.google.com", record2_obj->target());
56 57
57 ASSERT_TRUE(record1_obj->IsEqual(record1_obj.get())); 58 ASSERT_TRUE(record1_obj->IsEqual(record1_obj.get()));
58 ASSERT_FALSE(record1_obj->IsEqual(record2_obj.get())); 59 ASSERT_FALSE(record1_obj->IsEqual(record2_obj.get()));
59 } 60 }
60 61
61 TEST(RecordRdataTest, ParseARecord) { 62 TEST(RecordRdataTest, ParseARecord) {
62 scoped_ptr<ARecordRdata> record_obj; 63 scoped_ptr<ARecordRdata> record_obj;
63 64
64 // These are just the rdata portions of the DNS records, rather than complete 65 // These are just the rdata portions of the DNS records, rather than complete
65 // records, but it works well enough for this test. 66 // records, but it works well enough for this test.
66 67
67 const char record[] = { 68 const uint8 record[] = {
68 '\x7F', '\x00', '\x00', '\x01' // 127.0.0.1 69 0x7F, 0x00, 0x00, 0x01 // 127.0.0.1
69 }; 70 };
70 71
71 DnsRecordParser parser(record, sizeof(record), 0); 72 DnsRecordParser parser(record, sizeof(record), 0);
72 base::StringPiece record_strpiece(record, sizeof(record)); 73 base::StringPiece record_strpiece(
74 reinterpret_cast<const char*>(record), sizeof(record));
73 75
74 record_obj = ARecordRdata::Create(record_strpiece, parser); 76 record_obj = ARecordRdata::Create(record_strpiece, parser);
75 ASSERT_TRUE(record_obj != NULL); 77 ASSERT_TRUE(record_obj != NULL);
76 78
77 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address())); 79 ASSERT_EQ("127.0.0.1", IPAddressToString(record_obj->address()));
78 80
79 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); 81 ASSERT_TRUE(record_obj->IsEqual(record_obj.get()));
80 } 82 }
81 83
82 TEST(RecordRdataTest, ParseAAAARecord) { 84 TEST(RecordRdataTest, ParseAAAARecord) {
83 scoped_ptr<AAAARecordRdata> record_obj; 85 scoped_ptr<AAAARecordRdata> record_obj;
84 86
85 // 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
86 // records, but it works well enough for this test. 88 // records, but it works well enough for this test.
87 89
88 const char record[] = { 90 const uint8 record[] = {
89 '\x12', '\x34', '\x56', '\x78', 91 0x12, 0x34, 0x56, 0x78,
90 '\x00', '\x00', '\x00', '\x00', 92 0x00, 0x00, 0x00, 0x00,
91 '\x00', '\x00', '\x00', '\x00', 93 0x00, 0x00, 0x00, 0x00,
92 '\x00', '\x00', '\x00', '\x09' // 1234:5678::9A 94 0x00, 0x00, 0x00, 0x09 // 1234:5678::9A
93 }; 95 };
94 96
95 DnsRecordParser parser(record, sizeof(record), 0); 97 DnsRecordParser parser(record, sizeof(record), 0);
96 base::StringPiece record_strpiece(record, sizeof(record)); 98 base::StringPiece record_strpiece(
99 reinterpret_cast<const char*>(record), sizeof(record));
97 100
98 record_obj = AAAARecordRdata::Create(record_strpiece, parser); 101 record_obj = AAAARecordRdata::Create(record_strpiece, parser);
99 ASSERT_TRUE(record_obj != NULL); 102 ASSERT_TRUE(record_obj != NULL);
100 103
101 ASSERT_EQ("1234:5678::9", 104 ASSERT_EQ("1234:5678::9",
102 IPAddressToString(record_obj->address())); 105 IPAddressToString(record_obj->address()));
103 106
104 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); 107 ASSERT_TRUE(record_obj->IsEqual(record_obj.get()));
105 } 108 }
106 109
107 TEST(RecordRdataTest, ParseCnameRecord) { 110 TEST(RecordRdataTest, ParseCnameRecord) {
108 scoped_ptr<CnameRecordRdata> record_obj; 111 scoped_ptr<CnameRecordRdata> record_obj;
109 112
110 // These are just the rdata portions of the DNS records, rather than complete 113 // These are just the rdata portions of the DNS records, rather than complete
111 // records, but it works well enough for this test. 114 // records, but it works well enough for this test.
112 115
113 const char record[] = { 116 const uint8 record[] = {
114 '\x03', 'w', 'w', 'w', 117 0x03, 'w', 'w', 'w',
115 '\x06', 'g', 'o', 'o', 'g', 'l', 'e', 118 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
116 '\x03', 'c', 'o', 'm', 119 0x03, 'c', 'o', 'm',
117 '\x00' 120 0x00
118 }; 121 };
119 122
120 DnsRecordParser parser(record, sizeof(record), 0); 123 DnsRecordParser parser(record, sizeof(record), 0);
121 base::StringPiece record_strpiece(record, sizeof(record)); 124 base::StringPiece record_strpiece(
125 reinterpret_cast<const char*>(record), sizeof(record));
122 126
123 record_obj = CnameRecordRdata::Create(record_strpiece, parser); 127 record_obj = CnameRecordRdata::Create(record_strpiece, parser);
124 ASSERT_TRUE(record_obj != NULL); 128 ASSERT_TRUE(record_obj != NULL);
125 129
126 ASSERT_EQ("www.google.com", record_obj->cname()); 130 ASSERT_EQ("www.google.com", record_obj->cname());
127 131
128 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); 132 ASSERT_TRUE(record_obj->IsEqual(record_obj.get()));
129 } 133 }
130 134
131 TEST(RecordRdataTest, ParsePtrRecord) { 135 TEST(RecordRdataTest, ParsePtrRecord) {
132 scoped_ptr<PtrRecordRdata> record_obj; 136 scoped_ptr<PtrRecordRdata> record_obj;
133 137
134 // These are just the rdata portions of the DNS records, rather than complete 138 // These are just the rdata portions of the DNS records, rather than complete
135 // records, but it works well enough for this test. 139 // records, but it works well enough for this test.
136 140
137 const char record[] = { 141 const uint8 record[] = {
138 '\x03', 'w', 'w', 'w', 142 0x03, 'w', 'w', 'w',
139 '\x06', 'g', 'o', 'o', 'g', 'l', 'e', 143 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
140 '\x03', 'c', 'o', 'm', 144 0x03, 'c', 'o', 'm',
141 '\x00' 145 0x00
142 }; 146 };
143 147
144 DnsRecordParser parser(record, sizeof(record), 0); 148 DnsRecordParser parser(record, sizeof(record), 0);
145 base::StringPiece record_strpiece(record, sizeof(record)); 149 base::StringPiece record_strpiece(
150 reinterpret_cast<const char*>(record), sizeof(record));
146 151
147 record_obj = PtrRecordRdata::Create(record_strpiece, parser); 152 record_obj = PtrRecordRdata::Create(record_strpiece, parser);
148 ASSERT_TRUE(record_obj != NULL); 153 ASSERT_TRUE(record_obj != NULL);
149 154
150 ASSERT_EQ("www.google.com", record_obj->ptrdomain()); 155 ASSERT_EQ("www.google.com", record_obj->ptrdomain());
151 156
152 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); 157 ASSERT_TRUE(record_obj->IsEqual(record_obj.get()));
153 } 158 }
154 159
155 TEST(RecordRdataTest, ParseTxtRecord) { 160 TEST(RecordRdataTest, ParseTxtRecord) {
156 scoped_ptr<TxtRecordRdata> record_obj; 161 scoped_ptr<TxtRecordRdata> record_obj;
157 162
158 // These are just the rdata portions of the DNS records, rather than complete 163 // These are just the rdata portions of the DNS records, rather than complete
159 // records, but it works well enough for this test. 164 // records, but it works well enough for this test.
160 165
161 const char record[] = { 166 const uint8 record[] = {
162 '\x03', 'w', 'w', 'w', 167 0x03, 'w', 'w', 'w',
163 '\x06', 'g', 'o', 'o', 'g', 'l', 'e', 168 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
164 '\x03', 'c', 'o', 'm' 169 0x03, 'c', 'o', 'm'
165 }; 170 };
166 171
167 DnsRecordParser parser(record, sizeof(record), 0); 172 DnsRecordParser parser(record, sizeof(record), 0);
168 base::StringPiece record_strpiece(record, sizeof(record)); 173 base::StringPiece record_strpiece(
174 reinterpret_cast<const char*>(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(
205 reinterpret_cast<const char*>(record), sizeof(record));
199 206
200 record_obj = NsecRecordRdata::Create(record_strpiece, parser); 207 record_obj = NsecRecordRdata::Create(record_strpiece, parser);
201 ASSERT_TRUE(record_obj != NULL); 208 ASSERT_TRUE(record_obj != NULL);
202 209
203 ASSERT_EQ(16u, record_obj->bitmap_length()); 210 ASSERT_EQ(16u, record_obj->bitmap_length());
204 211
205 EXPECT_FALSE(record_obj->GetBit(0)); 212 EXPECT_FALSE(record_obj->GetBit(0));
206 EXPECT_TRUE(record_obj->GetBit(1)); 213 EXPECT_TRUE(record_obj->GetBit(1));
207 for (int i = 2; i < 15; i++) { 214 for (int i = 2; i < 15; i++) {
208 EXPECT_FALSE(record_obj->GetBit(i)); 215 EXPECT_FALSE(record_obj->GetBit(i));
209 } 216 }
210 EXPECT_TRUE(record_obj->GetBit(15)); 217 EXPECT_TRUE(record_obj->GetBit(15));
211 218
212 ASSERT_TRUE(record_obj->IsEqual(record_obj.get())); 219 ASSERT_TRUE(record_obj->IsEqual(record_obj.get()));
213 } 220 }
214 221
215 222
216 } // namespace net 223 } // namespace net
OLDNEW
« net/dns/mdns_client_unittest.cc ('K') | « net/dns/record_parsed_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698