Skip to content

Commit

Permalink
node-api: test promise null-checks
Browse files Browse the repository at this point in the history
Also adds a missing check for deferred being NULL during resolution.
  • Loading branch information
gabrielschulhof committed Apr 13, 2023
1 parent 5273947 commit 66b139d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ inline napi_status ConcludeDeferred(napi_env env,
napi_value result,
bool is_resolved) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, deferred);
CHECK_ARG(env, result);

v8::Local<v8::Context> context = env->context();
Expand Down
2 changes: 2 additions & 0 deletions test/js-native-api/test_promise/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"target_name": "test_promise",
"sources": [
"../common.c",
"../entry_point.c",
"test_null.c",
"test_promise.c"
]
}
Expand Down
83 changes: 83 additions & 0 deletions test/js-native-api/test_promise/test_null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <js_native_api.h>

#include "../common.h"
#include "test_null.h"

static napi_value CreatePromise(napi_env env, napi_callback_info info) {
napi_value return_value, promise;
napi_deferred deferred;

NODE_API_CALL(env, napi_create_object(env, &return_value));

add_returned_status(env,
"envIsNull",
return_value,
"Invalid argument",
napi_invalid_arg,
napi_create_promise(NULL, &deferred, &promise));

napi_create_promise(env, NULL, &promise);
add_last_status(env, "deferredIsNull", return_value);

napi_create_promise(env, &deferred, NULL);
add_last_status(env, "promiseIsNull", return_value);

return return_value;
}

static napi_value test_resolution_api(napi_env env,
napi_callback_info info,
napi_status (*api)(napi_env,
napi_deferred,
napi_value)) {
napi_value return_value, promise, undefined;
napi_deferred deferred;

NODE_API_CALL(env, napi_create_object(env, &return_value));
NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise));
NODE_API_CALL(env, napi_get_undefined(env, &undefined));

add_returned_status(env,
"envIsNull",
return_value,
"Invalid argument",
napi_invalid_arg,
api(NULL, deferred, undefined));

api(env, NULL, undefined);
add_last_status(env, "deferredIsNull", return_value);

api(env, deferred, NULL);
add_last_status(env, "valueIsNull", return_value);

return return_value;
}

static napi_value ResolveDeferred(napi_env env, napi_callback_info info) {
return test_resolution_api(env, info, napi_resolve_deferred);
}

static napi_value RejectDeferred(napi_env env, napi_callback_info info) {
return test_resolution_api(env, info, napi_reject_deferred);
}

void init_test_null(napi_env env, napi_value exports) {
napi_value test_null;

const napi_property_descriptor test_null_props[] = {
DECLARE_NODE_API_PROPERTY("createPromise", CreatePromise),
DECLARE_NODE_API_PROPERTY("resolveDeferred", ResolveDeferred),
DECLARE_NODE_API_PROPERTY("rejectDeferred", RejectDeferred),
};

NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
NODE_API_CALL_RETURN_VOID(
env,
napi_define_properties(env,
test_null,
sizeof(test_null_props) / sizeof(*test_null_props),
test_null_props));

NODE_API_CALL_RETURN_VOID(
env, napi_set_named_property(env, exports, "testNull", test_null));
}
8 changes: 8 additions & 0 deletions test/js-native-api/test_promise/test_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
#define TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_

#include <js_native_api.h>

void init_test_null(napi_env env, napi_value exports);

#endif // TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
21 changes: 21 additions & 0 deletions test/js-native-api/test_promise/test_null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../../common');
const assert = require('assert');

// Test passing NULL to object-related N-APIs.
const { testNull } = require(`./build/${common.buildType}/test_promise`);

const expectedForCreatePromise = {
envIsNull: 'Invalid argument',
deferredIsNull: 'Invalid argument',
promiseIsNull: 'Invalid argument',
};
assert.deepStrictEqual(testNull.createPromise(), expectedForCreatePromise);

const expectedForResolution = {
envIsNull: 'Invalid argument',
deferredIsNull: 'Invalid argument',
valueIsNull: 'Invalid argument',
};
assert.deepStrictEqual(testNull.resolveDeferred(), expectedForResolution);
assert.deepStrictEqual(testNull.rejectDeferred(), expectedForResolution);
3 changes: 3 additions & 0 deletions test/js-native-api/test_promise/test_promise.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "test_null.h"

napi_deferred deferred = NULL;

Expand Down Expand Up @@ -58,6 +59,8 @@ napi_value Init(napi_env env, napi_value exports) {
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

init_test_null(env, exports);

return exports;
}
EXTERN_C_END

0 comments on commit 66b139d

Please sign in to comment.