File: csp_test.js

package info (click to toggle)
chromium 141.0.7390.107-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 6,246,428 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 5,434 bytes parent folder | download | duplicates (10)
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
/**
 * @license
 * Copyright The Closure Library Authors.
 * SPDX-License-Identifier: Apache-2.0
 */

goog.setTestOnly();

goog.require('goog.testing.TestCase');
goog.require('goog.testing.asserts');
goog.require('goog.testing.jsunit');

const meta = /**@type {!HTMLMetaElement} */ (document.createElement('meta'));
meta.httpEquiv = 'Content-Security-Policy';
meta.content = 'object-src \'none\'; ' +
    'script-src \'nonce-CSP+Nonce+For+Tests+Only\' \'unsafe-inline\' ' +
    '\'unsafe-eval\' \'strict-dynamic\' https: http:; base-uri \'none\'';
document.head.appendChild(meta);

function shouldRunTests() {
  return !!window.ReportingObserver;
}

function testCspViolationCausesTestCaseToFail() {
  goog.testing.TestCase.getActiveTestCase().observeCspViolations(false);

  const testCase = new goog.testing.TestCase();
  testCase.addNewTest('test', function() {
    doCspViolation1();
  });
  testCase.runTests();
  assertFalse(testCase.isSuccess());
  assertRegExp(/\bContent Security Policy\b/, testCase.getReport());
}

function testCspViolationReportedForFailingTest() {
  goog.testing.TestCase.getActiveTestCase().observeCspViolations(false);

  const testCase = new goog.testing.TestCase();
  testCase.addNewTest('test', function() {
    doCspViolation2();
    assertTrue(false);
  });
  testCase.runTests();
  assertFalse(testCase.isSuccess());
  assertRegExp(/\bContent Security Policy\b/, testCase.getReport());
}

function testCspViolationCausesTestCaseToFail_lifeCycle() {
  goog.testing.TestCase.getActiveTestCase().observeCspViolations(false);

  const tests = [
    ['setUp', doCspViolation3], ['tearDown', doCspViolation4],
    ['setUpPage', doCspViolation5], ['runTests', doCspViolation6],
    ['shouldRunTests', doCspViolation7]
  ];
  for (const [name, doCspViolation] of tests) {
    const testCase = new goog.testing.TestCase();
    testCase.ignoreStartupCspViolations();
    let didRun = false;
    testCase.setLifecycleObj({
      [name]: function() {
        didRun = true;
        doCspViolation();
        if (name == 'shouldRunTests') {
          return true;
        }
      },
    });
    testCase.addNewTest('test1', function() {
      // do nothing
    });
    testCase.addNewTest('test2', function() {
      // do nothing
    });
    testCase.runTests();
    assertTrue(`${name}() did not run`, didRun);
    assertFalse(`${name}() should have failed`, testCase.isSuccess());
  }
}


async function testCspViolationCausesTestCaseToFail_lifeCycleAsync() {
  goog.testing.TestCase.getActiveTestCase().observeCspViolations(false);

  const tests = [
    ['setUp', doCspViolation8],
    ['tearDown', doCspViolation9],
    ['setUpPage', doCspViolation10],

  ];
  for (const [name, doCspViolation] of tests) {
    const testCase = new goog.testing.TestCase();
    testCase.ignoreStartupCspViolations();
    let didRun = false;
    testCase.setLifecycleObj({
      [name]: function() {
        return new Promise((resolve, reject) => {
          didRun = true;
          doCspViolation();
          resolve();
        });
      },
    });
    testCase.addNewTest('test1', function() {
      // do nothing
    });
    testCase.addNewTest('test2', function() {
      // do nothing
    });
    await testCase.runTestsReturningPromise();
    assertTrue(`${name}() did not run`, didRun);
    assertFalse(`${name}() should have failed`, testCase.isSuccess());
  }
}

// The ReportingObserver API in Chrome only issues a report when a violation
// is trigggered for the first time. The repetition below is used to get around
// this behavior.

/** Generate a CSP violation **/
function doCspViolation1() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation2() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation3() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation4() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation5() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation6() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation7() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation8() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation9() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}

/** Generate a CSP violation **/
function doCspViolation10() {
  const elem = document.createElement('A');
  elem.setAttribute('onclick', 'window["VIOLATION"] = true');
  elem.click();
}