Debian Codegraph @

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

raw
1 // Copyright 2013 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.uncheckedconversions.
17 */
18
19 goog.provide('goog.html.uncheckedconversionsTest');
20
21 goog.require('goog.html.SafeHtml');
22 goog.require('goog.html.SafeScript');
23 goog.require('goog.html.SafeStyle');
24 goog.require('goog.html.SafeStyleSheet');
25 goog.require('goog.html.SafeUrl');
26 goog.require('goog.html.TrustedResourceUrl');
27 goog.require('goog.html.uncheckedconversions');
28 goog.require('goog.i18n.bidi.Dir');
29 goog.require('goog.string.Const');
30 goog.require('goog.testing.jsunit');
31
32 goog.setTestOnly('goog.html.uncheckedconversionsTest');
33
34
35 function testSafeHtmlFromStringKnownToSatisfyTypeContract_ok() {
36 var html = '<div>irrelevant</div>';
37 var safeHtml =
38 goog.html.uncheckedconversions
39 .safeHtmlFromStringKnownToSatisfyTypeContract(
40 goog.string.Const.from('Test'), html, goog.i18n.bidi.Dir.LTR);
41 assertEquals(html, goog.html.SafeHtml.unwrap(safeHtml));
42 assertEquals(goog.i18n.bidi.Dir.LTR, safeHtml.getDirection());
43 }
44
45
46 function testSafeHtmlFromStringKnownToSatisfyTypeContract_error() {
47 assertThrows(function() {
48 goog.html.uncheckedconversions.safeHtmlFromStringKnownToSatisfyTypeContract(
49 goog.string.Const.from(''), 'irrelevant');
50 });
51 }
52
53
54 function testSafeScriptFromStringKnownToSatisfyTypeContract_ok() {
55 var script = 'functionCall(\'irrelevant\');';
56 var safeScript =
57 goog.html.uncheckedconversions
58 .safeScriptFromStringKnownToSatisfyTypeContract(
59 goog.string.Const.from(
60 'Safe because value is constant. Security review: b/7685625.'),
61 script);
62 assertEquals(script, goog.html.SafeScript.unwrap(safeScript));
63 }
64
65
66 function testSafeScriptFromStringKnownToSatisfyTypeContract_error() {
67 assertThrows(function() {
68 goog.html.uncheckedconversions
69 .safeScriptFromStringKnownToSatisfyTypeContract(
70 goog.string.Const.from(''), 'irrelevant');
71 });
72 }
73
74
75 function testSafeStyleFromStringKnownToSatisfyTypeContract_ok() {
76 var style = 'P.special { color:red ; }';
77 var safeStyle =
78 goog.html.uncheckedconversions
79 .safeStyleFromStringKnownToSatisfyTypeContract(
80 goog.string.Const.from(
81 'Safe because value is constant. Security review: b/7685625.'),
82 style);
83 assertEquals(style, goog.html.SafeStyle.unwrap(safeStyle));
84 }
85
86
87 function testSafeStyleFromStringKnownToSatisfyTypeContract_error() {
88 assertThrows(function() {
89 goog.html.uncheckedconversions
90 .safeStyleFromStringKnownToSatisfyTypeContract(
91 goog.string.Const.from(''), 'irrelevant');
92 });
93 }
94
95
96 function testSafeStyleSheetFromStringKnownToSatisfyTypeContract_ok() {
97 var styleSheet = 'P.special { color:red ; }';
98 var safeStyleSheet =
99 goog.html.uncheckedconversions
100 .safeStyleSheetFromStringKnownToSatisfyTypeContract(
101 goog.string.Const.from(
102 'Safe because value is constant. Security review: b/7685625.'),
103 styleSheet);
104 assertEquals(styleSheet, goog.html.SafeStyleSheet.unwrap(safeStyleSheet));
105 }
106
107
108 function testSafeStyleSheetFromStringKnownToSatisfyTypeContract_error() {
109 assertThrows(function() {
110 goog.html.uncheckedconversions
111 .safeStyleSheetFromStringKnownToSatisfyTypeContract(
112 goog.string.Const.from(''), 'irrelevant');
113 });
114 }
115
116
117 function testSafeUrlFromStringKnownToSatisfyTypeContract_ok() {
118 var url = 'http://www.irrelevant.com';
119 var safeUrl =
120 goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract(
121 goog.string.Const.from(
122 'Safe because value is constant. Security review: b/7685625.'),
123 url);
124 assertEquals(url, goog.html.SafeUrl.unwrap(safeUrl));
125 }
126
127
128 function testSafeUrlFromStringKnownToSatisfyTypeContract_error() {
129 assertThrows(function() {
130 goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract(
131 goog.string.Const.from(''), 'http://irrelevant.com');
132 });
133 }
134
135
136 function testTrustedResourceUrlFromStringKnownToSatisfyTypeContract_ok() {
137 var url = 'http://www.irrelevant.com';
138 var trustedResourceUrl =
139 goog.html.uncheckedconversions
140 .trustedResourceUrlFromStringKnownToSatisfyTypeContract(
141 goog.string.Const.from(
142 'Safe because value is constant. Security review: b/7685625.'),
143 url);
144 assertEquals(url, goog.html.TrustedResourceUrl.unwrap(trustedResourceUrl));
145 }
146
147
148 function testTrustedResourceFromStringKnownToSatisfyTypeContract_error() {
149 assertThrows(function() {
150 goog.html.uncheckedconversions
151 .trustedResourceUrlFromStringKnownToSatisfyTypeContract(
152 goog.string.Const.from(''), 'http://irrelevant.com');
153 });
154 }