builtinFuncHasOwnPropCallerArguments.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. /*
  6. See:
  7. gh-249
  8. OS#14101048
  9. According to #sec-forbidden-extensions
  10. For all built-in functions `func`, `func.hasOwnProperty('arguments')` and `func.hasOwnProperty('caller')` must return false.
  11. Array.prototype.push.hasOwnProperty('arguments') // === false
  12. Array.prototype.push.hasOwnProperty('caller') // === false
  13. */
  14. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  15. let isVerbose = WScript.Arguments[0] != "summary";
  16. let global = this;
  17. function getFunctions(ns) {
  18. return Object.getOwnPropertyNames(ns)
  19. .filter(x => {
  20. // print(x);
  21. if (['caller', 'arguments'].includes(x)) {
  22. return false;
  23. }
  24. return typeof ns[x] === 'function'
  25. })
  26. .sort()
  27. .map(x => {
  28. return [x, ns[x]];
  29. })
  30. }
  31. let builtins = {
  32. "global": getFunctions(global).filter(x => ![
  33. 'getFunctions',
  34. ].includes(x[0])),
  35. "Object.prototype": getFunctions(Object.prototype),
  36. "String.prototype": getFunctions(String.prototype),
  37. "RegExp.prototype": getFunctions(RegExp.prototype),
  38. "Function.prototype": getFunctions(Function.prototype),
  39. "Array.prototype": getFunctions(Array.prototype),
  40. "Uint8Array.prototype": getFunctions(Uint8Array.prototype),
  41. "Uint8ClampedArray.prototype": getFunctions(Uint8ClampedArray.prototype),
  42. "Uint16Array.prototype": getFunctions(Uint16Array.prototype),
  43. "Uint32Array.prototype": getFunctions(Uint32Array.prototype),
  44. "Int8Array.prototype": getFunctions(Int8Array.prototype),
  45. "Int16Array.prototype": getFunctions(Int16Array.prototype),
  46. "Int32Array.prototype": getFunctions(Int32Array.prototype),
  47. };
  48. if (typeof Intl !== "undefined") {
  49. builtins["Intl"] = getFunctions(Intl);
  50. builtins["Intl.Collator"] = getFunctions(Intl.Collator);
  51. builtins["Intl.Collator.prototype"] = getFunctions(Intl.Collator.prototype);
  52. builtins["Intl.DateTimeFormat"] = getFunctions(Intl.DateTimeFormat);
  53. builtins["Intl.DateTimeFormat.prototype"] = getFunctions(Intl.DateTimeFormat.prototype);
  54. builtins["Intl.NumberFormat"] = getFunctions(Intl.NumberFormat);
  55. builtins["Intl.NumberFormat.prototype"] = getFunctions(Intl.NumberFormat.prototype);
  56. }
  57. var tests = [
  58. {
  59. name: "builtin functions hasOwnProperty('arguments'|'caller') === false",
  60. body: function () {
  61. function test(f, p, name) {
  62. if (isVerbose) {
  63. print(`Checking: ${name}.hasOwnProperty('${p}') === false`);
  64. }
  65. assert.areEqual(false, f.hasOwnProperty(p), `Expected (${name}.hasOwnProperty('${p}') === false) but actually true`);
  66. }
  67. for (let ns in builtins) {
  68. let functions = builtins[ns];
  69. for (let f of functions) {
  70. let funcName = f[0];
  71. let func = f[1];
  72. let fullName = `${ns}.${funcName}`;
  73. test(func, 'arguments', fullName);
  74. test(func, 'caller', fullName);
  75. }
  76. }
  77. }
  78. }
  79. ];
  80. testRunner.runTests(tests, { verbose: isVerbose });