uncheckedconversions_test.js 5.2 KB

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