summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/test_user_agent_updates.html
blob: 839f9e000552cad9e5d098fbe142207f7bb9335d (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=897221
-->
<head>
  <title>Test for User Agent Updates</title>
  <script type="text/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=897221">Mozilla Bug 897221</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">

const PREF_APP_UPDATE_TIMERMINIMUMDELAY = "app.update.timerMinimumDelay";
const PREF_UPDATES = "general.useragent.updates.";
const PREF_UPDATES_ENABLED = PREF_UPDATES + "enabled";
const PREF_UPDATES_URL = PREF_UPDATES + "url";
const PREF_UPDATES_INTERVAL = PREF_UPDATES + "interval";
const PREF_UPDATES_TIMEOUT = PREF_UPDATES + "timeout";

const DEFAULT_UA = navigator.userAgent;
const UA_OVERRIDE = "DummyUserAgent";
const UA_ALT_OVERRIDE = "AltUserAgent";

const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
const UA_PARTIAL_SEP = "#";
const UA_PARTIAL_TO = UA_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 getUA(host) {
  var url = location.pathname;
  url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';

  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, false); // sync request
  xhr.send();
  is(xhr.status, 200, 'request failed');
  is(typeof xhr.response, 'string', 'invalid response');
  return xhr.response;
}

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);
}

const OVERRIDES = [
  {
    domain: 'example.org',
    override: '%DATE%',
    host: 'http://example.org'
  },
  {
    domain: 'test1.example.org',
    override: '%PRODUCT%',
    expected: SpecialPowers.Services.appinfo.name,
    host: 'http://test1.example.org'
  },
  {
    domain: 'test2.example.org',
    override: '%APP_ID%',
    expected: SpecialPowers.Services.appinfo.ID,
    host: 'http://test2.example.org'
  },
  {
    domain: 'sub1.test1.example.org',
    override: '%APP_VERSION%',
    expected: SpecialPowers.Services.appinfo.version,
    host: 'http://sub1.test1.example.org'
  },
  {
    domain: 'sub2.test1.example.org',
    override: '%BUILD_ID%',
    expected: SpecialPowers.Services.appinfo.appBuildID,
    host: 'http://sub2.test1.example.org'
  },
  {
    domain: 'sub1.test2.example.org',
    override: '%OS%',
    expected: SpecialPowers.Services.appinfo.OS,
    host: 'http://sub1.test2.example.org'
  },
  {
    domain: 'sub2.test2.example.org',
    override: UA_PARTIAL_OVERRIDE,
    expected: UA_PARTIAL_EXPECTED,
    host: 'http://sub2.test2.example.org'
  },
];

function getServerURL() {
  var url = location.pathname;
  return location.origin + url.slice(0, url.lastIndexOf('/')) + '/user_agent_update.sjs?';
}

function getUpdateURL() {
  var url = getServerURL();
  var overrides = {};
  overrides[location.hostname] = UA_OVERRIDE;
  OVERRIDES.forEach(function (val) {
    overrides[val.domain] = val.override;
  });
  url = url + encodeURIComponent(JSON.stringify(overrides)).replace(/%25/g, '%');
  return url;
}

function testDownload(callback) {
  var startTime = Date.now();
  var url = getUpdateURL();
  isnot(navigator.userAgent, UA_OVERRIDE, 'UA already overridden');
  info('Waiting for UA update: ' + url);

  chromeScript.sendAsyncMessage("notify-on-update");
  SpecialPowers.pushPrefEnv({
    set: [
      [PREF_UPDATES_ENABLED, true],
      [PREF_UPDATES_URL, url],
      [PREF_UPDATES_TIMEOUT, 10000],
      [PREF_UPDATES_INTERVAL, 1] // 1 second interval
    ]
  });

  function waitForUpdate() {
    info("Update Happened");
    testUAIFrameNoNav(location.origin, UA_OVERRIDE, true, 'Header UA not overridden', function() {
      var updateTime = parseInt(getUA('http://example.org'));
      todo(startTime <= updateTime, 'Update was before start time');
      todo(updateTime <= Date.now(), 'Update was after present time');

      let overs = OVERRIDES;
      (function nextOverride() {
        val = overs.shift();
        if (val.expected) {
           testUAIFrameNoNav(val.host, val.expected, true, 'Incorrect URL parameter: ' + val.override, function() {
            overs.length ? nextOverride() : callback();
          });
        } else {
          nextOverride();
        }
      })();
    });
  }

  chromeScript.addMessageListener("useragent-update-complete", waitForUpdate);
}

