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

Side by Side Diff: components/physical_web/eddystone/eddystone_encoder_unittest.cc

Issue 2731273004: Add Platform Independent Eddystone Encoder (Closed)
Patch Set: Matts Nits 1 Created 3 years, 9 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
(Empty)
1 // Copyright 2017 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 "components/physical_web/eddystone/eddystone_encoder.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 using std::string;
13 using std::vector;
14 using std::equal;
15
16 class EddystoneEncoderTest : public testing::Test {
17 public:
18 EddystoneEncoderTest() {}
19 ~EddystoneEncoderTest() override {}
20
21 void SetUp() override {}
22 void TearDown() override {}
23
24 bool ByteVectorCmp(const vector<uint8_t>& a, const vector<uint8_t>& b);
mattreynolds 2017/03/09 22:11:31 Let's rename this to ByteVectorEquals. Cmp suggest
iankc 2017/03/09 23:17:50 Done.
25 };
26
27 bool EddystoneEncoderTest::ByteVectorCmp(const vector<uint8_t>& a,
28 const vector<uint8_t>& b) {
29 size_t a_size = a.size();
mattreynolds 2017/03/09 22:11:31 I guess this isn't needed anymore.
iankc 2017/03/09 23:17:50 Done.
30 if (a_size != b.size()) {
31 return false;
32 }
33 return equal(a.begin(), a.end(), b.begin());
34 }
35
36 // Simple test to make sure ByteVectorCmp behaves correctly.
37 TEST_F(EddystoneEncoderTest, ByteVectorCmp) {
mattreynolds 2017/03/09 22:11:31 I think this test isn't really necessary because t
iankc 2017/03/09 23:17:50 Acknowledged.
38 vector<uint8_t> a;
39 vector<uint8_t> b;
40
41 EXPECT_TRUE(ByteVectorCmp(a, b));
42
43 // a larger.
44 a.push_back(1);
45 EXPECT_FALSE(ByteVectorCmp(a, b));
46
47 // b larger.
48 b.push_back(1);
49 b.push_back(3);
50 EXPECT_FALSE(ByteVectorCmp(a, b));
51
52 a.push_back(3);
53 EXPECT_TRUE(ByteVectorCmp(a, b));
54
55 a.pop_back();
56 EXPECT_FALSE(ByteVectorCmp(a, b));
57
58 b.pop_back();
59 EXPECT_TRUE(ByteVectorCmp(a, b));
60 }
61
62 TEST_F(EddystoneEncoderTest, EmptyUrl) {
63 string empty_url = "";
64
65 int expected_result = -1;
66 int actual_result;
67 vector<uint8_t> expected_vector;
68 vector<uint8_t> actual_vector;
69
70 actual_result = physical_web::EncodeUrl(empty_url, &actual_vector);
71
72 EXPECT_TRUE(expected_result == actual_result);
73 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
74 }
75
76 TEST_F(EddystoneEncoderTest, testTotallyInvalidUrl) {
77 string invalid_url = "InValidURL.duh";
78
79 int expected_result = -2;
80 int actual_result;
81 vector<uint8_t> expected_vector;
82 vector<uint8_t> actual_vector;
83
84 actual_result = physical_web::EncodeUrl(invalid_url, &actual_vector);
85
86 EXPECT_TRUE(expected_result == actual_result);
87 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
88 }
89
90 TEST_F(EddystoneEncoderTest, testAlmostvalidUrl) {
91 string invalid_url = "https;//.com";
92
93 int expected_result = -2;
94 int actual_result;
95 vector<uint8_t> expected_vector;
96 vector<uint8_t> actual_vector;
97
98 actual_result = physical_web::EncodeUrl(invalid_url, &actual_vector);
99
100 EXPECT_TRUE(expected_result == actual_result);
101 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
102 }
103
104 TEST_F(EddystoneEncoderTest, testIPAddressUrl) {
105 string invalid_url = "https://8.8.8.8/";
106
107 int expected_result = -3;
108 int actual_result;
109 vector<uint8_t> expected_vector;
110 vector<uint8_t> actual_vector;
111
112 actual_result = physical_web::EncodeUrl(invalid_url, &actual_vector);
113
114 EXPECT_TRUE(expected_result == actual_result);
115 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
116 }
117
118 TEST_F(EddystoneEncoderTest, testInvalidProtocol) {
119 string invalid_url = "file://data/foo.com/it";
120
121 int expected_result = -3;
122 int actual_result;
123 vector<uint8_t> expected_vector;
124 vector<uint8_t> actual_vector;
125
126 actual_result = physical_web::EncodeUrl(invalid_url, &actual_vector);
127
128 EXPECT_TRUE(expected_result == actual_result);
129 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
130 }
131
132 TEST_F(EddystoneEncoderTest, testShortUrl) {
133 string url = "http://a.com";
134 uint8_t expected_array[] = {0x02, // "http://"
135 0x61, // "a"
136 0x07}; // ".com"
137 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
138
139 int expected_result = expected_array_length;
140 int actual_result;
141
142 vector<uint8_t> expected_vector(expected_array,
143 expected_array + expected_array_length);
144 vector<uint8_t> actual_vector;
145
146 actual_result = physical_web::EncodeUrl(url, &actual_vector);
147
148 EXPECT_TRUE(expected_result == actual_result);
149 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
150 }
151
152 TEST_F(EddystoneEncoderTest, testStandardUrl) {
153 string url = "https://www.example.com/";
154 uint8_t expected_array[] = {0x01, // "https://www."
155 0x65, 0x78, 0x61, 0x6d,
156 0x70, 0x6c, 0x65, // "example"
157 0x00}; // ".com/"
158 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
159
160 int expected_result = expected_array_length;
161 int actual_result;
162
163 vector<uint8_t> expected_vector(expected_array,
164 expected_array + expected_array_length);
165 vector<uint8_t> actual_vector;
166
167 actual_result = physical_web::EncodeUrl(url, &actual_vector);
168
169 EXPECT_TRUE(expected_result == actual_result);
170 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
171 }
172
173 TEST_F(EddystoneEncoderTest, testStandardHttpUrl) {
174 string url = "http://www.example.com/";
175 uint8_t expected_array[] = {0x00, // "http://www."
176 0x65, 0x78, 0x61, 0x6d,
177 0x70, 0x6c, 0x65, // "example"
178 0x00}; // ".com/"
179 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
180
181 int expected_result = expected_array_length;
182 int actual_result;
183
184 vector<uint8_t> expected_vector(expected_array,
185 expected_array + expected_array_length);
186 vector<uint8_t> actual_vector;
187
188 actual_result = physical_web::EncodeUrl(url, &actual_vector);
189
190 EXPECT_TRUE(expected_result == actual_result);
191 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
192 }
193
194 TEST_F(EddystoneEncoderTest, testStandardHttpUrlWithoutSuffixSlash) {
195 string url = "http://www.example.com";
196 uint8_t expected_array[] = {0x00, // "http://www."
197 0x65, 0x78, 0x61, 0x6d,
198 0x70, 0x6c, 0x65, // "example"
199 0x07}; // ".com"
200 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
201
202 int expected_result = expected_array_length;
203 int actual_result;
204
205 vector<uint8_t> expected_vector(expected_array,
206 expected_array + expected_array_length);
207 vector<uint8_t> actual_vector;
208
209 actual_result = physical_web::EncodeUrl(url, &actual_vector);
210
211 EXPECT_TRUE(expected_result == actual_result);
212 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
213 }
214
215 TEST_F(EddystoneEncoderTest, testStandardInfoUrl) {
216 string url = "https://www.example.info/";
217 uint8_t expected_array[] = {0x01, // "https://www."
218 0x65, 0x78, 0x61, 0x6d,
219 0x70, 0x6c, 0x65, // "example"
220 0x04}; // ".info/"
221 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
222
223 int expected_result = expected_array_length;
224 int actual_result;
225
226 vector<uint8_t> expected_vector(expected_array,
227 expected_array + expected_array_length);
228 vector<uint8_t> actual_vector;
229
230 actual_result = physical_web::EncodeUrl(url, &actual_vector);
231
232 EXPECT_TRUE(expected_result == actual_result);
233 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
234 }
235
236 TEST_F(EddystoneEncoderTest, testAllowsSubDomains) {
237 string url = "https://www.example.cs.com/";
238 uint8_t expected_array[] = {0x01, // "https://www."
239 0x65, 0x78, 0x61, 0x6d,
240 0x70, 0x6c, 0x65, // "example"
241 0x2e, 0x63, 0x73, // ".cs"
242 0x00}; // ".com/"
243 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
244
245 int expected_result = expected_array_length;
246 int actual_result;
247
248 vector<uint8_t> expected_vector(expected_array,
249 expected_array + expected_array_length);
250 vector<uint8_t> actual_vector;
251
252 actual_result = physical_web::EncodeUrl(url, &actual_vector);
253
254 EXPECT_TRUE(expected_result == actual_result);
255 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
256 }
257
258 TEST_F(EddystoneEncoderTest, testAllowsPaths) {
259 string url = "https://www.example.cs.com/r";
260 uint8_t expected_array[] = {0x01, // "https://www."
261 0x65, 0x78, 0x61, 0x6d,
262 0x70, 0x6c, 0x65, // "example"
263 0x2e, 0x63, 0x73, // ".cs"
264 0x00, // ".com/"
265 0x72}; // "r"
266 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
267
268 int expected_result = expected_array_length;
269 int actual_result;
270
271 vector<uint8_t> expected_vector(expected_array,
272 expected_array + expected_array_length);
273 vector<uint8_t> actual_vector;
274
275 actual_result = physical_web::EncodeUrl(url, &actual_vector);
276
277 EXPECT_TRUE(expected_result == actual_result);
278 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
279 }
280
281 TEST_F(EddystoneEncoderTest, testAllowsMultiplePaths) {
282 string url = "https://www.example.cs.com/r/red/it";
283 uint8_t expected_array[] = {
284 0x01, // "https://www."
285 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, // "example"
286 0x2e, 0x63, 0x73, // ".cs"
287 0x00, // ".com/"
288 0x72, 0x2f, 0x72, 0x65, 0x64, 0x2f, 0x69, 0x74}; // "r/red/it"
289 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
290
291 int expected_result = expected_array_length;
292 int actual_result;
293
294 vector<uint8_t> expected_vector(expected_array,
295 expected_array + expected_array_length);
296 vector<uint8_t> actual_vector;
297
298 actual_result = physical_web::EncodeUrl(url, &actual_vector);
299
300 EXPECT_TRUE(expected_result == actual_result);
301 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
302 }
303
304 TEST_F(EddystoneEncoderTest, testHiddenCompression1) {
305 string url = "https://www.example.com/foo.com";
306 uint8_t expected_array[] = {0x01, // "https://www."
307 0x65, 0x78, 0x61, 0x6d,
308 0x70, 0x6c, 0x65, // "example"
309 0x00, // ".com/"
310 0x66, 0x6f, 0x6f, // "foo"
311 0x07}; // ".com"
312 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
313
314 int expected_result = expected_array_length;
315 int actual_result;
316
317 vector<uint8_t> expected_vector(expected_array,
318 expected_array + expected_array_length);
319 vector<uint8_t> actual_vector;
320
321 actual_result = physical_web::EncodeUrl(url, &actual_vector);
322
323 EXPECT_TRUE(expected_result == actual_result);
324 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
325 }
326
327 TEST_F(EddystoneEncoderTest, testHiddenCompression2) {
328 string url = "https://www.example.com/foo.comma/";
329 uint8_t expected_array[] = {0x01, // "https://www."
330 0x65, 0x78, 0x61, 0x6d,
331 0x70, 0x6c, 0x65, // "example"
332 0x00, // ".com/"
333 0x66, 0x6f, 0x6f, // "foo"
334 0x07, // ".com"
335 0x6d, 0x61, 0x2f}; // "ma/"
336 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
337
338 int expected_result = expected_array_length;
339 int actual_result;
340
341 vector<uint8_t> expected_vector(expected_array,
342 expected_array + expected_array_length);
343 vector<uint8_t> actual_vector;
344
345 actual_result = physical_web::EncodeUrl(url, &actual_vector);
346
347 EXPECT_TRUE(expected_result == actual_result);
348 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
349 }
350
351 TEST_F(EddystoneEncoderTest, testSharedCompression) {
352 string url = "https://www.communities.com/";
353 uint8_t expected_array[] = {0x01, // "https://www."
354 0x63, 0x6f, 0x6d, // "com"
355 0x6d, 0x75, 0x6e, 0x69,
356 0x74, 0x69, 0x65, 0x73, //"munities"
357 0x00}; // ".com/"
358 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
359
360 int expected_result = expected_array_length;
361 int actual_result;
362
363 vector<uint8_t> expected_vector(expected_array,
364 expected_array + expected_array_length);
365 vector<uint8_t> actual_vector;
366
367 actual_result = physical_web::EncodeUrl(url, &actual_vector);
368
369 EXPECT_TRUE(expected_result == actual_result);
370 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
371 }
372
373 TEST_F(EddystoneEncoderTest, testSpecCollision) {
374 // decode(encode(".com/aURL.com")) == decode(encode("http://www.aURL.com"))
375 // without url validation
376 string invalid_url = ".com/aURL.com";
377
378 int invalid_expected_result = -2;
379 int invalid_actual_result;
380 vector<uint8_t> invalid_expected_vector;
381 vector<uint8_t> invalid_actual_vector;
382
383 invalid_actual_result =
384 physical_web::EncodeUrl(invalid_url, &invalid_actual_vector);
385
386 EXPECT_TRUE(invalid_expected_result == invalid_actual_result);
387 EXPECT_TRUE(ByteVectorCmp(invalid_expected_vector, invalid_actual_vector));
388
389 // Now for the valid Url.
390 string valid_url = "http://www.aURL.com";
391 uint8_t valid_expected_array[] = {0x00, // "https://www."
392 0x61, 0x55, 0x52, 0x4c, // "aUrl"
393 0x07}; // ".com"
394 size_t valid_expected_array_length =
395 sizeof(valid_expected_array) / sizeof(uint8_t);
396
397 int valid_expected_result = valid_expected_array_length;
398 int valid_actual_result;
399
400 vector<uint8_t> valid_expected_vector(
401 valid_expected_array, valid_expected_array + valid_expected_array_length);
402 vector<uint8_t> valid_actual_vector;
403
404 valid_actual_result =
405 physical_web::EncodeUrl(valid_url, &valid_actual_vector);
406
407 EXPECT_TRUE(valid_expected_result == valid_actual_result);
408 EXPECT_TRUE(ByteVectorCmp(valid_expected_vector, valid_actual_vector));
409
410 EXPECT_FALSE(ByteVectorCmp(invalid_actual_vector, valid_actual_vector));
411 }
412
413 TEST_F(EddystoneEncoderTest, testComplexUrl) {
414 string url = "http://user:pass@google.com:99/foo;bar?q=a#ref";
415 uint8_t expected_array[] = {0x02, // "http://"
416 0x75, 0x73, 0x65, 0x72, 0x3a, // "user:"
417 0x70, 0x61, 0x73, 0x73, 0x40, // "pass@"
418 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, // "google"
419 0x07, // ".com"
420 0x3a, 0x39, 0x39, 0x2f, // ":99/"
421 0x66, 0x6f, 0x6f, 0x3b, // "foo;"
422 0x62, 0x61, 0x72, 0x3f, // "bar?"
423 0x71, 0x3d, 0x61, 0x23, // "q=a#"
424 0x72, 0x65, 0x66}; // "ref"
425 size_t expected_array_length = sizeof(expected_array) / sizeof(uint8_t);
426
427 int expected_result = expected_array_length;
428 int actual_result;
429
430 vector<uint8_t> expected_vector(expected_array,
431 expected_array + expected_array_length);
432 vector<uint8_t> actual_vector;
433
434 actual_result = physical_web::EncodeUrl(url, &actual_vector);
435
436 EXPECT_TRUE(expected_result == actual_result);
437 EXPECT_TRUE(ByteVectorCmp(expected_vector, actual_vector));
438 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698