123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- // Copyright 2013 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Unit tests for goog.html.uncheckedconversions.
- */
- goog.provide('goog.html.uncheckedconversionsTest');
- goog.require('goog.html.SafeHtml');
- goog.require('goog.html.SafeScript');
- goog.require('goog.html.SafeStyle');
- goog.require('goog.html.SafeStyleSheet');
- goog.require('goog.html.SafeUrl');
- goog.require('goog.html.TrustedResourceUrl');
- goog.require('goog.html.uncheckedconversions');
- goog.require('goog.i18n.bidi.Dir');
- goog.require('goog.string.Const');
- goog.require('goog.testing.jsunit');
- goog.setTestOnly('goog.html.uncheckedconversionsTest');
- function testSafeHtmlFromStringKnownToSatisfyTypeContract_ok() {
- var html = '<div>irrelevant</div>';
- var safeHtml =
- goog.html.uncheckedconversions
- .safeHtmlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from('Test'), html, goog.i18n.bidi.Dir.LTR);
- assertEquals(html, goog.html.SafeHtml.unwrap(safeHtml));
- assertEquals(goog.i18n.bidi.Dir.LTR, safeHtml.getDirection());
- }
- function testSafeHtmlFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions.safeHtmlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'irrelevant');
- });
- }
- function testSafeScriptFromStringKnownToSatisfyTypeContract_ok() {
- var script = 'functionCall(\'irrelevant\');';
- var safeScript =
- goog.html.uncheckedconversions
- .safeScriptFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(
- 'Safe because value is constant. Security review: b/7685625.'),
- script);
- assertEquals(script, goog.html.SafeScript.unwrap(safeScript));
- }
- function testSafeScriptFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions
- .safeScriptFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'irrelevant');
- });
- }
- function testSafeStyleFromStringKnownToSatisfyTypeContract_ok() {
- var style = 'P.special { color:red ; }';
- var safeStyle =
- goog.html.uncheckedconversions
- .safeStyleFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(
- 'Safe because value is constant. Security review: b/7685625.'),
- style);
- assertEquals(style, goog.html.SafeStyle.unwrap(safeStyle));
- }
- function testSafeStyleFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions
- .safeStyleFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'irrelevant');
- });
- }
- function testSafeStyleSheetFromStringKnownToSatisfyTypeContract_ok() {
- var styleSheet = 'P.special { color:red ; }';
- var safeStyleSheet =
- goog.html.uncheckedconversions
- .safeStyleSheetFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(
- 'Safe because value is constant. Security review: b/7685625.'),
- styleSheet);
- assertEquals(styleSheet, goog.html.SafeStyleSheet.unwrap(safeStyleSheet));
- }
- function testSafeStyleSheetFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions
- .safeStyleSheetFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'irrelevant');
- });
- }
- function testSafeUrlFromStringKnownToSatisfyTypeContract_ok() {
- var url = 'http://www.irrelevant.com';
- var safeUrl =
- goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(
- 'Safe because value is constant. Security review: b/7685625.'),
- url);
- assertEquals(url, goog.html.SafeUrl.unwrap(safeUrl));
- }
- function testSafeUrlFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'http://irrelevant.com');
- });
- }
- function testTrustedResourceUrlFromStringKnownToSatisfyTypeContract_ok() {
- var url = 'http://www.irrelevant.com';
- var trustedResourceUrl =
- goog.html.uncheckedconversions
- .trustedResourceUrlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(
- 'Safe because value is constant. Security review: b/7685625.'),
- url);
- assertEquals(url, goog.html.TrustedResourceUrl.unwrap(trustedResourceUrl));
- }
- function testTrustedResourceFromStringKnownToSatisfyTypeContract_error() {
- assertThrows(function() {
- goog.html.uncheckedconversions
- .trustedResourceUrlFromStringKnownToSatisfyTypeContract(
- goog.string.Const.from(''), 'http://irrelevant.com');
- });
- }
|