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

Unified 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, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index a228a6f4999b6d02748aaac768944aaf10213f2c..8548b441e990d03ccf85cb79518e57d922791bb5 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -14023,3 +14023,77 @@ TEST(HasOwnProperty) {
CHECK(instance->HasOwnProperty(v8_str("bar")));
}
}
+
+
+void CheckCodeGenerationAllowed() {
+ Handle<Value> result = CompileRun("eval('42')");
+ CHECK_EQ(42, result->Int32Value());
+ result = CompileRun("(function(e) { return e('42'); })(eval)");
+ CHECK_EQ(42, result->Int32Value());
+ result = CompileRun("execScript('42')");
+ CHECK(!result.IsEmpty());
+ result = CompileRun("var f = new Function('return 42'); f()");
+ CHECK_EQ(42, result->Int32Value());
+}
+
+
+void CheckCodeGenerationDisallowed() {
+ TryCatch try_catch;
+
+ Handle<Value> result = CompileRun("eval('42')");
+ CHECK(result.IsEmpty());
+ CHECK(try_catch.HasCaught());
+ try_catch.Reset();
+
+ result = CompileRun("(function(e) { return e('42'); })(eval)");
+ CHECK(result.IsEmpty());
+ CHECK(try_catch.HasCaught());
+ try_catch.Reset();
+
+ result = CompileRun("execScript('42')");
+ CHECK(result.IsEmpty());
+ CHECK(try_catch.HasCaught());
+ try_catch.Reset();
+
+ result = CompileRun("var f = new Function('return 42'); f()");
+ CHECK(result.IsEmpty());
+ CHECK(try_catch.HasCaught());
+}
+
+
+bool CodeGenerationAllowed(Local<Context> context) {
+ ApiTestFuzzer::Fuzz();
+ return true;
+}
+
+
+bool CodeGenerationDisallowed(Local<Context> context) {
+ ApiTestFuzzer::Fuzz();
+ return false;
+}
+
+
+THREADED_TEST(AllowCodeGenFromStrings) {
+ v8::HandleScope scope;
+ LocalContext context;
+
+ // eval, execScript and the Function constructor allowed by default.
+ CheckCodeGenerationAllowed();
+
+ // Disallow eval, execScript and the Function constructor.
+ context->AllowCodeGenerationFromStrings(false);
+ CheckCodeGenerationDisallowed();
+
+ // Allow again.
+ context->AllowCodeGenerationFromStrings(true);
+ CheckCodeGenerationAllowed();
+
+ // Disallow but setting a global callback that will allow the calls.
+ context->AllowCodeGenerationFromStrings(false);
+ V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed);
+ CheckCodeGenerationAllowed();
+
+ // Set a callback that disallows the code generation.
+ V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed);
+ CheckCodeGenerationDisallowed();
+}
« 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