michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import('resource://gre/modules/CSPUtils.jsm'); michael@0: michael@0: var ioService = Cc["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: var self = ioService.newURI("http://test1.example.com:80", null, null); michael@0: michael@0: function testValidSRCsHostSourceWithSchemeAndPath() { michael@0: var csps = [ michael@0: "http://test1.example.com", michael@0: "http://test1.example.com/", michael@0: "http://test1.example.com/path-1", michael@0: "http://test1.example.com/path-1/", michael@0: "http://test1.example.com/path-1/path_2/", michael@0: "http://test1.example.com/path-1/path_2/file.js", michael@0: "http://test1.example.com/path-1/path_2/file_1.js", michael@0: "http://test1.example.com/path-1/path_2/file-2.js", michael@0: "http://test1.example.com/path-1/path_2/f.js", michael@0: "http://test1.example.com/path-1/path_2/f.oo.js" michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = "http://test1.example.com:80"; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: dump("expected: " + expected + "\n"); michael@0: dump("got: " + obj._sources[0] + "\n"); michael@0: do_check_eq(1, obj._sources.length); michael@0: do_check_eq(obj._sources[0], expected); michael@0: } michael@0: } michael@0: michael@0: function testValidSRCsRegularHost() { michael@0: var csps = [ michael@0: "test1.example.com", michael@0: "test1.example.com/", michael@0: "test1.example.com/path-1", michael@0: "test1.example.com/path-1/", michael@0: "test1.example.com/path-1/path_2/", michael@0: "test1.example.com/path-1/path_2/file.js", michael@0: "test1.example.com/path-1/path_2/file_1.js", michael@0: "test1.example.com/path-1/path_2/file-2.js", michael@0: "test1.example.com/path-1/path_2/f.js", michael@0: "test1.example.com/path-1/path_2/f.oo.js" michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = "http://test1.example.com:80"; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: do_check_eq(1, obj._sources.length); michael@0: do_check_eq(obj._sources[0], expected); michael@0: } michael@0: } michael@0: michael@0: function testValidSRCsWildCardHost() { michael@0: var csps = [ michael@0: "*.example.com", michael@0: "*.example.com/", michael@0: "*.example.com/path-1", michael@0: "*.example.com/path-1/", michael@0: "*.example.com/path-1/path_2/", michael@0: "*.example.com/path-1/path_2/file.js", michael@0: "*.example.com/path-1/path_2/file_1.js", michael@0: "*.example.com/path-1/path_2/file-2.js", michael@0: "*.example.com/path-1/path_2/f.js", michael@0: "*.example.com/path-1/path_2/f.oo.js" michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = "http://*.example.com:80"; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: do_check_eq(1, obj._sources.length); michael@0: do_check_eq(obj._sources[0], expected); michael@0: } michael@0: } michael@0: michael@0: function testValidSRCsRegularPort() { michael@0: var csps = [ michael@0: "test1.example.com:80", michael@0: "test1.example.com:80/", michael@0: "test1.example.com:80/path-1", michael@0: "test1.example.com:80/path-1/", michael@0: "test1.example.com:80/path-1/path_2", michael@0: "test1.example.com:80/path-1/path_2/", michael@0: "test1.example.com:80/path-1/path_2/file.js", michael@0: "test1.example.com:80/path-1/path_2/f.ile.js" michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = "http://test1.example.com:80"; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: do_check_eq(1, obj._sources.length); michael@0: do_check_eq(obj._sources[0], expected); michael@0: } michael@0: } michael@0: michael@0: function testValidSRCsWildCardPort() { michael@0: var csps = [ michael@0: "test1.example.com:*", michael@0: "test1.example.com:*/", michael@0: "test1.example.com:*/path-1", michael@0: "test1.example.com:*/path-1/", michael@0: "test1.example.com:*/path-1/path_2", michael@0: "test1.example.com:*/path-1/path_2/", michael@0: "test1.example.com:*/path-1/path_2/file.js", michael@0: "test1.example.com:*/path-1/path_2/f.ile.js" michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = "http://test1.example.com:*"; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: do_check_eq(1, obj._sources.length); michael@0: do_check_eq(obj._sources[0], expected); michael@0: } michael@0: } michael@0: michael@0: michael@0: function testInvalidSRCs() { michael@0: var csps = [ michael@0: "test1.example.com:88path-1/", michael@0: "test1.example.com:80.js", michael@0: "test1.example.com:*.js", michael@0: "test1.example.com:*." michael@0: ] michael@0: michael@0: var obj; michael@0: var expected = []; michael@0: for (let i in csps) { michael@0: var src = csps[i]; michael@0: obj = CSPSourceList.fromString(src, undefined, self); michael@0: do_check_eq(0, obj._sources.length); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: testValidSRCsHostSourceWithSchemeAndPath(); michael@0: testValidSRCsRegularHost(); michael@0: testValidSRCsWildCardHost(); michael@0: testValidSRCsRegularPort(); michael@0: testValidSRCsWildCardPort(); michael@0: testInvalidSRCs(); michael@0: do_test_finished(); michael@0: }