• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 SwanLink (Jiangsu) Technology Development 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 #include "napi_json_test.h"
16 #include "napi/napi_json_test.h"
17 const size_t BUF_SIZE_MAX = 128;
18 //OH_JSVM_JsonParse
19 //null string
TestJsonParseTest1(JSVM_Env env,JSVM_CallbackInfo info)20 [[maybe_unused]] JSVM_Value TestJsonParseTest1(JSVM_Env env, JSVM_CallbackInfo info)
21 {
22     size_t argc = 1;
23     JSVM_Value argv[1] = {nullptr};
24     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
25 
26     const char* strJson = "";
27     JSVM_Value rstJsonValue = nullptr;
28     JSVM_Status status = OH_JSVM_CreateStringUtf8(env, strJson, JSVM_AUTO_LENGTH, &rstJsonValue);
29     if (status != JSVM_OK) {
30         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest1: OH_JSVM_CreateStringUtf8 Failed");
31         return nullptr;
32     }
33     JSVM_Value rstJsonString;
34     status = OH_JSVM_JsonParse(env, rstJsonValue, &rstJsonString);
35     if (status == JSVM_OK) {
36         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest1: OH_JSVM_JsonParse Failed");
37         return nullptr;
38     }
39     JSVM_Value rstError = nullptr;
40     status = OH_JSVM_GetAndClearLastException(env, &rstError);
41     if (status != JSVM_OK) {
42         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest1: OH_JSVM_GetAndClearLastException Failed");
43         return nullptr;
44     }
45 
46     bool result = true;
47     JSVM_Value value = nullptr;
48     OH_JSVM_GetBoolean(env, result, &value);
49     return value;
50 }
51 //error js string
TestJsonParseTest2(JSVM_Env env,JSVM_CallbackInfo info)52 [[maybe_unused]] JSVM_Value TestJsonParseTest2(JSVM_Env env, JSVM_CallbackInfo info)
53 {
54     size_t argc = 1;
55     JSVM_Value argv[1] = {nullptr};
56     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
57 
58     const char* strJson = "{\"name\" \"John\", \"age\": 30, \"city\": \"ChongQing\"}";
59     JSVM_Value rstJsonValue = nullptr;
60     JSVM_Status status = OH_JSVM_CreateStringUtf8(env, strJson, JSVM_AUTO_LENGTH, &rstJsonValue);
61     if (status != JSVM_OK) {
62         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest2: OH_JSVM_CreateStringUtf8 Failed");
63         return nullptr;
64     }
65     JSVM_Value rstJsonString;
66     status = OH_JSVM_JsonParse(env, rstJsonValue, &rstJsonString);
67     if (status == JSVM_OK) {
68         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest2: OH_JSVM_JsonParse Failed");
69         return nullptr;
70     }
71     JSVM_Value rstError = nullptr;
72     status = OH_JSVM_GetAndClearLastException(env, &rstError);
73     if (status != JSVM_OK) {
74         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest1: OH_JSVM_GetAndClearLastException Failed");
75         return nullptr;
76     }
77 
78     bool result = true;
79     JSVM_Value value = nullptr;
80     OH_JSVM_GetBoolean(env, result, &value);
81     return value;
82 }
83 //json string, return ok
TestJsonParseTest3(JSVM_Env env,JSVM_CallbackInfo info)84 [[maybe_unused]] JSVM_Value TestJsonParseTest3(JSVM_Env env, JSVM_CallbackInfo info)
85 {
86     size_t argc = 1;
87     JSVM_Value argv[1] = {nullptr};
88     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
89 
90     const char* strJson = "{\"name\": \"John\", \"age\": 30, \"city\": \"ChongQing\"}";
91     JSVM_Value rstJsonValue = nullptr;
92     JSVM_Status status = OH_JSVM_CreateStringUtf8(env, strJson, JSVM_AUTO_LENGTH, &rstJsonValue);
93     if (status != JSVM_OK) {
94         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest3: OH_JSVM_CreateStringUtf8 Failed");
95         return nullptr;
96     }
97     JSVM_Value rstJsonString;
98     status = OH_JSVM_JsonParse(env, rstJsonValue, &rstJsonString);
99     if (status != JSVM_OK) {
100         OH_JSVM_ThrowError(env, nullptr, "TestJsonParseTest3: OH_JSVM_JsonParse Failed");
101         return nullptr;
102     }
103 
104     bool result = true;
105     JSVM_Value value = nullptr;
106     OH_JSVM_GetBoolean(env, result, &value);
107     return value;
108 }
109 //OH_JSVM_JsonStringify
110 //jsonObject is nullptr,return n-ok
TestJsonStringifyTest1(JSVM_Env env,JSVM_CallbackInfo info)111 [[maybe_unused]] JSVM_Value TestJsonStringifyTest1(JSVM_Env env, JSVM_CallbackInfo info)
112 {
113     size_t argc = 1;
114     JSVM_Value argv[1] = {nullptr};
115     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
116 
117     JSVM_Value rstJsonValue = nullptr;
118     JSVM_Status status = OH_JSVM_JsonStringify(env, nullptr, &rstJsonValue);
119     if (status == JSVM_OK) {
120         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest1: OH_JSVM_JsonStringify Failed");
121         return nullptr;
122     }
123 
124     bool result = true;
125     JSVM_Value value = nullptr;
126     OH_JSVM_GetBoolean(env, result, &value);
127     return value;
128 }
129 //jsonObject为object对象,预期接口返回jsvmok, result预期结果为空序列
TestJsonStringifyTest2(JSVM_Env env,JSVM_CallbackInfo info)130 [[maybe_unused]] JSVM_Value TestJsonStringifyTest2(JSVM_Env env, JSVM_CallbackInfo info)
131 {
132     size_t argc = 1;
133     JSVM_Value argv[1] = {nullptr};
134     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
135 
136     JSVM_Value jsonObject = nullptr;
137     JSVM_Status status = OH_JSVM_CreateObject(env, &jsonObject);
138     if (status != JSVM_OK) {
139         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest2: OH_JSVM_CreateObject Failed");
140         return nullptr;
141     }
142     JSVM_Value rstJsonString = nullptr;
143     status = OH_JSVM_JsonStringify(env, jsonObject, &rstJsonString);
144     if (status != JSVM_OK) {
145         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest2: OH_JSVM_JsonStringify Failed");
146         return nullptr;
147     }
148     char strGetBuf[BUF_SIZE_MAX] = {0};
149     size_t strLength = 0;
150     status = OH_JSVM_GetValueStringUtf8(env, rstJsonString, strGetBuf, BUF_SIZE_MAX, &strLength);
151     if (strcmp("", strGetBuf) == 0) {
152         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest1: OH_JSVM_GetValueStringUtf8 Failed");
153         return nullptr;
154     }
155 
156     bool result = true;
157     JSVM_Value value = nullptr;
158     OH_JSVM_GetBoolean(env, result, &value);
159     return value;
160 }
161 //jsonobject is TypedArray object,return jsvm ok
TestJsonStringifyTest3(JSVM_Env env,JSVM_CallbackInfo info)162 [[maybe_unused]] JSVM_Value TestJsonStringifyTest3(JSVM_Env env, JSVM_CallbackInfo info)
163 {
164     size_t argc = 1;
165     JSVM_Value argv[1] = {nullptr};
166     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
167 
168     JSVM_Value arrayBuffer = nullptr;
169     void *arrayBufferPtr = nullptr;
170     const size_t arrayBufferSize = BUF_SIZE_MAX;
171     const size_t typedArrayLength = 1;
172     JSVM_Value jsonValue = nullptr;
173     JSVM_Status status = OH_JSVM_CreateArraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer);
174     if (status != JSVM_OK) {
175         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest3: OH_JSVM_CreateArraybuffer Failed");
176         return nullptr;
177     }
178     status = OH_JSVM_CreateTypedarray(env, JSVM_INT8_ARRAY, typedArrayLength, arrayBuffer, 0, &jsonValue);
179     if (status != JSVM_OK) {
180         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest3: OH_JSVM_CreateTypedarray Failed");
181     }
182 
183     JSVM_Value rstJsonString;
184     status = OH_JSVM_JsonStringify(env, jsonValue, &rstJsonString);
185     if (status != JSVM_OK) {
186         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest3: OH_JSVM_JsonStringify Failed");
187         return nullptr;
188     }
189     char jsonStrBuf[BUF_SIZE_MAX] = {0};
190     size_t rstLength = 0;
191     status = OH_JSVM_GetValueStringUtf8(env, rstJsonString, jsonStrBuf, BUF_SIZE_MAX, &rstLength);
192     if (status != JSVM_OK) {
193         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest3: OH_JSVM_JsonStringify Failed");
194         return nullptr;
195     }
196     if (rstLength != strlen(jsonStrBuf)) {
197         OH_JSVM_ThrowError(env, nullptr, "TestJsonStringifyTest3: rstLength != arrayBufferSize");
198         return nullptr;
199     }
200 
201     bool result = true;
202     JSVM_Value value = nullptr;
203     OH_JSVM_GetBoolean(env, result, &value);
204     return value;
205 }
206 //string -- parse
TestJsonCombinationTest1(JSVM_Env env,JSVM_CallbackInfo info)207 [[maybe_unused]] JSVM_Value TestJsonCombinationTest1(JSVM_Env env, JSVM_CallbackInfo info)
208 {
209     size_t argc = 1;
210     JSVM_Value argv[1] = {nullptr};
211     OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
212 
213     // create object
214     JSVM_Value jsonObject = nullptr;
215     JSVM_Status status = OH_JSVM_CreateObject(env, &jsonObject);
216     if (status != JSVM_OK) {
217         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_CreateObject Failed");
218         return nullptr;
219     }
220     // set property
221     char* strJsonValue = "key value";
222     JSVM_Value jsonValue = nullptr;
223     char* strJsonKey = "key";
224     JSVM_Value jsonKey = nullptr;
225     status = OH_JSVM_CreateStringUtf8(env, strJsonKey, JSVM_AUTO_LENGTH, &jsonKey);
226     if (status != JSVM_OK) {
227         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
228         return nullptr;
229     }
230     status = OH_JSVM_CreateStringUtf8(env, strJsonValue, JSVM_AUTO_LENGTH, &jsonValue);
231     if (status != JSVM_OK) {
232         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_CreateStringUtf8 Failed");
233         return nullptr;
234     }
235     status = OH_JSVM_SetProperty(env, jsonObject, jsonKey, jsonValue);
236     if (status != JSVM_OK) {
237         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_SetProperty Failed");
238         return nullptr;
239     }
240     JSVM_Value rstJsonString = nullptr;
241     status = OH_JSVM_JsonStringify(env, jsonObject, &rstJsonString);
242     if (status != JSVM_OK) {
243         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_JsonStringify Failed");
244         return nullptr;
245     }
246     //parse string
247     JSVM_Value rstJsonParseStr = nullptr;
248     status = OH_JSVM_JsonParse(env, rstJsonString, &rstJsonParseStr);
249     if (status != JSVM_OK) {
250         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_JsonParse Failed");
251         return nullptr;
252     }
253     char strGetBuf[BUF_SIZE_MAX] = {0};
254     size_t strLength = 0;
255     status = OH_JSVM_GetValueStringUtf8(env, rstJsonParseStr, strGetBuf, BUF_SIZE_MAX, &strLength);
256     if (status != JSVM_STRING_EXPECTED) {
257         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_GetValueStringUtf8 Failed");
258         return nullptr;
259     }
260     char strGetObjBuf[BUF_SIZE_MAX] = {0};
261     size_t strObjLength = 0;
262     status = OH_JSVM_GetValueStringUtf8(env, jsonObject, strGetObjBuf, BUF_SIZE_MAX, &strObjLength);
263     if (status != JSVM_STRING_EXPECTED) {
264         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: OH_JSVM_GetValueStringUtf8 Failed");
265         return nullptr;
266     }
267     if (strcmp(strGetObjBuf, strGetBuf) != 0) {
268         OH_JSVM_ThrowError(env, nullptr, "TestJsonCombinationTest1: strGetObjBuf != strGetBuf");
269         return nullptr;
270     }
271 
272     bool result = true;
273     JSVM_Value value = nullptr;
274     OH_JSVM_GetBoolean(env, result, &value);
275     return value;
276 }