summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/referrer_worker.html
blob: 5693fc3408858498258835e45d6274a80a55bc6c (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
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="tests.next();">
<script type="text/javascript;version=1.7">
const SJS = "referrer_test_server.sjs?";
const BASE_URL = "https://example.com/tests/dom/workers/test/" + SJS;
const GET_RESULT = BASE_URL + 'ACTION=get-test-results';
const RESET_STATE = BASE_URL + 'ACTION=resetState';

function ok(val, message) {
  val = val ? "true" : "false";
  window.parent.postMessage("SimpleTest.ok(" + val + ", '" + message + "');", "*");
}

function info(val) {
  window.parent.postMessage("SimpleTest.info(" + val + ");", "*");
}

function is(a, b, message) {
  ok(a == b, message);
}

function finish() {
  // Let window.onerror have a chance to fire
  setTimeout(function() {
    setTimeout(function() {
      tests.close();
      window.parent.postMessage("SimpleTest.finish();", "*");
    }, 0);
  }, 0);
}

var testCases = {
  'same-origin':  { 'Referrer-Policy' : { 'default' : 'full',
                                          'origin' : 'origin',
                                          'origin-when-cross-origin' : 'full',
                                          'unsafe-url' : 'full',
                                          'same-origin' : 'full',
                                          'strict-origin' : 'origin',
                                          'strict-origin-when-cross-origin' : 'full',
                                          'no-referrer' : 'none',
                                          'unsafe-url, no-referrer' : 'none',
                                          'invalid' : 'full' }},

  'cross-origin':  { 'Referrer-Policy' : { 'default' : 'full',
                                           'origin' : 'origin',
                                           'origin-when-cross-origin' : 'origin',
                                           'unsafe-url' : 'full',
                                           'same-origin' : 'none',
                                           'strict-origin' : 'origin',
                                           'strict-origin-when-cross-origin' : 'origin',
                                           'no-referrer' : 'none',
                                           'unsafe-url, no-referrer' : 'none',
                                           'invalid' : 'full' }},

  // Downgrading in worker is blocked entirely without unblock option
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1198078#c17
  // Skip the downgrading test
  /* 'downgrade':  { 'Referrer-Policy' : { 'default' : 'full',
                                          'origin'  : 'full',
                                          'origin-when-cross-origin"'  : 'full',
                                          'unsafe-url'  : 'full',
                                          'same-origin' : 'none',
                                          'strict-origin' : 'none',
                                          'strict-origin-when-cross-origin' : 'none',
                                          'no-referrer'  : 'full',
                                          'unsafe-url, no-referrer' : 'none',
                                          'invalid'  : 'full' }}, */


};

var advance = function() { tests.next(); };

/**
 * helper to perform an XHR
 * to do checkIndividualResults and resetState
 */
function doXHR(aUrl, onSuccess, onFail) {
  var xhr = new XMLHttpRequest({mozSystem: true});
  xhr.responseType = "json";
  xhr.onload = function () {
    onSuccess(xhr);
  };
  xhr.onerror = function () {
    onFail(xhr);
  };
  xhr.open('GET', aUrl, true);
  xhr.send(null);
}


function resetState() {
  doXHR(RESET_STATE,
    advance,
    function(xhr) {
      ok(false, "error in reset state");
      finish();
    });
}

function checkIndividualResults(aType, aPolicy, aExpected) {
  var onload = xhr => {
    var results = xhr.response;
    dump(JSON.stringify(xhr.response));
    // test id equals type + "-" + policy
    // Ex: same-origin-default
    var id = aType + "-" + aPolicy;
    ok(id in results, id + " tests have to be performed.");
    is(results[id].policy, aExpected, id + ' --- ' + results[id].policy + ' (' + results[id].referrer + ')');
    advance();
  };
  var onerror = xhr => {
    ok(false, "Can't get results from the counter server.");
    finish();
  };
  doXHR(GET_RESULT, onload, onerror);
}

var tests = (function() {

  for (var type in testCases) {
    for (var policy in testCases[type]['Referrer-Policy']) {
      yield resetState();
      var searchParams = new URLSearchParams();
      searchParams.append("TYPE", type);
      searchParams.append("ACTION", "test");
      searchParams.append("Referrer-Policy", policy);
      var worker = new Worker(BASE_URL + searchParams.toString());
      worker.onmessage = function () {
        advance();
      };
      yield worker.postMessage(42);
      yield checkIndividualResults(type, policy, escape(testCases[type]['Referrer-Policy'][policy]));
    }
  }

  // complete.  Be sure to yield so we don't call this twice.
  yield finish();
})();
</script>
</body>
</html>