PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/extensions/common/url_pattern_set_unittest.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 479 lines | 358 code | 97 blank | 24 comment | 1 complexity | b080b9c1cde796f3857621cee6180ba7 MD5 | raw file
  1. // Copyright (c) 2012 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. #include "extensions/common/url_pattern_set.h"
  5. #include <stddef.h>
  6. #include <sstream>
  7. #include "base/values.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "url/gurl.h"
  10. namespace extensions {
  11. namespace {
  12. void AddPattern(URLPatternSet* set, const std::string& pattern) {
  13. int schemes = URLPattern::SCHEME_ALL;
  14. set->AddPattern(URLPattern(schemes, pattern));
  15. }
  16. URLPatternSet Patterns(const std::string& pattern) {
  17. URLPatternSet set;
  18. AddPattern(&set, pattern);
  19. return set;
  20. }
  21. URLPatternSet Patterns(const std::string& pattern1,
  22. const std::string& pattern2) {
  23. URLPatternSet set;
  24. AddPattern(&set, pattern1);
  25. AddPattern(&set, pattern2);
  26. return set;
  27. }
  28. } // namespace
  29. TEST(URLPatternSetTest, Empty) {
  30. URLPatternSet set;
  31. EXPECT_FALSE(set.MatchesURL(GURL("http://www.foo.com/bar")));
  32. EXPECT_FALSE(set.MatchesURL(GURL()));
  33. EXPECT_FALSE(set.MatchesURL(GURL("invalid")));
  34. }
  35. TEST(URLPatternSetTest, One) {
  36. URLPatternSet set;
  37. AddPattern(&set, "http://www.google.com/*");
  38. EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/")));
  39. EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/monkey")));
  40. EXPECT_FALSE(set.MatchesURL(GURL("https://www.google.com/")));
  41. EXPECT_FALSE(set.MatchesURL(GURL("https://www.microsoft.com/")));
  42. }
  43. TEST(URLPatternSetTest, Two) {
  44. URLPatternSet set;
  45. AddPattern(&set, "http://www.google.com/*");
  46. AddPattern(&set, "http://www.yahoo.com/*");
  47. EXPECT_TRUE(set.MatchesURL(GURL("http://www.google.com/monkey")));
  48. EXPECT_TRUE(set.MatchesURL(GURL("http://www.yahoo.com/monkey")));
  49. EXPECT_FALSE(set.MatchesURL(GURL("https://www.apple.com/monkey")));
  50. }
  51. TEST(URLPatternSetTest, StreamOperatorEmpty) {
  52. URLPatternSet set;
  53. std::ostringstream stream;
  54. stream << set;
  55. EXPECT_EQ("{ }", stream.str());
  56. }
  57. TEST(URLPatternSetTest, StreamOperatorOne) {
  58. URLPatternSet set;
  59. AddPattern(&set, "http://www.google.com/*");
  60. std::ostringstream stream;
  61. stream << set;
  62. EXPECT_EQ("{ \"http://www.google.com/*\" }", stream.str());
  63. }
  64. TEST(URLPatternSetTest, StreamOperatorTwo) {
  65. URLPatternSet set;
  66. AddPattern(&set, "http://www.google.com/*");
  67. AddPattern(&set, "http://www.yahoo.com/*");
  68. std::ostringstream stream;
  69. stream << set;
  70. EXPECT_EQ("{ \"http://www.google.com/*\", \"http://www.yahoo.com/*\" }",
  71. stream.str());
  72. }
  73. TEST(URLPatternSetTest, OverlapsWith) {
  74. URLPatternSet set1;
  75. AddPattern(&set1, "http://www.google.com/f*");
  76. AddPattern(&set1, "http://www.yahoo.com/b*");
  77. URLPatternSet set2;
  78. AddPattern(&set2, "http://www.reddit.com/f*");
  79. AddPattern(&set2, "http://www.yahoo.com/z*");
  80. URLPatternSet set3;
  81. AddPattern(&set3, "http://www.google.com/q/*");
  82. AddPattern(&set3, "http://www.yahoo.com/b/*");
  83. EXPECT_FALSE(set1.OverlapsWith(set2));
  84. EXPECT_FALSE(set2.OverlapsWith(set1));
  85. EXPECT_TRUE(set1.OverlapsWith(set3));
  86. EXPECT_TRUE(set3.OverlapsWith(set1));
  87. }
  88. TEST(URLPatternSetTest, CreateDifference) {
  89. URLPatternSet expected;
  90. URLPatternSet set1;
  91. URLPatternSet set2;
  92. AddPattern(&set1, "http://www.google.com/f*");
  93. AddPattern(&set1, "http://www.yahoo.com/b*");
  94. // Subtract an empty set.
  95. URLPatternSet result = URLPatternSet::CreateDifference(set1, set2);
  96. EXPECT_EQ(set1, result);
  97. // Subtract a real set.
  98. AddPattern(&set2, "http://www.reddit.com/f*");
  99. AddPattern(&set2, "http://www.yahoo.com/z*");
  100. AddPattern(&set2, "http://www.google.com/f*");
  101. AddPattern(&expected, "http://www.yahoo.com/b*");
  102. result = URLPatternSet::CreateDifference(set1, set2);
  103. EXPECT_EQ(expected, result);
  104. EXPECT_FALSE(result.is_empty());
  105. EXPECT_TRUE(set1.Contains(result));
  106. EXPECT_FALSE(result.Contains(set2));
  107. EXPECT_FALSE(set2.Contains(result));
  108. URLPatternSet intersection = URLPatternSet::CreateIntersection(result, set2);
  109. EXPECT_TRUE(intersection.is_empty());
  110. }
  111. TEST(URLPatternSetTest, CreateIntersection) {
  112. URLPatternSet empty_set;
  113. URLPatternSet expected;
  114. URLPatternSet set1;
  115. AddPattern(&set1, "http://www.google.com/f*");
  116. AddPattern(&set1, "http://www.yahoo.com/b*");
  117. // Intersection with an empty set.
  118. URLPatternSet result = URLPatternSet::CreateIntersection(set1, empty_set);
  119. EXPECT_EQ(expected, result);
  120. EXPECT_TRUE(result.is_empty());
  121. EXPECT_TRUE(empty_set.Contains(result));
  122. EXPECT_TRUE(result.Contains(empty_set));
  123. EXPECT_TRUE(set1.Contains(result));
  124. // Intersection with a real set.
  125. URLPatternSet set2;
  126. AddPattern(&set2, "http://www.reddit.com/f*");
  127. AddPattern(&set2, "http://www.yahoo.com/z*");
  128. AddPattern(&set2, "http://www.google.com/f*");
  129. AddPattern(&expected, "http://www.google.com/f*");
  130. result = URLPatternSet::CreateIntersection(set1, set2);
  131. EXPECT_EQ(expected, result);
  132. EXPECT_FALSE(result.is_empty());
  133. EXPECT_TRUE(set1.Contains(result));
  134. EXPECT_TRUE(set2.Contains(result));
  135. }
  136. TEST(URLPatternSetTest, CreateSemanticIntersection) {
  137. {
  138. URLPatternSet set1;
  139. AddPattern(&set1, "http://*.google.com/*");
  140. AddPattern(&set1, "http://*.yahoo.com/*");
  141. URLPatternSet set2;
  142. AddPattern(&set2, "http://google.com/*");
  143. // The semantic intersection should contain only those patterns that are in
  144. // both set 1 and set 2, or "http://google.com/*".
  145. URLPatternSet intersection =
  146. URLPatternSet::CreateSemanticIntersection(set1, set2);
  147. ASSERT_EQ(1u, intersection.size());
  148. EXPECT_EQ("http://google.com/*", intersection.begin()->GetAsString());
  149. }
  150. {
  151. // We don't handle funny intersections, where the resultant pattern is
  152. // neither of the two compared patterns. So the intersection of these two
  153. // is not http://www.google.com/*, but rather nothing.
  154. URLPatternSet set1;
  155. AddPattern(&set1, "http://*/*");
  156. URLPatternSet set2;
  157. AddPattern(&set2, "*://www.google.com/*");
  158. EXPECT_TRUE(
  159. URLPatternSet::CreateSemanticIntersection(set1, set2).is_empty());
  160. }
  161. }
  162. TEST(URLPatternSetTest, CreateUnion) {
  163. URLPatternSet empty_set;
  164. URLPatternSet set1;
  165. AddPattern(&set1, "http://www.google.com/f*");
  166. AddPattern(&set1, "http://www.yahoo.com/b*");
  167. URLPatternSet expected;
  168. AddPattern(&expected, "http://www.google.com/f*");
  169. AddPattern(&expected, "http://www.yahoo.com/b*");
  170. // Union with an empty set.
  171. URLPatternSet result = URLPatternSet::CreateUnion(set1, empty_set);
  172. EXPECT_EQ(expected, result);
  173. // Union with a real set.
  174. URLPatternSet set2;
  175. AddPattern(&set2, "http://www.reddit.com/f*");
  176. AddPattern(&set2, "http://www.yahoo.com/z*");
  177. AddPattern(&set2, "http://www.google.com/f*");
  178. AddPattern(&expected, "http://www.reddit.com/f*");
  179. AddPattern(&expected, "http://www.yahoo.com/z*");
  180. result = URLPatternSet::CreateUnion(set1, set2);
  181. EXPECT_EQ(expected, result);
  182. }
  183. TEST(URLPatternSetTest, Contains) {
  184. URLPatternSet set1;
  185. URLPatternSet set2;
  186. URLPatternSet empty_set;
  187. AddPattern(&set1, "http://www.google.com/*");
  188. AddPattern(&set1, "http://www.yahoo.com/*");
  189. AddPattern(&set2, "http://www.reddit.com/*");
  190. EXPECT_FALSE(set1.Contains(set2));
  191. EXPECT_TRUE(set1.Contains(empty_set));
  192. EXPECT_FALSE(empty_set.Contains(set1));
  193. AddPattern(&set2, "http://www.yahoo.com/*");
  194. EXPECT_FALSE(set1.Contains(set2));
  195. EXPECT_FALSE(set2.Contains(set1));
  196. AddPattern(&set2, "http://www.google.com/*");
  197. EXPECT_FALSE(set1.Contains(set2));
  198. EXPECT_TRUE(set2.Contains(set1));
  199. // Note that this checks if individual patterns contain other patterns, not
  200. // just equality. For example:
  201. AddPattern(&set1, "http://*.reddit.com/*");
  202. EXPECT_TRUE(set1.Contains(set2));
  203. EXPECT_FALSE(set2.Contains(set1));
  204. }
  205. TEST(URLPatternSetTest, Duplicates) {
  206. URLPatternSet set1;
  207. URLPatternSet set2;
  208. AddPattern(&set1, "http://www.google.com/*");
  209. AddPattern(&set2, "http://www.google.com/*");
  210. AddPattern(&set1, "http://www.google.com/*");
  211. // The sets should still be equal after adding a duplicate.
  212. EXPECT_EQ(set2, set1);
  213. }
  214. TEST(URLPatternSetTest, ToValueAndPopulate) {
  215. URLPatternSet set1;
  216. URLPatternSet set2;
  217. std::vector<std::string> patterns;
  218. patterns.push_back("http://www.google.com/*");
  219. patterns.push_back("http://www.yahoo.com/*");
  220. for (size_t i = 0; i < patterns.size(); ++i)
  221. AddPattern(&set1, patterns[i]);
  222. std::string error;
  223. bool allow_file_access = false;
  224. scoped_ptr<base::ListValue> value(set1.ToValue());
  225. set2.Populate(*value, URLPattern::SCHEME_ALL, allow_file_access, &error);
  226. EXPECT_EQ(set1, set2);
  227. set2.ClearPatterns();
  228. set2.Populate(patterns, URLPattern::SCHEME_ALL, allow_file_access, &error);
  229. EXPECT_EQ(set1, set2);
  230. }
  231. TEST(URLPatternSetTest, NwayUnion) {
  232. std::string google_a = "http://www.google.com/a*";
  233. std::string google_b = "http://www.google.com/b*";
  234. std::string google_c = "http://www.google.com/c*";
  235. std::string yahoo_a = "http://www.yahoo.com/a*";
  236. std::string yahoo_b = "http://www.yahoo.com/b*";
  237. std::string yahoo_c = "http://www.yahoo.com/c*";
  238. std::string reddit_a = "http://www.reddit.com/a*";
  239. std::string reddit_b = "http://www.reddit.com/b*";
  240. std::string reddit_c = "http://www.reddit.com/c*";
  241. // Empty list.
  242. {
  243. std::vector<URLPatternSet> empty;
  244. URLPatternSet result = URLPatternSet::CreateUnion(empty);
  245. URLPatternSet expected;
  246. EXPECT_EQ(expected, result);
  247. }
  248. // Singleton list.
  249. {
  250. std::vector<URLPatternSet> test;
  251. test.push_back(Patterns(google_a));
  252. URLPatternSet result = URLPatternSet::CreateUnion(test);
  253. URLPatternSet expected = Patterns(google_a);
  254. EXPECT_EQ(expected, result);
  255. }
  256. // List with 2 elements.
  257. {
  258. std::vector<URLPatternSet> test;
  259. test.push_back(Patterns(google_a, google_b));
  260. test.push_back(Patterns(google_b, google_c));
  261. URLPatternSet result = URLPatternSet::CreateUnion(test);
  262. URLPatternSet expected;
  263. AddPattern(&expected, google_a);
  264. AddPattern(&expected, google_b);
  265. AddPattern(&expected, google_c);
  266. EXPECT_EQ(expected, result);
  267. }
  268. // List with 3 elements.
  269. {
  270. std::vector<URLPatternSet> test;
  271. test.push_back(Patterns(google_a, google_b));
  272. test.push_back(Patterns(google_b, google_c));
  273. test.push_back(Patterns(yahoo_a, yahoo_b));
  274. URLPatternSet result = URLPatternSet::CreateUnion(test);
  275. URLPatternSet expected;
  276. AddPattern(&expected, google_a);
  277. AddPattern(&expected, google_b);
  278. AddPattern(&expected, google_c);
  279. AddPattern(&expected, yahoo_a);
  280. AddPattern(&expected, yahoo_b);
  281. EXPECT_EQ(expected, result);
  282. }
  283. // List with 7 elements.
  284. {
  285. std::vector<URLPatternSet> test;
  286. test.push_back(Patterns(google_a));
  287. test.push_back(Patterns(google_b));
  288. test.push_back(Patterns(google_c));
  289. test.push_back(Patterns(yahoo_a));
  290. test.push_back(Patterns(yahoo_b));
  291. test.push_back(Patterns(yahoo_c));
  292. test.push_back(Patterns(reddit_a));
  293. URLPatternSet result = URLPatternSet::CreateUnion(test);
  294. URLPatternSet expected;
  295. AddPattern(&expected, google_a);
  296. AddPattern(&expected, google_b);
  297. AddPattern(&expected, google_c);
  298. AddPattern(&expected, yahoo_a);
  299. AddPattern(&expected, yahoo_b);
  300. AddPattern(&expected, yahoo_c);
  301. AddPattern(&expected, reddit_a);
  302. EXPECT_EQ(expected, result);
  303. }
  304. // List with 8 elements.
  305. {
  306. std::vector<URLPatternSet> test;
  307. test.push_back(Patterns(google_a));
  308. test.push_back(Patterns(google_b));
  309. test.push_back(Patterns(google_c));
  310. test.push_back(Patterns(yahoo_a));
  311. test.push_back(Patterns(yahoo_b));
  312. test.push_back(Patterns(yahoo_c));
  313. test.push_back(Patterns(reddit_a));
  314. test.push_back(Patterns(reddit_b));
  315. URLPatternSet result = URLPatternSet::CreateUnion(test);
  316. URLPatternSet expected;
  317. AddPattern(&expected, google_a);
  318. AddPattern(&expected, google_b);
  319. AddPattern(&expected, google_c);
  320. AddPattern(&expected, yahoo_a);
  321. AddPattern(&expected, yahoo_b);
  322. AddPattern(&expected, yahoo_c);
  323. AddPattern(&expected, reddit_a);
  324. AddPattern(&expected, reddit_b);
  325. EXPECT_EQ(expected, result);
  326. }
  327. // List with 9 elements.
  328. {
  329. std::vector<URLPatternSet> test;
  330. test.push_back(Patterns(google_a));
  331. test.push_back(Patterns(google_b));
  332. test.push_back(Patterns(google_c));
  333. test.push_back(Patterns(yahoo_a));
  334. test.push_back(Patterns(yahoo_b));
  335. test.push_back(Patterns(yahoo_c));
  336. test.push_back(Patterns(reddit_a));
  337. test.push_back(Patterns(reddit_b));
  338. test.push_back(Patterns(reddit_c));
  339. URLPatternSet result = URLPatternSet::CreateUnion(test);
  340. URLPatternSet expected;
  341. AddPattern(&expected, google_a);
  342. AddPattern(&expected, google_b);
  343. AddPattern(&expected, google_c);
  344. AddPattern(&expected, yahoo_a);
  345. AddPattern(&expected, yahoo_b);
  346. AddPattern(&expected, yahoo_c);
  347. AddPattern(&expected, reddit_a);
  348. AddPattern(&expected, reddit_b);
  349. AddPattern(&expected, reddit_c);
  350. EXPECT_EQ(expected, result);
  351. }
  352. }
  353. TEST(URLPatternSetTest, AddOrigin) {
  354. URLPatternSet set;
  355. EXPECT_TRUE(set.AddOrigin(
  356. URLPattern::SCHEME_ALL, GURL("https://www.google.com/")));
  357. EXPECT_TRUE(set.MatchesURL(GURL("https://www.google.com/foo/bar")));
  358. EXPECT_FALSE(set.MatchesURL(GURL("http://www.google.com/foo/bar")));
  359. EXPECT_FALSE(set.MatchesURL(GURL("https://en.google.com/foo/bar")));
  360. set.ClearPatterns();
  361. EXPECT_TRUE(set.AddOrigin(
  362. URLPattern::SCHEME_ALL, GURL("https://google.com/")));
  363. EXPECT_FALSE(set.MatchesURL(GURL("https://www.google.com/foo/bar")));
  364. EXPECT_TRUE(set.MatchesURL(GURL("https://google.com/foo/bar")));
  365. EXPECT_FALSE(set.AddOrigin(
  366. URLPattern::SCHEME_HTTP, GURL("https://google.com/")));
  367. }
  368. TEST(URLPatternSetTest, ToStringVector) {
  369. URLPatternSet set;
  370. AddPattern(&set, "https://google.com/");
  371. AddPattern(&set, "https://google.com/");
  372. AddPattern(&set, "https://yahoo.com/");
  373. scoped_ptr<std::vector<std::string>> string_vector(set.ToStringVector());
  374. EXPECT_EQ(2UL, string_vector->size());
  375. const auto begin = string_vector->begin();
  376. const auto end = string_vector->end();
  377. auto it = std::find(begin, end, "https://google.com/");
  378. EXPECT_NE(it, end);
  379. it = std::find(begin, end, "https://yahoo.com/");
  380. EXPECT_NE(it, end);
  381. }
  382. } // namespace extensions