Debian Codegraph @

closure-library/closure/goog/html/safescript_test.js

raw
1 // Copyright 2014 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Unit tests for goog.html.SafeScript and its builders.
17 */
18
19 goog.provide('goog.html.safeScriptTest');
20
21 goog.require('goog.html.SafeScript');
22 goog.require('goog.object');
23 goog.require('goog.string.Const');
24 goog.require('goog.testing.jsunit');
25
26 goog.setTestOnly('goog.html.safeScriptTest');
27
28
29 function testSafeScript() {
30 var script = 'var string = \'hello\';';
31 var safeScript =
32 goog.html.SafeScript.fromConstant(goog.string.Const.from(script));
33 var extracted = goog.html.SafeScript.unwrap(safeScript);
34 assertEquals(script, extracted);
35 assertEquals(script, safeScript.getTypedStringValue());
36 assertEquals('SafeScript{' + script + '}', String(safeScript));
37
38 // Interface marker is present.
39 assertTrue(safeScript.implementsGoogStringTypedString);
40 }
41
42
43 /** @suppress {checkTypes} */
44 function testUnwrap() {
45 var privateFieldName = 'privateDoNotAccessOrElseSafeScriptWrappedValue_';
46 var markerFieldName = 'SAFE_SCRIPT_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_';
47 var propNames = goog.object.getKeys(
48 goog.html.SafeScript.fromConstant(goog.string.Const.from('')));
49 assertContains(privateFieldName, propNames);
50 assertContains(markerFieldName, propNames);
51 var evil = {};
52 evil[privateFieldName] = 'var string = \'evil\';';
53 evil[markerFieldName] = {};
54
55 var exception =
56 assertThrows(function() { goog.html.SafeScript.unwrap(evil); });
57 assertContains('expected object of type SafeScript', exception.message);
58 }
59
60
61 function testFromConstant_allowsEmptyString() {
62 assertEquals(
63 goog.html.SafeScript.EMPTY,
64 goog.html.SafeScript.fromConstant(goog.string.Const.from('')));
65 }
66
67
68 function testEmpty() {
69 assertEquals('', goog.html.SafeScript.unwrap(goog.html.SafeScript.EMPTY));
70 }