| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/service_worker/service_worker_utils.h" | 5 #include "content/browser/service_worker/service_worker_utils.h" | 
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" | 
| 7 | 7 | 
| 8 namespace content { | 8 namespace content { | 
| 9 | 9 | 
| 10 TEST(ServiceWorkerUtilsTest, ScopeMatches) { | 10 TEST(ServiceWorkerUtilsTest, ScopeMatches) { | 
| 11   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 11   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
| 12       GURL("http://www.example.com/*"), GURL("http://www.example.com/"))); | 12             ServiceWorkerUtils::ScopeMatches( | 
| 13   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 13                 GURL("http://www.example.com/*"), | 
| 14       GURL("http://www.example.com/*"), | 14                 GURL("http://www.example.com/"))); | 
| 15       GURL("http://www.example.com/page.html"))); | 15   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 16             ServiceWorkerUtils::ScopeMatches( | 
|  | 17                 GURL("http://www.example.com/*"), | 
|  | 18                 GURL("http://www.example.com/page.html"))); | 
| 16 | 19 | 
| 17   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 20   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 18       GURL("http://www.example.com/*"), GURL("https://www.example.com/"))); | 21             ServiceWorkerUtils::ScopeMatches( | 
| 19   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 22                 GURL("http://www.example.com/*"), | 
| 20       GURL("http://www.example.com/*"), | 23                 GURL("https://www.example.com/"))); | 
| 21       GURL("https://www.example.com/page.html"))); | 24   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
|  | 25             ServiceWorkerUtils::ScopeMatches( | 
|  | 26                 GURL("http://www.example.com/*"), | 
|  | 27                 GURL("https://www.example.com/page.html"))); | 
| 22 | 28 | 
| 23   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 29   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 24       GURL("http://www.example.com/*"), GURL("http://www.foo.com/"))); | 30             ServiceWorkerUtils::ScopeMatches( | 
| 25   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 31                 GURL("http://www.example.com/*"), | 
| 26       GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html"))); | 32                 GURL("http://www.foo.com/"))); | 
|  | 33   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
|  | 34             ServiceWorkerUtils::ScopeMatches( | 
|  | 35                 GURL("http://www.example.com/*"), | 
|  | 36                 GURL("https://www.foo.com/page.html"))); | 
| 27 | 37 | 
| 28   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 38   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
| 29       GURL("http://www.example.com/"), GURL("http://www.example.com/"))); | 39             ServiceWorkerUtils::ScopeMatches( | 
| 30   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 40                 GURL("http://www.example.com/"), | 
| 31       GURL("http://www.example.com/"), GURL("http://www.example.com/x"))); | 41                 GURL("http://www.example.com/"))); | 
|  | 42   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
|  | 43             ServiceWorkerUtils::ScopeMatches( | 
|  | 44                 GURL("http://www.example.com/"), | 
|  | 45                 GURL("http://www.example.com/x"))); | 
| 32 | 46 | 
| 33   // '?' is not a wildcard. | 47   // '?' is not a wildcard. | 
| 34   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 48   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 35       GURL("http://www.example.com/?"), GURL("http://www.example.com/x"))); | 49             ServiceWorkerUtils::ScopeMatches( | 
| 36   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 50                 GURL("http://www.example.com/?"), | 
| 37       GURL("http://www.example.com/?"), GURL("http://www.example.com/"))); | 51                 GURL("http://www.example.com/x"))); | 
| 38   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 52   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 39       GURL("http://www.example.com/?"), GURL("http://www.example.com/xx"))); | 53             ServiceWorkerUtils::ScopeMatches( | 
| 40   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 54                 GURL("http://www.example.com/?"), | 
| 41       GURL("http://www.example.com/?"), GURL("http://www.example.com/?"))); | 55                 GURL("http://www.example.com/"))); | 
|  | 56   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
|  | 57             ServiceWorkerUtils::ScopeMatches( | 
|  | 58                 GURL("http://www.example.com/?"), | 
|  | 59                 GURL("http://www.example.com/xx"))); | 
|  | 60   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 61             ServiceWorkerUtils::ScopeMatches( | 
|  | 62                 GURL("http://www.example.com/?"), | 
|  | 63                 GURL("http://www.example.com/?"))); | 
| 42 | 64 | 
| 43   // Query string is part of the resource. | 65   // Query string is part of the resource. | 
| 44   ASSERT_TRUE( | 66   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
| 45       ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=b"), | 67             ServiceWorkerUtils::ScopeMatches( | 
| 46                                        GURL("http://www.example.com/?a=b"))); | 68                 GURL("http://www.example.com/?a=b"), | 
| 47   ASSERT_TRUE( | 69                 GURL("http://www.example.com/?a=b"))); | 
| 48       ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/?a=*"), | 70   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
| 49                                        GURL("http://www.example.com/?a=b"))); | 71             ServiceWorkerUtils::ScopeMatches( | 
| 50   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 72                 GURL("http://www.example.com/?a=*"), | 
| 51       GURL("http://www.example.com/*"), GURL("http://www.example.com/?a=b"))); | 73                 GURL("http://www.example.com/?a=b"))); | 
| 52   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 74   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
| 53       GURL("http://www.example.com/"), GURL("http://www.example.com/?a=b"))); | 75             ServiceWorkerUtils::ScopeMatches( | 
|  | 76                 GURL("http://www.example.com/*"), | 
|  | 77                 GURL("http://www.example.com/?a=b"))); | 
|  | 78   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
|  | 79             ServiceWorkerUtils::ScopeMatches( | 
|  | 80                 GURL("http://www.example.com/"), | 
|  | 81                 GURL("http://www.example.com/?a=b"))); | 
| 54 | 82 | 
| 55   // '*' only has special meaning in terminal position. | 83   // '*' only has special meaning in terminal position. | 
| 56   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 84   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
| 57       GURL("http://www.example.com/*/x"), GURL("http://www.example.com/*/x"))); | 85             ServiceWorkerUtils::ScopeMatches( | 
| 58   ASSERT_FALSE(ServiceWorkerUtils::ScopeMatches( | 86                 GURL("http://www.example.com/*/x"), | 
| 59       GURL("http://www.example.com/*/x"), GURL("http://www.example.com/a/x"))); | 87                 GURL("http://www.example.com/*/x"))); | 
| 60   ASSERT_FALSE( | 88   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 61       ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), | 89             ServiceWorkerUtils::ScopeMatches( | 
| 62                                        GURL("http://www.example.com/a/x/b"))); | 90                 GURL("http://www.example.com/*/x"), | 
| 63   ASSERT_TRUE( | 91                 GURL("http://www.example.com/a/x"))); | 
| 64       ServiceWorkerUtils::ScopeMatches(GURL("http://www.example.com/*/x/*"), | 92   ASSERT_EQ(ServiceWorkerUtils::SCOPE_NO_MATCH, | 
| 65                                        GURL("http://www.example.com/*/x/b"))); | 93             ServiceWorkerUtils::ScopeMatches( | 
|  | 94                 GURL("http://www.example.com/*/x/*"), | 
|  | 95                 GURL("http://www.example.com/a/x/b"))); | 
|  | 96   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 97             ServiceWorkerUtils::ScopeMatches( | 
|  | 98                 GURL("http://www.example.com/*/x/*"), | 
|  | 99                 GURL("http://www.example.com/*/x/b"))); | 
| 66 | 100 | 
| 67   // URLs canonicalize \ to / so this is equivalent to  "...//*" and "...//x" | 101   // URLs canonicalize \ to / so this is equivalent to  "...//*" and "...//x" | 
| 68   ASSERT_TRUE(ServiceWorkerUtils::ScopeMatches( | 102   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
| 69       GURL("http://www.example.com/\\*"), GURL("http://www.example.com/\\x"))); | 103             ServiceWorkerUtils::ScopeMatches( | 
|  | 104                 GURL("http://www.example.com/\\*"), | 
|  | 105                 GURL("http://www.example.com/\\x"))); | 
|  | 106 } | 
|  | 107 | 
|  | 108 TEST(ServiceWorkerUtilsTest, CompareScopePriorities) { | 
|  | 109   GURL url("http://www.example.com/xxx"); | 
|  | 110 | 
|  | 111   GURL scope1("http://www.example.com/x*"); | 
|  | 112   GURL scope2("http://www.example.com/xxx"); | 
|  | 113   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 114             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 115   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 116             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 117   ASSERT_GT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 118 | 
|  | 119   scope1 = GURL("http://www.example.com/xxx"); | 
|  | 120   scope2 = GURL("http://www.example.com/x*"); | 
|  | 121   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 122             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 123   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 124             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 125   ASSERT_LT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 126 | 
|  | 127   scope1 = GURL("http://www.example.com/xxx*"); | 
|  | 128   scope2 = GURL("http://www.example.com/xxx"); | 
|  | 129   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 130             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 131   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 132             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 133   ASSERT_GT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 134 | 
|  | 135   scope1 = GURL("http://www.example.com/xxx"); | 
|  | 136   scope2 = GURL("http://www.example.com/xxx*"); | 
|  | 137   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 138             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 139   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 140             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 141   ASSERT_LT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 142 | 
|  | 143   scope1 = GURL("http://www.example.com/xxx"); | 
|  | 144   scope2 = GURL("http://www.example.com/xxx"); | 
|  | 145   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 146             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 147   ASSERT_EQ(ServiceWorkerUtils::SCOPE_EXACT_MATCH, | 
|  | 148             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 149   ASSERT_EQ(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 150 | 
|  | 151   scope1 = GURL("http://www.example.com/x*"); | 
|  | 152   scope2 = GURL("http://www.example.com/x*"); | 
|  | 153   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 154             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 155   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 156             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 157   ASSERT_EQ(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 158 | 
|  | 159   scope1 = GURL("http://www.example.com/*"); | 
|  | 160   scope2 = GURL("http://www.example.com/xx*"); | 
|  | 161   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 162             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 163   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 164             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 165   ASSERT_GT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
|  | 166 | 
|  | 167   scope1 = GURL("http://www.example.com/xx*"); | 
|  | 168   scope2 = GURL("http://www.example.com/*"); | 
|  | 169   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 170             ServiceWorkerUtils::ScopeMatches(scope1, url)); | 
|  | 171   ASSERT_EQ(ServiceWorkerUtils::SCOPE_WILDCARD_MATCH, | 
|  | 172             ServiceWorkerUtils::ScopeMatches(scope2, url)); | 
|  | 173   ASSERT_LT(0, ServiceWorkerUtils::CompareScopePriorities(scope1, scope2)); | 
| 70 } | 174 } | 
| 71 | 175 | 
| 72 }  // namespace content | 176 }  // namespace content | 
| OLD | NEW | 
|---|