interceptor_unittest.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "gin/interceptor.h"
  5. #include <stdint.h>
  6. #include "base/bind.h"
  7. #include "gin/arguments.h"
  8. #include "gin/handle.h"
  9. #include "gin/object_template_builder.h"
  10. #include "gin/per_isolate_data.h"
  11. #include "gin/public/isolate_holder.h"
  12. #include "gin/test/v8_test.h"
  13. #include "gin/try_catch.h"
  14. #include "gin/wrappable.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. #include "v8/include/v8-function.h"
  17. #include "v8/include/v8-script.h"
  18. #include "v8/include/v8-util.h"
  19. namespace gin {
  20. class MyInterceptor : public Wrappable<MyInterceptor>,
  21. public NamedPropertyInterceptor,
  22. public IndexedPropertyInterceptor {
  23. public:
  24. MyInterceptor(const MyInterceptor&) = delete;
  25. MyInterceptor& operator=(const MyInterceptor&) = delete;
  26. static WrapperInfo kWrapperInfo;
  27. static gin::Handle<MyInterceptor> Create(v8::Isolate* isolate) {
  28. return CreateHandle(isolate, new MyInterceptor(isolate));
  29. }
  30. int value() const { return value_; }
  31. void set_value(int value) { value_ = value; }
  32. // gin::NamedPropertyInterceptor
  33. v8::Local<v8::Value> GetNamedProperty(v8::Isolate* isolate,
  34. const std::string& property) override {
  35. if (property == "value") {
  36. return ConvertToV8(isolate, value_);
  37. } else if (property == "func") {
  38. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  39. return GetFunctionTemplate(isolate, "func")
  40. ->GetFunction(context)
  41. .ToLocalChecked();
  42. } else {
  43. return v8::Local<v8::Value>();
  44. }
  45. }
  46. bool SetNamedProperty(v8::Isolate* isolate,
  47. const std::string& property,
  48. v8::Local<v8::Value> value) override {
  49. if (property == "value") {
  50. ConvertFromV8(isolate, value, &value_);
  51. return true;
  52. }
  53. return false;
  54. }
  55. std::vector<std::string> EnumerateNamedProperties(
  56. v8::Isolate* isolate) override {
  57. std::vector<std::string> result;
  58. result.push_back("func");
  59. result.push_back("value");
  60. return result;
  61. }
  62. // gin::IndexedPropertyInterceptor
  63. v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate,
  64. uint32_t index) override {
  65. if (index == 0)
  66. return ConvertToV8(isolate, value_);
  67. return v8::Local<v8::Value>();
  68. }
  69. bool SetIndexedProperty(v8::Isolate* isolate,
  70. uint32_t index,
  71. v8::Local<v8::Value> value) override {
  72. if (index == 0) {
  73. ConvertFromV8(isolate, value, &value_);
  74. return true;
  75. }
  76. // Don't allow bypassing the interceptor.
  77. return true;
  78. }
  79. std::vector<uint32_t> EnumerateIndexedProperties(
  80. v8::Isolate* isolate) override {
  81. std::vector<uint32_t> result;
  82. result.push_back(0);
  83. return result;
  84. }
  85. private:
  86. explicit MyInterceptor(v8::Isolate* isolate)
  87. : NamedPropertyInterceptor(isolate, this),
  88. IndexedPropertyInterceptor(isolate, this),
  89. value_(0),
  90. template_cache_(isolate) {}
  91. ~MyInterceptor() override = default;
  92. // gin::Wrappable
  93. ObjectTemplateBuilder GetObjectTemplateBuilder(
  94. v8::Isolate* isolate) override {
  95. return Wrappable<MyInterceptor>::GetObjectTemplateBuilder(isolate)
  96. .AddNamedPropertyInterceptor()
  97. .AddIndexedPropertyInterceptor();
  98. }
  99. int Call(int value) {
  100. int tmp = value_;
  101. value_ = value;
  102. return tmp;
  103. }
  104. v8::Local<v8::FunctionTemplate> GetFunctionTemplate(v8::Isolate* isolate,
  105. const std::string& name) {
  106. v8::Local<v8::FunctionTemplate> function_template =
  107. template_cache_.Get(name);
  108. if (!function_template.IsEmpty())
  109. return function_template;
  110. function_template = CreateFunctionTemplate(
  111. isolate, base::BindRepeating(&MyInterceptor::Call),
  112. InvokerOptions{true, nullptr});
  113. template_cache_.Set(name, function_template);
  114. return function_template;
  115. }
  116. int value_;
  117. v8::StdGlobalValueMap<std::string, v8::FunctionTemplate> template_cache_;
  118. };
  119. WrapperInfo MyInterceptor::kWrapperInfo = {kEmbedderNativeGin};
  120. class InterceptorTest : public V8Test {
  121. public:
  122. void RunInterceptorTest(const std::string& script_source) {
  123. v8::Isolate* isolate = instance_->isolate();
  124. v8::HandleScope handle_scope(isolate);
  125. gin::Handle<MyInterceptor> obj = MyInterceptor::Create(isolate);
  126. obj->set_value(42);
  127. EXPECT_EQ(42, obj->value());
  128. v8::Local<v8::String> source = StringToV8(isolate, script_source);
  129. EXPECT_FALSE(source.IsEmpty());
  130. gin::TryCatch try_catch(isolate);
  131. v8::Local<v8::Script> script =
  132. v8::Script::Compile(context_.Get(isolate), source).ToLocalChecked();
  133. v8::Local<v8::Value> val =
  134. script->Run(context_.Get(isolate)).ToLocalChecked();
  135. EXPECT_FALSE(val.IsEmpty());
  136. v8::Local<v8::Function> func;
  137. EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
  138. v8::Local<v8::Value> argv[] = {
  139. ConvertToV8(isolate, obj.get()).ToLocalChecked(),
  140. };
  141. func->Call(context_.Get(isolate), v8::Undefined(isolate), 1, argv)
  142. .ToLocalChecked();
  143. EXPECT_FALSE(try_catch.HasCaught());
  144. EXPECT_EQ("", try_catch.GetStackTrace());
  145. EXPECT_EQ(191, obj->value());
  146. }
  147. };
  148. TEST_F(InterceptorTest, NamedInterceptor) {
  149. RunInterceptorTest(
  150. "(function (obj) {"
  151. " if (obj.value !== 42) throw 'FAIL';"
  152. " else obj.value = 191; })");
  153. }
  154. TEST_F(InterceptorTest, NamedInterceptorCall) {
  155. RunInterceptorTest(
  156. "(function (obj) {"
  157. " if (obj.func(191) !== 42) throw 'FAIL';"
  158. " })");
  159. }
  160. TEST_F(InterceptorTest, IndexedInterceptor) {
  161. RunInterceptorTest(
  162. "(function (obj) {"
  163. " if (obj[0] !== 42) throw 'FAIL';"
  164. " else obj[0] = 191; })");
  165. }
  166. TEST_F(InterceptorTest, BypassInterceptorAllowed) {
  167. RunInterceptorTest(
  168. "(function (obj) {"
  169. " obj.value = 191 /* make test happy */;"
  170. " obj.foo = 23;"
  171. " if (obj.foo !== 23) throw 'FAIL'; })");
  172. }
  173. TEST_F(InterceptorTest, BypassInterceptorForbidden) {
  174. RunInterceptorTest(
  175. "(function (obj) {"
  176. " obj.value = 191 /* make test happy */;"
  177. " obj[1] = 23;"
  178. " if (obj[1] === 23) throw 'FAIL'; })");
  179. }
  180. } // namespace gin