1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/unit/test_csp_ignores_path.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Cc = Components.classes; 1.9 +const Ci = Components.interfaces; 1.10 +const Cu = Components.utils; 1.11 + 1.12 +Cu.import('resource://gre/modules/CSPUtils.jsm'); 1.13 + 1.14 +var ioService = Cc["@mozilla.org/network/io-service;1"] 1.15 + .getService(Ci.nsIIOService); 1.16 +var self = ioService.newURI("http://test1.example.com:80", null, null); 1.17 + 1.18 +function testValidSRCsHostSourceWithSchemeAndPath() { 1.19 + var csps = [ 1.20 + "http://test1.example.com", 1.21 + "http://test1.example.com/", 1.22 + "http://test1.example.com/path-1", 1.23 + "http://test1.example.com/path-1/", 1.24 + "http://test1.example.com/path-1/path_2/", 1.25 + "http://test1.example.com/path-1/path_2/file.js", 1.26 + "http://test1.example.com/path-1/path_2/file_1.js", 1.27 + "http://test1.example.com/path-1/path_2/file-2.js", 1.28 + "http://test1.example.com/path-1/path_2/f.js", 1.29 + "http://test1.example.com/path-1/path_2/f.oo.js" 1.30 + ] 1.31 + 1.32 + var obj; 1.33 + var expected = "http://test1.example.com:80"; 1.34 + for (let i in csps) { 1.35 + var src = csps[i]; 1.36 + obj = CSPSourceList.fromString(src, undefined, self); 1.37 + dump("expected: " + expected + "\n"); 1.38 + dump("got: " + obj._sources[0] + "\n"); 1.39 + do_check_eq(1, obj._sources.length); 1.40 + do_check_eq(obj._sources[0], expected); 1.41 + } 1.42 +} 1.43 + 1.44 +function testValidSRCsRegularHost() { 1.45 + var csps = [ 1.46 + "test1.example.com", 1.47 + "test1.example.com/", 1.48 + "test1.example.com/path-1", 1.49 + "test1.example.com/path-1/", 1.50 + "test1.example.com/path-1/path_2/", 1.51 + "test1.example.com/path-1/path_2/file.js", 1.52 + "test1.example.com/path-1/path_2/file_1.js", 1.53 + "test1.example.com/path-1/path_2/file-2.js", 1.54 + "test1.example.com/path-1/path_2/f.js", 1.55 + "test1.example.com/path-1/path_2/f.oo.js" 1.56 + ] 1.57 + 1.58 + var obj; 1.59 + var expected = "http://test1.example.com:80"; 1.60 + for (let i in csps) { 1.61 + var src = csps[i]; 1.62 + obj = CSPSourceList.fromString(src, undefined, self); 1.63 + do_check_eq(1, obj._sources.length); 1.64 + do_check_eq(obj._sources[0], expected); 1.65 + } 1.66 +} 1.67 + 1.68 +function testValidSRCsWildCardHost() { 1.69 + var csps = [ 1.70 + "*.example.com", 1.71 + "*.example.com/", 1.72 + "*.example.com/path-1", 1.73 + "*.example.com/path-1/", 1.74 + "*.example.com/path-1/path_2/", 1.75 + "*.example.com/path-1/path_2/file.js", 1.76 + "*.example.com/path-1/path_2/file_1.js", 1.77 + "*.example.com/path-1/path_2/file-2.js", 1.78 + "*.example.com/path-1/path_2/f.js", 1.79 + "*.example.com/path-1/path_2/f.oo.js" 1.80 + ] 1.81 + 1.82 + var obj; 1.83 + var expected = "http://*.example.com:80"; 1.84 + for (let i in csps) { 1.85 + var src = csps[i]; 1.86 + obj = CSPSourceList.fromString(src, undefined, self); 1.87 + do_check_eq(1, obj._sources.length); 1.88 + do_check_eq(obj._sources[0], expected); 1.89 + } 1.90 +} 1.91 + 1.92 +function testValidSRCsRegularPort() { 1.93 + var csps = [ 1.94 + "test1.example.com:80", 1.95 + "test1.example.com:80/", 1.96 + "test1.example.com:80/path-1", 1.97 + "test1.example.com:80/path-1/", 1.98 + "test1.example.com:80/path-1/path_2", 1.99 + "test1.example.com:80/path-1/path_2/", 1.100 + "test1.example.com:80/path-1/path_2/file.js", 1.101 + "test1.example.com:80/path-1/path_2/f.ile.js" 1.102 + ] 1.103 + 1.104 + var obj; 1.105 + var expected = "http://test1.example.com:80"; 1.106 + for (let i in csps) { 1.107 + var src = csps[i]; 1.108 + obj = CSPSourceList.fromString(src, undefined, self); 1.109 + do_check_eq(1, obj._sources.length); 1.110 + do_check_eq(obj._sources[0], expected); 1.111 + } 1.112 +} 1.113 + 1.114 +function testValidSRCsWildCardPort() { 1.115 + var csps = [ 1.116 + "test1.example.com:*", 1.117 + "test1.example.com:*/", 1.118 + "test1.example.com:*/path-1", 1.119 + "test1.example.com:*/path-1/", 1.120 + "test1.example.com:*/path-1/path_2", 1.121 + "test1.example.com:*/path-1/path_2/", 1.122 + "test1.example.com:*/path-1/path_2/file.js", 1.123 + "test1.example.com:*/path-1/path_2/f.ile.js" 1.124 + ] 1.125 + 1.126 + var obj; 1.127 + var expected = "http://test1.example.com:*"; 1.128 + for (let i in csps) { 1.129 + var src = csps[i]; 1.130 + obj = CSPSourceList.fromString(src, undefined, self); 1.131 + do_check_eq(1, obj._sources.length); 1.132 + do_check_eq(obj._sources[0], expected); 1.133 + } 1.134 +} 1.135 + 1.136 + 1.137 +function testInvalidSRCs() { 1.138 + var csps = [ 1.139 + "test1.example.com:88path-1/", 1.140 + "test1.example.com:80.js", 1.141 + "test1.example.com:*.js", 1.142 + "test1.example.com:*." 1.143 + ] 1.144 + 1.145 + var obj; 1.146 + var expected = []; 1.147 + for (let i in csps) { 1.148 + var src = csps[i]; 1.149 + obj = CSPSourceList.fromString(src, undefined, self); 1.150 + do_check_eq(0, obj._sources.length); 1.151 + } 1.152 +} 1.153 + 1.154 +function run_test() { 1.155 + testValidSRCsHostSourceWithSchemeAndPath(); 1.156 + testValidSRCsRegularHost(); 1.157 + testValidSRCsWildCardHost(); 1.158 + testValidSRCsRegularPort(); 1.159 + testValidSRCsWildCardPort(); 1.160 + testInvalidSRCs(); 1.161 + do_test_finished(); 1.162 +}