root/chrome/renderer/extensions/safe_builtins_unittest.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. TEST_F
  2. TEST_F
  3. TEST_F
  4. TEST_F

// Copyright 2013 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 "chrome/test/base/module_system_test.h"

#include "grit/renderer_resources.h"

namespace extensions {
namespace {

class SafeBuiltinsUnittest : public ModuleSystemTest {
};

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

TEST_F(SafeBuiltinsUnittest, TestSelf) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      context_->module_system());
  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"
  );
  context_->module_system()->Require("test");
}

TEST_F(SafeBuiltinsUnittest, TestStaticFunction) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      context_->module_system());
  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"
  );
  context_->module_system()->Require("test");
}

TEST_F(SafeBuiltinsUnittest, TestInstanceMethod) {
  ModuleSystem::NativesEnabledScope natives_enabled_scope(
      context_->module_system());
  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"
  );
  context_->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

/* [<][>][^][v][top][bottom][index][help] */