function testBadUpdate(callback) {
  var url = getServerURL() + 'invalid-json';
  var prevOverride = navigator.userAgent;
  SpecialPowers.pushPrefEnv({
    set: [
      [PREF_UPDATES_URL, url],
      [PREF_UPDATES_INTERVAL, 1] // 1 second interval
    ]
  }, function () { setTimeout(function () {
    var ifr = document.createElement('IFRAME');
    ifr.src = "about:blank";

    ifr.addEventListener('load', function() {
      // We want to make sure a bad update doesn't cancel out previous
      // overrides. We do this by waiting for 5 seconds (assuming the update
      // occurs within 5 seconds), and check that the previous override hasn't
      // changed.
      is(navigator.userAgent, prevOverride,
        'Invalid update deleted previous override');
      callback();
    }, false);
    document.getElementById('content').appendChild(ifr);
  }, 5000); });
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("Test sets timeouts to wait for updates to happen.");

SpecialPowers.pushPrefEnv({
  set: [
    [PREF_APP_UPDATE_TIMERMINIMUMDELAY, 0]
  ]
}, function () {
  chromeScript.sendSyncMessage("UAO-uninit");

  // Sets the OVERRIDES var in the chrome script.
  // We do this to avoid code duplication.
  chromeScript.sendSyncMessage("set-overrides", OVERRIDES);

  // testProfileLoad, testDownload, and testProfileSave must run in this order
  //  because testDownload depends on testProfileLoad to call UAO.init()
  //  and testProfileSave depends on testDownload to save overrides to the profile
  chromeScript.sendAsyncMessage("testProfileLoad", location.hostname);
});


const chromeScript = SpecialPowers.loadChromeScript(_ => {
  // Enter update timer manager test mode
  Components.classes["@mozilla.org/updates/timer-manager;1"].getService(
    Components.interfaces.nsIObserver).observe(null, "utm-test-init", "");

  Components.utils.import("resource://gre/modules/UserAgentOverrides.jsm");

  var _notifyOnUpdate = false;

  var UAO = UserAgentOverrides;
  UAO.uninit();

  Components.utils.import("resource://gre/modules/FileUtils.jsm");
  var FU = FileUtils;

  const { TextDecoder, TextEncoder, OS } = Components.utils.import("resource://gre/modules/osfile.jsm");
  var OSF = OS.File;

  const KEY_PREFDIR = "PrefD";
  const KEY_APPDIR = "XCurProcD";
  const FILE_UPDATES = "ua-update.json";

  const UA_OVERRIDE = "DummyUserAgent";
  const UA_ALT_OVERRIDE = "AltUserAgent";

  const PREF_UPDATES = "general.useragent.updates.";
  const PREF_UPDATES_ENABLED = PREF_UPDATES + "enabled";
  const PREF_UPDATES_LASTUPDATED = PREF_UPDATES + "lastupdated";

  Components.utils.import("resource://gre/modules/Services.jsm");
  Services.prefs.addObserver(PREF_UPDATES_LASTUPDATED, () => {
    if (_notifyOnUpdate) {
      _notifyOnUpdate = false; // Only notify once, for the first update.
      sendAsyncMessage("useragent-update-complete");
    }
  } , false);

  var OVERRIDES = null;

  function is(value, expected, message) {
    sendAsyncMessage("is-message", {value, expected, message});
  }

  function info(message) {
    sendAsyncMessage("info-message", message);
  }

  function testProfileSave(hostname) {
    info('Waiting for saving to profile');
    var file = FU.getFile(KEY_PREFDIR, [FILE_UPDATES]).path;
    (function waitForSave() {
      OSF.exists(file).then(
        (exists) => {
          if (!exists) {
            setTimeout(waitForSave, 100);
            return;
          }
          return OSF.read(file).then(
            (bytes) => {
              info('Saved new overrides');
              var decoder = new TextDecoder();
              var overrides = JSON.parse(decoder.decode(bytes));
              is(overrides[hostname], UA_OVERRIDE, 'Incorrect saved override');
              OVERRIDES.forEach(function (val) {
                val.expected && is(overrides[val.domain], val.expected,
                  'Incorrect saved override: ' + val.override);
              });
              sendAsyncMessage("testProfileSaveDone");
            }
          );
        }
      ).then(null,
        (reason) => {
          throw reason
        }
      );
    })();
  }

  function testProfileLoad(hostname) {
    var file = FU.getFile(KEY_APPDIR, [FILE_UPDATES]).path;
    var encoder = new TextEncoder();
    var overrides = {};
    overrides[hostname] = UA_ALT_OVERRIDE;
    var bytes = encoder.encode(JSON.stringify(overrides));

    var badfile = FU.getFile(KEY_PREFDIR, [FILE_UPDATES]).path;
    var badbytes = encoder.encode("null");

    OSF.writeAtomic(file, bytes, {tmpPath: file + ".tmp"}).then(
      () => OSF.writeAtomic(badfile, badbytes, {tmpPath: badfile + ".tmp"})
    ).then(
      () => {
        sendAsyncMessage("testProfileLoadDone");
      },
      (reason) => {
        throw reason
      }
    );
  }


  addMessageListener("testProfileSave", testProfileSave);
  addMessageListener("testProfileLoad", testProfileLoad);
  addMessageListener("set-overrides", function(overrides) { OVERRIDES = overrides});
  addMessageListener("UAO-init", function() { UAO.init(); });
  addMessageListener("UAO-uninit", function() { UAO.uninit(); });
  addMessageListener("notify-on-update", () => { _notifyOnUpdate = true });
});

chromeScript.addMessageListener("testProfileSaveDone", SimpleTest.finish);
chromeScript.addMessageListener("testProfileLoadDone", function() {
  SpecialPowers.pushPrefEnv({
    set: [[PREF_UPDATES_ENABLED, true]]
  }, function () {
    // initialize UserAgentOverrides.jsm and
    // UserAgentUpdates.jsm and load saved file
    chromeScript.sendSyncMessage("UAO-init");
    (function waitForLoad() {
      var ifr = document.createElement('IFRAME');
      ifr.src = "about:blank";

      ifr.addEventListener('load', function() {
        var nav = ifr.contentWindow.navigator;
        if (nav.userAgent !== UA_ALT_OVERRIDE) {
          setTimeout(waitForLoad, 100);
          return;
        }
        testUAIFrameNoNav(location.origin, UA_ALT_OVERRIDE, true, 'Did not apply saved override', function () {
          testDownload(function() {
            testBadUpdate(function() {
              chromeScript.sendAsyncMessage("testProfileSave", location.hostname);
            })
          })
        });
      }, true);

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

chromeScript.addMessageListener("is-message", function(params) {
  let {value, expected, message} = params;
  is(value, expected, message);
});
chromeScript.addMessageListener("info-message", function(message) {
  info(message);
});

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