Index: components/physical_web/eddystone/eddystone_encoder_unittest.cc |
diff --git a/components/physical_web/eddystone/eddystone_encoder_unittest.cc b/components/physical_web/eddystone/eddystone_encoder_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3907f07b6a1e85b6f8b27fd7f663109f57e3de96 |
--- /dev/null |
+++ b/components/physical_web/eddystone/eddystone_encoder_unittest.cc |
@@ -0,0 +1,310 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/physical_web/eddystone/eddystone_encoder.h" |
+ |
+#include <algorithm> |
+#include <vector> |
+ |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using std::string; |
+using std::vector; |
+using std::equal; |
+ |
+class EddystoneEncoderTest : public testing::Test { |
+ public: |
+ EddystoneEncoderTest() {} |
+ ~EddystoneEncoderTest() override {} |
+ |
+ void SetUp() override {} |
+ void TearDown() override {} |
+ |
+ 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.
|
+}; |
+ |
+bool EddystoneEncoderTest::ByteVectorCmp(vector<uint8_t> a, vector<uint8_t> b) { |
+ size_t aSize = a.size(); |
mattreynolds
2017/03/08 19:14:47
snake_case
iankc
2017/03/09 20:45:45
Done.
|
+ if (aSize != b.size()) { |
+ return false; |
+ } |
+ return equal(a.begin(), a.end(), b.begin()); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, ByteVectorCmp) { |
+ vector<uint8_t> a; |
+ vector<uint8_t> b; |
+ |
+ EXPECT_TRUE(ByteVectorCmp(a, b)); |
+ |
+ // a larger. |
+ a.push_back(1); |
+ EXPECT_FALSE(ByteVectorCmp(a, b)); |
+ |
+ // b larger. |
+ b.push_back(1); |
+ b.push_back(3); |
+ EXPECT_FALSE(ByteVectorCmp(a, b)); |
+ |
+ a.push_back(3); |
+ EXPECT_TRUE(ByteVectorCmp(a, b)); |
+ |
+ a.pop_back(); |
+ EXPECT_FALSE(ByteVectorCmp(a, b)); |
+ |
+ b.pop_back(); |
+ EXPECT_TRUE(ByteVectorCmp(a, b)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, EmptyUrl) { |
+ string emptyUrl = ""; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-1); |
+ actual = physical_web::EncodeUrl(emptyUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testTotallyInvalidUrl) { |
+ string invalidUrl = "InValidURL.duh"; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-2); |
+ actual = physical_web::EncodeUrl(invalidUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testIPAddressUrl) { |
+ string invalidUrl = "https://8.8.8.8/"; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-3); |
+ actual = physical_web::EncodeUrl(invalidUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testAlmostvalidUrl) { |
+ string invalidUrl = "https;//.com"; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-2); |
+ actual = physical_web::EncodeUrl(invalidUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testShortUrl) { |
+ string url = "http://a.com"; |
+ uint8_t expectedArray[] = {0x02, // "http://" |
+ 0x61, // "a" |
+ 0x07}; // ".com" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testStandardUrl) { |
+ string url = "https://www.example.com/"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x00}; // ".com/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testStandardHttpUrl) { |
+ string url = "http://www.example.com/"; |
+ uint8_t expectedArray[] = {0x00, // "http://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x00}; // ".com/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testStandardHttpUrlWithoutSuffixSlash) { |
+ string url = "http://www.example.com"; |
+ uint8_t expectedArray[] = {0x00, // "http://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x07}; // ".com" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testStandardInfoUrl) { |
+ string url = "https://www.example.info/"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x04}; // ".info/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testAllowsSubDomains) { |
+ string url = "https://www.example.cs.com/"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x2e, 0x63, 0x73, // ".cs" |
+ 0x00}; // ".com/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testAllowsPaths) { |
+ string url = "https://www.example.cs.com/r"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x2e, 0x63, 0x73, // ".cs" |
+ 0x00, // ".com/" |
+ 0x72}; // "r" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testAllowsMultiplePaths) { |
+ string url = "https://www.example.cs.com/r/red/it"; |
+ uint8_t expectedArray[] = { |
+ 0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, // "example" |
+ 0x2e, 0x63, 0x73, // ".cs" |
+ 0x00, // ".com/" |
+ 0x72, 0x2f, 0x72, 0x65, 0x64, 0x2f, 0x69, 0x74}; // "r/red/it" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testHiddenCompression1) { |
+ string url = "https://www.example.com/foo.com"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x00, // ".com/" |
+ 0x66, 0x6f, 0x6f, // "foo" |
+ 0x07}; // ".com" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testHiddenCompression2) { |
+ string url = "https://www.example.com/foo.comma/"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x65, 0x78, 0x61, 0x6d, |
+ 0x70, 0x6c, 0x65, // "example" |
+ 0x00, // ".com/" |
+ 0x66, 0x6f, 0x6f, // "foo" |
+ 0x07, // ".com" |
+ 0x6d, 0x61, 0x2f}; // "ma/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testSharedCompression) { |
+ string url = "https://www.communities.com/"; |
+ uint8_t expectedArray[] = {0x01, // "https://www." |
+ 0x63, 0x6f, 0x6d, // "com" |
+ 0x6d, 0x75, 0x6e, 0x69, |
+ 0x74, 0x69, 0x65, 0x73, //"munities" |
+ 0x00}; // ".com/" |
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testInvalidProtocol) { |
+ string invalidUrl = "file://data/foo.com/it"; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-3); |
+ actual = physical_web::EncodeUrl(invalidUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testSpecCollision) { |
+ // 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.
|
+ // validation |
+ string invalidUrl = ".com/aURL"; |
+ vector<uint8_t> expected; |
+ vector<uint8_t> actual; |
+ |
+ expected.push_back(-2); |
+ actual = physical_web::EncodeUrl(invalidUrl); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |
+ |
+TEST_F(EddystoneEncoderTest, testComplexUrl) { |
+ string url = "http://user:pass@google.com:99/foo;bar?q=a#ref"; |
+ uint8_t expectedArray[] = {0x02, // "http://" |
+ 0x75, 0x73, 0x65, 0x72, 0x3a, // "user:" |
+ 0x70, 0x61, 0x73, 0x73, 0x40, // "pass@" |
+ 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, // "google" |
+ 0x07, // ".com" |
+ 0x3a, 0x39, 0x39, 0x2f, // ":99/" |
+ 0x66, 0x6f, 0x6f, 0x3b, // "foo;" |
+ 0x62, 0x61, 0x72, 0x3f, // "bar?" |
+ 0x71, 0x3d, 0x61, 0x23, // "q=a#" |
+ 0x72, 0x65, 0x66}; // "ref" |
mattreynolds
2017/03/08 19:14:47
This is perfect, thanks!
iankc
2017/03/09 20:45:45
Acknowledged.
|
+ size_t expectedArrayLength = sizeof(expectedArray) / sizeof(uint8_t); |
+ |
+ vector<uint8_t> expected(expectedArray, expectedArray + expectedArrayLength); |
+ vector<uint8_t> actual = physical_web::EncodeUrl(url); |
+ |
+ EXPECT_TRUE(ByteVectorCmp(expected, actual)); |
+} |