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

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

Issue 2731273004: Add Platform Independent Eddystone Encoder (Closed)
Patch Set: 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(vector<uint8_t> a, vector<uint8_t> b);
mattreynolds 2017/03/08 19:14:47 Use const references to avoid copies: bool ByteVe
iankc 2017/03/09 20:45:45 Done.
25 };
26
27 bool EddystoneEncoderTest::ByteVectorCmp(vector<uint8_t> a, vector<uint8_t> b) {
28 size_t aSize = a.size();
mattreynolds 2017/03/08 19:14:47 snake_case
iankc 2017/03/09 20:45:45 Done.
29 if (aSize != b.size()) {
30 return false;
31 }
32 return equal(a.begin(), a.end(), b.begin());
33 }
34
35 TEST_F(EddystoneEncoderTest, ByteVectorCmp) {
36 vector<uint8_t> a;
37 vector<uint8_t> b;
38
39 EXPECT_TRUE(ByteVectorCmp(a, b));
40
41 // a larger.
42 a.push_back(1);
43 EXPECT_FALSE(ByteVectorCmp(a, b));
44
45 // b larger.
46 b.push_back(1);
47 b.push_back(3);
48 EXPECT_FALSE(ByteVectorCmp(a, b));
49
50 a.push_back(3);
51 EXPECT_TRUE(ByteVectorCmp(a, b));
52
53 a.pop_back();
54 EXPECT_FALSE(ByteVectorCmp(a, b));
55
56 b.pop_back();
57 EXPECT_TRUE(ByteVectorCmp(a, b));
58 }
59
60 TEST_F(EddystoneEncoderTest, EmptyUrl) {
61 string emptyUrl = "";
62 vector<uint8_t> expected;
63 vector<uint8_t> actual;
64
65 expected.push_back(-1);
66 actual = physical_web::EncodeUrl(emptyUrl);
67
68 EXPECT_TRUE(ByteVectorCmp(expected, actual));
69 }
70
71 TEST_F(EddystoneEncoderTest, testTotallyInvalidUrl) {
72 string invalidUrl = "InValidURL.duh";
73 vector<uint8_t> expected;
74 vector<uint8_t> actual;
75
76 expected.push_back(-2);
77 actual = physical_web::EncodeUrl(invalidUrl);
78
79 EXPECT_TRUE(ByteVectorCmp(expected, actual));
80 }
81
82 TEST_F(EddystoneEncoderTest, testIPAddressUrl) {
83 string invalidUrl = "https://8.8.8.8/";
84 vector<uint8_t> expected;
85 vector<uint8_t> actual;
86
87 expected.push_back(-3);
88 actual = physical_web::EncodeUrl(invalidUrl);
89
90 EXPECT_TRUE(ByteVectorCmp(expected, actual));
91 }
92
93 TEST_F(EddystoneEncoderTest, testAlmostvalidUrl) {
94 string invalidUrl = "https;//.com";
95 vector<uint8_t> expected;
96 vector<uint8_t> actual;
97
98 expected.push_back(-2);
99 actual = physical_web::EncodeUrl(invalidUrl);
100
101 EXPECT_TRUE(ByteVectorCmp(expected, actual));
102 }
103
104 TEST_F(EddystoneEncoderTest, testShortUrl) {
105 string url = "http://a.com";
106 uint8_t expectedArray[] = {0x02, // "http://"
107 0x61, // "a"
108 0x07}; // ".com"
109 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
110
111 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
112 vector<uint8_t> actual = physical_web::EncodeUrl(url);
113
114 EXPECT_TRUE(ByteVectorCmp(expected, actual));
115 }
116
117 TEST_F(EddystoneEncoderTest, testStandardUrl) {
118 string url = "https://www.example.com/";
119 uint8_t expectedArray[] = {0x01, // "https://www."
120 0x65, 0x78, 0x61, 0x6d,
121 0x70, 0x6c, 0x65, // "example"
122 0x00}; // ".com/"
123 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
124
125 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
126 vector<uint8_t> actual = physical_web::EncodeUrl(url);
127
128 EXPECT_TRUE(ByteVectorCmp(expected, actual));
129 }
130
131 TEST_F(EddystoneEncoderTest, testStandardHttpUrl) {
132 string url = "http://www.example.com/";
133 uint8_t expectedArray[] = {0x00, // "http://www."
134 0x65, 0x78, 0x61, 0x6d,
135 0x70, 0x6c, 0x65, // "example"
136 0x00}; // ".com/"
137 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
138
139 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
140 vector<uint8_t> actual = physical_web::EncodeUrl(url);
141
142 EXPECT_TRUE(ByteVectorCmp(expected, actual));
143 }
144
145 TEST_F(EddystoneEncoderTest, testStandardHttpUrlWithoutSuffixSlash) {
146 string url = "http://www.example.com";
147 uint8_t expectedArray[] = {0x00, // "http://www."
148 0x65, 0x78, 0x61, 0x6d,
149 0x70, 0x6c, 0x65, // "example"
150 0x07}; // ".com"
151 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
152
153 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
154 vector<uint8_t> actual = physical_web::EncodeUrl(url);
155
156 EXPECT_TRUE(ByteVectorCmp(expected, actual));
157 }
158
159 TEST_F(EddystoneEncoderTest, testStandardInfoUrl) {
160 string url = "https://www.example.info/";
161 uint8_t expectedArray[] = {0x01, // "https://www."
162 0x65, 0x78, 0x61, 0x6d,
163 0x70, 0x6c, 0x65, // "example"
164 0x04}; // ".info/"
165 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
166
167 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
168 vector<uint8_t> actual = physical_web::EncodeUrl(url);
169
170 EXPECT_TRUE(ByteVectorCmp(expected, actual));
171 }
172
173 TEST_F(EddystoneEncoderTest, testAllowsSubDomains) {
174 string url = "https://www.example.cs.com/";
175 uint8_t expectedArray[] = {0x01, // "https://www."
176 0x65, 0x78, 0x61, 0x6d,
177 0x70, 0x6c, 0x65, // "example"
178 0x2e, 0x63, 0x73, // ".cs"
179 0x00}; // ".com/"
180 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
181
182 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
183 vector<uint8_t> actual = physical_web::EncodeUrl(url);
184
185 EXPECT_TRUE(ByteVectorCmp(expected, actual));
186 }
187
188 TEST_F(EddystoneEncoderTest, testAllowsPaths) {
189 string url = "https://www.example.cs.com/r";
190 uint8_t expectedArray[] = {0x01, // "https://www."
191 0x65, 0x78, 0x61, 0x6d,
192 0x70, 0x6c, 0x65, // "example"
193 0x2e, 0x63, 0x73, // ".cs"
194 0x00, // ".com/"
195 0x72}; // "r"
196 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
197
198 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
199 vector<uint8_t> actual = physical_web::EncodeUrl(url);
200
201 EXPECT_TRUE(ByteVectorCmp(expected, actual));
202 }
203
204 TEST_F(EddystoneEncoderTest, testAllowsMultiplePaths) {
205 string url = "https://www.example.cs.com/r/red/it";
206 uint8_t expectedArray[] = {
207 0x01, // "https://www."
208 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, // "example"
209 0x2e, 0x63, 0x73, // ".cs"
210 0x00, // ".com/"
211 0x72, 0x2f, 0x72, 0x65, 0x64, 0x2f, 0x69, 0x74}; // "r/red/it"
212 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
213
214 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
215 vector<uint8_t> actual = physical_web::EncodeUrl(url);
216
217 EXPECT_TRUE(ByteVectorCmp(expected, actual));
218 }
219
220 TEST_F(EddystoneEncoderTest, testHiddenCompression1) {
221 string url = "https://www.example.com/foo.com";
222 uint8_t expectedArray[] = {0x01, // "https://www."
223 0x65, 0x78, 0x61, 0x6d,
224 0x70, 0x6c, 0x65, // "example"
225 0x00, // ".com/"
226 0x66, 0x6f, 0x6f, // "foo"
227 0x07}; // ".com"
228 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
229
230 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
231 vector<uint8_t> actual = physical_web::EncodeUrl(url);
232
233 EXPECT_TRUE(ByteVectorCmp(expected, actual));
234 }
235
236 TEST_F(EddystoneEncoderTest, testHiddenCompression2) {
237 string url = "https://www.example.com/foo.comma/";
238 uint8_t expectedArray[] = {0x01, // "https://www."
239 0x65, 0x78, 0x61, 0x6d,
240 0x70, 0x6c, 0x65, // "example"
241 0x00, // ".com/"
242 0x66, 0x6f, 0x6f, // "foo"
243 0x07, // ".com"
244 0x6d, 0x61, 0x2f}; // "ma/"
245 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
246
247 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
248 vector<uint8_t> actual = physical_web::EncodeUrl(url);
249
250 EXPECT_TRUE(ByteVectorCmp(expected, actual));
251 }
252
253 TEST_F(EddystoneEncoderTest, testSharedCompression) {
254 string url = "https://www.communities.com/";
255 uint8_t expectedArray[] = {0x01, // "https://www."
256 0x63, 0x6f, 0x6d, // "com"
257 0x6d, 0x75, 0x6e, 0x69,
258 0x74, 0x69, 0x65, 0x73, //"munities"
259 0x00}; // ".com/"
260 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
261
262 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
263 vector<uint8_t> actual = physical_web::EncodeUrl(url);
264
265 EXPECT_TRUE(ByteVectorCmp(expected, actual));
266 }
267
268 TEST_F(EddystoneEncoderTest, testInvalidProtocol) {
269 string invalidUrl = "file://data/foo.com/it";
270 vector<uint8_t> expected;
271 vector<uint8_t> actual;
272
273 expected.push_back(-3);
274 actual = physical_web::EncodeUrl(invalidUrl);
275
276 EXPECT_TRUE(ByteVectorCmp(expected, actual));
277 }
278
279 TEST_F(EddystoneEncoderTest, testSpecCollision) {
280 // decode(encode(".com/aURL")) == decode(encode("http://aURL")) without url
mattreynolds 2017/03/08 19:14:47 Should we check "http://aURL" here too?
iankc 2017/03/09 20:45:45 Done.
281 // validation
282 string invalidUrl = ".com/aURL";
283 vector<uint8_t> expected;
284 vector<uint8_t> actual;
285
286 expected.push_back(-2);
287 actual = physical_web::EncodeUrl(invalidUrl);
288
289 EXPECT_TRUE(ByteVectorCmp(expected, actual));
290 }
291
292 TEST_F(EddystoneEncoderTest, testComplexUrl) {
293 string url = "http://user:pass@google.com:99/foo;bar?q=a#ref";
294 uint8_t expectedArray[] = {0x02, // "http://"
295 0x75, 0x73, 0x65, 0x72, 0x3a, // "user:"
296 0x70, 0x61, 0x73, 0x73, 0x40, // "pass@"
297 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, // "google"
298 0x07, // ".com"
299 0x3a, 0x39, 0x39, 0x2f, // ":99/"
300 0x66, 0x6f, 0x6f, 0x3b, // "foo;"
301 0x62, 0x61, 0x72, 0x3f, // "bar?"
302 0x71, 0x3d, 0x61, 0x23, // "q=a#"
303 0x72, 0x65, 0x66}; // "ref"
mattreynolds 2017/03/08 19:14:47 This is perfect, thanks!
iankc 2017/03/09 20:45:45 Acknowledged.
304 size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t);
305
306 vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength);
307 vector<uint8_t> actual = physical_web::EncodeUrl(url);
308
309 EXPECT_TRUE(ByteVectorCmp(expected, actual));
310 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698