Debian Codegraph @

closure-library/closure/goog/html/trustedresourceurl_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.TrustedResourceUrl and its builders.
17 */
18
19 goog.provide('goog.html.trustedResourceUrlTest');
20
21 goog.require('goog.html.TrustedResourceUrl');
22 goog.require('goog.i18n.bidi.Dir');
23 goog.require('goog.object');
24 goog.require('goog.string.Const');
25 goog.require('goog.testing.jsunit');
26
27 goog.setTestOnly('goog.html.trustedResourceUrlTest');
28
29
30 function testTrustedResourceUrl() {
31 var url = 'javascript:trusted();';
32 var trustedResourceUrl =
33 goog.html.TrustedResourceUrl.fromConstant(goog.string.Const.from(url));
34 var extracted = goog.html.TrustedResourceUrl.unwrap(trustedResourceUrl);
35 assertEquals(url, extracted);
36 assertEquals(url, trustedResourceUrl.getTypedStringValue());
37 assertEquals(
38 'TrustedResourceUrl{javascript:trusted();}', String(trustedResourceUrl));
39
40 // URLs are always LTR.
41 assertEquals(goog.i18n.bidi.Dir.LTR, trustedResourceUrl.getDirection());
42
43 // Interface markers are present.
44 assertTrue(trustedResourceUrl.implementsGoogStringTypedString);
45 assertTrue(trustedResourceUrl.implementsGoogI18nBidiDirectionalString);
46 }
47
48
49 function testFormat_validFormatString() {
50 // With scheme.
51 assertValidFormat(goog.string.Const.from('httpS://www.gOOgle.com/'));
52 // Scheme-relative.
53 assertValidFormat(goog.string.Const.from('//www.google.com/'));
54 // Origin with hyphen and port.
55 assertValidFormat(goog.string.Const.from('//ww-w.google.com:1000/path'));
56 // IPv6 origin.
57 assertValidFormat(goog.string.Const.from('//[::1]/path'));
58 // Path-absolute.
59 assertValidFormat(goog.string.Const.from('/path'));
60 assertValidFormat(goog.string.Const.from('/path/x'));
61 assertValidFormat(goog.string.Const.from('/path#x'));
62 assertValidFormat(goog.string.Const.from('/path?x'));
63 // Mixed case.
64 assertValidFormat(goog.string.Const.from('httpS://www.google.cOm/pAth'));
65 assertValidFormat(goog.string.Const.from('about:blank'));
66 assertValidFormat(goog.string.Const.from('about:blank#x'));
67 }
68
69
70 /**
71 * Asserts that format with no arguments is allowed and results in a URL
72 * with itself.
73 * @param {!goog.string.Const} format
74 */
75 function assertValidFormat(format) {
76 var url = goog.html.TrustedResourceUrl.format(format, {});
77 assertEquals(
78 goog.string.Const.unwrap(format),
79 goog.html.TrustedResourceUrl.unwrap(url));
80 }
81
82
83 function testFormat_args() {
84 var url = goog.html.TrustedResourceUrl.format(
85 goog.string.Const.from('/path/%{dir1}/%{dir2}?n1=v1%{opt_param}'), {
86 'dir1': 'd%/?#=',
87 'dir2': 2,
88 'opt_param': goog.string.Const.from('n2=v2%/?#=')
89 });
90 assertEquals(
91 '/path/d%25%2F%3F%23%3D/2?n1=v1n2=v2%/?#=',
92 goog.html.TrustedResourceUrl.unwrap(url));
93
94 // Only \w is permitted inside %{...}.
95 var url = goog.html.TrustedResourceUrl.format(
96 goog.string.Const.from('/path/%{!%{label}}%{foo'), {'label': 'value'});
97 assertEquals(
98 '/path/%{!value}%{foo', goog.html.TrustedResourceUrl.unwrap(url));
99 }
100
101
102 function testFormat_missingArgs() {
103 var exception = assertThrows(function() {
104 goog.html.TrustedResourceUrl.format(
105 goog.string.Const.from('https://www.google.com/path/%{arg1}'),
106 {'arg2': 'irrelevant'});
107 });
108 assertContains('no valid label mapping found', exception.message);
109 }
110
111
112 function testFormat_invalidFormatString() {
113 // Invalid scheme.
114 assertInvalidFormat(goog.string.Const.from('ftp://'));
115 // Missing origin.
116 assertInvalidFormat(goog.string.Const.from('https://'));
117 assertInvalidFormat(goog.string.Const.from('https:///'));
118 assertInvalidFormat(goog.string.Const.from('//'));
119 assertInvalidFormat(goog.string.Const.from('///'));
120 // Missing / after origin.
121 assertInvalidFormat(goog.string.Const.from('https://google.com'));
122 // Invalid char in origin.
123 assertInvalidFormat(goog.string.Const.from('https://www.google%.com/'));
124 assertInvalidFormat(goog.string.Const.from('https://www.google\\.com/'));
125 assertInvalidFormat(
126 goog.string.Const.from('https://user:[email protected]/'));
127 // Two slashes, would allow origin to be set dynamically.
128 assertInvalidFormat(goog.string.Const.from('//'));
129 // Two slashes. IE allowed (allows?) '\' instead of '/'.
130 assertInvalidFormat(goog.string.Const.from('/\\'));
131 // Relative path.
132 assertInvalidFormat(goog.string.Const.from('abc'));
133 assertInvalidFormat(goog.string.Const.from('about:blankX'));
134 }
135
136
137 /**
138 * Asserts that format with no arguments throws.
139 * @param {!goog.string.Const} format
140 */
141 function assertInvalidFormat(format) {
142 var exception = assertThrows(goog.string.Const.unwrap(format), function() {
143 goog.html.TrustedResourceUrl.format(format, {});
144 });
145 assertContains('Invalid TrustedResourceUrl format', exception.message);
146 }
147
148
149 function testFromConstants() {
150 assertEquals('', goog.html.TrustedResourceUrl.unwrap(
151 goog.html.TrustedResourceUrl.fromConstants([])));
152 assertEquals('foo', goog.html.TrustedResourceUrl.unwrap(
153 goog.html.TrustedResourceUrl.fromConstants([
154 goog.string.Const.from('foo')
155 ])));
156 assertEquals('foobar', goog.html.TrustedResourceUrl.unwrap(
157 goog.html.TrustedResourceUrl.fromConstants([
158 goog.string.Const.from('foo'),
159 goog.string.Const.from('bar')
160 ])));
161 }
162
163
164 function testCloneWithParams() {
165 var url = goog.html.TrustedResourceUrl.fromConstant(
166 goog.string.Const.from('https://example.com/'));
167
168 assertEquals(
169 'https://example.com/?a=%26',
170 url.cloneWithParams({'a': '&'}).getTypedStringValue());
171
172 assertEquals(
173 'https://example.com/?b=1',
174 url.cloneWithParams({'b': 1, 'c': null, 'd': undefined})
175 .getTypedStringValue());
176
177 assertEquals(
178 'https://example.com/?a=x&a=y',
179 url.cloneWithParams({'a': ['x', 'y']}).getTypedStringValue());
180
181 url = goog.html.TrustedResourceUrl.fromConstant(
182 goog.string.Const.from('https://example.com/?a=x'));
183 assertEquals(
184 'https://example.com/?a=x&b=y',
185 url.cloneWithParams({'b': 'y'}).getTypedStringValue());
186 }
187
188
189 function testFormatWithParams() {
190 var url = goog.html.TrustedResourceUrl.formatWithParams(
191 goog.string.Const.from('https://example.com/'), {}, {'a': '&'});
192 assertEquals('https://example.com/?a=%26', url.getTypedStringValue());
193
194 url = goog.html.TrustedResourceUrl.formatWithParams(
195 goog.string.Const.from('https://example.com/%{file}'), {'file': 'abc'},
196 {'b': 1, 'c': null, 'd': undefined});
197 assertEquals('https://example.com/abc?b=1', url.getTypedStringValue());
198
199 url = goog.html.TrustedResourceUrl.formatWithParams(
200 goog.string.Const.from('https://example.com/'), {}, {'a': ['x', 'y']});
201 assertEquals('https://example.com/?a=x&a=y', url.getTypedStringValue());
202 }
203
204
205 /** @suppress {checkTypes} */
206 function testUnwrap() {
207 var privateFieldName =
208 'privateDoNotAccessOrElseTrustedResourceUrlWrappedValue_';
209 var markerFieldName =
210 'TRUSTED_RESOURCE_URL_TYPE_MARKER_GOOG_HTML_SECURITY_PRIVATE_';
211 var propNames = goog.object.getKeys(
212 goog.html.TrustedResourceUrl.fromConstant(goog.string.Const.from('')));
213 assertContains(privateFieldName, propNames);
214 assertContains(markerFieldName, propNames);
215 var evil = {};
216 evil[privateFieldName] = 'http://example.com/evil.js';
217 evil[markerFieldName] = {};
218
219 var exception =
220 assertThrows(function() { goog.html.TrustedResourceUrl.unwrap(evil); });
221 assertContains(
222 'expected object of type TrustedResourceUrl', exception.message);
223 }