|
5 | 5 | #include "node_api.h" |
6 | 6 | #include "node_test_fixture.h" |
7 | 7 |
|
| 8 | +#include <string> |
| 9 | + |
8 | 10 | void InitializeBinding(v8::Local<v8::Object> exports, |
9 | 11 | v8::Local<v8::Value> module, |
10 | 12 | v8::Local<v8::Context> context, |
@@ -46,6 +48,52 @@ TEST_F(LinkedBindingTest, SimpleTest) { |
46 | 48 | CHECK_EQ(strcmp(*utf8val, "value"), 0); |
47 | 49 | } |
48 | 50 |
|
| 51 | +static std::string RunScript(v8::Isolate* isolate, const char* source) { |
| 52 | + v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 53 | + v8::Local<v8::Script> script = |
| 54 | + v8::Script::Compile(context, |
| 55 | + v8::String::NewFromOneByte( |
| 56 | + isolate, reinterpret_cast<const uint8_t*>(source)) |
| 57 | + .ToLocalChecked()) |
| 58 | + .ToLocalChecked(); |
| 59 | + v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked(); |
| 60 | + v8::String::Utf8Value utf8val(isolate, completion_value); |
| 61 | + CHECK_NOT_NULL(*utf8val); |
| 62 | + return *utf8val; |
| 63 | +} |
| 64 | + |
| 65 | +TEST_F(LinkedBindingTest, PermissionModelDeniesLinkedBindingTest) { |
| 66 | + const v8::HandleScope handle_scope(isolate_); |
| 67 | + const Argv argv; |
| 68 | + isolate_data_->options()->per_env->permission = true; |
| 69 | + Env test_env{handle_scope, argv}; |
| 70 | + |
| 71 | + const char* run_script = |
| 72 | + "try { process._linkedBinding('cctest_linkedbinding').key; }" |
| 73 | + " catch (err) { err.code; }"; |
| 74 | + CHECK_EQ(RunScript(isolate_, run_script), "ERR_ACCESS_DENIED"); |
| 75 | +} |
| 76 | + |
| 77 | +TEST_F(LinkedBindingTest, NoAddonPermissionForLinkedBindingsTest) { |
| 78 | + const v8::HandleScope handle_scope(isolate_); |
| 79 | + const Argv argv; |
| 80 | + isolate_data_->options()->per_env->permission = true; |
| 81 | + Env test_env{ |
| 82 | + handle_scope, |
| 83 | + argv, |
| 84 | + static_cast<node::EnvironmentFlags::Flags>( |
| 85 | + node::EnvironmentFlags::kDefaultFlags | |
| 86 | + node::EnvironmentFlags::kNoAddonPermissionForLinkedBindings)}; |
| 87 | + |
| 88 | + const char* linked_script = |
| 89 | + "process._linkedBinding('cctest_linkedbinding').key"; |
| 90 | + CHECK_EQ(RunScript(isolate_, linked_script), "value"); |
| 91 | + const char* dlopen_script = |
| 92 | + "try { process.dlopen({ exports: {} }, 'addon.node'); }" |
| 93 | + " catch (err) { err.code; }"; |
| 94 | + CHECK_EQ(RunScript(isolate_, dlopen_script), "ERR_DLOPEN_DISABLED"); |
| 95 | +} |
| 96 | + |
49 | 97 | void InitializeLocalBinding(v8::Local<v8::Object> exports, |
50 | 98 | v8::Local<v8::Value> module, |
51 | 99 | v8::Local<v8::Context> context, |
|
0 commit comments