summaryrefslogtreecommitdiffstats
path: root/dom/base/test/referrer_helper.js
blob: e2e3e32835367a3f66a042d3575ff4dabd760e3a (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
// This helper expects these globals to be defined.
/* global PARAMS, SJS, testCases */

/*
 * common functionality for iframe, anchor, and area referrer attribute tests
 */
const GET_RESULT = SJS + "ACTION=get-test-results";
const RESET_STATE = SJS + "ACTION=resetState";

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

/**
 * Listen for notifications from the child.
 * These are sent in case of error, or when the loads we await have completed.
 */
window.addEventListener("message", function (event) {
  if (event.data == "childLoadComplete") {
    // all loads happen, continue the test.
    advance();
  }
});

/**
 * helper to perform an XHR
 * to do checkIndividualResults and resetState
 */
function doXHR(aUrl, onSuccess, onFail) {
  // The server is at http[s]://example.com so we need cross-origin XHR.
  var xhr = new XMLHttpRequest({ mozSystem: true });
  xhr.responseType = "json";
  xhr.onload = function () {
    onSuccess(xhr);
  };
  xhr.onerror = function () {
    onFail(xhr);
  };
  xhr.open("GET", "http" + aUrl, true);
  xhr.send(null);
}

/**
 * Grabs the results via XHR and passes to checker.
 */
function checkIndividualResults(aTestname, aExpectedReferrer, aName) {
  var onload = xhr => {
    var results = xhr.response;
    info(JSON.stringify(xhr.response));
    ok(aName in results, aName + " tests have to be performed.");
    is(
      results[aName].policy,
      aExpectedReferrer,
      aTestname +
        " --- " +
        results[aName].policy +
        " (" +
        results[aName].referrer +
        ")"
    );
    advance();
  };
  var onerror = xhr => {
    ok(false, "Can't get results from the counter server.");
    SimpleTest.finish();
  };
  doXHR(GET_RESULT, onload, onerror);
}

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

/**
 * testing if referrer header is sent correctly
 */
var tests = (function* () {
  yield SpecialPowers.pushPrefEnv(
    { set: [["network.preload", true]] },
    advance
  );
  yield SpecialPowers.pushPrefEnv(
    { set: [["security.mixed_content.block_active_content", false]] },
    advance
  );
  yield SpecialPowers.pushPrefEnv(
    { set: [["network.http.referer.disallowRelaxingDefault", false]] },
    advance
  );
  yield SpecialPowers.pushPermissions(
    [{ type: "systemXHR", allow: true, context: document }],
    advance
  );

  var iframe = document.getElementById("testframe");

  for (var j = 0; j < testCases.length; j++) {
    if (testCases[j].PREFS) {
      yield SpecialPowers.pushPrefEnv({ set: testCases[j].PREFS }, advance);
    }

    var actions = testCases[j].ACTION;
    var subTests = testCases[j].TESTS;
    for (var k = 0; k < actions.length; k++) {
      var actionString = actions[k];
      for (var i = 0; i < subTests.length; i++) {
        yield resetState();
        var searchParams = new URLSearchParams();
        searchParams.append("ACTION", actionString);
        searchParams.append("NAME", subTests[i].NAME);
        for (var l of PARAMS) {
          if (subTests[i][l]) {
            searchParams.append(l, subTests[i][l]);
          }
        }
        var schemeFrom = subTests[i].SCHEME_FROM || "http";
        yield (iframe.src = schemeFrom + SJS + searchParams.toString());
        yield checkIndividualResults(
          subTests[i].DESC,
          subTests[i].RESULT,
          subTests[i].NAME
        );
      }
    }
  }

  // complete.
  SimpleTest.finish();
})();