gurl_os_handler_unittest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2021 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 "chromeos/crosapi/cpp/gurl_os_handler_utils.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. #include "url/gurl.h"
  7. #include "url/url_util.h"
  8. namespace crosapi {
  9. namespace gurl_os_handler_utils {
  10. TEST(GurlOsHandlerUtilsTest, SanitizeAshURL) {
  11. // Using a known GURL scheme, we should get scheme + host + sub-host.
  12. EXPECT_EQ(SanitizeAshURL(GURL("http://version")), GURL("http://version"));
  13. EXPECT_EQ(SanitizeAshURL(GURL("http://version/#foo")),
  14. GURL("http://version"));
  15. EXPECT_EQ(SanitizeAshURL(GURL("http://version/1/#foo")),
  16. GURL("http://version/1/"));
  17. EXPECT_EQ(SanitizeAshURL(GURL("http://version/1/?foo")),
  18. GURL("http://version/1/"));
  19. EXPECT_EQ(SanitizeAshURL(GURL("http://version/foo/?bar")),
  20. GURL("http://version/foo/"));
  21. EXPECT_EQ(SanitizeAshURL(GURL("http://version/foo/?bar"),
  22. /*include_path=*/false),
  23. GURL("http://version/"));
  24. EXPECT_EQ(SanitizeAshURL(GURL("http://version/foo")),
  25. GURL("http://version/foo"));
  26. // Using an unknown GURL scheme, we should get the same.
  27. EXPECT_EQ(SanitizeAshURL(GURL("os://version")), GURL("os://version"));
  28. EXPECT_EQ(SanitizeAshURL(GURL("os://version/#foo")), GURL("os://version"));
  29. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/#foo")),
  30. GURL("os://version/1"));
  31. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/?foo")),
  32. GURL("os://version/1"));
  33. EXPECT_EQ(SanitizeAshURL(GURL("os://version/foo/?bar")),
  34. GURL("os://version/foo"));
  35. // Using a standard path, we should get scheme + host.
  36. EXPECT_EQ(SanitizeAshURL(GURL("os://version")), GURL("os://version"));
  37. // Passing in an empty scheme will lead into an invalid result.
  38. EXPECT_EQ(SanitizeAshURL(GURL("os://")), GURL(""));
  39. // Any path more than that will be ignored.
  40. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1")), GURL("os://version/1"));
  41. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/foo")), GURL("os://version/1"));
  42. EXPECT_EQ(SanitizeAshURL(GURL("os://version#")), GURL("os://version"));
  43. EXPECT_EQ(SanitizeAshURL(GURL("os://version?")), GURL("os://version"));
  44. EXPECT_EQ(SanitizeAshURL(GURL("os://version&")), GURL("os://version"));
  45. // Special characters get ignored
  46. EXPECT_EQ(SanitizeAshURL(GURL("os://version%65")), GURL("os://version"));
  47. // Passing in any parameters, etc. we should get nothing as that is invalid.
  48. EXPECT_EQ(SanitizeAshURL(GURL("os://version/query?foo=1&bar=1")),
  49. GURL("os://version/query"));
  50. EXPECT_EQ(SanitizeAshURL(GURL("os://version/+/query")), GURL("os://version"));
  51. EXPECT_EQ(SanitizeAshURL(GURL("os://version/#foo")), GURL("os://version"));
  52. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/#foo")),
  53. GURL("os://version/1"));
  54. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/?foo")),
  55. GURL("os://version/1"));
  56. EXPECT_EQ(SanitizeAshURL(GURL("os://version/1/?foo"), /*include_path=*/false),
  57. GURL("os://version"));
  58. // Invalid syntax of kind will be detected by GURL as well.
  59. EXPECT_EQ(SanitizeAshURL(GURL("os://version/foo#")),
  60. GURL("os://version/foo"));
  61. EXPECT_EQ(SanitizeAshURL(GURL("os://version/ver\\")),
  62. GURL("os://version/ver"));
  63. EXPECT_EQ(SanitizeAshURL(GURL("os://version/foo bar")),
  64. GURL("os://version/foo"));
  65. // Case insensitive
  66. EXPECT_EQ(SanitizeAshURL(GURL("Os://Foo/Bar")), GURL("os://foo/bar"));
  67. // Check that a port get removed.
  68. EXPECT_EQ(SanitizeAshURL(GURL("https://a.test:35649/title1.html")),
  69. GURL("https://a.test/title1.html"));
  70. }
  71. TEST(GurlOsHandlerUtilsTest, IsURLInList) {
  72. std::vector<GURL> list_of_urls = {
  73. GURL("os://version"), GURL("Os://version2"), GURL("http://version"),
  74. GURL("http://Foobar"), GURL("os://flags"), GURL("os://foo/bar")};
  75. // As we expect the input to be sanitized, we cannot add any parameters.
  76. EXPECT_TRUE(IsUrlInList(GURL("os://version"), list_of_urls));
  77. EXPECT_TRUE(IsUrlInList(GURL("os://flags"), list_of_urls));
  78. EXPECT_TRUE(IsUrlInList(GURL("http://version"), list_of_urls));
  79. // Sub hosts can - or cannot be supplied.
  80. EXPECT_TRUE(IsUrlInList(GURL("os://version/12"), list_of_urls));
  81. // Does not exist.
  82. EXPECT_FALSE(IsUrlInList(GURL("http://flags"), list_of_urls));
  83. // Our internal URLs will be treated part in part in/sensitive. The scheme
  84. // is treated insensitive, while the host is not - so if an os:// URL in the
  85. // list is upper case, it cannot be (ever) found.
  86. // Note that DCHECKs make sure that no case insensitive host can be passed.
  87. EXPECT_TRUE(IsUrlInList(GURL("Os://version"), list_of_urls));
  88. EXPECT_TRUE(IsUrlInList(GURL("Os://version2"), list_of_urls));
  89. // Whereas - if there is a valid scheme, the rest of the host will be handled
  90. // case insensitive and be found.
  91. EXPECT_TRUE(IsUrlInList(GURL("http://fOOBar"), list_of_urls));
  92. // Sub hosts should also be handled.
  93. EXPECT_TRUE(IsUrlInList(GURL("os://foo/bar"), list_of_urls));
  94. EXPECT_FALSE(IsUrlInList(GURL("os://foo/ba"), list_of_urls));
  95. }
  96. TEST(GurlOsHandlerUtilsTest, IsAshOsUrl) {
  97. // As we expect the input to be sanitized, we cannot add any parameters.
  98. EXPECT_TRUE(IsAshOsUrl(GURL("os://version")));
  99. EXPECT_TRUE(IsAshOsUrl(GURL("os://flags")));
  100. EXPECT_TRUE(IsAshOsUrl(GURL("OS://flags"))); // case insensitive.
  101. EXPECT_FALSE(IsAshOsUrl(GURL("os:/flags"))); // Proper '://' required.
  102. EXPECT_FALSE(IsAshOsUrl(GURL("os://"))); // There needs to be a host.
  103. EXPECT_FALSE(IsAshOsUrl(GURL("oo://version"))); // scheme need matching.
  104. EXPECT_FALSE(IsAshOsUrl(GURL(""))); // No crash.
  105. EXPECT_FALSE(IsAshOsUrl(GURL("::/bar"))); // No crash.
  106. EXPECT_FALSE(IsAshOsUrl(GURL("osos::/bar"))); // In string correct.
  107. }
  108. TEST(GurlOsHandlerUtilsTest, IsAshOsAsciiScheme) {
  109. // As we expect the input to be sanitized, we cannot add any parameters.
  110. EXPECT_TRUE(IsAshOsAsciiScheme("os"));
  111. EXPECT_TRUE(IsAshOsAsciiScheme("Os"));
  112. EXPECT_FALSE(IsAshOsAsciiScheme("")); // Should not crash.
  113. EXPECT_FALSE(IsAshOsAsciiScheme("so"));
  114. EXPECT_FALSE(IsAshOsAsciiScheme("soo"));
  115. }
  116. TEST(GurlOsHandlerUtilsTest, AshOsUrlHost) {
  117. EXPECT_EQ(AshOsUrlHost(GURL("os://flags")), "flags");
  118. EXPECT_EQ(AshOsUrlHost(GURL("os://flags/test")), "flags/test");
  119. EXPECT_EQ(AshOsUrlHost(GURL("os://flags?foo::bar")), "flags");
  120. EXPECT_EQ(AshOsUrlHost(GURL("os://flags#foo")), "flags");
  121. EXPECT_EQ(AshOsUrlHost(GURL("os://flags/foo")), "flags/foo");
  122. EXPECT_EQ(AshOsUrlHost(GURL("os://FlagS/foo")), "flags/foo");
  123. EXPECT_EQ(AshOsUrlHost(GURL("os://FlagS/foo/1")), "flags/foo");
  124. EXPECT_EQ(AshOsUrlHost(GURL("os://")), "");
  125. EXPECT_EQ(AshOsUrlHost(GURL("")), "");
  126. EXPECT_EQ(AshOsUrlHost(GURL("foo")), "");
  127. EXPECT_EQ(AshOsUrlHost(GURL("://")), "");
  128. }
  129. TEST(GurlOsHandlerUtilsTest, GetSystemUrlFromChromeUrl) {
  130. url::ScopedSchemeRegistryForTests scoped_registry;
  131. url::AddStandardScheme("chrome", url::SCHEME_WITH_HOST);
  132. EXPECT_EQ(GetSystemUrlFromChromeUrl(GURL("chrome://flags/")),
  133. GURL("os://flags"));
  134. EXPECT_EQ(GetSystemUrlFromChromeUrl(GURL("chrome://flags/abc")),
  135. GURL("os://flags"));
  136. EXPECT_EQ(GetSystemUrlFromChromeUrl(GURL("chrome://flags?foo")),
  137. GURL("os://flags"));
  138. EXPECT_EQ(GetSystemUrlFromChromeUrl(GURL("chrome://foo")), GURL("os://foo"));
  139. }
  140. TEST(GurlOsHandlerUtilsTest, GetChromeUrlFromSystemUrl) {
  141. EXPECT_EQ(GetChromeUrlFromSystemUrl(GURL("os://flags/abc/def")),
  142. GURL("chrome://flags/abc"));
  143. EXPECT_EQ(GetChromeUrlFromSystemUrl(GURL("os://flags/abc")),
  144. GURL("chrome://flags/abc"));
  145. EXPECT_EQ(GetChromeUrlFromSystemUrl(GURL("os://flags?foo")),
  146. GURL("chrome://flags"));
  147. EXPECT_EQ(GetChromeUrlFromSystemUrl(GURL("os://foo")), GURL("chrome://foo"));
  148. }
  149. TEST(GurlOsHandlerUtilsTest, GetAshUrlFromLacrosUrl) {
  150. // To allow the "chrome" schemer, we need to add it to the registry.
  151. url::ScopedSchemeRegistryForTests scoped_registry;
  152. url::AddStandardScheme("chrome", url::SCHEME_WITH_HOST);
  153. // os://settings need to be converted to chrome://os-settings
  154. EXPECT_EQ(GetTargetURLFromLacrosURL(GURL("os://settings/foo")),
  155. GURL("chrome://os-settings/foo"));
  156. // os-settings should not be changed
  157. EXPECT_EQ(GetTargetURLFromLacrosURL(GURL("os://os-settings")),
  158. GURL("os://os-settings"));
  159. // chrome://settings should also not be touched.
  160. EXPECT_EQ(GetTargetURLFromLacrosURL(GURL("chrome://settings/foo")),
  161. GURL("chrome://settings/foo"));
  162. // Needs to be sanitized.
  163. EXPECT_EQ(GetTargetURLFromLacrosURL(GURL("chrome://settings/foo#bar")),
  164. GURL("chrome://settings/foo"));
  165. }
  166. } // namespace gurl_os_handler_utils
  167. } // namespace crosapi