1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "test.h"
17 #include "test_common.h"
18 #include "gtest/gtest.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "napi/native_common.h"
22 #include "securec.h"
23 #include "utils/log.h"
24
25 static constexpr int THREAD_PAUSE_THREE = 300 * 1000; // 300 ms
26 static constexpr int THREAD_PAUSE_ONE = 100 * 1000; // 100 ms
27 static constexpr int MAX_BUFFER_SIZE = 2;
28 static constexpr int BUFFER_SIZE_FIVE = 5;
29 static constexpr size_t TEST_STR_LENGTH = 30;
30
31 class NapiBasicTest : public NativeEngineTest {
32 public:
SetUpTestCase()33 static void SetUpTestCase()
34 {
35 GTEST_LOG_(INFO) << "NapiBasicTest SetUpTestCase";
36 }
37
TearDownTestCase()38 static void TearDownTestCase()
39 {
40 GTEST_LOG_(INFO) << "NapiBasicTest TearDownTestCase";
41 }
42
SetUp()43 void SetUp() override {}
TearDown()44 void TearDown() override {}
45 };
46
47 static const napi_type_tag typeTags[5] = { // 5:array element size is 5.
48 {0xdaf987b3cc62481a, 0xb745b0497f299531},
49 {0xbb7936c374084d9b, 0xa9548d0762eeedb9},
50 {0xa5ed9ce2e4c00c38, 0},
51 {0, 0},
52 {0xa5ed9ce2e4c00c34, 0xdaf987b3cc62481a},
53 };
54
55 /**
56 * @tc.name: UndefinedTest001
57 * @tc.desc: Test undefined type.
58 * @tc.type: FUNC
59 */
60 HWTEST_F(NapiBasicTest, UndefinedTest001, testing::ext::TestSize.Level1)
61 {
62 napi_env env = (napi_env)engine_;
63 napi_value result = nullptr;
64 ASSERT_CHECK_CALL(napi_get_undefined(env, &result));
65 ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
66 }
67
68 /**
69 * @tc.name: NullTest001
70 * @tc.desc: Test null type.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(NapiBasicTest, NullTest001, testing::ext::TestSize.Level1)
74 {
75 napi_env env = (napi_env)engine_;
76 napi_value result = nullptr;
77 ASSERT_CHECK_CALL(napi_get_null(env, &result));
78 ASSERT_CHECK_VALUE_TYPE(env, result, napi_null);
79 }
80
81 /**
82 * @tc.name: BooleanTest001
83 * @tc.desc: Test boolean type.
84 * @tc.type: FUNC
85 */
86 HWTEST_F(NapiBasicTest, BooleanTest001, testing::ext::TestSize.Level1)
87 {
88 napi_env env = (napi_env)engine_;
89 napi_value result = nullptr;
90 ASSERT_CHECK_CALL(napi_get_boolean(env, true, &result));
91 ASSERT_CHECK_VALUE_TYPE(env, result, napi_boolean);
92
93 bool resultValue = false;
94 ASSERT_CHECK_CALL(napi_get_value_bool(env, result, &resultValue));
95 ASSERT_TRUE(resultValue);
96 }
97
98 /**
99 * @tc.name: NumberTest001
100 * @tc.desc: Test number type.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(NapiBasicTest, NumberTest001, testing::ext::TestSize.Level1)
104 {
105 napi_env env = (napi_env)engine_;
106 {
107 int32_t testValue = INT32_MAX;
108 napi_value result = nullptr;
109 ASSERT_CHECK_CALL(napi_create_int32(env, testValue, &result));
110 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
111
112 int32_t resultValue = 0;
113 ASSERT_CHECK_CALL(napi_get_value_int32(env, result, &resultValue));
114 ASSERT_EQ(resultValue, INT32_MAX);
115 }
116 {
117 uint32_t testValue = UINT32_MAX;
118 napi_value result = nullptr;
119 ASSERT_CHECK_CALL(napi_create_uint32(env, testValue, &result));
120 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
121
122 uint32_t resultValue = 0;
123 ASSERT_CHECK_CALL(napi_get_value_uint32(env, result, &resultValue));
124 ASSERT_EQ(resultValue, UINT32_MAX);
125 }
126 {
127 int64_t testValue = 9007199254740991;
128 napi_value result = nullptr;
129 ASSERT_CHECK_CALL(napi_create_int64(env, testValue, &result));
130 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
131
132 int64_t resultValue = 0;
133 ASSERT_CHECK_CALL(napi_get_value_int64(env, result, &resultValue));
134 ASSERT_EQ(resultValue, testValue);
135 }
136 {
137 double testValue = DBL_MAX;
138 napi_value result = nullptr;
139 ASSERT_CHECK_CALL(napi_create_double(env, testValue, &result));
140 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
141
142 double resultValue = 0;
143 ASSERT_CHECK_CALL(napi_get_value_double(env, result, &resultValue));
144 ASSERT_EQ(resultValue, DBL_MAX);
145 }
146 }
147
148 /**
149 * @tc.name: StringTest001
150 * @tc.desc: Test string type.
151 * @tc.type: FUNC
152 */
153 HWTEST_F(NapiBasicTest, StringTest001, testing::ext::TestSize.Level1)
154 {
155 napi_env env = (napi_env)engine_;
156 const char testStr[] = "中文,English,123456,!@#$%$#^%&";
157 size_t testStrLength = strlen(testStr);
158 napi_value result = nullptr;
159 ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, testStrLength, &result));
160 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
161
162 char* buffer = nullptr;
163 size_t bufferSize = 0;
164 size_t strLength = 0;
165 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize));
166 ASSERT_GT(bufferSize, (size_t)0);
__anonb945b3c60102null167 buffer = new char[bufferSize + 1]{ 0 };
168 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength));
169 ASSERT_STREQ(testStr, buffer);
170 ASSERT_EQ(testStrLength, strLength);
171 delete []buffer;
172 buffer = nullptr;
173 }
174
175 /**
176 * @tc.name: StringTest002
177 * @tc.desc: Test string type.
178 * @tc.type: FUNC
179 */
180 HWTEST_F(NapiBasicTest, StringTest002, testing::ext::TestSize.Level1)
181 {
182 napi_env env = (napi_env)engine_;
183 const char testStr[] = "中测";
184 size_t testStrLength = strlen(testStr);
185 napi_value result = nullptr;
186 ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, testStrLength, &result));
187 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
188
189 std::string str = "";
190 size_t strSize = 0;
191 napi_get_value_string_latin1(env, result, nullptr, 0, &strSize);
192 str.reserve(strSize + 1);
193 str.resize(strSize);
194 napi_get_value_string_latin1(env, result, str.data(), strSize + 1, &strSize);
195
196 ASSERT_EQ(str, "-K");
197 }
198
199 /**
200 * @tc.name: StringTest003
201 * @tc.desc: Test string type.
202 * @tc.type: FUNC
203 */
204 HWTEST_F(NapiBasicTest, StringTest003, testing::ext::TestSize.Level1)
205 {
206 napi_env env = (napi_env)engine_;
207 const char16_t testStr[] = u"abc56";
208 size_t testStrLength = sizeof(testStr) / sizeof(char16_t);
209 napi_value res = nullptr;
210 ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &res));
211 ASSERT_CHECK_VALUE_TYPE(env, res, napi_string);
212
213 char16_t* buffer = nullptr;
214 size_t bufSize = 0;
215 size_t copied = 0;
216 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, res, nullptr, 0, &bufSize));
217 ASSERT_EQ(bufSize, 6);
__anonb945b3c60202null218 buffer = new char16_t[bufSize]{ 0 };
219 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, res, buffer, bufSize, &copied));
220 for (size_t i = 0; i < copied; i++) {
221 ASSERT_TRUE(testStr[i] == buffer[i]);
222 }
223 ASSERT_EQ(testStrLength - 1, copied);
224 delete []buffer;
225 buffer = nullptr;
226 }
227
228 /**
229 * @tc.name: StringTest004
230 * @tc.desc: Test string type.
231 * @tc.type: FUNC
232 */
233 HWTEST_F(NapiBasicTest, StringTest004, testing::ext::TestSize.Level1)
234 {
235 napi_env env = (napi_env)engine_;
236 const char16_t testStr[] = u"abc56";
237 size_t testStrLength = sizeof(testStr) / sizeof(char16_t);
238 napi_value result = nullptr;
239 ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &result));
240 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
241
242 char16_t buffer[4]; // 4: char16_t type of array size
243 size_t bufferSize = 4; // 4: char16_t type of array size
244 size_t copied;
245
246 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, buffer, bufferSize, &copied));
247 for (size_t i = 0; i < copied; i++) {
248 ASSERT_TRUE(testStr[i] == buffer[i]);
249 }
250 ASSERT_EQ(copied, 3);
251 }
252
253 /**
254 * @tc.name: TypetagTest001
255 * @tc.desc: Test typetag type.
256 * @tc.type: FUNC
257 */
258 HWTEST_F(NapiBasicTest, TypetagTest001, testing::ext::TestSize.Level1)
259 {
260 napi_env env = (napi_env)engine_;
261 napi_value instance = nullptr;
262 bool result;
263 for (size_t i = 0; i < 5; i++) {
264 napi_create_object(env, &instance);
265 napi_type_tag_object(env, instance, &typeTags[i]);
266 napi_check_object_type_tag(env, instance, &typeTags[i], &result);
267 ASSERT_TRUE(result);
268 }
269 }
270
271 /**
272 * @tc.name: TypetagTest002
273 * @tc.desc: Test typetag type.
274 * @tc.type: FUNC
275 */
276 HWTEST_F(NapiBasicTest, TypetagTest002, testing::ext::TestSize.Level1)
277 {
278 napi_env env = (napi_env)engine_;
279 uint32_t typeIndex = 0;
280 napi_value instance = nullptr;
281 bool result;
282 napi_create_object(env, &instance);
283
284 napi_type_tag_object(env, instance, &typeTags[typeIndex]);
285 napi_check_object_type_tag(env, instance, &typeTags[typeIndex + 1], &result);
286
287 ASSERT_FALSE(result);
288 }
289
290 /**
291 * @tc.name: SymbolTest001
292 * @tc.desc: Test symbol type.
293 * @tc.type: FUNC
294 */
295 HWTEST_F(NapiBasicTest, SymbolTest001, testing::ext::TestSize.Level1)
296 {
297 napi_env env = (napi_env)engine_;
298
299 const char testStr[] = "testSymbol";
300 napi_value result = nullptr;
301
302 napi_create_string_latin1(env, testStr, strlen(testStr), &result);
303
304 napi_value symbolVal = nullptr;
305 napi_create_symbol(env, result, &symbolVal);
306
307 ASSERT_CHECK_VALUE_TYPE(env, symbolVal, napi_symbol);
308 }
309
310 /**
311 * @tc.name: ExternalTest001
312 * @tc.desc: Test external type.
313 * @tc.type: FUNC
314 */
315 HWTEST_F(NapiBasicTest, ExternalTest001, testing::ext::TestSize.Level1)
316 {
317 napi_env env = (napi_env)engine_;
318 const char testStr[] = "test";
319 napi_value external = nullptr;
320 napi_create_external(
321 env, (void*)testStr,
__anonb945b3c60302(napi_env env, void* data, void* hint) 322 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
323 (void*)testStr, &external);
324
325 ASSERT_CHECK_VALUE_TYPE(env, external, napi_external);
326 void* tmpExternal = nullptr;
327 napi_get_value_external(env, external, &tmpExternal);
328 ASSERT_TRUE(tmpExternal);
329 ASSERT_EQ(tmpExternal, testStr);
330 }
331
332 /**
333 * @tc.name: ObjectTest001
334 * @tc.desc: Test object type.
335 * @tc.type: FUNC
336 */
337 HWTEST_F(NapiBasicTest, ObjectTest001, testing::ext::TestSize.Level1)
338 {
339 napi_env env = (napi_env)engine_;
340
341 napi_value result = nullptr;
342 ASSERT_CHECK_CALL(napi_create_object(env, &result));
343 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
344
345 const char testStr[] = "1234567";
346 napi_value strAttribute = nullptr;
347 ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute));
348 ASSERT_CHECK_VALUE_TYPE(env, strAttribute, napi_string);
349 ASSERT_CHECK_CALL(napi_set_named_property(env, result, "strAttribute", strAttribute));
350
351 napi_value retStrAttribute = nullptr;
352 ASSERT_CHECK_CALL(napi_get_named_property(env, result, "strAttribute", &retStrAttribute));
353 ASSERT_CHECK_VALUE_TYPE(env, retStrAttribute, napi_string);
354
355 int32_t testNumber = 12345;
356 napi_value numberAttribute = nullptr;
357 ASSERT_CHECK_CALL(napi_create_int32(env, testNumber, &numberAttribute));
358 ASSERT_CHECK_VALUE_TYPE(env, numberAttribute, napi_number);
359 ASSERT_CHECK_CALL(napi_set_named_property(env, result, "numberAttribute", numberAttribute));
360
361 napi_value propNames = nullptr;
362 ASSERT_CHECK_CALL(napi_get_property_names(env, result, &propNames));
363 ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
364 bool isArray = false;
365 ASSERT_CHECK_CALL(napi_is_array(env, propNames, &isArray));
366 ASSERT_TRUE(isArray);
367 uint32_t arrayLength = 0;
368 ASSERT_CHECK_CALL(napi_get_array_length(env, propNames, &arrayLength));
369 ASSERT_EQ(arrayLength, static_cast<uint32_t>(2));
370
371 for (uint32_t i = 0; i < arrayLength; i++) {
372 bool hasElement = false;
373 ASSERT_CHECK_CALL(napi_has_element(env, propNames, i, &hasElement));
374
375 napi_value propName = nullptr;
376 ASSERT_CHECK_CALL(napi_get_element(env, propNames, i, &propName));
377 ASSERT_CHECK_VALUE_TYPE(env, propName, napi_string);
378
379 bool hasProperty = false;
380 napi_has_property(env, result, propName, &hasProperty);
381 ASSERT_TRUE(hasProperty);
382
383 napi_value propValue = nullptr;
384 napi_get_property(env, result, propName, &propValue);
385 ASSERT_TRUE(propValue != nullptr);
386 }
387 }
388
389 /**
390 * @tc.name: ObjectTest002
391 * @tc.desc: Test Object Type.
392 * @tc.type: FUNC
393 */
394 HWTEST_F(NapiBasicTest, ObjectTest002, testing::ext::TestSize.Level1)
395 {
396 napi_env env = reinterpret_cast<napi_env>(engine_);
397
398 napi_value result = nullptr;
399 napi_create_object(env, &result);
400 napi_value messageKey = nullptr;
401 const char* messageKeyStr = "message";
402 napi_create_string_latin1(env, messageKeyStr, strlen(messageKeyStr), &messageKey);
403 napi_value messageValue = nullptr;
404 const char* messageValueStr = "OK";
405 napi_create_string_latin1(env, messageValueStr, strlen(messageValueStr), &messageValue);
406 napi_set_property(env, result, messageKey, messageValue);
407
408 napi_value propValue = nullptr;
409 napi_get_property(env, result, messageKey, &propValue);
410 ASSERT_TRUE(propValue != nullptr);
411
412 napi_delete_property(env, result, messageKey, nullptr);
413 bool resultVal = true;
414 napi_has_property(env, result, messageKey, &resultVal);
415 ASSERT_FALSE(resultVal);
416
417 napi_value newKey = nullptr;
418 const char* newKeyStr = "new";
419 napi_create_string_latin1(env, newKeyStr, strlen(newKeyStr), &newKey);
420 int32_t testnumber = 12345;
421 napi_value numberValue = nullptr;
422 napi_create_int32(env, testnumber, &numberValue);
423 napi_set_property(env, result, newKey, numberValue);
424
425 napi_value propNames = nullptr;
426 napi_get_property_names(env, result, &propNames);
427 uint32_t arrayLength = 0;
428 napi_get_array_length(env, propNames, &arrayLength);
429 ASSERT_EQ(arrayLength, static_cast<uint32_t>(1));
430 }
431
432 /**
433 * @tc.name: ObjectTest003
434 * @tc.desc: Test Object Type.
435 * @tc.type: FUNC
436 */
437 HWTEST_F(NapiBasicTest, ObjectTest003, testing::ext::TestSize.Level1)
438 {
439 napi_env env = reinterpret_cast<napi_env>(engine_);
440
441 napi_value result = nullptr;
442 napi_create_object(env, &result);
443
__anonb945b3c60402(napi_env env, napi_callback_info info) 444 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
445 return nullptr;
446 };
447 napi_value funcAttribute = nullptr;
448 napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcAttribute);
449
450 napi_value funcKey = nullptr;
451 const char* funcKeyStr = "func";
452 napi_create_string_latin1(env, funcKeyStr, strlen(funcKeyStr), &funcKey);
453 napi_status status = napi_set_property(env, result, funcKey, funcAttribute);
454 ASSERT_EQ(status, napi_status::napi_ok);
455
456 bool isFuncExist = false;
457 ASSERT_CHECK_CALL(napi_has_property(env, result, funcKey, &isFuncExist));
458 ASSERT_TRUE(isFuncExist);
459
460 napi_value propFuncValue = nullptr;
461 napi_get_property_names(env, result, &propFuncValue);
462 uint32_t arrayLength = 0;
463 napi_get_array_length(env, propFuncValue, &arrayLength);
464 ASSERT_EQ(arrayLength, static_cast<uint32_t>(1));
465
466 bool isFuncDelete = false;
467 ASSERT_CHECK_CALL(napi_delete_property(env, result, funcKey, &isFuncDelete));
468 ASSERT_TRUE(isFuncDelete);
469 }
470
471 /**
472 * @tc.name: FunctionTest001
473 * @tc.desc: Test function type.
474 * @tc.type: FUNC
475 */
476 HWTEST_F(NapiBasicTest, FunctionTest001, testing::ext::TestSize.Level1)
477 {
478 napi_env env = (napi_env)engine_;
479
__anonb945b3c60502(napi_env env, napi_callback_info info) 480 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
481 napi_value thisVar;
482 napi_value* argv = nullptr;
483 size_t argc = 0;
484 void* data = nullptr;
485
486 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
487 if (argc > 0) {
488 argv = new napi_value[argc];
489 }
490 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
491
492 napi_value result = nullptr;
493 napi_create_object(env, &result);
494
495 napi_value messageKey = nullptr;
496 const char* messageKeyStr = "message";
497 napi_create_string_latin1(env, messageKeyStr, strlen(messageKeyStr), &messageKey);
498 napi_value messageValue = nullptr;
499 const char* messageValueStr = "OK";
500 napi_create_string_latin1(env, messageValueStr, strlen(messageValueStr), &messageValue);
501 napi_set_property(env, result, messageKey, messageValue);
502
503 if (argv != nullptr) {
504 delete []argv;
505 }
506
507 return result;
508 };
509
510 napi_value recv = nullptr;
511 napi_value funcValue = nullptr;
512 napi_get_undefined(env, &recv);
513 ASSERT_NE(recv, nullptr);
514
515 napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
516 ASSERT_NE(funcValue, nullptr);
517
518 napi_handle_scope parentScope = nullptr;
519 napi_open_handle_scope(env, &parentScope);
520 ASSERT_NE(parentScope, nullptr);
521
522 napi_escapable_handle_scope childScope = nullptr;
523 napi_open_escapable_handle_scope(env, &childScope);
524 ASSERT_NE(childScope, nullptr);
525
526 napi_value funcResultValue = nullptr;
527 napi_value newFuncResultValue = nullptr;
528 napi_call_function(env, recv, funcValue, 0, nullptr, &funcResultValue);
529 ASSERT_NE(funcResultValue, nullptr);
530
531 napi_escape_handle(env, childScope, funcResultValue, &newFuncResultValue);
532 napi_close_escapable_handle_scope(env, childScope);
533 ASSERT_TRUE(newFuncResultValue != nullptr);
534 ASSERT_CHECK_VALUE_TYPE(env, newFuncResultValue, napi_object);
535 napi_close_handle_scope(env, parentScope);
536 }
537
538 /**
539 * @tc.name: FunctionTest002
540 * @tc.desc: Test function type.
541 * @tc.type: FUNC
542 */
543 HWTEST_F(NapiBasicTest, FunctionTest002, testing::ext::TestSize.Level1)
544 {
545 napi_env env = reinterpret_cast<napi_env>(engine_);
546
__anonb945b3c60602(napi_env env, napi_callback_info info) 547 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
548 return nullptr;
549 };
550 napi_value fn;
551 const char data[] = "data";
552 napi_status status = napi_create_function(nullptr, nullptr, 0, nullptr, nullptr, &fn);
553 ASSERT_EQ(status, napi_invalid_arg);
554 status = napi_create_function(env, nullptr, 0, nullptr, nullptr, &fn);
555 ASSERT_EQ(status, napi_invalid_arg);
556 status = napi_create_function(env, nullptr, 0, func, (void*)data, nullptr);
557 ASSERT_EQ(status, napi_invalid_arg);
558 status = napi_create_function(env, nullptr, 0, func, nullptr, &fn);
559 ASSERT_EQ(status, napi_ok);
560 status = napi_create_function(env, nullptr, 0, func, (void*)data, &fn);
561 ASSERT_EQ(status, napi_ok);
562 }
563
564 /**
565 * @tc.name: FunctionTest003
566 * @tc.desc: Test function type.
567 * @tc.type: FUNC
568 */
569 HWTEST_F(NapiBasicTest, FunctionTest003, testing::ext::TestSize.Level1)
570 {
571 napi_env env = reinterpret_cast<napi_env>(engine_);
__anonb945b3c60702(napi_env env, napi_callback_info info) 572 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
573 napi_value thisVar;
574 napi_value* argv = nullptr;
575 size_t argc = 0;
576 void* innerData = nullptr;
577
578 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
579 if (argc > 0) {
580 argv = new napi_value[argc];
581 }
582 napi_get_cb_info(env, info, &argc, argv, &thisVar, &innerData);
583 napi_value result;
584 if (argv) {
585 result = argv[0];
586 delete[] argv;
587 } else {
588 napi_get_undefined(env, &result);
589 }
590 return result;
591 };
592
593 napi_value fn;
594 napi_value funcResultValue;
595 napi_value recv;
596 napi_value jsNumber;
597 const static char data[] = "data";
598 napi_status status = napi_create_function(env, nullptr, 0, func, (void*)data, &fn);
599 ASSERT_EQ(napi_ok, status);
600
601 const int32_t testNumber = 1;
602 napi_create_int32(env, testNumber, &jsNumber);
603 napi_value argv[] = { jsNumber };
604 napi_get_undefined(env, &recv);
605 status = napi_call_function(env, recv, fn, 1, argv, &funcResultValue);
606 ASSERT_EQ(status, napi_ok);
607
608 int32_t cNumber;
609 napi_get_value_int32(env, funcResultValue, &cNumber);
610 ASSERT_EQ(cNumber, testNumber);
611
612 status = napi_call_function(env, nullptr, fn, 1, argv, &funcResultValue);
613 ASSERT_EQ(status, napi_ok);
614
615 status = napi_call_function(env, nullptr, nullptr, 1, argv, &funcResultValue);
616 ASSERT_EQ(status, napi_invalid_arg);
617
618 status = napi_call_function(env, nullptr, nullptr, 0, nullptr, &funcResultValue);
619 ASSERT_EQ(status, napi_invalid_arg);
620
621 status = napi_call_function(env, nullptr, fn, 1, argv, nullptr);
622 ASSERT_EQ(status, napi_ok);
623 }
624
TestCreateFunc(napi_env env,napi_callback_info info)625 static napi_value TestCreateFunc(napi_env env, napi_callback_info info)
626 {
627 napi_value result = nullptr;
628 napi_create_object(env, &result);
629 return result;
630 }
631
632 /**
633 * @tc.name: FunctionTest004
634 * @tc.desc: Test the second parameter as null
635 * @tc.type: FUNC
636 */
637 HWTEST_F(NapiBasicTest, FunctionTest004, testing::ext::TestSize.Level1)
638 {
639 napi_env env = reinterpret_cast<napi_env>(engine_);
640 napi_value funcValue = nullptr;
641 napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, TestCreateFunc, nullptr, &funcValue);
642 ASSERT_NE(funcValue, nullptr);
643
644 napi_value recv = nullptr;
645 napi_get_undefined(env, &recv);
646 ASSERT_NE(recv, nullptr);
647 napi_value funcResultValue = nullptr;
648 napi_call_function(env, recv, funcValue, 0, nullptr, &funcResultValue);
649 ASSERT_NE(funcResultValue, nullptr);
650 }
651
TestCallFunc(napi_env env,napi_callback_info info)652 static napi_value TestCallFunc(napi_env env, napi_callback_info info)
653 {
654 napi_value error = nullptr;
655 napi_throw_error(env, "500", "Common error");
656 return error;
657 }
658
659 /**
660 * @tc.name: FunctionTest005
661 * @tc.desc: Test callfunction throw error
662 * @tc.type: FUNC
663 */
664 HWTEST_F(NapiBasicTest, FunctionTest005, testing::ext::TestSize.Level1)
665 {
666 napi_env env = reinterpret_cast<napi_env>(engine_);
667 napi_value funcValue = nullptr;
668 napi_value exception = nullptr;
669 napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, TestCallFunc, nullptr, &funcValue);
670 ASSERT_NE(funcValue, nullptr);
671
672 napi_value recv = nullptr;
673 napi_get_undefined(env, &recv);
674 ASSERT_NE(recv, nullptr);
675 napi_value funcResultValue = nullptr;
676 bool isExceptionPending = false;
677 napi_call_function(env, recv, funcValue, 0, nullptr, &funcResultValue);
678 napi_is_exception_pending(env, &isExceptionPending);
679 ASSERT_TRUE(isExceptionPending);
680
681 napi_get_and_clear_last_exception(env, &exception);
682 }
683
684 /**
685 * @tc.name: ArrayTest001
686 * @tc.desc: Test array type.
687 * @tc.type: FUNC
688 */
689 HWTEST_F(NapiBasicTest, ArrayTest001, testing::ext::TestSize.Level1) {
690 napi_env env = (napi_env) engine_;
691
692 napi_value array = nullptr;
693 napi_create_array(env, &array);
694 ASSERT_NE(array, nullptr);
695 bool isArray = false;
696 napi_is_array(env, array, &isArray);
697 ASSERT_TRUE(isArray);
698
699 for (size_t i = 0; i < 10; i++) {
700 napi_value num = nullptr;
701 napi_create_uint32(env, i, &num);
702 napi_set_element(env, array, i, num);
703 }
704
705 uint32_t arrayLength = 0;
706 napi_get_array_length(env, array, &arrayLength);
707
708 ASSERT_EQ(arrayLength, static_cast<uint32_t>(10));
709
710 for (size_t i = 0; i < arrayLength; i++) {
711 bool hasIndex = false;
712 napi_has_element(env, array, i, &hasIndex);
713 ASSERT_TRUE(hasIndex);
714 }
715
716 for (size_t i = 0; i < arrayLength; i++) {
717 bool isDelete = false;
718 napi_delete_element(env, array, i, &isDelete);
719 ASSERT_TRUE(isDelete);
720 }
721 }
722
723 /**
724 * @tc.name: ArrayBufferTest001
725 * @tc.desc: Test array buffer type.
726 * @tc.type: FUNC
727 */
728 HWTEST_F(NapiBasicTest, ArrayBufferTest001, testing::ext::TestSize.Level1)
729 {
730 napi_env env = (napi_env)engine_;
731
732 napi_value arrayBuffer = nullptr;
733 void* arrayBufferPtr = nullptr;
734 size_t arrayBufferSize = 1024;
735 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
736
737 void* tmpArrayBufferPtr = nullptr;
738 size_t arrayBufferLength = 0;
739 napi_get_arraybuffer_info(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength);
740
741 ASSERT_EQ(arrayBufferPtr, tmpArrayBufferPtr);
742 ASSERT_EQ(arrayBufferSize, arrayBufferLength);
743 }
744
745 /**
746 * @tc.name: TypedArrayTest001
747 * @tc.desc: Test typed array type.
748 * @tc.type: FUNC
749 */
750 HWTEST_F(NapiBasicTest, TypedArrayTest001, testing::ext::TestSize.Level1)
751 {
752 napi_env env = (napi_env)engine_;
753
754 {
755 napi_value arrayBuffer = nullptr;
756 void* arrayBufferPtr = nullptr;
757 size_t arrayBufferSize = 1024;
758 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
759
760 void* tmpArrayBufferPtr = nullptr;
761 size_t arrayBufferLength = 0;
762 napi_get_arraybuffer_info(env, arrayBuffer, &tmpArrayBufferPtr, &arrayBufferLength);
763
764 ASSERT_EQ(arrayBufferPtr, tmpArrayBufferPtr);
765 ASSERT_EQ(arrayBufferSize, arrayBufferLength);
766
767 napi_value typedarray = nullptr;
768 napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &typedarray);
769 ASSERT_NE(typedarray, nullptr);
770 bool isTypedArray = false;
771 napi_is_typedarray(env, typedarray, &isTypedArray);
772 ASSERT_TRUE(isTypedArray);
773
774 napi_typedarray_type typedarrayType;
775 size_t typedarrayLength = 0;
776 void* typedarrayBufferPtr = nullptr;
777 napi_value tmpArrayBuffer = nullptr;
778 size_t byteOffset = 0;
779
780 napi_get_typedarray_info(env, typedarray, &typedarrayType, &typedarrayLength, &typedarrayBufferPtr,
781 &tmpArrayBuffer, &byteOffset);
782
783 ASSERT_EQ(typedarrayBufferPtr, arrayBufferPtr);
784 ASSERT_EQ(arrayBufferSize, typedarrayLength);
785 }
786 }
787
788 /**
789 * @tc.name: DataViewTest001
790 * @tc.desc: Test data view type.
791 * @tc.type: FUNC
792 */
793 HWTEST_F(NapiBasicTest, DataViewTest001, testing::ext::TestSize.Level1)
794 {
795 napi_env env = (napi_env)engine_;
796
797 napi_value arrayBuffer = nullptr;
798 void* arrayBufferPtr = nullptr;
799 size_t arrayBufferSize = 1024;
800 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
801 ASSERT_NE(arrayBuffer, nullptr);
802 ASSERT_NE(arrayBufferPtr, nullptr);
803 bool isArrayBuffer = false;
804 napi_is_arraybuffer(env, arrayBuffer, &isArrayBuffer);
805 ASSERT_TRUE(isArrayBuffer);
806
807 napi_value result = nullptr;
808 napi_create_dataview(env, arrayBufferSize, arrayBuffer, 0, &result);
809
810 bool isDataView = false;
811 napi_is_dataview(env, result, &isDataView);
812
813 napi_value retArrayBuffer = nullptr;
814 void* data = nullptr;
815 size_t byteLength = 0;
816 size_t byteOffset = 0;
817 napi_get_dataview_info(env, result, &byteLength, &data, &retArrayBuffer, &byteOffset);
818
819 bool retIsArrayBuffer = false;
820 napi_is_arraybuffer(env, arrayBuffer, &retIsArrayBuffer);
821 ASSERT_TRUE(retIsArrayBuffer);
822 ASSERT_EQ(arrayBufferPtr, data);
823 ASSERT_EQ(arrayBufferSize, byteLength);
824 ASSERT_EQ((size_t)0, byteOffset);
825 }
826
827 /**
828 * @tc.name: PromiseTest001
829 * @tc.desc: Test promise type.
830 * @tc.type: FUNC
831 */
832 HWTEST_F(NapiBasicTest, PromiseTest001, testing::ext::TestSize.Level1)
833 {
834 napi_env env = (napi_env)engine_;
835 {
836 napi_deferred deferred = nullptr;
837 napi_value promise = nullptr;
838 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
839 ASSERT_NE(deferred, nullptr);
840 ASSERT_NE(promise, nullptr);
841
842 bool isPromise = false;
843 ASSERT_CHECK_CALL(napi_is_promise(env, promise, &isPromise));
844 ASSERT_TRUE(isPromise);
845
846 napi_value undefined = nullptr;
847 napi_get_undefined(env, &undefined);
848 ASSERT_CHECK_CALL(napi_resolve_deferred(env, deferred, undefined));
849 }
850 {
851 napi_deferred deferred = nullptr;
852 napi_value promise = nullptr;
853 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
854 ASSERT_NE(deferred, nullptr);
855 ASSERT_NE(promise, nullptr);
856
857 bool isPromise = false;
858 ASSERT_CHECK_CALL(napi_is_promise(env, promise, &isPromise));
859 ASSERT_TRUE(isPromise);
860
861 napi_value undefined = nullptr;
862 napi_get_undefined(env, &undefined);
863 ASSERT_CHECK_CALL(napi_reject_deferred(env, deferred, undefined));
864 }
865 }
866
867 /**
868 * @tc.name: PromiseTest002
869 * @tc.desc: Test promise type.
870 * @tc.type: FUNC
871 */
872 HWTEST_F(NapiBasicTest, PromiseTest002, testing::ext::TestSize.Level1)
873 {
874 napi_env env = reinterpret_cast<napi_env>(engine_);
875 {
876 napi_deferred deferred = nullptr;
877 napi_value promise = nullptr;
878 napi_status status = napi_create_promise(nullptr, &deferred, &promise);
879 ASSERT_EQ(status, napi_status::napi_invalid_arg);
880 status = napi_create_promise(env, nullptr, &promise);
881 ASSERT_EQ(status, napi_status::napi_invalid_arg);
882 status = napi_create_promise(env, &deferred, nullptr);
883 ASSERT_EQ(status, napi_status::napi_invalid_arg);
884 }
885 {
886 napi_deferred deferred = nullptr;
887 napi_value promise = nullptr;
888 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
889
890 bool isPromise = false;
891 napi_status status = napi_is_promise(nullptr, promise, &isPromise);
892 ASSERT_EQ(status, napi_status::napi_invalid_arg);
893 status = napi_is_promise(env, nullptr, &isPromise);
894 ASSERT_EQ(status, napi_status::napi_invalid_arg);
895 status = napi_is_promise(env, promise, nullptr);
896 ASSERT_EQ(status, napi_status::napi_invalid_arg);
897 }
898 {
899 napi_deferred deferred = nullptr;
900 napi_value promise = nullptr;
901 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
902
903 napi_value undefined = nullptr;
904 napi_get_undefined(env, &undefined);
905 napi_status status = napi_resolve_deferred(nullptr, deferred, undefined);
906 ASSERT_EQ(status, napi_status::napi_invalid_arg);
907 status = napi_resolve_deferred(env, nullptr, undefined);
908 ASSERT_EQ(status, napi_status::napi_invalid_arg);
909 status = napi_resolve_deferred(env, deferred, nullptr);
910 ASSERT_EQ(status, napi_status::napi_invalid_arg);
911 }
912 {
913 napi_deferred deferred = nullptr;
914 napi_value promise = nullptr;
915 ASSERT_CHECK_CALL(napi_create_promise(env, &deferred, &promise));
916
917 napi_value undefined = nullptr;
918 napi_get_undefined(env, &undefined);
919 napi_status status = napi_reject_deferred(nullptr, deferred, undefined);
920 ASSERT_EQ(status, napi_status::napi_invalid_arg);
921 status = napi_reject_deferred(env, nullptr, undefined);
922 ASSERT_EQ(status, napi_status::napi_invalid_arg);
923 status = napi_reject_deferred(env, deferred, nullptr);
924 ASSERT_EQ(status, napi_status::napi_invalid_arg);
925 }
926 }
927
928 /**
929 * @tc.name: ErrorTest001
930 * @tc.desc: Test error type.
931 * @tc.type: FUNC
932 */
933 HWTEST_F(NapiBasicTest, ErrorTest001, testing::ext::TestSize.Level1)
934 {
935 napi_env env = (napi_env)engine_;
936 bool isExceptionPending = false;
937 napi_value exception = nullptr;
938
939 {
940 napi_value code = nullptr;
941 napi_value message = nullptr;
942
943 napi_create_string_latin1(env, "500", NAPI_AUTO_LENGTH, &code);
944 napi_create_string_latin1(env, "common error", NAPI_AUTO_LENGTH, &message);
945
946 napi_value error = nullptr;
947 napi_create_error(env, code, message, &error);
948 ASSERT_TRUE(error != nullptr);
949 bool isError = false;
950 napi_is_error(env, error, &isError);
951 ASSERT_TRUE(isError);
952 napi_throw(env, error);
953 napi_is_exception_pending(env, &isExceptionPending);
954 ASSERT_TRUE(isExceptionPending);
955 napi_get_and_clear_last_exception(env, &exception);
956 napi_is_exception_pending(env, &isExceptionPending);
957 ASSERT_FALSE(isExceptionPending);
958 }
959
960 {
961 napi_value code = nullptr;
962 napi_value message = nullptr;
963 napi_create_string_latin1(env, "500", NAPI_AUTO_LENGTH, &code);
964 napi_create_string_latin1(env, "range error", NAPI_AUTO_LENGTH, &message);
965 napi_value error = nullptr;
966 napi_create_range_error(env, code, message, &error);
967 ASSERT_TRUE(error != nullptr);
968 bool isError = false;
969 napi_is_error(env, error, &isError);
970 ASSERT_TRUE(isError);
971
972 napi_throw_range_error(env, "500", "Range error");
973 napi_is_exception_pending(env, &isExceptionPending);
974 ASSERT_TRUE(isExceptionPending);
975 napi_get_and_clear_last_exception(env, &exception);
976 napi_is_exception_pending(env, &isExceptionPending);
977 ASSERT_FALSE(isExceptionPending);
978 }
979
980 {
981 napi_value code = nullptr;
982 napi_value message = nullptr;
983 napi_create_string_latin1(env, "500", NAPI_AUTO_LENGTH, &code);
984 napi_create_string_latin1(env, "type error", NAPI_AUTO_LENGTH, &message);
985 napi_value error = nullptr;
986 napi_create_type_error(env, code, message, &error);
987 ASSERT_TRUE(error != nullptr);
988 bool isError = false;
989 napi_is_error(env, error, &isError);
990 ASSERT_TRUE(isError);
991
992 napi_throw_type_error(env, "500", "Type error");
993 napi_is_exception_pending(env, &isExceptionPending);
994 ASSERT_TRUE(isExceptionPending);
995 napi_get_and_clear_last_exception(env, &exception);
996 napi_is_exception_pending(env, &isExceptionPending);
997 ASSERT_FALSE(isExceptionPending);
998 }
999
1000 napi_throw_error(env, "500", "Common error");
1001 napi_is_exception_pending(env, &isExceptionPending);
1002 ASSERT_TRUE(isExceptionPending);
1003 napi_get_and_clear_last_exception(env, &exception);
1004 napi_is_exception_pending(env, &isExceptionPending);
1005 ASSERT_FALSE(isExceptionPending);
1006 }
1007
1008 /**
1009 * @tc.name: ReferenceTest001
1010 * @tc.desc: Test reference type.
1011 * @tc.type: FUNC
1012 */
1013 HWTEST_F(NapiBasicTest, ReferenceTest001, testing::ext::TestSize.Level1)
1014 {
1015 napi_env env = (napi_env)engine_;
1016
1017 napi_value result = nullptr;
1018 napi_ref resultRef = nullptr;
1019
1020 napi_create_object(env, &result);
1021 napi_create_reference(env, result, 1, &resultRef);
1022
1023 uint32_t resultRefCount = 0;
1024
1025 napi_reference_ref(env, resultRef, &resultRefCount);
1026 ASSERT_EQ(resultRefCount, static_cast<uint32_t>(2));
1027
1028 napi_reference_unref(env, resultRef, &resultRefCount);
1029 ASSERT_EQ(resultRefCount, static_cast<uint32_t>(1));
1030
1031 napi_value refValue = nullptr;
1032 napi_get_reference_value(env, resultRef, &refValue);
1033
1034 ASSERT_NE(refValue, nullptr);
1035
1036 napi_delete_reference(env, resultRef);
1037 }
1038
1039 /**
1040 * @tc.name: CustomClassTest001
1041 * @tc.desc: Test define class.
1042 * @tc.type: FUNC
1043 */
1044 HWTEST_F(NapiBasicTest, CustomClassTest001, testing::ext::TestSize.Level1)
1045 {
1046 napi_env env = (napi_env)engine_;
1047
__anonb945b3c60802(napi_env env, napi_callback_info info) 1048 auto constructor = [](napi_env env, napi_callback_info info) -> napi_value {
1049 napi_value thisVar = nullptr;
1050 napi_value* argv = nullptr;
1051 size_t argc = 0;
1052 void* data = nullptr;
1053 napi_value constructor = nullptr;
1054 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
1055 if (argc > 0) {
1056 argv = new napi_value[argc];
1057 }
1058 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1059 napi_get_new_target(env, info, &constructor);
1060 if (constructor == nullptr) {
1061 napi_throw_error(env, nullptr, "is not new instance");
1062 }
1063 if (argv != nullptr) {
1064 delete []argv;
1065 }
1066 return thisVar;
1067 };
1068
1069 napi_value ln2 = nullptr;
1070 napi_value e = nullptr;
1071
1072 napi_create_double(env, 2.718281828459045, &e);
1073 napi_create_double(env, 0.6931471805599453, &ln2);
1074
1075 napi_property_descriptor desc[] = {
__anonb945b3c60902() 1076 DECLARE_NAPI_FUNCTION("add", [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
__anonb945b3c60a02() 1077 DECLARE_NAPI_FUNCTION("sub", [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
__anonb945b3c60b02() 1078 DECLARE_NAPI_FUNCTION("mul", [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
__anonb945b3c60c02() 1079 DECLARE_NAPI_FUNCTION("div", [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
1080 DECLARE_NAPI_STATIC_FUNCTION("getTime",
__anonb945b3c60d02() 1081 [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
1082 DECLARE_NAPI_GETTER_SETTER(
__anonb945b3c60e02() 1083 "pi", [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; },
__anonb945b3c60f02() 1084 [](napi_env env, napi_callback_info info) -> napi_value { return nullptr; }),
1085
1086 };
1087
1088 napi_value customClass = nullptr;
1089
1090 ASSERT_CHECK_CALL(napi_define_class(env, "CustomClass", NAPI_AUTO_LENGTH, constructor, nullptr,
1091 sizeof(desc) / sizeof(desc[0]), desc, &customClass));
1092 ASSERT_CHECK_VALUE_TYPE(env, customClass, napi_function);
1093 napi_value customClassPrototype = nullptr;
1094 napi_get_prototype(env, customClass, &customClassPrototype);
1095 ASSERT_CHECK_VALUE_TYPE(env, customClassPrototype, napi_function);
1096
1097 napi_value customInstance = nullptr;
1098 ASSERT_CHECK_CALL(napi_new_instance(env, customClass, 0, nullptr, &customInstance));
1099
1100 bool isInstanceOf = false;
1101 ASSERT_CHECK_CALL(napi_instanceof(env, customInstance, customClass, &isInstanceOf));
1102 ASSERT_TRUE(isInstanceOf);
1103 }
1104
1105 /**
1106 * @tc.name: AsyncWorkTest001
1107 * @tc.desc: Test async work.
1108 * @tc.type: FUNC
1109 */
1110 HWTEST_F(NapiBasicTest, AsyncWorkTest001, testing::ext::TestSize.Level1)
1111 {
1112 struct AsyncWorkContext {
1113 napi_async_work work = nullptr;
1114 };
1115 napi_env env = (napi_env)engine_;
1116 {
1117 auto asyncWorkContext = new AsyncWorkContext();
1118 napi_value resourceName = nullptr;
1119 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1120 ASSERT_CHECK_CALL(napi_create_async_work(
__anonb945b3c61002(napi_env value, void* data) 1121 env, nullptr, resourceName, [](napi_env value, void* data) {},
__anonb945b3c61102(napi_env env, napi_status status, void* data) 1122 [](napi_env env, napi_status status, void* data) {
1123 AsyncWorkContext* asyncWorkContext = (AsyncWorkContext*)data;
1124 ASSERT_CHECK_CALL(napi_delete_async_work(env, asyncWorkContext->work));
1125 delete asyncWorkContext;
1126 },
1127 asyncWorkContext, &asyncWorkContext->work));
1128 ASSERT_CHECK_CALL(napi_queue_async_work(env, asyncWorkContext->work));
1129 }
1130 {
1131 auto asyncWorkContext = new AsyncWorkContext();
1132 napi_value resourceName = nullptr;
1133 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1134 napi_create_async_work(
__anonb945b3c61202(napi_env value, void* data) 1135 env, nullptr, resourceName, [](napi_env value, void* data) {},
__anonb945b3c61302(napi_env env, napi_status status, void* data) 1136 [](napi_env env, napi_status status, void* data) {
1137 AsyncWorkContext* asyncWorkContext = (AsyncWorkContext*)data;
1138 napi_delete_async_work(env, asyncWorkContext->work);
1139 delete asyncWorkContext;
1140 },
1141 asyncWorkContext, &asyncWorkContext->work);
1142 napi_queue_async_work(env, asyncWorkContext->work);
1143 ASSERT_CHECK_CALL(napi_cancel_async_work(env, asyncWorkContext->work));
1144 }
1145 }
1146
1147 /**
1148 * @tc.name: AsyncWorkTest002
1149 * @tc.desc: Test async work.
1150 * @tc.type: FUNC
1151 */
1152 HWTEST_F(NapiBasicTest, AsyncWorkTest002, testing::ext::TestSize.Level1)
1153 {
1154 struct AsyncWorkContext {
1155 napi_async_work work = nullptr;
1156 };
1157 napi_env env = reinterpret_cast<napi_env>(engine_);
1158 auto asyncWorkContext = new AsyncWorkContext();
1159 napi_value resourceName = nullptr;
1160 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1161 napi_create_async_work(
__anonb945b3c61402(napi_env value, void* data) 1162 env, nullptr, resourceName, [](napi_env value, void* data) {
1163 // Simulate long-term running tasks.
1164 usleep(THREAD_PAUSE_THREE);
1165 },
__anonb945b3c61502(napi_env env, napi_status status, void* data) 1166 [](napi_env env, napi_status status, void* data) {
1167 AsyncWorkContext* asyncWorkContext = (AsyncWorkContext*)data;
1168 ASSERT_EQ(status, napi_status::napi_cancelled);
1169 napi_delete_async_work(env, asyncWorkContext->work);
1170 delete asyncWorkContext;
1171 },
1172 asyncWorkContext, &asyncWorkContext->work);
1173 napi_queue_async_work(env, asyncWorkContext->work);
1174
1175 // Sleep for a short duration to allow the async work to start executing.
1176 usleep(THREAD_PAUSE_ONE);
1177 napi_cancel_async_work(env, asyncWorkContext->work);
1178 }
1179
1180 /**
1181 * @tc.name: AsyncWorkTest003
1182 * @tc.desc: Test async work.
1183 * @tc.type: FUNC
1184 */
1185 HWTEST_F(NapiBasicTest, AsyncWorkTest003, testing::ext::TestSize.Level1)
1186 {
1187 struct AsyncWorkContext {
1188 napi_async_work work = nullptr;
1189 };
1190 napi_env env = reinterpret_cast<napi_env>(engine_);
1191 std::unique_ptr<AsyncWorkContext> asyncWorkContext = std::make_unique<AsyncWorkContext>();
1192 napi_value resourceName = nullptr;
1193 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1194 napi_status status = napi_create_async_work(
__anonb945b3c61602(napi_env env, void* data) 1195 env, nullptr, nullptr, [](napi_env env, void* data) {},
__anonb945b3c61702(napi_env env, napi_status status, void* data) 1196 [](napi_env env, napi_status status, void* data) {},
1197 asyncWorkContext.get(), &asyncWorkContext->work);
1198 ASSERT_EQ(status, napi_invalid_arg);
1199
1200 status = napi_create_async_work(
1201 env, nullptr, resourceName, nullptr,
__anonb945b3c61802(napi_env env, napi_status status, void* data) 1202 [](napi_env env, napi_status status, void* data) {},
1203 asyncWorkContext.get(), &asyncWorkContext->work);
1204 ASSERT_EQ(status, napi_invalid_arg);
1205
1206 status = napi_create_async_work(
__anonb945b3c61902(napi_env env, void* data) 1207 env, nullptr, resourceName, [](napi_env env, void* data) {},
1208 nullptr,
1209 asyncWorkContext.get(), &asyncWorkContext->work);
1210 ASSERT_EQ(status, napi_invalid_arg);
1211
1212 status = napi_create_async_work(
__anonb945b3c61a02(napi_env env, void* data) 1213 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anonb945b3c61b02(napi_env env, napi_status status, void* data) 1214 [](napi_env env, napi_status status, void* data) {},
1215 nullptr, &asyncWorkContext->work);
1216 ASSERT_EQ(status, napi_ok);
1217
1218 status = napi_create_async_work(
__anonb945b3c61c02(napi_env env, void* data) 1219 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anonb945b3c61d02(napi_env env, napi_status status, void* data) 1220 [](napi_env env, napi_status status, void* data) {},
1221 asyncWorkContext.get(), nullptr);
1222 ASSERT_EQ(status, napi_invalid_arg);
1223 }
1224
1225 /**
1226 * @tc.name: AsyncWorkTest004
1227 * @tc.desc: Test async work.
1228 * @tc.type: FUNC
1229 */
1230 HWTEST_F(NapiBasicTest, AsyncWorkTest004, testing::ext::TestSize.Level1)
1231 {
1232 struct AsyncWorkContext {
1233 napi_async_work work = nullptr;
1234 };
1235 napi_env env = reinterpret_cast<napi_env>(engine_);
1236 auto asyncWorkContext = new AsyncWorkContext();
1237 napi_value resourceName = nullptr;
1238 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1239 napi_create_async_work(
__anonb945b3c61e02(napi_env env, void* data) 1240 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anonb945b3c61f02(napi_env env, napi_status status, void* data) 1241 [](napi_env env, napi_status status, void* data) {
1242 AsyncWorkContext* asyncWorkContext = reinterpret_cast<AsyncWorkContext*>(data);
1243 ASSERT_NE(asyncWorkContext, nullptr);
1244 delete asyncWorkContext;
1245 },
1246 nullptr, &asyncWorkContext->work);
1247 napi_delete_async_work(env, asyncWorkContext->work);
1248 }
1249
1250 /**
1251 * @tc.name: AsyncWorkTest005
1252 * @tc.desc: Test async work.
1253 * @tc.type: FUNC
1254 */
1255 HWTEST_F(NapiBasicTest, AsyncWorkTest005, testing::ext::TestSize.Level1)
1256 {
1257 struct AsyncWorkContext {
1258 napi_async_work work = nullptr;
1259 };
1260 napi_env env = reinterpret_cast<napi_env>(engine_);
1261 auto asyncWorkContext = new AsyncWorkContext();
1262 napi_value resourceName = nullptr;
1263 napi_create_string_utf8(env, "AsyncWorkTest", NAPI_AUTO_LENGTH, &resourceName);
1264 napi_create_async_work(
__anonb945b3c62002(napi_env env, void* data) 1265 env, nullptr, resourceName, [](napi_env env, void* data) {},
__anonb945b3c62102(napi_env env, napi_status status, void* data) 1266 [](napi_env env, napi_status status, void* data) {
1267 AsyncWorkContext* asyncWorkContext = reinterpret_cast<AsyncWorkContext*>(data);
1268 ASSERT_NE(asyncWorkContext, nullptr);
1269 delete asyncWorkContext;
1270 },
1271 asyncWorkContext, &asyncWorkContext->work);
1272 napi_status status = napi_queue_async_work(env, asyncWorkContext->work);
1273 ASSERT_EQ(status, napi_ok);
1274 status = napi_queue_async_work(env, nullptr);
1275 ASSERT_EQ(status, napi_invalid_arg);
1276 }
1277
1278 /**
1279 * @tc.name: ObjectWrapperTest001
1280 * @tc.desc: Test object wrapper.
1281 * @tc.type: FUNC
1282 */
1283 HWTEST_F(NapiBasicTest, ObjectWrapperTest001, testing::ext::TestSize.Level1)
1284 {
1285 napi_env env = (napi_env)engine_;
1286
1287 napi_value testClass = nullptr;
1288 napi_define_class(
1289 env, "TestClass", NAPI_AUTO_LENGTH,
__anonb945b3c62202(napi_env env, napi_callback_info info) 1290 [](napi_env env, napi_callback_info info) -> napi_value {
1291 napi_value thisVar = nullptr;
1292 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1293
1294 return thisVar;
1295 },
1296 nullptr, 0, nullptr, &testClass);
1297
1298 napi_value instanceValue = nullptr;
1299 napi_new_instance(env, testClass, 0, nullptr, &instanceValue);
1300
1301 const char* testStr = "test";
1302 napi_wrap(
__anonb945b3c62302(napi_env env, void* data, void* hint) 1303 env, instanceValue, (void*)testStr, [](napi_env env, void* data, void* hint) {}, nullptr, nullptr);
1304
1305 char* tmpTestStr = nullptr;
1306 napi_unwrap(env, instanceValue, (void**)&tmpTestStr);
1307 ASSERT_STREQ(testStr, tmpTestStr);
1308
1309 char* tmpTestStr1 = nullptr;
1310 napi_remove_wrap(env, instanceValue, (void**)&tmpTestStr1);
1311 ASSERT_STREQ(testStr, tmpTestStr1);
1312 }
1313
1314 /**
1315 * @tc.name: StrictEqualsTest001
1316 * @tc.desc: Test date type.
1317 * @tc.type: FUNC
1318 */
1319 HWTEST_F(NapiBasicTest, StrictEqualsTest001, testing::ext::TestSize.Level1)
1320 {
1321 napi_env env = (napi_env)engine_;
1322
1323 const char* testStringStr = "test";
1324 napi_value testString = nullptr;
1325 napi_create_string_utf8(env, testStringStr, strlen(testStringStr), &testString);
1326 bool isStrictEquals = false;
1327 napi_strict_equals(env, testString, testString, &isStrictEquals);
1328 ASSERT_TRUE(isStrictEquals);
1329
1330 napi_value testObject = nullptr;
1331 napi_create_object(env, &testObject);
1332 isStrictEquals = false;
1333 napi_strict_equals(env, testObject, testObject, &isStrictEquals);
1334 ASSERT_TRUE(isStrictEquals);
1335 }
1336
1337 /**
1338 * @tc.name: CreateRuntimeTest001
1339 * @tc.desc: Test create runtime.
1340 * @tc.type: FUNC
1341 */
1342 HWTEST_F(NapiBasicTest, CreateRuntimeTest001, testing::ext::TestSize.Level1)
1343 {
1344 napi_env env = (napi_env)engine_;
1345
1346 napi_env newEnv = nullptr;
1347 napi_create_runtime(env, &newEnv);
1348 }
1349
1350 /**
1351 * @tc.name: SerializeDeSerializeTest001
1352 * @tc.desc: Test serialize & deserialize.
1353 * @tc.type: FUNC
1354 */
1355 HWTEST_F(NapiBasicTest, SerializeDeSerializeTest001, testing::ext::TestSize.Level1)
1356 {
1357 napi_env env = (napi_env)engine_;
1358
1359 napi_value undefined = nullptr;
1360 napi_get_undefined(env, &undefined);
1361
1362 napi_value num = nullptr;
1363 uint32_t value = 1000;
1364 napi_create_uint32(env, value, &num);
1365 napi_value data = nullptr;
1366 napi_serialize(env, num, undefined, &data);
1367 ASSERT_NE(data, nullptr);
1368
1369 napi_value result = nullptr;
1370 napi_deserialize(env, data, &result);
1371 ASSERT_CHECK_VALUE_TYPE(env, result, napi_number);
1372 int32_t resultData = 0;
1373 napi_get_value_int32(env, result, &resultData);
1374 ASSERT_EQ(resultData, 1000);
1375 }
1376
1377 /**
1378 * @tc.name: IsCallableTest001
1379 * @tc.desc: Test is callable.
1380 * @tc.type: FUNC
1381 */
1382 HWTEST_F(NapiBasicTest, IsCallableTest001, testing::ext::TestSize.Level1)
1383 {
1384 napi_env env = (napi_env)engine_;
1385
__anonb945b3c62402(napi_env env, napi_callback_info info) 1386 auto func = [](napi_env env, napi_callback_info info) -> napi_value {
1387 napi_value thisVar;
1388 napi_value* argv = nullptr;
1389 size_t argc = 0;
1390 void* data = nullptr;
1391
1392 napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
1393 if (argc > 0) {
1394 argv = new napi_value[argc];
1395 }
1396 napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
1397
1398 napi_value result = nullptr;
1399 napi_create_object(env, &result);
1400
1401 napi_value messageKey = nullptr;
1402 const char* messageKeyStr = "message";
1403 napi_create_string_latin1(env, messageKeyStr, strlen(messageKeyStr), &messageKey);
1404 napi_value messageValue = nullptr;
1405 const char* messageValueStr = "OK";
1406 napi_create_string_latin1(env, messageValueStr, strlen(messageValueStr), &messageValue);
1407 napi_set_property(env, result, messageKey, messageValue);
1408
1409 if (argv != nullptr) {
1410 delete []argv;
1411 }
1412 return result;
1413 };
1414
1415 napi_value funcValue = nullptr;
1416 napi_create_function(env, "testFunc", NAPI_AUTO_LENGTH, func, nullptr, &funcValue);
1417 ASSERT_NE(funcValue, nullptr);
1418
1419 bool result = false;
1420 napi_is_callable(env, funcValue, &result);
1421 ASSERT_TRUE(result);
1422 }
1423
1424 /**
1425 * @tc.name: EncodeToUtf8Test001
1426 * @tc.desc: Test EncodeToUtf8 Func.
1427 * @tc.type: FUNC
1428 */
1429 HWTEST_F(NapiBasicTest, EncodeToUtf8Test001, testing::ext::TestSize.Level1)
1430 {
1431 std::string str = "encode";
1432 auto testStr = engine_->CreateString(str.c_str(), str.length());
1433 char* buffer = new char[str.length()];
1434 size_t bufferSize = str.length();
1435 int32_t written = 0;
1436 int32_t nchars = 0;
1437 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1438 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1439 ASSERT_EQ(written, 6);
1440 ASSERT_EQ(nchars, 6);
1441 delete[] buffer;
1442
1443 str = "encode\xc2\xab\xe2\x98\x80";
1444 testStr = engine_->CreateString(str.c_str(), str.length());
1445 buffer = new char[str.length()];
1446 bufferSize = str.length();
1447 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1448 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1449 ASSERT_EQ(written, 11);
1450 ASSERT_EQ(nchars, 8);
1451 delete[] buffer;
1452
1453 buffer = new char[str.length()];
1454 bufferSize = str.length();
1455 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1456 bufferSize--;
1457 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1458 ASSERT_EQ(written, 8);
1459 ASSERT_EQ(nchars, 7);
1460 delete[] buffer;
1461
1462 buffer = new char[str.length()];
1463 bufferSize = str.length();
1464 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1465 bufferSize -= 4;
1466 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1467 ASSERT_EQ(written, 6);
1468 ASSERT_EQ(nchars, 6);
1469 delete[] buffer;
1470
1471 str = "encode\xc2\xab\xe2\x98\x80t";
1472 testStr = engine_->CreateString(str.c_str(), str.length());
1473 buffer = new char[str.length()];
1474 bufferSize = str.length();
1475 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1476 bufferSize--;
1477 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1478 ASSERT_EQ(written, 11);
1479 ASSERT_EQ(nchars, 8);
1480 delete[] buffer;
1481
1482 str = "";
1483 testStr = engine_->CreateString(str.c_str(), str.length());
1484 buffer = new char[str.length() + 1];
1485 bufferSize = str.length() + 1;
1486 ASSERT_EQ(memset_s(buffer, str.length(), 0, str.length()), EOK);
1487 engine_->EncodeToUtf8(testStr, buffer, &written, bufferSize, &nchars);
1488 ASSERT_EQ(written, 0);
1489 ASSERT_EQ(nchars, 0);
1490 delete[] buffer;
1491 }
1492
1493 /**
1494 * @tc.name: WrapWithSizeTest001
1495 * @tc.desc: Test wrap with size.
1496 * @tc.type: FUNC
1497 */
1498 HWTEST_F(NapiBasicTest, WrapWithSizeTest001, testing::ext::TestSize.Level1)
1499 {
1500 napi_env env = (napi_env)engine_;
1501
1502 napi_value testWrapClass = nullptr;
1503 napi_define_class(
1504 env, "TestWrapClass", NAPI_AUTO_LENGTH,
__anonb945b3c62502(napi_env env, napi_callback_info info) 1505 [](napi_env env, napi_callback_info info) -> napi_value {
1506 napi_value thisVar = nullptr;
1507 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
1508
1509 return thisVar;
1510 },
1511 nullptr, 0, nullptr, &testWrapClass);
1512
1513 napi_value instanceValue = nullptr;
1514 napi_new_instance(env, testWrapClass, 0, nullptr, &instanceValue);
1515
1516 const char* testWrapStr = "testWrapStr";
1517 size_t size = sizeof(*testWrapStr) / sizeof(char);
1518 napi_wrap_with_size(
__anonb945b3c62602(napi_env env, void* data, void* hint) 1519 env, instanceValue, (void*)testWrapStr, [](napi_env env, void* data, void* hint) {}, nullptr, nullptr, size);
1520
1521 char* tempTestStr = nullptr;
1522 napi_unwrap(env, instanceValue, (void**)&tempTestStr);
1523 ASSERT_STREQ(testWrapStr, tempTestStr);
1524
1525 char* tempTestStr1 = nullptr;
1526 napi_remove_wrap(env, instanceValue, (void**)&tempTestStr1);
1527 ASSERT_STREQ(testWrapStr, tempTestStr1);
1528
1529 }
1530
1531 /**
1532 * @tc.name: CreateExternalWithSizeTest001
1533 * @tc.desc: Test create external with size.
1534 * @tc.type: FUNC
1535 */
1536 HWTEST_F(NapiBasicTest, CreateExternalWithSizeTest001, testing::ext::TestSize.Level1)
1537 {
1538 napi_env env = (napi_env)engine_;
1539 const char testStr[] = "test";
1540 size_t size = sizeof(testStr) / sizeof(char);
1541 napi_value external = nullptr;
1542 napi_create_external_with_size(
1543 env, (void*)testStr,
__anonb945b3c62702(napi_env env, void* data, void* hint) 1544 [](napi_env env, void* data, void* hint) { ASSERT_STREQ((const char*)data, (const char*)hint); },
1545 (void*)testStr, &external, size);
1546
1547 ASSERT_CHECK_VALUE_TYPE(env, external, napi_external);
1548 void* tempExternal = nullptr;
1549 napi_get_value_external(env, external, &tempExternal);
1550 ASSERT_TRUE(tempExternal);
1551 ASSERT_EQ(tempExternal, testStr);
1552 }
1553
1554 /**
1555 * @tc.name: BigArrayTest001
1556 * @tc.desc: Test is big int64 array and big uint64 array.
1557 * @tc.type: FUNC
1558 */
1559 HWTEST_F(NapiBasicTest, BigArrayTest001, testing::ext::TestSize.Level1) {
1560 napi_env env = (napi_env) engine_;
1561
1562 napi_value array = nullptr;
1563 napi_create_array(env, &array);
1564 ASSERT_NE(array, nullptr);
1565 bool isArray = false;
1566 napi_is_array(env, array, &isArray);
1567 ASSERT_TRUE(isArray);
1568
1569 bool isBigInt64Array = true;
1570 napi_is_big_int64_array(env, array, &isBigInt64Array);
1571 ASSERT_EQ(isBigInt64Array, false);
1572
1573 bool isBigUInt64Array = true;
1574 napi_is_big_uint64_array(env, array, &isBigUInt64Array);
1575 ASSERT_EQ(isBigUInt64Array, false);
1576 }
1577
1578 /**
1579 * @tc.name: SharedArrayBufferTest001
1580 * @tc.desc: Test is shared array buffer.
1581 * @tc.type: FUNC
1582 */
1583 HWTEST_F(NapiBasicTest, SharedArrayBufferTest001, testing::ext::TestSize.Level1) {
1584 napi_env env = (napi_env) engine_;
1585
1586 napi_value arrayBuffer = nullptr;
1587 void* arrayBufferPtr = nullptr;
1588 size_t arrayBufferSize = 1024;
1589 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
1590
1591 bool isSharedArrayBuffer = true;
1592 napi_is_shared_array_buffer(env, arrayBuffer, &isSharedArrayBuffer);
1593 ASSERT_EQ(isSharedArrayBuffer, false);
1594 }
1595
1596 /**
1597 * @tc.name: CreateBufferTest001
1598 * @tc.desc: Test is CreateBuffer.
1599 * @tc.type: FUNC
1600 */
1601 HWTEST_F(NapiBasicTest, CreateBufferTest001, testing::ext::TestSize.Level1)
1602 {
1603 napi_env env = (napi_env)engine_;
1604
1605 napi_value buffer = nullptr;
1606 void* bufferPtr = nullptr;
1607 size_t bufferSize = -1;
1608 napi_status creatresult = napi_create_buffer(env, bufferSize, &bufferPtr, &buffer);
1609
1610 ASSERT_EQ(creatresult, napi_status::napi_invalid_arg);
1611 ASSERT_EQ(bufferPtr, nullptr);
1612 }
1613
1614 /**
1615 * @tc.name: CreateBufferTest002
1616 * @tc.desc: Test is CreateBuffer.
1617 * @tc.type: FUNC
1618 */
1619 HWTEST_F(NapiBasicTest, CreateBufferTest002, testing::ext::TestSize.Level1)
1620 {
1621 napi_env env = (napi_env)engine_;
1622
1623 napi_value buffer = nullptr;
1624 void* bufferPtr = nullptr;
1625 const char* data = nullptr;
1626 size_t bufferSize = -1;
1627 napi_status creatresult = napi_create_buffer_copy(env, bufferSize, data, &bufferPtr, &buffer);
1628
1629 ASSERT_EQ(creatresult, napi_status::napi_invalid_arg);
1630 ASSERT_EQ(bufferPtr, nullptr);
1631 }
1632
1633 /**
1634 * @tc.name: CreateBufferTest003
1635 * @tc.desc: Test is CreateBuffer.
1636 * @tc.type: FUNC
1637 */
1638 HWTEST_F(NapiBasicTest, CreateBufferTest003, testing::ext::TestSize.Level1)
1639 {
1640 napi_env env = reinterpret_cast<napi_env>(engine_);
1641 napi_value buffer = nullptr;
1642 void* bufferPtr = nullptr;
1643 size_t bufferSize = 1;
1644 napi_status creatresult = napi_create_buffer(env, bufferSize, &bufferPtr, &buffer);
1645 ASSERT_EQ(creatresult, napi_status::napi_ok);
1646 creatresult = napi_create_buffer(env, bufferSize, nullptr, &buffer);
1647 ASSERT_EQ(creatresult, napi_status::napi_invalid_arg);
1648 }
1649
1650 /**
1651 * @tc.name: CreateBufferTest004
1652 * @tc.desc: Test is CreateBufferCopy.
1653 * @tc.type: FUNC
1654 */
1655 HWTEST_F(NapiBasicTest, CreateBufferTest004, testing::ext::TestSize.Level1)
1656 {
1657 napi_env env = reinterpret_cast<napi_env>(engine_);
1658
1659 napi_value buffer = nullptr;
1660 void* bufferPtr = nullptr;
1661 const char* data = nullptr;
1662 size_t bufferSize = 1;
1663 napi_status creatresult = napi_create_buffer_copy(env, bufferSize, nullptr, &bufferPtr, &buffer);
1664 ASSERT_EQ(creatresult, napi_status::napi_invalid_arg);
1665 creatresult = napi_create_buffer_copy(env, bufferSize, data, &bufferPtr, nullptr);
1666 ASSERT_EQ(creatresult, napi_status::napi_invalid_arg);
1667 }
1668
1669 /**
1670 * @tc.name: IsDetachedArrayBufferTest001
1671 * @tc.desc: Test is DetachedArrayBuffer.
1672 * @tc.type: FUNC
1673 */
1674 HWTEST_F(NapiBasicTest, IsDetachedArrayBufferTest001, testing::ext::TestSize.Level1)
1675 {
1676 static constexpr size_t arrayBufferSize = 1024;
1677 napi_env env = (napi_env)engine_;
1678 napi_value arrayBuffer = nullptr;
1679 void* arrayBufferPtr = nullptr;
1680 napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
1681
1682 bool result = false;
1683 ASSERT_CHECK_CALL(napi_is_detached_arraybuffer(env, arrayBuffer, &result));
1684
1685 auto out = napi_detach_arraybuffer(env, arrayBuffer);
1686 if (out == napi_ok) {
1687 arrayBufferPtr = nullptr;
1688 }
1689 ASSERT_EQ(out, napi_ok);
1690
1691 result = false;
1692 ASSERT_CHECK_CALL(napi_is_detached_arraybuffer(env, arrayBuffer, &result));
1693 ASSERT_TRUE(result);
1694 }
1695
1696 /**
1697 * @tc.name: FreezeObjectTest001
1698 * @tc.desc: Test is FreezeObject.
1699 * @tc.type: FUNC
1700 */
1701 HWTEST_F(NapiBasicTest, FreezeObjectTest001, testing::ext::TestSize.Level1)
1702 {
1703 constexpr int dataSize = 60;
1704 napi_env env = (napi_env)engine_;
1705 napi_value object = nullptr;
1706 napi_create_object(env, &object);
1707
1708 const char testStr[] = "1234567";
1709 napi_value strAttribute = nullptr;
1710 napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute);
1711 napi_set_named_property(env, object, "strAttribute", strAttribute);
1712
1713 int32_t testNumber = 1;
1714 napi_value numberAttribute = nullptr;
1715 napi_create_int32(env, testNumber, &numberAttribute);
1716 napi_set_named_property(env, object, "numberAttribute", numberAttribute);
1717
1718 ASSERT_CHECK_CALL(napi_object_freeze(env, object));
1719
1720 int32_t testNumber2 = 0;
1721 napi_value numberAttribute2 = nullptr;
1722 napi_create_int32(env, testNumber2, &numberAttribute2);
1723 ASSERT_CHECK_CALL(napi_set_named_property(env, object, "test", numberAttribute2));
1724
1725 napi_key_collection_mode keyMode = napi_key_own_only;
1726 napi_key_filter keyFilter = napi_key_all_properties;
1727 napi_key_conversion keyConversion = napi_key_keep_numbers;
1728 napi_value propNames = nullptr;
1729 ASSERT_CHECK_CALL(napi_get_all_property_names(env, object, keyMode, keyFilter, keyConversion, &propNames));
1730
1731 uint32_t arrayLength = 0;
1732 ASSERT_CHECK_CALL(napi_get_array_length(env, propNames, &arrayLength));
1733 ASSERT_EQ(arrayLength, MAX_BUFFER_SIZE);
1734
1735 char names[2][30];
1736 memset_s(names, dataSize, 0, dataSize);
1737 auto ret = memcpy_s(names[0], strlen("strAttribute"), "strAttribute", strlen("strAttribute"));
1738 ASSERT_EQ(ret, EOK);
1739 ret = memcpy_s(names[1], strlen("numberAttribute"), "numberAttribute", strlen("numberAttribute"));
1740 ASSERT_EQ(ret, EOK);
1741 for (uint32_t i = 0; i < arrayLength; i++) {
1742 bool hasElement = false;
1743 ASSERT_CHECK_CALL(napi_has_element(env, propNames, i, &hasElement));
1744
1745 napi_value propName = nullptr;
1746 ASSERT_CHECK_CALL(napi_get_element(env, propNames, i, &propName));
1747 ASSERT_CHECK_VALUE_TYPE(env, propName, napi_string);
1748
1749 size_t testStrLength = TEST_STR_LENGTH;
1750 char testStrInner[TEST_STR_LENGTH + 1];
1751 size_t outStrLength = 0;
1752 memset_s(testStrInner, testStrLength + 1, 0, testStrLength + 1);
1753 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, propName, testStrInner, testStrLength, &outStrLength));
1754
1755 int ret = strcmp(testStrInner, names[i]);
1756 ASSERT_EQ(ret, 0);
1757 }
1758 }
1759
1760 /**
1761 * @tc.name: SealObjectTest001
1762 * @tc.desc: Test is SealObject.
1763 * @tc.type: FUNC
1764 */
1765 HWTEST_F(NapiBasicTest, SealObjectTest001, testing::ext::TestSize.Level1)
1766 {
1767 napi_env env = (napi_env)engine_;
1768 napi_value object = nullptr;
1769
1770 napi_create_object(env, &object);
1771
1772 const char testStr[] = "1234567";
1773 napi_value strAttribute = nullptr;
1774 napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute);
1775 napi_set_named_property(env, object, "strAttribute", strAttribute);
1776
1777 int32_t testNumber = 1;
1778 napi_value numberAttribute = nullptr;
1779 napi_create_int32(env, testNumber, &numberAttribute);
1780 napi_set_named_property(env, object, "numberAttribute", numberAttribute);
1781
1782 ASSERT_CHECK_CALL(napi_object_seal(env, object));
1783
1784 bool testDeleted = false;
1785 ASSERT_CHECK_CALL(napi_delete_property(env, object, strAttribute, &testDeleted));
1786 ASSERT_TRUE(testDeleted);
1787
1788 const char modifiedStr[] = "modified";
1789 napi_value modifiedValue = nullptr;
1790 napi_create_string_utf8(env, modifiedStr, strlen(modifiedStr), &modifiedValue);
1791 ASSERT_CHECK_CALL(napi_set_named_property(env, object, "strAttribute", modifiedValue));
1792
1793 napi_value strAttribute2 = nullptr;
1794 napi_get_named_property(env, object, "strAttribute", &strAttribute2);
1795 char buffer[TEST_STR_LENGTH] = {0};
1796 size_t length = 0;
1797 napi_status status = napi_get_value_string_utf8(env, strAttribute2, buffer, sizeof(buffer) - 1, &length);
1798 ASSERT_EQ(status, napi_ok);
1799 ASSERT_EQ(length, strlen(modifiedStr));
1800 ASSERT_EQ(strcmp(buffer, modifiedStr), 0);
1801
1802 napi_key_collection_mode keyMode = napi_key_own_only;
1803 napi_key_filter keyFilter = napi_key_all_properties;
1804 napi_key_conversion keyConversion = napi_key_keep_numbers;
1805 napi_value propNames = nullptr;
1806 ASSERT_CHECK_CALL(napi_get_all_property_names(env, object, keyMode, keyFilter, keyConversion, &propNames));
1807
1808 uint32_t arrayLength = 0;
1809 ASSERT_CHECK_CALL(napi_get_array_length(env, propNames, &arrayLength));
1810 ASSERT_EQ(arrayLength, MAX_BUFFER_SIZE);
1811
1812 char names[2][TEST_STR_LENGTH];
1813 // There are 2 elements in the string array,
1814 // so the parameter is set to TEST_STR_LENGTH * 2 to clear the entire array.
1815 memset_s(names, TEST_STR_LENGTH * 2, 0, TEST_STR_LENGTH * 2);
1816 auto ret = memcpy_s(names[0], strlen("strAttribute"), "strAttribute", strlen("strAttribute"));
1817 ASSERT_EQ(ret, EOK);
1818 ret = memcpy_s(names[1], strlen("numberAttribute"), "numberAttribute", strlen("numberAttribute"));
1819 ASSERT_EQ(ret, EOK);
1820
1821 for (uint32_t i = 0; i < arrayLength; i++) {
1822 bool hasElement = false;
1823 ASSERT_CHECK_CALL(napi_has_element(env, propNames, i, &hasElement));
1824
1825 napi_value propName = nullptr;
1826 ASSERT_CHECK_CALL(napi_get_element(env, propNames, i, &propName));
1827 ASSERT_CHECK_VALUE_TYPE(env, propName, napi_string);
1828
1829 size_t testStrLength = TEST_STR_LENGTH;
1830 char testStrInner[TEST_STR_LENGTH + 1];
1831 size_t outStrLength = 0;
1832 memset_s(testStrInner, testStrLength + 1, 0, testStrLength + 1);
1833 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, propName, testStrInner, testStrLength, &outStrLength));
1834
1835 int ret = strcmp(testStrInner, names[i]);
1836 ASSERT_EQ(ret, 0);
1837 }
1838 }
1839
1840 /**
1841 * @tc.name: AllPropertyNamesTest001
1842 * @tc.desc: Test is AllPropertyNames.
1843 * @tc.type: FUNC
1844 */
1845 HWTEST_F(NapiBasicTest, AllPropertyNamesTest001, testing::ext::TestSize.Level1)
1846 {
1847 napi_env env = (napi_env)engine_;
1848 napi_key_collection_mode keyMode = napi_key_own_only;
1849 napi_key_filter keyFilter = napi_key_all_properties;
1850 napi_key_conversion keyConversion = napi_key_keep_numbers;
1851 napi_value result = nullptr;
1852 napi_value propNames = nullptr;
1853
1854 ASSERT_CHECK_CALL(napi_create_object(env, &result));
1855 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
1856
1857 const char testStr[] = "1234567";
1858 napi_value strAttribute = nullptr;
1859 napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute);
1860 napi_set_named_property(env, result, "strAttribute", strAttribute);
1861
1862 int32_t testNumber = 1;
1863 napi_value numberAttribute = nullptr;
1864 napi_create_int32(env, testNumber, &numberAttribute);
1865 napi_set_named_property(env, result, "numberAttribute", numberAttribute);
1866
1867 ASSERT_CHECK_CALL(napi_get_all_property_names(env, result, keyMode, keyFilter, keyConversion, &propNames));
1868
1869 ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
1870 bool isArray = false;
1871 ASSERT_CHECK_CALL(napi_is_array(env, propNames, &isArray));
1872 ASSERT_TRUE(isArray);
1873 uint32_t arrayLength = 0;
1874 ASSERT_CHECK_CALL(napi_get_array_length(env, propNames, &arrayLength));
1875 ASSERT_EQ(arrayLength, MAX_BUFFER_SIZE);
1876
1877 char names[2][TEST_STR_LENGTH];
1878 // There are 2 elements in the string array,
1879 // so the parameter is set to TEST_STR_LENGTH * 2 to clear the entire array.
1880 memset_s(names, TEST_STR_LENGTH * 2, 0, TEST_STR_LENGTH * 2);
1881 auto ret = memcpy_s(names[0], strlen("strAttribute"), "strAttribute", strlen("strAttribute"));
1882 ASSERT_EQ(ret, EOK);
1883 ret = memcpy_s(names[1], strlen("numberAttribute"), "numberAttribute", strlen("numberAttribute"));
1884 ASSERT_EQ(ret, EOK);
1885
1886 for (uint32_t i = 0; i < arrayLength; i++) {
1887 bool hasElement = false;
1888 ASSERT_CHECK_CALL(napi_has_element(env, propNames, i, &hasElement));
1889
1890 napi_value propName = nullptr;
1891 ASSERT_CHECK_CALL(napi_get_element(env, propNames, i, &propName));
1892 ASSERT_CHECK_VALUE_TYPE(env, propName, napi_string);
1893
1894 size_t testStrLength = TEST_STR_LENGTH;
1895 char testStrInner[TEST_STR_LENGTH + 1];
1896 size_t outStrLength = 0;
1897 memset_s(testStrInner, testStrLength + 1, 0, testStrLength + 1);
1898 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, propName, testStrInner, testStrLength, &outStrLength));
1899
1900 int ret = strcmp(testStrInner, names[i]);
1901 ASSERT_EQ(ret, 0);
1902 }
1903 }
1904
1905 /**
1906 * @tc.name: AllPropertyNamesTest002
1907 * @tc.desc: Test is AllPropertyNames.
1908 * @tc.type: FUNC
1909 */
1910 HWTEST_F(NapiBasicTest, AllPropertyNamesTest002, testing::ext::TestSize.Level1)
1911 {
1912 napi_env env = (napi_env)engine_;
1913 napi_key_collection_mode keyMode = napi_key_own_only;
1914 napi_key_filter keyFilter = napi_key_writable;
1915 napi_key_conversion keyConversion = napi_key_keep_numbers;
1916 napi_value result = nullptr;
1917 napi_value propNames = nullptr;
1918 // Create napi_values for 123, 456 and 789
1919 napi_value unenumerAble, writAble, configurAble;
1920 napi_create_int32(env, 123, &unenumerAble);
1921 napi_create_int32(env, 456, &writAble);
1922 napi_create_int32(env, 789, &configurAble);
1923
1924 napi_property_descriptor descriptors[] = {
1925 {"unenumerable",
1926 nullptr, nullptr, nullptr, nullptr, unenumerAble,
1927 napi_default_method, nullptr},
1928 {"writable",
1929 nullptr, nullptr, nullptr, nullptr, writAble,
1930 static_cast<napi_property_attributes>(napi_enumerable | napi_writable), nullptr},
1931 {"configurable",
1932 nullptr, nullptr, nullptr, nullptr, configurAble,
1933 static_cast<napi_property_attributes>(napi_enumerable | napi_configurable), nullptr}
1934 };
1935
1936 ASSERT_CHECK_CALL(napi_create_object(env, &result));
1937 ASSERT_CHECK_VALUE_TYPE(env, result, napi_object);
1938 ASSERT_CHECK_CALL(napi_define_properties(env, result, sizeof(descriptors) / sizeof(descriptors[0]), descriptors));
1939
1940 const char testStr[] = "1234567";
1941 napi_value strAttribute = nullptr;
1942 napi_create_string_utf8(env, testStr, strlen(testStr), &strAttribute);
1943 napi_set_named_property(env, result, "strAttribute", strAttribute);
1944
1945 int32_t testNumber = 1;
1946 napi_value numberAttribute = nullptr;
1947 napi_create_int32(env, testNumber, &numberAttribute);
1948 napi_set_named_property(env, result, "numberAttribute", numberAttribute);
1949
1950 ASSERT_CHECK_CALL(napi_get_all_property_names(env, result, keyMode, keyFilter, keyConversion, &propNames));
1951
1952 ASSERT_CHECK_VALUE_TYPE(env, propNames, napi_object);
1953 bool isArray = false;
1954 ASSERT_CHECK_CALL(napi_is_array(env, propNames, &isArray));
1955 ASSERT_TRUE(isArray);
1956 uint32_t arrayLength = 0;
1957 ASSERT_CHECK_CALL(napi_get_array_length(env, propNames, &arrayLength));
1958 ASSERT_EQ(arrayLength, 4); // 4 means array length.
1959
1960 char names[4][TEST_STR_LENGTH];
1961 // There are 4 elements in the string array,
1962 // so the parameter is set to TEST_STR_LENGTH * 4 to clear the entire array.
1963 memset_s(names, TEST_STR_LENGTH * 4, 0, TEST_STR_LENGTH * 4);
1964 auto ret = memcpy_s(names[0], strlen("unenumerable"), "unenumerable", strlen("unenumerable"));
1965 ASSERT_EQ(ret, EOK);
1966 ret = memcpy_s(names[1], strlen("writable"), "writable", strlen("writable"));
1967 ASSERT_EQ(ret, EOK);
1968 ret = memcpy_s(names[2], strlen("strAttribute"), "strAttribute", strlen("strAttribute"));
1969 ASSERT_EQ(ret, EOK);
1970 ret = memcpy_s(names[3], strlen("numberAttribute"), "numberAttribute", strlen("numberAttribute"));
1971 ASSERT_EQ(ret, EOK);
1972
1973 for (uint32_t i = 0; i < arrayLength; i++) {
1974 bool hasElement = false;
1975 ASSERT_CHECK_CALL(napi_has_element(env, propNames, i, &hasElement));
1976
1977 napi_value propName = nullptr;
1978 ASSERT_CHECK_CALL(napi_get_element(env, propNames, i, &propName));
1979 ASSERT_CHECK_VALUE_TYPE(env, propName, napi_string);
1980
1981 size_t testStrLength = TEST_STR_LENGTH;
1982 char testStrInner[TEST_STR_LENGTH + 1];
1983 size_t outStrLength = 0;
1984 memset_s(testStrInner, testStrLength + 1, 0, testStrLength + 1);
1985 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, propName, testStrInner, testStrLength, &outStrLength));
1986
1987 int ret = strcmp(testStrInner, names[i]);
1988 ASSERT_EQ(ret, 0);
1989 }
1990 }
1991
1992 /**
1993 * @tc.name: StringUtf16Test001
1994 * @tc.desc: Test is Chinese space character special character truncation.
1995 * @tc.type: FUNC
1996 */
1997 HWTEST_F(NapiBasicTest, StringUtf16Test001, testing::ext::TestSize.Level1)
1998 {
1999 napi_env env = reinterpret_cast<napi_env>(engine_);
2000 const char16_t testStr[] = u"中文,English,123456,!@#$%$#^%&12345 ";
2001 int testStrLength = static_cast<int>(std::char_traits<char16_t>::length(testStr));
2002 napi_value result = nullptr;
2003 ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &result));
2004 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
2005
2006 char16_t* buffer = nullptr;
2007 size_t bufferSize = 0;
2008 size_t strLength = 0;
2009 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, nullptr, 0, &bufferSize));
2010 ASSERT_GT(bufferSize, 0);
__anonb945b3c62802null2011 buffer = new char16_t[bufferSize + 1] { 0 };
2012 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, buffer, bufferSize + 1, &strLength));
2013 for (int i = 0; i < testStrLength; i++) {
2014 ASSERT_EQ(testStr[i], buffer[i]);
2015 }
2016 ASSERT_EQ(testStrLength, strLength);
2017 delete[] buffer;
2018 buffer = nullptr;
2019
2020 char16_t* bufferShort = nullptr;
2021 int bufferShortSize = 3;
__anonb945b3c62902null2022 bufferShort = new char16_t[bufferShortSize] { 0 };
2023 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, bufferShort, bufferShortSize, &strLength));
2024 for (int i = 0; i < bufferShortSize; i++) {
2025 if (i == (bufferShortSize - 1)) {
2026 ASSERT_EQ(0, bufferShort[i]);
2027 } else {
2028 ASSERT_EQ(testStr[i], bufferShort[i]);
2029 }
2030 }
2031 ASSERT_EQ(strLength, MAX_BUFFER_SIZE);
2032 delete[] bufferShort;
2033 bufferShort = nullptr;
2034 }
2035
2036 /**
2037 * @tc.name: StringUtf16Test002
2038 * @tc.desc: Test string type.
2039 * @tc.type: FUNC
2040 */
2041 HWTEST_F(NapiBasicTest, StringUtf16Test002, testing::ext::TestSize.Level2)
2042 {
2043 napi_env env = reinterpret_cast<napi_env>(engine_);
2044 char16_t testStr[] = u"ut.utf16test.napi.!@#%中^&*()6666";
2045 int testStrLength = static_cast<int>(std::char_traits<char16_t>::length(testStr));
2046 napi_value result = nullptr;
2047 {
2048 napi_status ret = napi_create_string_utf16(env, nullptr, testStrLength, &result);
2049 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2050 }
2051 {
2052 napi_status ret = napi_create_string_utf16(env, testStr, (size_t)INT_MAX + 1, &result);
2053 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2054 }
2055 }
2056
2057 /**
2058 * @tc.name: StringUtf16Test003
2059 * @tc.desc: Test string type.
2060 * @tc.type: FUNC
2061 */
2062 HWTEST_F(NapiBasicTest, StringUtf16Test003, testing::ext::TestSize.Level2)
2063 {
2064 napi_env env = reinterpret_cast<napi_env>(engine_);
2065 char16_t testStr[] = u"ut.utf16test.napi.!@#$%^&*123";
2066 size_t testStrLength = static_cast<size_t>(std::char_traits<char16_t>::length(testStr));
2067 char16_t buffer[] = u"12345";
2068 size_t bufferSize = 0;
2069 size_t copied = 0;
2070 napi_value result = nullptr;
2071
2072 ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &result));
2073 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, buffer, bufferSize, &copied));
2074
2075 for (size_t i = 0; i < MAX_BUFFER_SIZE; i++) {
2076 ASSERT_NE(buffer[i], testStr[i]);
2077 }
2078 }
2079
2080 /**
2081 * @tc.name: StringUtf16Test004
2082 * @tc.desc: Test string type.
2083 * @tc.type: FUNC
2084 */
2085 HWTEST_F(NapiBasicTest, StringUtf16Test004, testing::ext::TestSize.Level2)
2086 {
2087 napi_env env = reinterpret_cast<napi_env>(engine_);
2088 char16_t buffer[BUFFER_SIZE_FIVE];
2089 int testStrLength = static_cast<int>(std::char_traits<char16_t>::length(buffer));
2090 size_t copied;
2091 int64_t testValue = INT64_MAX;
2092 napi_value result = nullptr;
2093
2094 ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &result));
2095 ASSERT_CHECK_VALUE_TYPE(env, result, napi_bigint);
2096
2097 napi_status ret = napi_get_value_string_utf16(env, result, buffer, testStrLength, &copied);
2098 ASSERT_EQ(ret, napi_status::napi_string_expected);
2099 }
2100
2101 /**
2102 * @tc.name: StringUtf16Test005
2103 * @tc.desc: Test string type.
2104 * @tc.type: FUNC
2105 */
2106 HWTEST_F(NapiBasicTest, StringUtf16Test005, testing::ext::TestSize.Level2)
2107 {
2108 napi_env env = reinterpret_cast<napi_env>(engine_);
2109 char16_t testStr[] = u"ut.utf16test.napi.!@#$%^&*123";
2110 int testStrLength = static_cast<int>(std::char_traits<char16_t>::length(testStr));
2111 char16_t buffer[testStrLength];
2112 size_t copied;
2113 napi_value result = nullptr;
2114
2115 napi_status ret = napi_get_value_string_utf16(env, result, buffer, testStrLength, &copied);
2116 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2117 }
2118
2119 /**
2120 * @tc.name: StringUtf16Test006
2121 * @tc.desc: Test string length.
2122 * @tc.type: FUNC
2123 */
2124 HWTEST_F(NapiBasicTest, StringUtf16Test006, testing::ext::TestSize.Level1)
2125 {
2126 napi_env env = reinterpret_cast<napi_env>(engine_);
2127 char16_t testStr[] = u"ut.utf16test.napi.!@#$%^&*123";
2128 size_t testStrLength = static_cast<size_t>(std::char_traits<char16_t>::length(testStr));
2129 size_t copied = 0;
2130 napi_value result = nullptr;
2131
2132 ASSERT_CHECK_CALL(napi_create_string_utf16(env, testStr, testStrLength, &result));
2133 ASSERT_CHECK_CALL(napi_get_value_string_utf16(env, result, nullptr, testStrLength, &copied));
2134
2135 ASSERT_EQ(testStrLength, copied);
2136 }
2137
2138 /**
2139 * @tc.name: StringUtf8Test001
2140 * @tc.desc: Test string type.
2141 * @tc.type: FUNC
2142 */
2143 HWTEST_F(NapiBasicTest, StringUtf8Test001, testing::ext::TestSize.Level2)
2144 {
2145 napi_env env = reinterpret_cast<napi_env>(engine_);
2146 const char testStr[] = "ut.utf8test.napi.!@#%中^&*()6666";
2147 size_t testStrLength = strlen(testStr);
2148
2149 napi_status ret = napi_create_string_utf8(env, testStr, testStrLength, nullptr);
2150 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2151 }
2152
2153 /**
2154 * @tc.name: StringUtf8Test002
2155 * @tc.desc: Test string type.
2156 * @tc.type: FUNC
2157 */
2158 HWTEST_F(NapiBasicTest, StringUtf8Test002, testing::ext::TestSize.Level2)
2159 {
2160 napi_env env = reinterpret_cast<napi_env>(engine_);
2161 char buffer[BUFFER_SIZE_FIVE] = { 0 };
2162 size_t testStrLength = strlen(buffer);
2163 size_t copied;
2164 napi_value result = nullptr;
2165 napi_get_boolean(env, true, &result);
2166 ASSERT_CHECK_VALUE_TYPE(env, result, napi_boolean);
2167
2168 napi_status ret = napi_get_value_string_utf8(env, result, buffer, testStrLength, &copied);
2169 ASSERT_EQ(ret, napi_status::napi_string_expected);
2170 }
2171
2172 /**
2173 * @tc.name: StringUtf8Test003
2174 * @tc.desc: Test string type.
2175 * @tc.type: FUNC
2176 */
2177 HWTEST_F(NapiBasicTest, StringUtf8Test003, testing::ext::TestSize.Level2)
2178 {
2179 napi_env env = reinterpret_cast<napi_env>(engine_);
2180 const char testStr[] = "ut.utf8test.napi.!@#$%^&*123";
2181 size_t testStrLength = strlen(testStr);
2182 char buffer[testStrLength];
2183 size_t copied;
2184 napi_value result = nullptr;
2185
2186 napi_status ret = napi_get_value_string_utf8(env, result, buffer, testStrLength, &copied);
2187 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2188 }
2189
2190 /**
2191 * @tc.name: StringUtf8Test004
2192 * @tc.desc: Test string length.
2193 * @tc.type: FUNC
2194 */
2195 HWTEST_F(NapiBasicTest, StringUtf8Test004, testing::ext::TestSize.Level1)
2196 {
2197 napi_env env = reinterpret_cast<napi_env>(engine_);
2198 const char testStr[] = "ut.utf8test.napi.!@#$%^&*123";
2199 size_t testStrLength = strlen(testStr);
2200 size_t copied = 0;
2201 napi_value result = nullptr;
2202
2203 ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, testStrLength, &result));
2204 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, nullptr, testStrLength, &copied));
2205
2206 ASSERT_EQ(testStrLength, copied);
2207 }
2208
2209 /**
2210 * @tc.name: StringLatin1Test001
2211 * @tc.desc: Test string type.
2212 * @tc.type: FUNC
2213 */
2214 HWTEST_F(NapiBasicTest, StringLatin1Test001, testing::ext::TestSize.Level1)
2215 {
2216 napi_env env = reinterpret_cast<napi_env>(engine_);
2217 const char testStr[] = "ut.latin1test.napi.!@#%^&*()6666";
2218 size_t testStrLength = strlen(testStr);
2219 napi_value result = nullptr;
2220 ASSERT_CHECK_CALL(napi_create_string_latin1(env, testStr, testStrLength, &result));
2221 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
2222
2223 char* buffer = nullptr;
2224 size_t bufferSize = 0;
2225 size_t strLength = 0;
2226 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, nullptr, 0, &bufferSize));
2227 ASSERT_GT(bufferSize, 0);
__anonb945b3c62a02null2228 buffer = new char[bufferSize + 1]{ 0 };
2229 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, buffer, bufferSize + 1, &strLength));
2230 ASSERT_STREQ(testStr, buffer);
2231 ASSERT_EQ(testStrLength, strLength);
2232 delete []buffer;
2233 buffer = nullptr;
2234 }
2235
2236 /**
2237 * @tc.name: StringLatin1Test002
2238 * @tc.desc: Test string type.
2239 * @tc.type: FUNC
2240 */
2241 HWTEST_F(NapiBasicTest, StringLatin1Test002, testing::ext::TestSize.Level1)
2242 {
2243 napi_env env = reinterpret_cast<napi_env>(engine_);
2244 const char testStr[] = "ut.latin1test.中文测试";
2245 size_t testStrLength = strlen(testStr);
2246 napi_value result = nullptr;
2247 ASSERT_CHECK_CALL(napi_create_string_latin1(env, testStr, testStrLength, &result));
2248 ASSERT_CHECK_VALUE_TYPE(env, result, napi_string);
2249
2250 char* buffer = nullptr;
2251 size_t bufferSize = 0;
2252 size_t strLength = 0;
2253 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, nullptr, 0, &bufferSize));
2254 ASSERT_GT(bufferSize, 0);
__anonb945b3c62b02null2255 buffer = new char[bufferSize + 1]{ 0 };
2256 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, buffer, bufferSize + 1, &strLength));
2257 ASSERT_STRNE(testStr, buffer);
2258 ASSERT_GT(testStrLength, strLength);
2259 delete []buffer;
2260 buffer = nullptr;
2261 }
2262
2263 /**
2264 * @tc.name: StringLatin1Test003
2265 * @tc.desc: Test string type.
2266 * @tc.type: FUNC
2267 */
2268 HWTEST_F(NapiBasicTest, StringLatin1Test003, testing::ext::TestSize.Level2)
2269 {
2270 napi_env env = reinterpret_cast<napi_env>(engine_);
2271 napi_value result = nullptr;
2272
2273 const char testStr[] = "ut.latin1test.napi.!@#%^&*()6666";
2274 size_t testStrLength = strlen(testStr);
2275
2276 napi_status ret = napi_create_string_latin1(env, nullptr, testStrLength, &result);
2277 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2278 }
2279
2280 /**
2281 * @tc.name: StringLatin1Test004
2282 * @tc.desc: Test string type.
2283 * @tc.type: FUNC
2284 */
2285 HWTEST_F(NapiBasicTest, StringLatin1Test004, testing::ext::TestSize.Level2)
2286 {
2287 napi_env env = reinterpret_cast<napi_env>(engine_);
2288 napi_value result = nullptr;
2289
2290 const char testStr[] = "ut.latin1test.napi.!@#%^&*()6666";
2291
2292 napi_status ret = napi_create_string_latin1(env, testStr, 0, &result);
2293 ASSERT_EQ(ret, napi_status::napi_ok);
2294
2295 size_t bufferSize = 0;
2296 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, nullptr, 0, &bufferSize));
2297 ASSERT_EQ(bufferSize, 0);
2298 }
2299
2300 /**
2301 * @tc.name: StringLatin1Test005
2302 * @tc.desc: Test string type.
2303 * @tc.type: FUNC
2304 */
2305 HWTEST_F(NapiBasicTest, StringLatin1Test005, testing::ext::TestSize.Level2)
2306 {
2307 napi_env env = reinterpret_cast<napi_env>(engine_);
2308 char buffer[BUFFER_SIZE_FIVE] = { 0 };
2309 size_t testStrLength = strlen(buffer);
2310 size_t copied;
2311 napi_value result = nullptr;
2312 napi_get_boolean(env, true, &result);
2313 ASSERT_CHECK_VALUE_TYPE(env, result, napi_boolean);
2314
2315 napi_status ret = napi_get_value_string_latin1(env, result, buffer, testStrLength, &copied);
2316 ASSERT_EQ(ret, napi_status::napi_string_expected);
2317 }
2318
2319 /**
2320 * @tc.name: StringLatin1Test006
2321 * @tc.desc: Test string type.
2322 * @tc.type: FUNC
2323 */
2324 HWTEST_F(NapiBasicTest, StringLatin1Test006, testing::ext::TestSize.Level2)
2325 {
2326 napi_env env = reinterpret_cast<napi_env>(engine_);
2327 const char testStr[] = "ut.latin1test.napi.!@#$%^&*123";
2328 size_t testStrLength = strlen(testStr);
2329 char buffer[testStrLength];
2330 size_t copied;
2331 napi_value result = nullptr;
2332
2333 napi_status ret = napi_get_value_string_latin1(env, result, buffer, testStrLength, &copied);
2334 ASSERT_EQ(ret, napi_status::napi_invalid_arg);
2335 }
2336
2337 /**
2338 * @tc.name: StringLatin1Test007
2339 * @tc.desc: Test string type.
2340 * @tc.type: FUNC
2341 */
2342 HWTEST_F(NapiBasicTest, StringLatin1Test007, testing::ext::TestSize.Level1)
2343 {
2344 napi_env env = reinterpret_cast<napi_env>(engine_);
2345 const char testStr[] = "ut.latin1test.napi.!@#$%^&*123";
2346 size_t testStrLength = strlen(testStr);
2347 size_t copied = 0;
2348 napi_value result = nullptr;
2349
2350 ASSERT_CHECK_CALL(napi_create_string_latin1(env, testStr, testStrLength, &result));
2351 ASSERT_CHECK_CALL(napi_get_value_string_latin1(env, result, nullptr, testStrLength, &copied));
2352
2353 ASSERT_EQ(testStrLength, copied);
2354 }
2355
2356 /**
2357 * @tc.name: ToStringTest001
2358 * @tc.desc: Test string type of str.
2359 * @tc.type: FUNC
2360 */
2361 HWTEST_F(NapiBasicTest, ToStringTest001, testing::ext::TestSize.Level1)
2362 {
2363 napi_env env = reinterpret_cast<napi_env>(engine_);
2364 const char testStr[] = "中文,English,123456,!@#$%$#^%&";
2365 size_t testStrLength = strlen(testStr);
2366 napi_value str = nullptr;
2367 ASSERT_CHECK_CALL(napi_create_string_utf8(env, testStr, testStrLength, &str));
2368 ASSERT_CHECK_VALUE_TYPE(env, str, napi_string);
2369
2370 napi_value result = nullptr;
2371 ASSERT_CHECK_CALL(napi_coerce_to_string(env, str, &result));
2372 char* buffer = nullptr;
2373 size_t bufferSize = 0;
2374 size_t strLength = 0;
2375 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize));
2376 ASSERT_GT(bufferSize, 0);
__anonb945b3c62c02null2377 buffer = new char[bufferSize + 1]{ 0 };
2378 ASSERT_CHECK_CALL(napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength));
2379 ASSERT_STREQ(testStr, buffer);
2380 ASSERT_EQ(testStrLength, strLength);
2381 delete []buffer;
2382 buffer = nullptr;
2383 }
2384
2385 /**
2386 * @tc.name: ToStringTest002
2387 * @tc.desc: Test string type of undefined.
2388 * @tc.type: FUNC
2389 */
2390 HWTEST_F(NapiBasicTest, ToStringTest002, testing::ext::TestSize.Level1)
2391 {
2392 napi_env env = reinterpret_cast<napi_env>(engine_);
2393 napi_value argument;
2394 napi_get_undefined(env, &argument);
2395 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_undefined);
2396
2397 napi_value result;
2398 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
2399
2400 const char expected[] = "undefined";
2401 size_t expectedLength = strlen(expected);
2402 char* buffer = nullptr;
2403 size_t bufferSize = 0;
2404 size_t strLength = 0;
2405 napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize);
2406 ASSERT_GT(bufferSize, 0);
__anonb945b3c62d02null2407 buffer = new char[bufferSize + 1]{ 0 };
2408 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength);
2409 ASSERT_EQ(expectedLength, strLength);
2410 ASSERT_STREQ(expected, buffer);
2411 delete []buffer;
2412 buffer = nullptr;
2413 }
2414
2415 /**
2416 * @tc.name: ToStringTest003
2417 * @tc.desc: Test string type of null.
2418 * @tc.type: FUNC
2419 */
2420 HWTEST_F(NapiBasicTest, ToStringTest003, testing::ext::TestSize.Level1)
2421 {
2422 napi_env env = reinterpret_cast<napi_env>(engine_);
2423 napi_value argument;
2424 napi_get_null(env, &argument);
2425 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_null);
2426
2427 napi_value result;
2428 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
2429
2430 const char expected[] = "null";
2431 size_t expectedLength = strlen(expected);
2432 char* buffer = nullptr;
2433 size_t bufferSize = 0;
2434 size_t strLength = 0;
2435 napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize);
2436 ASSERT_GT(bufferSize, 0);
__anonb945b3c62e02null2437 buffer = new char[bufferSize + 1]{ 0 };
2438 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength);
2439 ASSERT_EQ(expectedLength, strLength);
2440 ASSERT_STREQ(expected, buffer);
2441 delete []buffer;
2442 buffer = nullptr;
2443 }
2444
2445 /**
2446 * @tc.name: ToStringTest004
2447 * @tc.desc: Test string type of bool.
2448 * @tc.type: FUNC
2449 */
2450 HWTEST_F(NapiBasicTest, ToStringTest004, testing::ext::TestSize.Level1)
2451 {
2452 napi_env env = reinterpret_cast<napi_env>(engine_);
2453 napi_value argument;
2454 napi_get_boolean(env, true, &argument);
2455 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_boolean);
2456
2457 napi_value result;
2458 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
2459
2460 const char expected[] = "true";
2461 size_t expectedLength = strlen(expected);
2462 char* buffer = nullptr;
2463 size_t bufferSize = 0;
2464 size_t strLength = 0;
2465 napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize);
2466 ASSERT_GT(bufferSize, 0);
__anonb945b3c62f02null2467 buffer = new char[bufferSize + 1]{ 0 };
2468 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength);
2469 ASSERT_EQ(expectedLength, strLength);
2470 ASSERT_STREQ(expected, buffer);
2471 delete []buffer;
2472 buffer = nullptr;
2473 }
2474
2475 /**
2476 * @tc.name: ToStringTest005
2477 * @tc.desc: Test string type of number.
2478 * @tc.type: FUNC
2479 */
2480 HWTEST_F(NapiBasicTest, ToStringTest005, testing::ext::TestSize.Level1)
2481 {
2482 napi_env env = reinterpret_cast<napi_env>(engine_);
2483 napi_value argument;
2484 double number = 0.1;
2485 napi_create_double(env, number, &argument);
2486 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_number);
2487
2488 napi_value result;
2489 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
2490
2491 double numberValue;
2492 napi_get_value_double(env, argument, &numberValue);
2493 std::string expected = std::to_string(numberValue);
2494 // Remove excess '0' after delimiter
2495 while (!expected.empty() && expected.back() == '0')
2496 {
2497 expected.pop_back();
2498 }
2499
2500 size_t expectedLength = expected.length();
2501 char* buffer = nullptr;
2502 size_t bufferSize = 0;
2503 size_t strLength = 0;
2504 napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize);
2505 ASSERT_GT(bufferSize, 0);
__anonb945b3c63002null2506 buffer = new char[bufferSize + 1]{ 0 };
2507 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength);
2508 ASSERT_EQ(expectedLength, strLength);
2509 ASSERT_STREQ(expected.c_str(), buffer);
2510 delete []buffer;
2511 buffer = nullptr;
2512 }
2513
2514 /**
2515 * @tc.name: ToStringTest006
2516 * @tc.desc: Test string type of bigint.
2517 * @tc.type: FUNC
2518 */
2519 HWTEST_F(NapiBasicTest, ToStringTest006, testing::ext::TestSize.Level1)
2520 {
2521 napi_env env = reinterpret_cast<napi_env>(engine_);
2522 int64_t testValue = INT64_MAX;
2523 napi_value argument;
2524 bool flag = false;
2525 ASSERT_CHECK_CALL(napi_create_bigint_int64(env, testValue, &argument));
2526 ASSERT_CHECK_VALUE_TYPE(env, argument, napi_bigint);
2527
2528 napi_value result;
2529 ASSERT_CHECK_CALL(napi_coerce_to_string(env, argument, &result));
2530
2531 int64_t numberValue = 0;
2532 ASSERT_CHECK_CALL(napi_get_value_bigint_int64(env, argument, &numberValue, &flag));
2533 ASSERT_EQ(numberValue, INT64_MAX);
2534 ASSERT_TRUE(flag);
2535 std::string expected = std::to_string(numberValue);
2536
2537 size_t expectedLength = expected.length();
2538 char* buffer = nullptr;
2539 size_t bufferSize = 0;
2540 size_t strLength = 0;
2541 napi_get_value_string_utf8(env, result, nullptr, 0, &bufferSize);
2542 ASSERT_GT(bufferSize, 0);
__anonb945b3c63102null2543 buffer = new char[bufferSize + 1]{ 0 };
2544 napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &strLength);
2545 ASSERT_EQ(expectedLength, strLength);
2546 ASSERT_STREQ(expected.c_str(), buffer);
2547 delete []buffer;
2548 buffer = nullptr;
2549 }
2550
2551 /**
2552 * @tc.name: ToStringTest007
2553 * @tc.desc: Test string type of symbol.
2554 * @tc.type: FUNC
2555 */
2556 HWTEST_F(NapiBasicTest, ToStringTest007, testing::ext::TestSize.Level1)
2557 {
2558 napi_env env = reinterpret_cast<napi_env>(engine_);
2559 const char testStr[] = "testSymbol";
2560 size_t testStrLength = strlen(testStr);
2561 napi_value testSymbol = nullptr;
2562 napi_create_string_utf8(env, testStr, testStrLength, &testSymbol);
2563 napi_value symbolVal = nullptr;
2564 napi_create_symbol(env, testSymbol, &symbolVal);
2565 ASSERT_CHECK_VALUE_TYPE(env, symbolVal, napi_symbol);
2566
2567 napi_value result = nullptr;
2568 ASSERT_CHECK_CALL(napi_coerce_to_string(env, symbolVal, &result));
2569 ASSERT_CHECK_VALUE_TYPE(env, result, napi_undefined);
2570 }
2571
2572 /**
2573 * @tc.name: ToStringTest001
2574 * @tc.desc: Test string type.
2575 * @tc.type: FUNC
2576 */
2577 HWTEST_F(NapiBasicTest, ToStringTest008, testing::ext::TestSize.Level1)
2578 {
2579 napi_env env = reinterpret_cast<napi_env>(engine_);
2580
2581 napi_value result;
2582 napi_status status = napi_coerce_to_string(env, nullptr, &result);
2583 ASSERT_EQ(status, napi_status::napi_invalid_arg);
2584 }