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

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

Issue 6905167: Implement API to disallow code generation from strings for a context. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix comment Created 9 years, 7 months 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 | Annotate | Revision Log
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 14005 matching lines...) Expand 10 before | Expand all | Expand 10 after
14016 { // Check that query wins on disagreement. 14016 { // Check that query wins on disagreement.
14017 Handle<ObjectTemplate> templ = ObjectTemplate::New(); 14017 Handle<ObjectTemplate> templ = ObjectTemplate::New();
14018 templ->SetNamedPropertyHandler(HasOwnPropertyNamedPropertyGetter, 14018 templ->SetNamedPropertyHandler(HasOwnPropertyNamedPropertyGetter,
14019 0, 14019 0,
14020 HasOwnPropertyNamedPropertyQuery2); 14020 HasOwnPropertyNamedPropertyQuery2);
14021 Handle<Object> instance = templ->NewInstance(); 14021 Handle<Object> instance = templ->NewInstance();
14022 CHECK(!instance->HasOwnProperty(v8_str("foo"))); 14022 CHECK(!instance->HasOwnProperty(v8_str("foo")));
14023 CHECK(instance->HasOwnProperty(v8_str("bar"))); 14023 CHECK(instance->HasOwnProperty(v8_str("bar")));
14024 } 14024 }
14025 } 14025 }
14026
14027
14028 void CheckCodeGenerationAllowed() {
14029 Handle<Value> result = CompileRun("eval('42')");
14030 CHECK_EQ(42, result->Int32Value());
14031 result = CompileRun("(function(e) { return e('42'); })(eval)");
14032 CHECK_EQ(42, result->Int32Value());
14033 result = CompileRun("execScript('42')");
14034 CHECK(!result.IsEmpty());
14035 result = CompileRun("var f = new Function('return 42'); f()");
14036 CHECK_EQ(42, result->Int32Value());
14037 }
14038
14039
14040 void CheckCodeGenerationDisallowed() {
14041 TryCatch try_catch;
14042
14043 Handle<Value> result = CompileRun("eval('42')");
14044 CHECK(result.IsEmpty());
14045 CHECK(try_catch.HasCaught());
14046 try_catch.Reset();
14047
14048 result = CompileRun("(function(e) { return e('42'); })(eval)");
14049 CHECK(result.IsEmpty());
14050 CHECK(try_catch.HasCaught());
14051 try_catch.Reset();
14052
14053 result = CompileRun("execScript('42')");
14054 CHECK(result.IsEmpty());
14055 CHECK(try_catch.HasCaught());
14056 try_catch.Reset();
14057
14058 result = CompileRun("var f = new Function('return 42'); f()");
14059 CHECK(result.IsEmpty());
14060 CHECK(try_catch.HasCaught());
14061 }
14062
14063
14064 bool CodeGenerationAllowed(Local<Context> context) {
14065 ApiTestFuzzer::Fuzz();
14066 return true;
14067 }
14068
14069
14070 bool CodeGenerationDisallowed(Local<Context> context) {
14071 ApiTestFuzzer::Fuzz();
14072 return false;
14073 }
14074
14075
14076 THREADED_TEST(AllowCodeGenFromStrings) {
14077 v8::HandleScope scope;
14078 LocalContext context;
14079
14080 // eval, execScript and the Function constructor allowed by default.
14081 CheckCodeGenerationAllowed();
14082
14083 // Disallow eval, execScript and the Function constructor.
14084 context->AllowCodeGenerationFromStrings(false);
14085 CheckCodeGenerationDisallowed();
14086
14087 // Allow again.
14088 context->AllowCodeGenerationFromStrings(true);
14089 CheckCodeGenerationAllowed();
14090
14091 // Disallow but setting a global callback that will allow the calls.
14092 context->AllowCodeGenerationFromStrings(false);
14093 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed);
14094 CheckCodeGenerationAllowed();
14095
14096 // Set a callback that disallows the code generation.
14097 V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed);
14098 CheckCodeGenerationDisallowed();
14099 }
OLDNEW
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698