OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/password_manager/core/browser/affiliation_utils.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace password_manager { |
| 10 |
| 11 namespace { |
| 12 const char kTestFacetURI1[] = "https://alpha.example.com/"; |
| 13 const char kTestFacetURI2[] = "https://beta.example.com/"; |
| 14 const char kTestFacetURI3[] = "https://gamma.example.com/"; |
| 15 } // namespace |
| 16 |
| 17 TEST(AffiliationUtilsTest, ValidWebFacetURIs) { |
| 18 struct { |
| 19 const char* valid_facet_uri; |
| 20 const char* expected_canonical_facet_uri; |
| 21 } kTestCases[] = { |
| 22 {"https://www.example.com", "https://www.example.com"}, |
| 23 {"HTTPS://www.EXAMPLE.com", "https://www.example.com"}, |
| 24 {"https://0321.0x86.161.0043", "https://209.134.161.35"}, |
| 25 {"https://www.%65xample.com", "https://www.example.com"}, |
| 26 {"https://sz\xc3\xb3t\xc3\xa1r.example.com", |
| 27 "https://xn--sztr-7na0i.example.com"}, |
| 28 {"https://www.example.com/", "https://www.example.com"}, |
| 29 {"https://www.example.com:", "https://www.example.com"}, |
| 30 {"https://@www.example.com", "https://www.example.com"}, |
| 31 {"https://:@www.example.com", "https://www.example.com"}, |
| 32 {"https://www.example.com:8080", "https://www.example.com:8080"}, |
| 33 {"https://new-gtld", "https://new-gtld"}}; |
| 34 for (const auto& test_case : kTestCases) { |
| 35 SCOPED_TRACE(testing::Message("URI = ") << test_case.valid_facet_uri); |
| 36 FacetURI facet_uri = |
| 37 FacetURI::FromPotentiallyInvalidSpec(test_case.valid_facet_uri); |
| 38 EXPECT_TRUE(facet_uri.IsValidWebFacetURI()); |
| 39 EXPECT_EQ(std::string(test_case.expected_canonical_facet_uri), |
| 40 facet_uri.canonical_spec()); |
| 41 } |
| 42 } |
| 43 |
| 44 TEST(AffiliationUtilsTest, InvalidWebFacetURIs) { |
| 45 const char* kInvalidFacetURIs[]{ |
| 46 // Invalid URL (actually, will be treated as having only a host part). |
| 47 "Does not look like a valid URL.", |
| 48 // Path is more than just the root path ('/'). |
| 49 "https://www.example.com/path", |
| 50 // Empty scheme and not HTTPS scheme. |
| 51 "://www.example.com", |
| 52 "http://www.example.com/", |
| 53 // Forbidden non-empty components. |
| 54 "https://user@www.example.com/", |
| 55 "https://:password@www.example.com/", |
| 56 "https://www.example.com/?", |
| 57 "https://www.example.com/?query", |
| 58 "https://www.example.com/#", |
| 59 "https://www.example.com/#ref", |
| 60 // Valid Android facet URI. |
| 61 "android://hash@com.example.android"}; |
| 62 for (const char* uri : kInvalidFacetURIs) { |
| 63 SCOPED_TRACE(testing::Message("URI = ") << uri); |
| 64 FacetURI facet_uri = FacetURI::FromPotentiallyInvalidSpec(uri); |
| 65 EXPECT_FALSE(facet_uri.IsValidWebFacetURI()); |
| 66 } |
| 67 } |
| 68 |
| 69 TEST(AffiliationUtilsTest, ValidAndroidFacetURIs) { |
| 70 struct { |
| 71 const char* valid_facet_uri; |
| 72 const char* expected_canonical_facet_uri; |
| 73 } kTestCases[] = { |
| 74 {"android://" |
| 75 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" |
| 76 "@com.example.android", |
| 77 "android://" |
| 78 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" |
| 79 "@com.example.android"}, |
| 80 {"ANDROID://" |
| 81 "hash@abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
| 82 "android://" |
| 83 "hash@abcdefghijklmnopqrstuvwxyz_0123456789.ABCDEFGHIJKLMNOPQRSTUVWXYZ"}, |
| 84 {"android://padding=@com.example.android", |
| 85 "android://padding=@com.example.android"}, |
| 86 {"android://escape%3D%3D@com.%65xample.android", |
| 87 "android://escape==@com.example.android"}, |
| 88 {"ANDROID://hash@com.example.android", |
| 89 "android://hash@com.example.android"}, |
| 90 {"android://hash@com.example.android/", |
| 91 "android://hash@com.example.android"}, |
| 92 {"android://hash:@com.example.android", |
| 93 "android://hash@com.example.android"}}; |
| 94 for (const auto& test_case : kTestCases) { |
| 95 SCOPED_TRACE(testing::Message("URI = ") << test_case.valid_facet_uri); |
| 96 FacetURI facet_uri = |
| 97 FacetURI::FromPotentiallyInvalidSpec(test_case.valid_facet_uri); |
| 98 EXPECT_TRUE(facet_uri.IsValidAndroidFacetURI()); |
| 99 EXPECT_EQ(test_case.expected_canonical_facet_uri, |
| 100 facet_uri.canonical_spec()); |
| 101 } |
| 102 } |
| 103 |
| 104 TEST(AffiliationUtilsTest, InvalidAndroidFacetURIs) { |
| 105 const char* kInvalidFacetURIs[]{ |
| 106 // Invalid URL (actually, will be treated as having only a host part). |
| 107 "Does not look like a valid URL.", |
| 108 // Path is more than just the root path ('/'). |
| 109 "android://hash@com.example.android/path", |
| 110 // Empty scheme or not "android" scheme. |
| 111 "://hash@com.example.android", |
| 112 "http://hash@com.example.android", |
| 113 // Package name with illegal characters. |
| 114 "android://hash@com.$example.android", |
| 115 "android://hash@com-example-android", |
| 116 "android://hash@com%2Dexample.android", // Escaped '-'. |
| 117 "android://hash@com.example.sz\xc3\xb3t\xc3\xa1r", // UTF-8 o' and a'. |
| 118 // Empty, non-existent and malformed hash part. |
| 119 "android://@com.example.android", |
| 120 "android://com.example.android", |
| 121 "android://badlength@com.example.android", |
| 122 "android://toolongpaddin===@com.example.android", |
| 123 "android://invalid+characters@com.example.android", |
| 124 "android://invalid%2Fcharacters@com.example.android", // Escaped '/'. |
| 125 // Forbidden non-empty components. |
| 126 "android://hash:password@com.example.android", |
| 127 "android://hash@com.example.android:port", |
| 128 "android://hash@com.example.android/?", |
| 129 "android://hash@com.example.android/?query", |
| 130 "android://hash@com.example.android/#", |
| 131 "android://hash@com.example.android/#ref", |
| 132 // Valid Web facet URI. |
| 133 "https://www.example.com/"}; |
| 134 for (const char* uri : kInvalidFacetURIs) { |
| 135 SCOPED_TRACE(testing::Message("URI = ") << uri); |
| 136 FacetURI facet_uri = FacetURI::FromPotentiallyInvalidSpec(uri); |
| 137 EXPECT_FALSE(facet_uri.IsValidAndroidFacetURI()); |
| 138 } |
| 139 } |
| 140 |
| 141 TEST(AffiliationUtilsTest, EqualEquivalenceClasses) { |
| 142 AffiliatedFacets a; |
| 143 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI1)); |
| 144 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI2)); |
| 145 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI3)); |
| 146 |
| 147 AffiliatedFacets b; |
| 148 b.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI3)); |
| 149 b.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI1)); |
| 150 b.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI2)); |
| 151 |
| 152 EXPECT_TRUE(AreEquivalenceClassesEqual(a, a)); |
| 153 EXPECT_TRUE(AreEquivalenceClassesEqual(b, b)); |
| 154 EXPECT_TRUE(AreEquivalenceClassesEqual(b, a)); |
| 155 EXPECT_TRUE(AreEquivalenceClassesEqual(a, b)); |
| 156 } |
| 157 |
| 158 TEST(AffiliationUtilsTest, NotEqualEquivalenceClasses) { |
| 159 AffiliatedFacets a; |
| 160 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI1)); |
| 161 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI2)); |
| 162 |
| 163 AffiliatedFacets b; |
| 164 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI2)); |
| 165 b.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI3)); |
| 166 |
| 167 AffiliatedFacets c; |
| 168 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI1)); |
| 169 a.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI2)); |
| 170 b.push_back(FacetURI::FromCanonicalSpec(kTestFacetURI3)); |
| 171 |
| 172 EXPECT_FALSE(AreEquivalenceClassesEqual(a, b)); |
| 173 EXPECT_FALSE(AreEquivalenceClassesEqual(a, c)); |
| 174 EXPECT_FALSE(AreEquivalenceClassesEqual(b, a)); |
| 175 EXPECT_FALSE(AreEquivalenceClassesEqual(b, c)); |
| 176 EXPECT_FALSE(AreEquivalenceClassesEqual(c, a)); |
| 177 EXPECT_FALSE(AreEquivalenceClassesEqual(c, b)); |
| 178 } |
| 179 |
| 180 } // namespace password_manager |
OLD | NEW |