Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: test/cctest/test-api-interceptors.cc

Issue 1410883006: Plumb accessing context through to access control callbacks (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/cctest/test-api.cc ('k') | test/cctest/test-object-observe.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "test/cctest/test-api.h" 7 #include "test/cctest/test-api.h"
8 8
9 #include "include/v8-util.h" 9 #include "include/v8-util.h"
10 #include "src/api.h" 10 #include "src/api.h"
(...skipping 1642 matching lines...) Expand 10 before | Expand all | Expand 10 after
1653 " if (v != 0) throw 'Wrong value ' + v + ' at iteration ' + i;" 1653 " if (v != 0) throw 'Wrong value ' + v + ' at iteration ' + i;"
1654 " }" 1654 " }"
1655 " 'PASSED'" 1655 " 'PASSED'"
1656 "} catch(e) {" 1656 "} catch(e) {"
1657 " e" 1657 " e"
1658 "}"; 1658 "}";
1659 ExpectString(code, "PASSED"); 1659 ExpectString(code, "PASSED");
1660 } 1660 }
1661 1661
1662 1662
1663 static bool AccessAlwaysBlocked(Local<v8::Object> global, Local<Value> name, 1663 static bool AccessAlwaysBlocked(Local<v8::Context> accessing_context,
1664 v8::AccessType type, Local<Value> data) { 1664 Local<v8::Object> accessed_object) {
1665 return false; 1665 return false;
1666 } 1666 }
1667 1667
1668 1668
1669 THREADED_TEST(IndexedInterceptorWithAccessorCheck) { 1669 THREADED_TEST(IndexedInterceptorWithAccessorCheck) {
1670 v8::Isolate* isolate = CcTest::isolate(); 1670 v8::Isolate* isolate = CcTest::isolate();
1671 v8::HandleScope scope(isolate); 1671 v8::HandleScope scope(isolate);
1672 Local<ObjectTemplate> templ = ObjectTemplate::New(isolate); 1672 Local<ObjectTemplate> templ = ObjectTemplate::New(isolate);
1673 templ->SetHandler( 1673 templ->SetHandler(
1674 v8::IndexedPropertyHandlerConfiguration(IdentityIndexedPropertyGetter)); 1674 v8::IndexedPropertyHandlerConfiguration(IdentityIndexedPropertyGetter));
1675 1675
1676 templ->SetAccessCheckCallbacks(AccessAlwaysBlocked, nullptr); 1676 templ->SetAccessCheckCallback(AccessAlwaysBlocked);
1677 1677
1678 LocalContext context; 1678 LocalContext context;
1679 Local<v8::Object> obj = templ->NewInstance(); 1679 Local<v8::Object> obj = templ->NewInstance();
1680 context->Global()->Set(v8_str("obj"), obj); 1680 context->Global()->Set(v8_str("obj"), obj);
1681 1681
1682 const char* code = 1682 const char* code =
1683 "var result = 'PASSED';" 1683 "var result = 'PASSED';"
1684 "for (var i = 0; i < 100; i++) {" 1684 "for (var i = 0; i < 100; i++) {"
1685 " try {" 1685 " try {"
1686 " var v = obj[0];" 1686 " var v = obj[0];"
(...skipping 1213 matching lines...) Expand 10 before | Expand all | Expand 10 after
2900 return reinterpret_cast<T*>( 2900 return reinterpret_cast<T*>(
2901 Object::Cast(*data)->GetAlignedPointerFromInternalField(0)); 2901 Object::Cast(*data)->GetAlignedPointerFromInternalField(0));
2902 } 2902 }
2903 2903
2904 2904
2905 struct AccessCheckData { 2905 struct AccessCheckData {
2906 int count; 2906 int count;
2907 bool result; 2907 bool result;
2908 }; 2908 };
2909 2909
2910 AccessCheckData* g_access_check_data = nullptr;
2910 2911
2911 bool SimpleAccessChecker(Local<v8::Object> global, Local<Value> name, 2912
2912 v8::AccessType type, Local<Value> data) { 2913 bool SimpleAccessChecker(Local<v8::Context> accessing_context,
2913 auto access_check_data = GetWrappedObject<AccessCheckData>(data); 2914 Local<v8::Object> access_object) {
2914 access_check_data->count++; 2915 g_access_check_data->count++;
2915 return access_check_data->result; 2916 return g_access_check_data->result;
2916 } 2917 }
2917 2918
2918 2919
2919 struct ShouldInterceptData { 2920 struct ShouldInterceptData {
2920 int value; 2921 int value;
2921 bool should_intercept; 2922 bool should_intercept;
2922 }; 2923 };
2923 2924
2924 2925
2925 void ShouldNamedInterceptor(Local<Name> name, 2926 void ShouldNamedInterceptor(Local<Name> name,
(...skipping 11 matching lines...) Expand all
2937 ApiTestFuzzer::Fuzz(); 2938 ApiTestFuzzer::Fuzz();
2938 CheckReturnValue(info, FUNCTION_ADDR(ShouldIndexedInterceptor)); 2939 CheckReturnValue(info, FUNCTION_ADDR(ShouldIndexedInterceptor));
2939 auto data = GetWrappedObject<ShouldInterceptData>(info.Data()); 2940 auto data = GetWrappedObject<ShouldInterceptData>(info.Data());
2940 if (!data->should_intercept) return; 2941 if (!data->should_intercept) return;
2941 info.GetReturnValue().Set(v8_num(data->value)); 2942 info.GetReturnValue().Set(v8_num(data->value));
2942 } 2943 }
2943 2944
2944 } // namespace 2945 } // namespace
2945 2946
2946 2947
2947 THREADED_TEST(NamedAllCanReadInterceptor) { 2948 TEST(NamedAllCanReadInterceptor) {
2948 auto isolate = CcTest::isolate(); 2949 auto isolate = CcTest::isolate();
2949 v8::HandleScope handle_scope(isolate); 2950 v8::HandleScope handle_scope(isolate);
2950 LocalContext context; 2951 LocalContext context;
2951 2952
2952 AccessCheckData access_check_data; 2953 AccessCheckData access_check_data;
2953 access_check_data.result = true; 2954 access_check_data.result = true;
2954 access_check_data.count = 0; 2955 access_check_data.count = 0;
2955 2956
2957 g_access_check_data = &access_check_data;
2958
2956 ShouldInterceptData intercept_data_0; 2959 ShouldInterceptData intercept_data_0;
2957 intercept_data_0.value = 239; 2960 intercept_data_0.value = 239;
2958 intercept_data_0.should_intercept = true; 2961 intercept_data_0.should_intercept = true;
2959 2962
2960 ShouldInterceptData intercept_data_1; 2963 ShouldInterceptData intercept_data_1;
2961 intercept_data_1.value = 165; 2964 intercept_data_1.value = 165;
2962 intercept_data_1.should_intercept = false; 2965 intercept_data_1.should_intercept = false;
2963 2966
2964 auto intercepted_0 = v8::ObjectTemplate::New(isolate); 2967 auto intercepted_0 = v8::ObjectTemplate::New(isolate);
2965 { 2968 {
2966 v8::NamedPropertyHandlerConfiguration conf(ShouldNamedInterceptor); 2969 v8::NamedPropertyHandlerConfiguration conf(ShouldNamedInterceptor);
2967 conf.flags = v8::PropertyHandlerFlags::kAllCanRead; 2970 conf.flags = v8::PropertyHandlerFlags::kAllCanRead;
2968 conf.data = 2971 conf.data =
2969 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_0); 2972 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_0);
2970 intercepted_0->SetHandler(conf); 2973 intercepted_0->SetHandler(conf);
2971 } 2974 }
2972 2975
2973 auto intercepted_1 = v8::ObjectTemplate::New(isolate); 2976 auto intercepted_1 = v8::ObjectTemplate::New(isolate);
2974 { 2977 {
2975 v8::NamedPropertyHandlerConfiguration conf(ShouldNamedInterceptor); 2978 v8::NamedPropertyHandlerConfiguration conf(ShouldNamedInterceptor);
2976 conf.flags = v8::PropertyHandlerFlags::kAllCanRead; 2979 conf.flags = v8::PropertyHandlerFlags::kAllCanRead;
2977 conf.data = 2980 conf.data =
2978 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_1); 2981 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_1);
2979 intercepted_1->SetHandler(conf); 2982 intercepted_1->SetHandler(conf);
2980 } 2983 }
2981 2984
2982 auto checked = v8::ObjectTemplate::New(isolate); 2985 auto checked = v8::ObjectTemplate::New(isolate);
2983 checked->SetAccessCheckCallbacks( 2986 checked->SetAccessCheckCallback(SimpleAccessChecker);
2984 SimpleAccessChecker, nullptr,
2985 BuildWrappedObject<AccessCheckData>(isolate, &access_check_data));
2986 2987
2987 context->Global()->Set(v8_str("intercepted_0"), intercepted_0->NewInstance()); 2988 context->Global()->Set(v8_str("intercepted_0"), intercepted_0->NewInstance());
2988 context->Global()->Set(v8_str("intercepted_1"), intercepted_1->NewInstance()); 2989 context->Global()->Set(v8_str("intercepted_1"), intercepted_1->NewInstance());
2989 auto checked_instance = checked->NewInstance(); 2990 auto checked_instance = checked->NewInstance();
2990 checked_instance->Set(v8_str("whatever"), v8_num(17)); 2991 checked_instance->Set(v8_str("whatever"), v8_num(17));
2991 context->Global()->Set(v8_str("checked"), checked_instance); 2992 context->Global()->Set(v8_str("checked"), checked_instance);
2992 CompileRun( 2993 CompileRun(
2993 "checked.__proto__ = intercepted_1;" 2994 "checked.__proto__ = intercepted_1;"
2994 "intercepted_1.__proto__ = intercepted_0;"); 2995 "intercepted_1.__proto__ = intercepted_0;");
2995 2996
(...skipping 14 matching lines...) Expand all
3010 CHECK_EQ(7, access_check_data.count); 3011 CHECK_EQ(7, access_check_data.count);
3011 3012
3012 intercept_data_1.should_intercept = true; 3013 intercept_data_1.should_intercept = true;
3013 ExpectInt32("checked.whatever", intercept_data_1.value); 3014 ExpectInt32("checked.whatever", intercept_data_1.value);
3014 { 3015 {
3015 v8::TryCatch try_catch(isolate); 3016 v8::TryCatch try_catch(isolate);
3016 CompileRun("Object.getOwnPropertyDescriptor(checked, 'whatever')"); 3017 CompileRun("Object.getOwnPropertyDescriptor(checked, 'whatever')");
3017 CHECK(try_catch.HasCaught()); 3018 CHECK(try_catch.HasCaught());
3018 } 3019 }
3019 CHECK_EQ(9, access_check_data.count); 3020 CHECK_EQ(9, access_check_data.count);
3021 g_access_check_data = nullptr;
3020 } 3022 }
3021 3023
3022 3024
3023 THREADED_TEST(IndexedAllCanReadInterceptor) { 3025 TEST(IndexedAllCanReadInterceptor) {
3024 auto isolate = CcTest::isolate(); 3026 auto isolate = CcTest::isolate();
3025 v8::HandleScope handle_scope(isolate); 3027 v8::HandleScope handle_scope(isolate);
3026 LocalContext context; 3028 LocalContext context;
3027 3029
3028 AccessCheckData access_check_data; 3030 AccessCheckData access_check_data;
3029 access_check_data.result = true; 3031 access_check_data.result = true;
3030 access_check_data.count = 0; 3032 access_check_data.count = 0;
3031 3033
3034 g_access_check_data = &access_check_data;
3035
3032 ShouldInterceptData intercept_data_0; 3036 ShouldInterceptData intercept_data_0;
3033 intercept_data_0.value = 239; 3037 intercept_data_0.value = 239;
3034 intercept_data_0.should_intercept = true; 3038 intercept_data_0.should_intercept = true;
3035 3039
3036 ShouldInterceptData intercept_data_1; 3040 ShouldInterceptData intercept_data_1;
3037 intercept_data_1.value = 165; 3041 intercept_data_1.value = 165;
3038 intercept_data_1.should_intercept = false; 3042 intercept_data_1.should_intercept = false;
3039 3043
3040 auto intercepted_0 = v8::ObjectTemplate::New(isolate); 3044 auto intercepted_0 = v8::ObjectTemplate::New(isolate);
3041 { 3045 {
3042 v8::IndexedPropertyHandlerConfiguration conf(ShouldIndexedInterceptor); 3046 v8::IndexedPropertyHandlerConfiguration conf(ShouldIndexedInterceptor);
3043 conf.flags = v8::PropertyHandlerFlags::kAllCanRead; 3047 conf.flags = v8::PropertyHandlerFlags::kAllCanRead;
3044 conf.data = 3048 conf.data =
3045 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_0); 3049 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_0);
3046 intercepted_0->SetHandler(conf); 3050 intercepted_0->SetHandler(conf);
3047 } 3051 }
3048 3052
3049 auto intercepted_1 = v8::ObjectTemplate::New(isolate); 3053 auto intercepted_1 = v8::ObjectTemplate::New(isolate);
3050 { 3054 {
3051 v8::IndexedPropertyHandlerConfiguration conf(ShouldIndexedInterceptor); 3055 v8::IndexedPropertyHandlerConfiguration conf(ShouldIndexedInterceptor);
3052 conf.flags = v8::PropertyHandlerFlags::kAllCanRead; 3056 conf.flags = v8::PropertyHandlerFlags::kAllCanRead;
3053 conf.data = 3057 conf.data =
3054 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_1); 3058 BuildWrappedObject<ShouldInterceptData>(isolate, &intercept_data_1);
3055 intercepted_1->SetHandler(conf); 3059 intercepted_1->SetHandler(conf);
3056 } 3060 }
3057 3061
3058 auto checked = v8::ObjectTemplate::New(isolate); 3062 auto checked = v8::ObjectTemplate::New(isolate);
3059 checked->SetAccessCheckCallbacks( 3063 checked->SetAccessCheckCallback(SimpleAccessChecker);
3060 SimpleAccessChecker, nullptr,
3061 BuildWrappedObject<AccessCheckData>(isolate, &access_check_data));
3062 3064
3063 context->Global()->Set(v8_str("intercepted_0"), intercepted_0->NewInstance()); 3065 context->Global()->Set(v8_str("intercepted_0"), intercepted_0->NewInstance());
3064 context->Global()->Set(v8_str("intercepted_1"), intercepted_1->NewInstance()); 3066 context->Global()->Set(v8_str("intercepted_1"), intercepted_1->NewInstance());
3065 auto checked_instance = checked->NewInstance(); 3067 auto checked_instance = checked->NewInstance();
3066 context->Global()->Set(v8_str("checked"), checked_instance); 3068 context->Global()->Set(v8_str("checked"), checked_instance);
3067 checked_instance->Set(15, v8_num(17)); 3069 checked_instance->Set(15, v8_num(17));
3068 CompileRun( 3070 CompileRun(
3069 "checked.__proto__ = intercepted_1;" 3071 "checked.__proto__ = intercepted_1;"
3070 "intercepted_1.__proto__ = intercepted_0;"); 3072 "intercepted_1.__proto__ = intercepted_0;");
3071 3073
(...skipping 15 matching lines...) Expand all
3087 CHECK_EQ(7, access_check_data.count); 3089 CHECK_EQ(7, access_check_data.count);
3088 3090
3089 intercept_data_1.should_intercept = true; 3091 intercept_data_1.should_intercept = true;
3090 ExpectInt32("checked[15]", intercept_data_1.value); 3092 ExpectInt32("checked[15]", intercept_data_1.value);
3091 { 3093 {
3092 v8::TryCatch try_catch(isolate); 3094 v8::TryCatch try_catch(isolate);
3093 CompileRun("Object.getOwnPropertyDescriptor(checked, '15')"); 3095 CompileRun("Object.getOwnPropertyDescriptor(checked, '15')");
3094 CHECK(try_catch.HasCaught()); 3096 CHECK(try_catch.HasCaught());
3095 } 3097 }
3096 CHECK_EQ(9, access_check_data.count); 3098 CHECK_EQ(9, access_check_data.count);
3099
3100 g_access_check_data = nullptr;
3097 } 3101 }
3098 3102
3099 3103
3100 THREADED_TEST(NonMaskingInterceptorOwnProperty) { 3104 THREADED_TEST(NonMaskingInterceptorOwnProperty) {
3101 auto isolate = CcTest::isolate(); 3105 auto isolate = CcTest::isolate();
3102 v8::HandleScope handle_scope(isolate); 3106 v8::HandleScope handle_scope(isolate);
3103 LocalContext context; 3107 LocalContext context;
3104 3108
3105 ShouldInterceptData intercept_data; 3109 ShouldInterceptData intercept_data;
3106 intercept_data.value = 239; 3110 intercept_data.value = 239;
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
3279 "var obj = intercepted_1;" 3283 "var obj = intercepted_1;"
3280 "obj.x = 4;" 3284 "obj.x = 4;"
3281 "eval('obj.x');" 3285 "eval('obj.x');"
3282 "eval('obj.x');" 3286 "eval('obj.x');"
3283 "eval('obj.x');" 3287 "eval('obj.x');"
3284 "obj = intercepted_2;" 3288 "obj = intercepted_2;"
3285 "obj.x = 9;" 3289 "obj.x = 9;"
3286 "eval('obj.x');", 3290 "eval('obj.x');",
3287 9); 3291 9);
3288 } 3292 }
OLDNEW
« no previous file with comments | « test/cctest/test-api.cc ('k') | test/cctest/test-object-observe.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698