referrer_util_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright 2013 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 "ios/web/common/referrer_util.h"
  5. #include "ios/web/public/navigation/referrer.h"
  6. #include "net/url_request/referrer_policy.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "testing/platform_test.h"
  9. #include "url/gurl.h"
  10. namespace {
  11. const char* const kTestUrls[] = {
  12. "",
  13. "http://user:password@foo.com/a/b/c.html",
  14. "https://foo.com/d.html#fragment",
  15. "http://user:password@bar.net/e/f.html#fragment",
  16. "https://user:password@bar.net/g/h.html",
  17. };
  18. } // namespace
  19. namespace web {
  20. using ReferrerUtilTest = PlatformTest;
  21. // Tests that no matter what the transition and policy, the result is always
  22. // stripped of things that should not be in a referrer (e.g., passwords).
  23. TEST_F(ReferrerUtilTest, ReferrerSanitization) {
  24. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  25. for (unsigned int dest = 0; dest < std::size(kTestUrls); ++dest) {
  26. for (unsigned int policy = 0; policy <= ReferrerPolicyLast; ++policy) {
  27. Referrer referrer(GURL(kTestUrls[source]),
  28. static_cast<ReferrerPolicy>(policy));
  29. std::string value =
  30. ReferrerHeaderValueForNavigation(GURL(kTestUrls[dest]), referrer);
  31. EXPECT_EQ(GURL(value).GetAsReferrer().spec(), value);
  32. }
  33. }
  34. }
  35. }
  36. // Tests that the Always policy works as expected.
  37. TEST_F(ReferrerUtilTest, AlwaysPolicy) {
  38. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  39. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  40. GURL source_url(kTestUrls[source]);
  41. GURL dest_url(kTestUrls[dest]);
  42. Referrer referrer(source_url, ReferrerPolicyAlways);
  43. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  44. // Everything should have a full referrer.
  45. EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
  46. }
  47. }
  48. }
  49. // Tests that the Default policy works as expected, and matches
  50. // NoReferrerWhenDowngrade.
  51. TEST_F(ReferrerUtilTest, DefaultPolicy) {
  52. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  53. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  54. GURL source_url(kTestUrls[source]);
  55. GURL dest_url(kTestUrls[dest]);
  56. Referrer referrer(source_url, ReferrerPolicyDefault);
  57. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  58. // All but secure->insecure should have a full referrer.
  59. if (source_url.SchemeIsCryptographic() &&
  60. !dest_url.SchemeIsCryptographic())
  61. EXPECT_EQ("", value);
  62. else
  63. EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
  64. // Default should match NoReferrerWhenDowngrade in all cases.
  65. referrer.policy = ReferrerPolicyNoReferrerWhenDowngrade;
  66. EXPECT_EQ(value, ReferrerHeaderValueForNavigation(dest_url, referrer));
  67. }
  68. }
  69. }
  70. // Tests that the Never policy works as expected.
  71. TEST_F(ReferrerUtilTest, NeverPolicy) {
  72. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  73. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  74. GURL source_url(kTestUrls[source]);
  75. GURL dest_url(kTestUrls[dest]);
  76. Referrer referrer(source_url, ReferrerPolicyNever);
  77. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  78. // No referrer in any navigation.
  79. EXPECT_EQ("", value);
  80. }
  81. }
  82. }
  83. // Tests that the Origin policy works as expected.
  84. TEST_F(ReferrerUtilTest, OriginPolicy) {
  85. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  86. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  87. GURL source_url(kTestUrls[source]);
  88. GURL dest_url(kTestUrls[dest]);
  89. Referrer referrer(source_url, ReferrerPolicyOrigin);
  90. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  91. // Origin should be sent in all cases, even secure->insecure.
  92. EXPECT_EQ(source_url.DeprecatedGetOriginAsURL().spec(), value);
  93. }
  94. }
  95. }
  96. // Tests that the OriginWhenCrossOrigin policy works as expected.
  97. TEST_F(ReferrerUtilTest, OriginWhenCrossOriginPolicy) {
  98. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  99. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  100. GURL source_url(kTestUrls[source]);
  101. GURL dest_url(kTestUrls[dest]);
  102. Referrer referrer(source_url, ReferrerPolicyOriginWhenCrossOrigin);
  103. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  104. // Full URL for the same origin, and origin for all other cases (even
  105. // secure->insecure).
  106. if (source_url.DeprecatedGetOriginAsURL() ==
  107. dest_url.DeprecatedGetOriginAsURL())
  108. EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
  109. else
  110. EXPECT_EQ(source_url.DeprecatedGetOriginAsURL().spec(), value);
  111. }
  112. }
  113. }
  114. // Tests that the same-origin policy works as expected.
  115. TEST_F(ReferrerUtilTest, SameOriginPolicy) {
  116. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  117. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  118. GURL source_url(kTestUrls[source]);
  119. GURL dest_url(kTestUrls[dest]);
  120. Referrer referrer(source_url, ReferrerPolicySameOrigin);
  121. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  122. // Full URL for the same origin, and nothing for all other cases.
  123. if (source_url.DeprecatedGetOriginAsURL() ==
  124. dest_url.DeprecatedGetOriginAsURL())
  125. EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
  126. else
  127. EXPECT_EQ(std::string(), value);
  128. }
  129. }
  130. }
  131. // Tests that the strict-origin policy works as expected.
  132. TEST_F(ReferrerUtilTest, StrictOriginPolicy) {
  133. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  134. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  135. GURL source_url(kTestUrls[source]);
  136. GURL dest_url(kTestUrls[dest]);
  137. Referrer referrer(source_url, ReferrerPolicyStrictOrigin);
  138. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  139. // No referrer when downgrading, and origin otherwise.
  140. if (source_url.SchemeIsCryptographic() &&
  141. !dest_url.SchemeIsCryptographic())
  142. EXPECT_EQ("", value);
  143. else
  144. EXPECT_EQ(source_url.DeprecatedGetOriginAsURL().spec(), value);
  145. }
  146. }
  147. }
  148. // Tests that the strict-origin-when-cross-origin policy works as expected.
  149. TEST_F(ReferrerUtilTest, StrictOriginWhenCrossOriginPolicy) {
  150. for (unsigned int source = 0; source < std::size(kTestUrls); ++source) {
  151. for (unsigned int dest = 1; dest < std::size(kTestUrls); ++dest) {
  152. GURL source_url(kTestUrls[source]);
  153. GURL dest_url(kTestUrls[dest]);
  154. Referrer referrer(source_url, ReferrerPolicyStrictOriginWhenCrossOrigin);
  155. std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
  156. // No referrer when downgrading, origin when cross-origin but not
  157. // downgrading, and full referrer otherwise.
  158. if (source_url.SchemeIsCryptographic() &&
  159. !dest_url.SchemeIsCryptographic())
  160. EXPECT_EQ("", value);
  161. else if (source_url.DeprecatedGetOriginAsURL() ==
  162. dest_url.DeprecatedGetOriginAsURL())
  163. EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
  164. else
  165. EXPECT_EQ(source_url.DeprecatedGetOriginAsURL().spec(), value);
  166. }
  167. }
  168. }
  169. // Tests that PolicyForNavigation gives the right values.
  170. TEST_F(ReferrerUtilTest, PolicyForNavigation) {
  171. // The request and destination URLs are unused in the current implementation,
  172. // so use a dummy value.
  173. GURL dummy_url;
  174. for (unsigned int policy = 0; policy <= ReferrerPolicyLast; ++policy) {
  175. Referrer referrer(dummy_url, static_cast<ReferrerPolicy>(policy));
  176. net::ReferrerPolicy net_request_policy =
  177. PolicyForNavigation(dummy_url, referrer);
  178. // The test here is deliberately backward from the way the test would
  179. // intuitively work so that it's structured differently from the code it's
  180. // testing, and thus less likely to have a copy/paste bug that passes
  181. // incorrect mappings.
  182. switch (net_request_policy) {
  183. case net::ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
  184. // This corresponds directly to ReferrerPolicyNoReferrerWhenDowngrade,
  185. // which is also how Default works on iOS.
  186. EXPECT_TRUE(policy == ReferrerPolicyDefault ||
  187. policy == ReferrerPolicyNoReferrerWhenDowngrade);
  188. break;
  189. case net::ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN:
  190. EXPECT_EQ(ReferrerPolicyStrictOriginWhenCrossOrigin, policy);
  191. break;
  192. case net::ReferrerPolicy::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN:
  193. EXPECT_EQ(ReferrerPolicyOriginWhenCrossOrigin, policy);
  194. break;
  195. case net::ReferrerPolicy::NEVER_CLEAR:
  196. EXPECT_EQ(ReferrerPolicyAlways, policy);
  197. break;
  198. case net::ReferrerPolicy::ORIGIN:
  199. EXPECT_EQ(ReferrerPolicyOrigin, policy);
  200. break;
  201. case net::ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN:
  202. EXPECT_EQ(ReferrerPolicySameOrigin, policy);
  203. break;
  204. case net::ReferrerPolicy::
  205. ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
  206. EXPECT_EQ(ReferrerPolicyStrictOrigin, policy);
  207. break;
  208. case net::ReferrerPolicy::NO_REFERRER:
  209. EXPECT_EQ(ReferrerPolicyNever, policy);
  210. break;
  211. }
  212. }
  213. }
  214. // Tests that all the strings corresponding to web::ReferrerPolicy values are
  215. // correctly handled.
  216. TEST_F(ReferrerUtilTest, PolicyFromString) {
  217. // The ordering here must match web::ReferrerPolicy; this makes the test
  218. // simpler, at the cost of needing to re-order if the enum is re-ordered.
  219. const char* const kPolicyStrings[] = {
  220. "unsafe-url",
  221. nullptr, // Default is skipped, because no string maps to Default.
  222. "no-referrer-when-downgrade",
  223. "no-referrer",
  224. "origin",
  225. "origin-when-cross-origin",
  226. "same-origin",
  227. "strict-origin",
  228. "strict-origin-when-cross-origin",
  229. };
  230. // Test that all the values are supported.
  231. for (int i = 0; i < ReferrerPolicyLast; ++i) {
  232. if (!kPolicyStrings[i])
  233. continue;
  234. EXPECT_EQ(i, ReferrerPolicyFromString(kPolicyStrings[i]));
  235. }
  236. // Verify that if something is added to the enum, its string value gets added
  237. // to the mapping function.
  238. EXPECT_EQ(ReferrerPolicyLast + 1,
  239. static_cast<int>(std::size(kPolicyStrings)));
  240. // Test the legacy policy names.
  241. EXPECT_EQ(ReferrerPolicyNever, ReferrerPolicyFromString("never"));
  242. // Note that per the spec, "default" maps to NoReferrerWhenDowngrade; the
  243. // Default enum value is not actually a spec'd value.
  244. EXPECT_EQ(ReferrerPolicyNoReferrerWhenDowngrade,
  245. ReferrerPolicyFromString("default"));
  246. EXPECT_EQ(ReferrerPolicyAlways, ReferrerPolicyFromString("always"));
  247. // Test that invalid values map to Default.
  248. EXPECT_EQ(ReferrerPolicyDefault, ReferrerPolicyFromString(""));
  249. EXPECT_EQ(ReferrerPolicyDefault, ReferrerPolicyFromString("made-up"));
  250. }
  251. } // namespace web