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
16 #include "napi/native_api.h"
17 #include "napi_heapstatisticsdebugg_test.h"
18
hello(JSVM_Env env,JSVM_CallbackInfo info)19 static JSVM_Value hello(JSVM_Env env, JSVM_CallbackInfo info)
20 {
21 JSVM_Value output;
22 void *data = nullptr;
23 OH_JSVM_GetCbInfo(env, info, nullptr, nullptr, nullptr, &data);
24 OH_JSVM_CreateStringUtf8(env, (char *)data, strlen((char *)data), &output);
25 return output;
26 }
27
28 //OH_JSVM_GetHeapStatistics:Call the interface to be tested,return ok
TestGetHeapStatisticsCase01(JSVM_Env env,JSVM_CallbackInfo info)29 [[maybe_unused]] JSVM_Value TestGetHeapStatisticsCase01(JSVM_Env env, JSVM_CallbackInfo info)
30 {
31 JSVM_VM testVm;
32 OH_JSVM_GetVM(env, &testVm);
33 JSVM_HeapStatistics result;
34 JSVM_Status status = OH_JSVM_GetHeapStatistics(testVm, &result);
35 if (status != JSVM_OK) {
36 OH_JSVM_ThrowError(env, nullptr, "TestGetHeapStatisticsCase01:OH_JSVM_GetHeapStatistics Failed.");
37 return nullptr;
38 }
39
40 bool setValue = true;
41 JSVM_Value retValue = nullptr;
42 OH_JSVM_GetBoolean(env, setValue, &retValue);
43 return retValue;
44 }
45 //OH_JSVM_StartCpuProfiler:Call the interface to be tested,return ok,result not nullptr
TestStartCpuProfilerCase01(JSVM_Env env,JSVM_CallbackInfo info)46 [[maybe_unused]] JSVM_Value TestStartCpuProfilerCase01(JSVM_Env env, JSVM_CallbackInfo info)
47 {
48 JSVM_VM vm;
49 JSVM_Status status = OH_JSVM_CreateVM(nullptr, &vm);
50 if (status != JSVM_OK) {
51 OH_JSVM_ThrowError(env, nullptr, "TestStartCpuProfilerCase01:OH_JSVM_CreateVM Failed.");
52 return nullptr;
53 }
54
55 JSVM_CpuProfiler cpuProfiler = nullptr;
56 status = OH_JSVM_StartCpuProfiler(vm, &cpuProfiler);
57 if (status != JSVM_OK || cpuProfiler == nullptr) {
58 OH_JSVM_ThrowError(env, nullptr, "TestStartCpuProfilerCase01:OH_JSVM_StartCpuProfiler Failed.");
59 return nullptr;
60 }
61
62 bool setValue = true;
63 JSVM_Value retValue = nullptr;
64 OH_JSVM_GetBoolean(env, setValue, &retValue);
65 return retValue;
66 }
67 //OH_JSVM_StopCpuProfiler:call OH_JSVM_StartCpuProfiler,Call the interface to be tested,return ok
OutputStream(const char * data,int size,void * streamData)68 static bool OutputStream(const char *data, int size, void *streamData)
69 {
70 return true;
71 }
72
TestStopCpuProfilerCase01(JSVM_Env env,JSVM_CallbackInfo info)73 [[maybe_unused]] JSVM_Value TestStopCpuProfilerCase01(JSVM_Env env, JSVM_CallbackInfo info)
74 {
75 JSVM_VM vm;
76 OH_JSVM_CreateVM(nullptr, &vm);
77 JSVM_CpuProfiler cpuProfiler;
78 JSVM_Status status = OH_JSVM_StartCpuProfiler(vm, &cpuProfiler);
79 if (status != JSVM_OK || cpuProfiler == nullptr) {
80 OH_JSVM_ThrowError(env, nullptr, "TestStopCpuProfilerCase01:OH_JSVM_StartCpuProfiler Failed.");
81 return nullptr;
82 }
83
84 void *data = nullptr;
85 status = OH_JSVM_StopCpuProfiler(vm, cpuProfiler, OutputStream, data);
86 if (status != JSVM_OK) {
87 OH_JSVM_ThrowError(env, nullptr, "TestStopCpuProfilerCase01:OH_JSVM_StopCpuProfiler Failed.");
88 return nullptr;
89 }
90
91 bool setValue = true;
92 JSVM_Value retValue = nullptr;
93 OH_JSVM_GetBoolean(env, setValue, &retValue);
94 return retValue;
95 }
96 //input valid value (callback function used to process the returned tuning data),return ok
TestTakeHeapSnapshotCase01(JSVM_Env env,JSVM_CallbackInfo info)97 [[maybe_unused]] JSVM_Value TestTakeHeapSnapshotCase01(JSVM_Env env, JSVM_CallbackInfo info)
98 {
99 JSVM_VM vm;
100 JSVM_Status status = OH_JSVM_CreateVM(nullptr, &vm);
101 if (status != JSVM_OK) {
102 OH_JSVM_ThrowError(env, nullptr, "TestTakeHeapSnapshotCase01:OH_JSVM_CreateVM Failed.");
103 return nullptr;
104 }
105 void *data = nullptr;
106
107 status = OH_JSVM_TakeHeapSnapshot(vm, OutputStream, data);
108 if (status != JSVM_OK) {
109 OH_JSVM_ThrowError(env, nullptr, "TestTakeHeapSnapshotCase01:OH_JSVM_TakeHeapSnapshot Failed.");
110 return nullptr;
111 }
112
113 bool setValue = true;
114 JSVM_Value retValue = nullptr;
115 OH_JSVM_GetBoolean(env, setValue, &retValue);
116 return retValue;
117 }
118 //host valid value, port as a negative number, return ok
TestOpenInspectorCase01(JSVM_Env env,JSVM_CallbackInfo info)119 [[maybe_unused]] JSVM_Value TestOpenInspectorCase01(JSVM_Env env, JSVM_CallbackInfo info)
120 {
121 int16_t num = -128;
122 JSVM_Status status = OH_JSVM_OpenInspector(env, "localhost", num);
123 if (status != JSVM_OK) {
124 OH_JSVM_ThrowError(env, nullptr, "TestOpenInspectorCase01:OH_JSVM_OpenInspector Execution abnormal.");
125 return nullptr;
126 }
127
128 bool setValue = true;
129 JSVM_Value retValue = nullptr;
130 OH_JSVM_GetBoolean(env, setValue, &retValue);
131 return retValue;
132 }
133 //host valid value,port 65536,return ok
TestOpenInspectorCase02(JSVM_Env env,JSVM_CallbackInfo info)134 [[maybe_unused]] JSVM_Value TestOpenInspectorCase02(JSVM_Env env, JSVM_CallbackInfo info)
135 {
136 uint16_t num = 65536;
137 JSVM_Status status = OH_JSVM_OpenInspector(env, "localhost", num);
138 if (status != JSVM_OK) {
139 OH_JSVM_ThrowError(env, nullptr, "TestOpenInspectorCase02:OH_JSVM_OpenInspector Execution abnormal.");
140 return nullptr;
141 }
142
143 bool setValue = true;
144 JSVM_Value retValue = nullptr;
145 OH_JSVM_GetBoolean(env, setValue, &retValue);
146 return retValue;
147 }
148 //host valid value,port is a reserved port number,return ok
TestOpenInspectorCase03(JSVM_Env env,JSVM_CallbackInfo info)149 [[maybe_unused]] JSVM_Value TestOpenInspectorCase03(JSVM_Env env, JSVM_CallbackInfo info)
150 {
151 int16_t num = 0;
152 JSVM_Status status = OH_JSVM_OpenInspector(env, "localhost", num);
153 if (status != JSVM_OK) {
154 OH_JSVM_ThrowError(env, nullptr, "TestOpenInspectorCase03:OH_JSVM_OpenInspector Failed.");
155 return nullptr;
156 }
157
158 bool setValue = true;
159 JSVM_Value retValue = nullptr;
160 OH_JSVM_GetBoolean(env, setValue, &retValue);
161 return retValue;
162 }
163 //OH_JSVM_CloseInspector:no open inspectors in the env,call test func,return not ok
TestCloseInspectorCase01(JSVM_Env env,JSVM_CallbackInfo info)164 [[maybe_unused]] JSVM_Value TestCloseInspectorCase01(JSVM_Env env, JSVM_CallbackInfo info)
165 {
166 JSVM_Status status = OH_JSVM_CloseInspector(env);
167 if (status == JSVM_OK) {
168 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase01:OH_JSVM_CloseInspector Failed.");
169 return nullptr;
170 }
171
172 bool setValue = true;
173 JSVM_Value retValue = nullptr;
174 OH_JSVM_GetBoolean(env, setValue, &retValue);
175 return retValue;
176 }
177 //an open inspector in the env,call test func,return ok
TestCloseInspectorCase02(JSVM_Env env,JSVM_CallbackInfo info)178 [[maybe_unused]] JSVM_Value TestCloseInspectorCase02(JSVM_Env env, JSVM_CallbackInfo info)
179 {
180 int32_t num = 65534;
181 JSVM_Status status = OH_JSVM_OpenInspector(env, "localhost", num);
182 if (status != JSVM_OK) {
183 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase02:OH_JSVM_OpenInspector Failed.");
184 return nullptr;
185 }
186
187 status = OH_JSVM_CloseInspector(env);
188 if (status != JSVM_OK) {
189 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase02:OH_JSVM_CloseInspector Failed.");
190 return nullptr;
191 }
192
193 bool setValue = true;
194 JSVM_Value retValue = nullptr;
195 OH_JSVM_GetBoolean(env, setValue, &retValue);
196 return retValue;
197 }
198 //there are multiple open inspectors in the env,call test func,return ok
TestCloseInspectorCase03(JSVM_Env env,JSVM_CallbackInfo info)199 [[maybe_unused]] JSVM_Value TestCloseInspectorCase03(JSVM_Env env, JSVM_CallbackInfo info)
200 {
201 int32_t num = 65534;
202 JSVM_Status status = OH_JSVM_OpenInspector(env, "localhost", num);
203 if (status != JSVM_OK) {
204 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase03:OH_JSVM_OpenInspector Failed.");
205 return nullptr;
206 }
207
208 int32_t num1 = 300;
209 status = OH_JSVM_OpenInspector(env, "localhost", num1);
210 if (status != JSVM_OK) {
211 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase03:OH_JSVM_OpenInspector Failed.");
212 return nullptr;
213 }
214
215 status = OH_JSVM_CloseInspector(env);
216 if (status != JSVM_OK) {
217 OH_JSVM_ThrowError(env, nullptr, "TestCloseInspectorCase03:OH_JSVM_CloseInspector Failed.");
218 return nullptr;
219 }
220
221 bool setValue = true;
222 JSVM_Value retValue = nullptr;
223 OH_JSVM_GetBoolean(env, setValue, &retValue);
224 return retValue;
225 }
226 //OH_JSVM_OpenInspectorWithName:pid is a valid value,return ok
TestOpenInspectorWithNameCase01(JSVM_Env env,JSVM_CallbackInfo info)227 [[maybe_unused]] JSVM_Value TestOpenInspectorWithNameCase01(JSVM_Env env, JSVM_CallbackInfo info)
228 {
229 JSVM_Status status = OH_JSVM_OpenInspectorWithName(env, 9000, "test");
230 if (status != JSVM_OK) {
231 OH_JSVM_ThrowError(env, nullptr, "TestOpenInspectorWithNameCase01:OH_JSVM_OpenInspectorWithName Failed.");
232 return nullptr;
233 }
234
235 bool setValue = true;
236 JSVM_Value retValue = nullptr;
237 OH_JSVM_GetBoolean(env, setValue, &retValue);
238 return retValue;
239 }
240 //name null,return ok
TestOpenInspectorWithNameCase02(JSVM_Env env,JSVM_CallbackInfo info)241 [[maybe_unused]] JSVM_Value TestOpenInspectorWithNameCase02(JSVM_Env env, JSVM_CallbackInfo info)
242 {
243 JSVM_Status status = OH_JSVM_OpenInspectorWithName(env, 1, "");
244 if (status != JSVM_OK) {
245 OH_JSVM_ThrowError(env,
246 nullptr,
247 "TestOpenInspectorWithNameCase02:OH_JSVM_OpenInspectorWithName Execution abnormal.");
248 return nullptr;
249 }
250
251 bool setValue = true;
252 JSVM_Value retValue = nullptr;
253 OH_JSVM_GetBoolean(env, setValue, &retValue);
254 return retValue;
255 }
256 //The API combination is not written, which involves single step debugging and manual configuration