test_user_agent_overrides.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=782453
  5. -->
  6. <head>
  7. <title>Test for User Agent Overrides</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=782453">Mozilla Bug 782453</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none"></div>
  15. <pre id="test">
  16. <script class="testbody" type="text/javascript">
  17. const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
  18. const PREF_OVERRIDES_BRANCH = "general.useragent.override.";
  19. const DEFAULT_UA = navigator.userAgent;
  20. const UA_WHOLE_OVERRIDE = "DummyUserAgent";
  21. const UA_WHOLE_EXPECTED = UA_WHOLE_OVERRIDE;
  22. const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
  23. const UA_PARTIAL_SEP = "#";
  24. const UA_PARTIAL_TO = UA_WHOLE_OVERRIDE;
  25. const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO;
  26. const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO);
  27. function testUAIFrame(host, expected, sameQ, message, testNavQ, navSameQ, navMessage, callback) {
  28. let url = location.pathname;
  29. url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
  30. let ifr = document.createElement('IFRAME');
  31. ifr.src = url;
  32. document.getElementById('content').appendChild(ifr);
  33. window.addEventListener("message", function recv(e) {
  34. ok(sameQ == (e.data.header.indexOf(expected) != -1), message);
  35. if (testNavQ) {
  36. ok(navSameQ == (e.data.nav.indexOf(expected) != -1), navMessage);
  37. }
  38. window.removeEventListener("message", recv, false);
  39. callback();
  40. }, false);
  41. }
  42. function testUAIFrameNoNav(host, expected, sameQ, message, callback) {
  43. testUAIFrame(host, expected, sameQ, message, false, true, '', callback);
  44. }
  45. function testUA(options, callback) {
  46. var [domain, override, test_hosts, expected] =
  47. [options.domain, options.override, options.test_hosts, options.expected];
  48. (function nextTest() {
  49. let test_host = test_hosts.shift();
  50. info("Overriding " + domain + " with " + override + " for " + test_host);
  51. function is_subdomain(host) {
  52. var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1);
  53. return test_domain === domain || test_domain.endsWith('.' + domain);
  54. }
  55. var localhost = location.origin;
  56. var overrideNavigator = is_subdomain(localhost);
  57. var navigator_ua, test_ua;
  58. if (overrideNavigator) {
  59. navigator_ua = navigator.userAgent;
  60. }
  61. let url = location.pathname;
  62. url = test_host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
  63. let ifr = document.createElement('IFRAME');
  64. ifr.src = url;
  65. document.getElementById('content').appendChild(ifr);
  66. window.addEventListener("message", function recv(e) {
  67. test_ua = e.data.header;
  68. SpecialPowers.pushPrefEnv({
  69. set: [[PREF_OVERRIDES_BRANCH + domain, override]],
  70. }, function () {
  71. testUAIFrame(test_host, expected, true, 'Header UA not overridden at step ' + (++step), true,
  72. true, 'Navigator UA not overridden at step ' + (++step), function () {
  73. // clear the override pref to undo overriding the UA
  74. SpecialPowers.pushPrefEnv({
  75. clear: [[PREF_OVERRIDES_BRANCH + domain]],
  76. }, function () {
  77. testUAIFrameNoNav(test_host, test_ua, true, 'Header UA not restored at step ' + (++step), function() {
  78. test_hosts.length ? nextTest() : callback();
  79. });
  80. });
  81. });
  82. });
  83. window.removeEventListener("message", recv, false);
  84. }, false);
  85. })();
  86. }
  87. var step = 0; // for logging
  88. var tests = [
  89. // should override both header and navigator.userAgent
  90. {
  91. domain: location.hostname,
  92. override: UA_WHOLE_OVERRIDE,
  93. test_hosts: [location.origin],
  94. expected: UA_WHOLE_EXPECTED
  95. },
  96. // should support partial overrides
  97. {
  98. domain: location.hostname,
  99. override: UA_PARTIAL_OVERRIDE,
  100. test_hosts: [location.origin],
  101. expected: UA_PARTIAL_EXPECTED
  102. },
  103. // should match domain and subdomains
  104. {
  105. domain: 'example.org',
  106. override: UA_WHOLE_OVERRIDE,
  107. test_hosts: ['http://example.org',
  108. 'http://test1.example.org',
  109. 'http://sub1.test1.example.org'],
  110. expected: UA_WHOLE_EXPECTED
  111. },
  112. // should not match superdomains
  113. {
  114. domain: 'sub1.test1.example.org',
  115. override: UA_WHOLE_OVERRIDE,
  116. test_hosts: ['http://example.org',
  117. 'http://test1.example.org'],
  118. expected: DEFAULT_UA
  119. },
  120. // should work with https
  121. {
  122. domain: 'example.com',
  123. override: UA_WHOLE_OVERRIDE,
  124. test_hosts: ['https://example.com',
  125. 'https://test1.example.com',
  126. 'https://sub1.test1.example.com'],
  127. expected: UA_WHOLE_EXPECTED
  128. },
  129. ];
  130. // test that UA is not overridden when the 'site_specific_overrides' pref is off
  131. function testInactive(callback) {
  132. SpecialPowers.pushPrefEnv({
  133. set: [
  134. [PREF_OVERRIDES_ENABLED, false],
  135. [PREF_OVERRIDES_BRANCH + location.hostname, UA_WHOLE_OVERRIDE]
  136. ]
  137. }, function () {
  138. testUAIFrame(location.origin, UA_WHOLE_OVERRIDE, false, 'Failed to disabled header UA override at step ' + (++step),
  139. true, false, 'Failed to disable navigator UA override at step + ' + (++step), function () {
  140. SpecialPowers.pushPrefEnv({
  141. clear: [
  142. [PREF_OVERRIDES_ENABLED],
  143. [PREF_OVERRIDES_BRANCH + location.hostname]
  144. ]
  145. }, callback);
  146. });
  147. });
  148. }
  149. function testPriority(callback) {
  150. // foo.bar.com override should have priority over bar.com override
  151. var tests = [
  152. ['example.org', 'test1.example.org', 'sub1.test1.example.org'],
  153. ['example.org', 'test1.example.org', 'sub2.test1.example.org'],
  154. ['example.org', 'test2.example.org', 'sub1.test2.example.org'],
  155. ['example.org', 'test2.example.org', 'sub2.test2.example.org'],
  156. ];
  157. (function nextTest() {
  158. var [level0, level1, level2] = tests.shift();
  159. var host = 'http://' + level2;
  160. SpecialPowers.pushPrefEnv({
  161. set: [
  162. [PREF_OVERRIDES_ENABLED, true],
  163. [PREF_OVERRIDES_BRANCH + level1, UA_WHOLE_OVERRIDE]
  164. ]
  165. }, function () {
  166. // should use first override at this point
  167. testUAIFrameNoNav(host, UA_WHOLE_EXPECTED, true, 'UA not overridden at step ' + (++step), function() {
  168. // add a second override that should be used
  169. testUA({
  170. domain: level2,
  171. override: UA_PARTIAL_OVERRIDE,
  172. test_hosts: [host],
  173. expected: UA_PARTIAL_EXPECTED
  174. }, function () {
  175. // add a third override that should not be used
  176. testUA({
  177. domain: level0,
  178. override: UA_PARTIAL_OVERRIDE,
  179. test_hosts: [host],
  180. expected: UA_WHOLE_EXPECTED
  181. }, tests.length ? nextTest : callback);
  182. });
  183. });
  184. });
  185. })();
  186. }
  187. function testOverrides(callback) {
  188. SpecialPowers.pushPrefEnv({
  189. set: [[PREF_OVERRIDES_ENABLED, true]]
  190. }, function nextTest() {
  191. testUA(tests.shift(), function() { tests.length ? nextTest() : callback() });
  192. });
  193. }
  194. SpecialPowers.loadChromeScript(_ => {
  195. Components.utils.import("resource://gre/modules/UserAgentOverrides.jsm");
  196. UserAgentOverrides.init();
  197. });
  198. SimpleTest.waitForExplicitFinish();
  199. SimpleTest.requestCompleteLog();
  200. SimpleTest.requestLongerTimeout(5);
  201. testOverrides(function() {
  202. testInactive(function() {
  203. testPriority(SimpleTest.finish)
  204. });
  205. });
  206. </script>
  207. </pre>
  208. </body>
  209. </html>