|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 const Ci = Components.interfaces; |
|
7 const Cc = Components.classes; |
|
8 const Cr = Components.results; |
|
9 |
|
10 function loadUtilsScript() { |
|
11 var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. |
|
12 getService(Ci.mozIJSSubScriptLoader); |
|
13 loader.loadSubScript("chrome://global/content/contentAreaUtils.js"); |
|
14 } |
|
15 |
|
16 function test_urlSecurityCheck() { |
|
17 var nullPrincipal = Cc["@mozilla.org/nullprincipal;1"]. |
|
18 createInstance(Ci.nsIPrincipal); |
|
19 |
|
20 const HTTP_URI = "http://www.mozilla.org/"; |
|
21 const CHROME_URI = "chrome://browser/content/browser.xul"; |
|
22 const DISALLOW_INHERIT_PRINCIPAL = |
|
23 Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL; |
|
24 |
|
25 try { |
|
26 urlSecurityCheck(makeURI(HTTP_URI), nullPrincipal, |
|
27 DISALLOW_INHERIT_PRINCIPAL); |
|
28 } |
|
29 catch(ex) { |
|
30 do_throw("urlSecurityCheck should not throw when linking to a http uri with a null principal"); |
|
31 } |
|
32 |
|
33 // urlSecurityCheck also supports passing the url as a string |
|
34 try { |
|
35 urlSecurityCheck(HTTP_URI, nullPrincipal, |
|
36 DISALLOW_INHERIT_PRINCIPAL); |
|
37 } |
|
38 catch(ex) { |
|
39 do_throw("urlSecurityCheck failed to handle the http URI as a string (uri spec)"); |
|
40 } |
|
41 |
|
42 let shouldThrow = true; |
|
43 try { |
|
44 urlSecurityCheck(CHROME_URI, nullPrincipal, |
|
45 DISALLOW_INHERIT_PRINCIPAL); |
|
46 } |
|
47 catch(ex) { |
|
48 shouldThrow = false; |
|
49 } |
|
50 if (shouldThrow) |
|
51 do_throw("urlSecurityCheck should throw when linking to a chrome uri with a null principal"); |
|
52 } |
|
53 |
|
54 function test_stringBundle() { |
|
55 // This test verifies that the elements that can be used as file picker title |
|
56 // keys in the save* functions are actually present in the string bundle. |
|
57 // These keys are part of the contentAreaUtils.js public API. |
|
58 var validFilePickerTitleKeys = [ |
|
59 "SaveImageTitle", |
|
60 "SaveVideoTitle", |
|
61 "SaveAudioTitle", |
|
62 "SaveLinkTitle", |
|
63 ]; |
|
64 |
|
65 for (let [, filePickerTitleKey] in Iterator(validFilePickerTitleKeys)) { |
|
66 // Just check that the string exists |
|
67 try { |
|
68 ContentAreaUtils.stringBundle.GetStringFromName(filePickerTitleKey); |
|
69 } catch (e) { |
|
70 do_throw("Error accessing file picker title key: " + filePickerTitleKey); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 function run_test() |
|
76 { |
|
77 loadUtilsScript(); |
|
78 test_urlSecurityCheck(); |
|
79 test_stringBundle(); |
|
80 } |