summaryrefslogtreecommitdiffstats
path: root/extensions/renderer/safe_builtins_unittest.cc
blob: 38910a2c5df5cdac8b6283b671de58a9a1ea73da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/module_system_test.h"

namespace extensions {
namespace {

class SafeBuiltinsUnittest : public ModuleSystemTest {};

TEST_F(SafeBuiltinsUnittest, TestNotOriginalObject) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      env()->module_system());
  env()->RegisterModule("test",
                        "var assert = requireNative('assert');\n"
                        "Array.foo = 10;\n"
                        "assert.AssertTrue(!$Array.hasOwnProperty('foo'));\n");
  env()->module_system()->Require("test");
}

TEST_F(SafeBuiltinsUnittest, TestSelf) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      env()->module_system());
  env()->RegisterModule("test",
                        "var assert = requireNative('assert');\n"
                        "Array.foo = 10;\n"
                        "assert.AssertTrue($Array.self.foo == 10);\n"
                        "var arr = $Array.self(1);\n"
                        "assert.AssertTrue(arr.length == 1);\n"
                        "assert.AssertTrue(arr[0] === undefined);\n");
  env()->module_system()->Require("test");
}

TEST_F(SafeBuiltinsUnittest, TestStaticFunction) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      env()->module_system());
  env()->RegisterModule("test",
                        "var assert = requireNative('assert');\n"
                        "Object.keys = function() {throw new Error()};\n"
                        "var obj = {a: 10};\n"
                        "var keys = $Object.keys(obj);\n"
                        "assert.AssertTrue(keys.length == 1);\n"
                        "assert.AssertTrue(keys[0] == 'a');\n");
  env()->module_system()->Require("test");
}

TEST_F(SafeBuiltinsUnittest, TestInstanceMethod) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      env()->module_system());
  env()->RegisterModule(
      "test",
      "var assert = requireNative('assert');\n"
      "Array.prototype.push = function() {throw new Error();}\n"
      "var arr = []\n"
      "$Array.push(arr, 1);\n"
      "assert.AssertTrue(arr.length == 1);\n"
      "assert.AssertTrue(arr[0] == 1);\n");
  env()->module_system()->Require("test");
}

// NOTE: JSON is already tested in ExtensionApiTest.Messaging, via
// chrome/test/data/extensions/api_test/messaging/connect/page.js.

}  // namespace
}  // namespace extensions