content/base/test/unit/test_csp_ignores_path.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7 const Cu = Components.utils;
michael@0 8
michael@0 9 Cu.import('resource://gre/modules/CSPUtils.jsm');
michael@0 10
michael@0 11 var ioService = Cc["@mozilla.org/network/io-service;1"]
michael@0 12 .getService(Ci.nsIIOService);
michael@0 13 var self = ioService.newURI("http://test1.example.com:80", null, null);
michael@0 14
michael@0 15 function testValidSRCsHostSourceWithSchemeAndPath() {
michael@0 16 var csps = [
michael@0 17 "http://test1.example.com",
michael@0 18 "http://test1.example.com/",
michael@0 19 "http://test1.example.com/path-1",
michael@0 20 "http://test1.example.com/path-1/",
michael@0 21 "http://test1.example.com/path-1/path_2/",
michael@0 22 "http://test1.example.com/path-1/path_2/file.js",
michael@0 23 "http://test1.example.com/path-1/path_2/file_1.js",
michael@0 24 "http://test1.example.com/path-1/path_2/file-2.js",
michael@0 25 "http://test1.example.com/path-1/path_2/f.js",
michael@0 26 "http://test1.example.com/path-1/path_2/f.oo.js"
michael@0 27 ]
michael@0 28
michael@0 29 var obj;
michael@0 30 var expected = "http://test1.example.com:80";
michael@0 31 for (let i in csps) {
michael@0 32 var src = csps[i];
michael@0 33 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 34 dump("expected: " + expected + "\n");
michael@0 35 dump("got: " + obj._sources[0] + "\n");
michael@0 36 do_check_eq(1, obj._sources.length);
michael@0 37 do_check_eq(obj._sources[0], expected);
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 function testValidSRCsRegularHost() {
michael@0 42 var csps = [
michael@0 43 "test1.example.com",
michael@0 44 "test1.example.com/",
michael@0 45 "test1.example.com/path-1",
michael@0 46 "test1.example.com/path-1/",
michael@0 47 "test1.example.com/path-1/path_2/",
michael@0 48 "test1.example.com/path-1/path_2/file.js",
michael@0 49 "test1.example.com/path-1/path_2/file_1.js",
michael@0 50 "test1.example.com/path-1/path_2/file-2.js",
michael@0 51 "test1.example.com/path-1/path_2/f.js",
michael@0 52 "test1.example.com/path-1/path_2/f.oo.js"
michael@0 53 ]
michael@0 54
michael@0 55 var obj;
michael@0 56 var expected = "http://test1.example.com:80";
michael@0 57 for (let i in csps) {
michael@0 58 var src = csps[i];
michael@0 59 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 60 do_check_eq(1, obj._sources.length);
michael@0 61 do_check_eq(obj._sources[0], expected);
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 function testValidSRCsWildCardHost() {
michael@0 66 var csps = [
michael@0 67 "*.example.com",
michael@0 68 "*.example.com/",
michael@0 69 "*.example.com/path-1",
michael@0 70 "*.example.com/path-1/",
michael@0 71 "*.example.com/path-1/path_2/",
michael@0 72 "*.example.com/path-1/path_2/file.js",
michael@0 73 "*.example.com/path-1/path_2/file_1.js",
michael@0 74 "*.example.com/path-1/path_2/file-2.js",
michael@0 75 "*.example.com/path-1/path_2/f.js",
michael@0 76 "*.example.com/path-1/path_2/f.oo.js"
michael@0 77 ]
michael@0 78
michael@0 79 var obj;
michael@0 80 var expected = "http://*.example.com:80";
michael@0 81 for (let i in csps) {
michael@0 82 var src = csps[i];
michael@0 83 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 84 do_check_eq(1, obj._sources.length);
michael@0 85 do_check_eq(obj._sources[0], expected);
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 function testValidSRCsRegularPort() {
michael@0 90 var csps = [
michael@0 91 "test1.example.com:80",
michael@0 92 "test1.example.com:80/",
michael@0 93 "test1.example.com:80/path-1",
michael@0 94 "test1.example.com:80/path-1/",
michael@0 95 "test1.example.com:80/path-1/path_2",
michael@0 96 "test1.example.com:80/path-1/path_2/",
michael@0 97 "test1.example.com:80/path-1/path_2/file.js",
michael@0 98 "test1.example.com:80/path-1/path_2/f.ile.js"
michael@0 99 ]
michael@0 100
michael@0 101 var obj;
michael@0 102 var expected = "http://test1.example.com:80";
michael@0 103 for (let i in csps) {
michael@0 104 var src = csps[i];
michael@0 105 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 106 do_check_eq(1, obj._sources.length);
michael@0 107 do_check_eq(obj._sources[0], expected);
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 function testValidSRCsWildCardPort() {
michael@0 112 var csps = [
michael@0 113 "test1.example.com:*",
michael@0 114 "test1.example.com:*/",
michael@0 115 "test1.example.com:*/path-1",
michael@0 116 "test1.example.com:*/path-1/",
michael@0 117 "test1.example.com:*/path-1/path_2",
michael@0 118 "test1.example.com:*/path-1/path_2/",
michael@0 119 "test1.example.com:*/path-1/path_2/file.js",
michael@0 120 "test1.example.com:*/path-1/path_2/f.ile.js"
michael@0 121 ]
michael@0 122
michael@0 123 var obj;
michael@0 124 var expected = "http://test1.example.com:*";
michael@0 125 for (let i in csps) {
michael@0 126 var src = csps[i];
michael@0 127 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 128 do_check_eq(1, obj._sources.length);
michael@0 129 do_check_eq(obj._sources[0], expected);
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133
michael@0 134 function testInvalidSRCs() {
michael@0 135 var csps = [
michael@0 136 "test1.example.com:88path-1/",
michael@0 137 "test1.example.com:80.js",
michael@0 138 "test1.example.com:*.js",
michael@0 139 "test1.example.com:*."
michael@0 140 ]
michael@0 141
michael@0 142 var obj;
michael@0 143 var expected = [];
michael@0 144 for (let i in csps) {
michael@0 145 var src = csps[i];
michael@0 146 obj = CSPSourceList.fromString(src, undefined, self);
michael@0 147 do_check_eq(0, obj._sources.length);
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 function run_test() {
michael@0 152 testValidSRCsHostSourceWithSchemeAndPath();
michael@0 153 testValidSRCsRegularHost();
michael@0 154 testValidSRCsWildCardHost();
michael@0 155 testValidSRCsRegularPort();
michael@0 156 testValidSRCsWildCardPort();
michael@0 157 testInvalidSRCs();
michael@0 158 do_test_finished();
michael@0 159 }

mercurial