summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/test_user_agent_overrides.html
blob: 7396c35e13140ed4fcb44032fcd8cc251d00b877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=782453
-->
<head>
  <title>Test for User Agent Overrides</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=782453">Mozilla Bug 782453</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">

const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
const PREF_OVERRIDES_BRANCH = "general.useragent.override.";

const DEFAULT_UA = navigator.userAgent;

const UA_WHOLE_OVERRIDE = "DummyUserAgent";
const UA_WHOLE_EXPECTED = UA_WHOLE_OVERRIDE;

const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
const UA_PARTIAL_SEP = "#";
const UA_PARTIAL_TO = UA_WHOLE_OVERRIDE;
const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO;
const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO);

function testUAIFrame(host, expected, sameQ, message, testNavQ, navSameQ, navMessage, callback) {
  let url = location.pathname;
  url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
  let ifr = document.createElement('IFRAME');

  ifr.src = url;

  document.getElementById('content').appendChild(ifr);

  window.addEventListener("message", function recv(e) {
    ok(sameQ == (e.data.header.indexOf(expected) != -1), message);
    if (testNavQ) {
      ok(navSameQ == (e.data.nav.indexOf(expected) != -1), navMessage);
    }
    window.removeEventListener("message", recv, false);
    callback();
  }, false);

}

function testUAIFrameNoNav(host, expected, sameQ, message, callback) {
  testUAIFrame(host, expected, sameQ, message, false, true, '', callback);
}

function testUA(options, callback) {
  var [domain, override, test_hosts, expected] =
    [options.domain, options.override, options.test_hosts, options.expected];

  (function nextTest() {
    let test_host = test_hosts.shift();

    info("Overriding " + domain + " with " + override + " for " + test_host);

    function is_subdomain(host) {
      var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1);
      return test_domain === domain || test_domain.endsWith('.' + domain);
    }

    var localhost = location.origin;
    var overrideNavigator = is_subdomain(localhost);
    var navigator_ua, test_ua;

    if (overrideNavigator) {
      navigator_ua = navigator.userAgent;
    }

    let url = location.pathname;
    url = test_host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
    let ifr = document.createElement('IFRAME');
    ifr.src = url;

    document.getElementById('content').appendChild(ifr);

    window.addEventListener("message", function recv(e) {
      test_ua = e.data.header;
      SpecialPowers.pushPrefEnv({
        set: [[PREF_OVERRIDES_BRANCH + domain, override]],
      }, function () {
        testUAIFrame(test_host, expected, true, 'Header UA not overridden at step ' + (++step), true,
          true, 'Navigator UA not overridden at step ' + (++step), function () {
          // clear the override pref to undo overriding the UA
          SpecialPowers.pushPrefEnv({
            clear: [[PREF_OVERRIDES_BRANCH + domain]],
          }, function () {
            testUAIFrameNoNav(test_host, test_ua, true, 'Header UA not restored at step ' + (++step), function() {
              test_hosts.length ? nextTest() : callback();
            });
          });
        });
      });
      window.removeEventListener("message", recv, false);
    }, false);
  })();
}

var step = 0; // for logging
var tests = [
  // should override both header and navigator.userAgent
  {
    domain: location.hostname,
    override: UA_WHOLE_OVERRIDE,
    test_hosts: [location.origin],
    expected: UA_WHOLE_EXPECTED
  },

  // should support partial overrides
  {
    domain: location.hostname,
    override: UA_PARTIAL_OVERRIDE,
    test_hosts: [location.origin],
    expected: UA_PARTIAL_EXPECTED
  },

  // should match domain and subdomains
  {
    domain: 'example.org',
    override: UA_WHOLE_OVERRIDE,
    test_hosts: ['http://example.org',
                 'http://test1.example.org',
                 'http://sub1.test1.example.org'],
    expected: UA_WHOLE_EXPECTED
  },

  // should not match superdomains
  {
    domain: 'sub1.test1.example.org',
    override: UA_WHOLE_OVERRIDE,
    test_hosts: ['http://example.org',
                 'http://test1.example.org'],
    expected: DEFAULT_UA
  },

  // should work with https
  {
    domain: 'example.com',
    override: UA_WHOLE_OVERRIDE,
    test_hosts: ['https://example.com',
                 'https://test1.example.com',
                 'https://sub1.test1.example.com'],
    expected: UA_WHOLE_EXPECTED
  },
];

// test that UA is not overridden when the 'site_specific_overrides' pref is off
function testInactive(callback) {
  SpecialPowers.pushPrefEnv({
    set: [
      [PREF_OVERRIDES_ENABLED, false],
      [PREF_OVERRIDES_BRANCH + location.hostname, UA_WHOLE_OVERRIDE]
    ]
  }, function () {
    testUAIFrame(location.origin, UA_WHOLE_OVERRIDE, false, 'Failed to disabled header UA override at step ' + (++step),
      true, false, 'Failed to disable navigator UA override at step + ' + (++step), function () {
      SpecialPowers.pushPrefEnv({
        clear: [
          [PREF_OVERRIDES_ENABLED],
          [PREF_OVERRIDES_BRANCH + location.hostname]
        ]
      }, callback);
    });
  });
}

function testPriority(callback) {
  // foo.bar.com override should have priority over bar.com override
  var tests = [
    ['example.org', 'test1.example.org', 'sub1.test1.example.org'],
    ['example.org', 'test1.example.org', 'sub2.test1.example.org'],
    ['example.org', 'test2.example.org', 'sub1.test2.example.org'],
    ['example.org', 'test2.example.org', 'sub2.test2.example.org'],
  ];
  (function nextTest() {
    var [level0, level1, level2] = tests.shift();
    var host = 'http://' + level2;
    SpecialPowers.pushPrefEnv({
      set: [
        [PREF_OVERRIDES_ENABLED, true],
        [PREF_OVERRIDES_BRANCH + level1, UA_WHOLE_OVERRIDE]
      ]
    }, function () {
      // should use first override at this point
      testUAIFrameNoNav(host, UA_WHOLE_EXPECTED, true, 'UA not overridden at step ' + (++step), function() {
        // add a second override that should be used
        testUA({
          domain: level2,
          override: UA_PARTIAL_OVERRIDE,
          test_hosts: [host],
          expected: UA_PARTIAL_EXPECTED
        }, function () {
          // add a third override that should not be used
          testUA({
            domain: level0,
            override: UA_PARTIAL_OVERRIDE,
            test_hosts: [host],
            expected: UA_WHOLE_EXPECTED
          }, tests.length ? nextTest : callback);
        });
      });
    });
  })();
}

function testOverrides(callback) {
  SpecialPowers.pushPrefEnv({
    set: [[PREF_OVERRIDES_ENABLED, true]]
  }, function nextTest() {
    testUA(tests.shift(), function() { tests.length ? nextTest() : callback() });
  });
}

SpecialPowers.loadChromeScript(_ => {
  Components.utils.import("resource://gre/modules/UserAgentOverrides.jsm");
  UserAgentOverrides.init();
});

SimpleTest.waitForExplicitFinish();
SimpleTest.requestCompleteLog();
SimpleTest.requestLongerTimeout(5);

testOverrides(function() {
  testInactive(function() {
    testPriority(SimpleTest.finish)
  });
});

</script>
</pre>
</body>
</html>