1 /*
2 * Copyright (c) 2022 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 "base/pt_types.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_handler.h"
19
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22
23 namespace panda::test {
24 class PtTypesTest : public testing::Test {
25 public:
SetUpTestCase()26 static void SetUpTestCase()
27 {
28 GTEST_LOG_(INFO) << "SetUpTestCase";
29 }
30
TearDownTestCase()31 static void TearDownTestCase()
32 {
33 GTEST_LOG_(INFO) << "TearDownCase";
34 }
35
SetUp()36 void SetUp() override
37 {
38 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39 }
40
TearDown()41 void TearDown() override
42 {
43 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44 }
45
46 protected:
47 EcmaVM *ecmaVm {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(PtTypesTest,BreakpointDetailsToString)52 HWTEST_F_L0(PtTypesTest, BreakpointDetailsToString)
53 {
54 BreakpointDetails input;
55 BreakpointId result = BreakpointDetails::ToString(input);
56 ASSERT_TRUE(result == "id:0:0:");
57 }
58
HWTEST_F_L0(PtTypesTest,BreakpointDetailsParseBreakpointId)59 HWTEST_F_L0(PtTypesTest, BreakpointDetailsParseBreakpointId)
60 {
61 BreakpointId input = "";
62 BreakpointDetails detail;
63 bool result = BreakpointDetails::ParseBreakpointId(input, &detail);
64 ASSERT_TRUE(!result);
65 input = "id:0";
66 result = BreakpointDetails::ParseBreakpointId(input, &detail);
67 ASSERT_TRUE(!result);
68 input = "id:0:0";
69 result = BreakpointDetails::ParseBreakpointId(input, &detail);
70 ASSERT_TRUE(!result);
71 input = "id:0:0:url";
72 result = BreakpointDetails::ParseBreakpointId(input, &detail);
73 ASSERT_TRUE(result);
74 }
75
HWTEST_F_L0(PtTypesTest,TypeNameValid)76 HWTEST_F_L0(PtTypesTest, TypeNameValid)
77 {
78 std::string type = "object";
79 bool result = RemoteObject::TypeName::Valid(type);
80 ASSERT_TRUE(result);
81 type = "function";
82 result = RemoteObject::TypeName::Valid(type);
83 ASSERT_TRUE(result);
84 type = "undefined";
85 result = RemoteObject::TypeName::Valid(type);
86 ASSERT_TRUE(result);
87 type = "string";
88 result = RemoteObject::TypeName::Valid(type);
89 ASSERT_TRUE(result);
90 type = "number";
91 result = RemoteObject::TypeName::Valid(type);
92 ASSERT_TRUE(result);
93 type = "boolean";
94 result = RemoteObject::TypeName::Valid(type);
95 ASSERT_TRUE(result);
96 type = "symbol";
97 result = RemoteObject::TypeName::Valid(type);
98 ASSERT_TRUE(result);
99 type = "bigint";
100 result = RemoteObject::TypeName::Valid(type);
101 ASSERT_TRUE(result);
102 type = "wasm";
103 result = RemoteObject::TypeName::Valid(type);
104 ASSERT_TRUE(result);
105 type = "int";
106 result = RemoteObject::TypeName::Valid(type);
107 ASSERT_TRUE(!result);
108 }
109
HWTEST_F_L0(PtTypesTest,SubTypeNameValid)110 HWTEST_F_L0(PtTypesTest, SubTypeNameValid)
111 {
112 std::string type = "array";
113 bool result = RemoteObject::SubTypeName::Valid(type);
114 ASSERT_TRUE(result);
115 type = "null";
116 result = RemoteObject::SubTypeName::Valid(type);
117 ASSERT_TRUE(result);
118 type = "node";
119 result = RemoteObject::SubTypeName::Valid(type);
120 ASSERT_TRUE(result);
121 type = "regexp";
122 result = RemoteObject::SubTypeName::Valid(type);
123 ASSERT_TRUE(result);
124 type = "map";
125 result = RemoteObject::SubTypeName::Valid(type);
126 ASSERT_TRUE(result);
127 type = "set";
128 result = RemoteObject::SubTypeName::Valid(type);
129 ASSERT_TRUE(result);
130 type = "weakmap";
131 result = RemoteObject::SubTypeName::Valid(type);
132 ASSERT_TRUE(result);
133 type = "iterator";
134 result = RemoteObject::SubTypeName::Valid(type);
135 ASSERT_TRUE(result);
136 type = "generator";
137 result = RemoteObject::SubTypeName::Valid(type);
138 ASSERT_TRUE(result);
139 type = "error";
140 result = RemoteObject::SubTypeName::Valid(type);
141 ASSERT_TRUE(result);
142 type = "proxy";
143 result = RemoteObject::SubTypeName::Valid(type);
144 ASSERT_TRUE(result);
145 type = "promise";
146 result = RemoteObject::SubTypeName::Valid(type);
147 ASSERT_TRUE(result);
148 type = "typedarray";
149 result = RemoteObject::SubTypeName::Valid(type);
150 ASSERT_TRUE(result);
151 type = "arraybuffer";
152 result = RemoteObject::SubTypeName::Valid(type);
153 ASSERT_TRUE(result);
154 type = "dataview";
155 result = RemoteObject::SubTypeName::Valid(type);
156 ASSERT_TRUE(result);
157 type = "i32";
158 result = RemoteObject::SubTypeName::Valid(type);
159 ASSERT_TRUE(result);
160 type = "i64";
161 result = RemoteObject::SubTypeName::Valid(type);
162 ASSERT_TRUE(result);
163 type = "f32";
164 result = RemoteObject::SubTypeName::Valid(type);
165 ASSERT_TRUE(result);
166 type = "f64";
167 result = RemoteObject::SubTypeName::Valid(type);
168 ASSERT_TRUE(result);
169 type = "v128";
170 result = RemoteObject::SubTypeName::Valid(type);
171 ASSERT_TRUE(result);
172 type = "externref";
173 result = RemoteObject::SubTypeName::Valid(type);
174 ASSERT_TRUE(result);
175 type = "int";
176 result = RemoteObject::SubTypeName::Valid(type);
177 ASSERT_TRUE(!result);
178 }
179
HWTEST_F_L0(PtTypesTest,ExceptionDetailsGetException)180 HWTEST_F_L0(PtTypesTest, ExceptionDetailsGetException)
181 {
182 ExceptionDetails exception;
183 RemoteObject* result = exception.GetException();
184 ASSERT_TRUE(result == nullptr);
185 }
186
HWTEST_F_L0(PtTypesTest,InternalPropertyDescriptorGetValue)187 HWTEST_F_L0(PtTypesTest, InternalPropertyDescriptorGetValue)
188 {
189 InternalPropertyDescriptor descriptor;
190 RemoteObject* result = descriptor.GetValue();
191 ASSERT_TRUE(result == nullptr);
192 }
193
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptor)194 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptor)
195 {
196 PrivatePropertyDescriptor descriptor;
197 ASSERT_TRUE(descriptor.GetName() == "");
198 }
199
HWTEST_F_L0(PtTypesTest,PropertyDescriptorGetValue)200 HWTEST_F_L0(PtTypesTest, PropertyDescriptorGetValue)
201 {
202 panda::ecmascript::tooling::PropertyDescriptor descriptor;
203 RemoteObject* result = descriptor.GetValue();
204 ASSERT_TRUE(result == nullptr);
205 result = descriptor.GetGet();
206 ASSERT_TRUE(result == nullptr);
207 result = descriptor.GetSet();
208 ASSERT_TRUE(result == nullptr);
209 result = descriptor.GetSymbol();
210 ASSERT_TRUE(result == nullptr);
211 bool res = descriptor.HasSymbol();
212 ASSERT_TRUE(!res);
213 }
214
HWTEST_F_L0(PtTypesTest,BreakLocationTypeValid)215 HWTEST_F_L0(PtTypesTest, BreakLocationTypeValid)
216 {
217 BreakLocation::Type type;
218 bool result = type.Valid("debuggerStatement");
219 ASSERT_TRUE(result);
220 result = type.Valid("call");
221 ASSERT_TRUE(result);
222 result = type.Valid("return");
223 ASSERT_TRUE(result);
224 result = type.Valid("test");
225 ASSERT_TRUE(!result);
226 }
227
HWTEST_F_L0(PtTypesTest,ScopeTypeValid)228 HWTEST_F_L0(PtTypesTest, ScopeTypeValid)
229 {
230 Scope::Type type;
231 bool result = type.Valid("global");
232 ASSERT_TRUE(result);
233 result = type.Valid("local");
234 ASSERT_TRUE(result);
235 result = type.Valid("with");
236 ASSERT_TRUE(result);
237 result = type.Valid("closure");
238 ASSERT_TRUE(result);
239 result = type.Valid("catch");
240 ASSERT_TRUE(result);
241 result = type.Valid("block");
242 ASSERT_TRUE(result);
243 result = type.Valid("script");
244 ASSERT_TRUE(result);
245 result = type.Valid("eval");
246 ASSERT_TRUE(result);
247 result = type.Valid("module");
248 ASSERT_TRUE(result);
249 result = type.Valid("wasm-expression-stack");
250 ASSERT_TRUE(result);
251 result = type.Valid("test");
252 ASSERT_TRUE(!result);
253 }
254
HWTEST_F_L0(PtTypesTest,CallFrameGetFunctionLocation)255 HWTEST_F_L0(PtTypesTest, CallFrameGetFunctionLocation)
256 {
257 CallFrame callFrame;
258 Location *location = callFrame.GetFunctionLocation();
259 ASSERT_TRUE(location == nullptr);
260 RemoteObject *result = callFrame.GetReturnValue();
261 ASSERT_TRUE(result == nullptr);
262 }
263
HWTEST_F_L0(PtTypesTest,MemoryDumpLevelOfDetailValuesValid)264 HWTEST_F_L0(PtTypesTest, MemoryDumpLevelOfDetailValuesValid)
265 {
266 bool result = MemoryDumpLevelOfDetailValues::Valid("background");
267 ASSERT_TRUE(result);
268 result = MemoryDumpLevelOfDetailValues::Valid("light");
269 ASSERT_TRUE(result);
270 result = MemoryDumpLevelOfDetailValues::Valid("detailed");
271 ASSERT_TRUE(result);
272 result = MemoryDumpLevelOfDetailValues::Valid("test");
273 ASSERT_TRUE(!result);
274 }
275
HWTEST_F_L0(PtTypesTest,TraceConfigRecordModeValuesValid)276 HWTEST_F_L0(PtTypesTest, TraceConfigRecordModeValuesValid)
277 {
278 bool result = TraceConfig::RecordModeValues::Valid("recordUntilFull");
279 ASSERT_TRUE(result);
280 result = TraceConfig::RecordModeValues::Valid("recordContinuously");
281 ASSERT_TRUE(result);
282 result = TraceConfig::RecordModeValues::Valid("recordAsMuchAsPossible");
283 ASSERT_TRUE(result);
284 result = TraceConfig::RecordModeValues::Valid("echoToConsole");
285 ASSERT_TRUE(result);
286 result = TraceConfig::RecordModeValues::Valid("test");
287 ASSERT_TRUE(!result);
288 }
289
HWTEST_F_L0(PtTypesTest,TracingBackendValues)290 HWTEST_F_L0(PtTypesTest, TracingBackendValues)
291 {
292 bool result = TracingBackendValues::Valid("auto");
293 ASSERT_TRUE(result);
294 result = TracingBackendValues::Valid("chrome");
295 ASSERT_TRUE(result);
296 result = TracingBackendValues::Valid("system");
297 ASSERT_TRUE(result);
298 result = TracingBackendValues::Valid("test");
299 ASSERT_TRUE(!result);
300 }
301
HWTEST_F_L0(PtTypesTest,TraceConfigToJsonTest)302 HWTEST_F_L0(PtTypesTest, TraceConfigToJsonTest)
303 {
304 TraceConfig treceConfig;
305 std::vector<std::string> vct {"one", "two"};
306 treceConfig.SetIncludedCategories(vct);
307 treceConfig.SetExcludedCategories(vct);
308 treceConfig.SetSyntheticDelays(vct);
309 std::unique_ptr<PtJson> result = treceConfig.ToJson();
310 ASSERT_TRUE(result);
311 }
HWTEST_F_L0(PtTypesTest,TraceConfigCreateTest)312 HWTEST_F_L0(PtTypesTest, TraceConfigCreateTest)
313 {
314 std::unique_ptr<PtJson> includedCategoriesArray = PtJson::CreateArray();
315 std::unique_ptr<PtJson> excludedCategoriesArray = PtJson::CreateArray();
316 std::unique_ptr<PtJson> syntheticDelaysArray = PtJson::CreateArray();
317 std::unique_ptr<PtJson> object = PtJson::CreateObject();
318 std::string attribute = "test";
319 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
320 ptJson->Add("recordMode", 0);
321 ptJson->Add("enableSampling", attribute.c_str());
322 ptJson->Add("enableSystrace", attribute.c_str());
323 ptJson->Add("enableArgumentFilter", attribute.c_str());
324 ptJson->Add("includedCategories", attribute.c_str());
325 ptJson->Add("excludedCategories", attribute.c_str());
326 ptJson->Add("syntheticDelays", attribute.c_str());
327 ptJson->Add("memoryDumpConfig", attribute.c_str());
328 std::unique_ptr<TraceConfig> traceConfig = TraceConfig::Create(*ptJson);
329 ASSERT_TRUE(!traceConfig);
330 object->Add("test", 0);
331 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
332 ptJson1->Add("includedCategories", includedCategoriesArray);
333 ptJson1->Add("recordMode", attribute.c_str());
334 ptJson1->Add("excludedCategories", excludedCategoriesArray);
335 ptJson1->Add("syntheticDelays", syntheticDelaysArray);
336 ptJson1->Add("memoryDumpConfig", object);
337 std::unique_ptr<TraceConfig> traceConfig1 = TraceConfig::Create(*ptJson1);
338 ASSERT_TRUE(!traceConfig1);
339 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
340 std::unique_ptr<TraceConfig> traceConfig2 = TraceConfig::Create(*ptJson2);
341 ASSERT_TRUE(traceConfig2);
342 std::unique_ptr<PtJson> ptJson3 = PtJson::CreateObject();
343 std::unique_ptr<PtJson> memoryDumpConfig = PtJson::CreateArray();
344 ptJson3->Add("memoryDumpConfig", std::move(memoryDumpConfig));
345 std::unique_ptr<TraceConfig> traceConfig3 = TraceConfig::Create(*ptJson3);
346 ASSERT_TRUE(!traceConfig3);
347 }
348
HWTEST_F_L0(PtTypesTest,ScriptTypeProfileCreateTest)349 HWTEST_F_L0(PtTypesTest, ScriptTypeProfileCreateTest)
350 {
351 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
352 std::unique_ptr<PtJson> array = PtJson::CreateArray();
353 std::unique_ptr<PtJson> object = PtJson::CreateObject();
354 object->Add("offset", "test");
355 array->Push(object);
356 ptJson->Add("entries", array);
357 std::unique_ptr<ScriptTypeProfile> profile = ScriptTypeProfile::Create(*ptJson);
358 ASSERT_TRUE(!profile);
359 }
360
HWTEST_F_L0(PtTypesTest,TypeProfileEntryCreateTest)361 HWTEST_F_L0(PtTypesTest, TypeProfileEntryCreateTest)
362 {
363 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
364 std::unique_ptr<PtJson> array = PtJson::CreateArray();
365 std::unique_ptr<PtJson> object = PtJson::CreateObject();
366 object->Add("name", 0);
367 array->Push(object);
368 ptJson->Add("types", array);
369 std::unique_ptr<TypeProfileEntry> entry = TypeProfileEntry::Create(*ptJson);
370 ASSERT_TRUE(!entry);
371 }
372
HWTEST_F_L0(PtTypesTest,ScriptCoverageCreateTest)373 HWTEST_F_L0(PtTypesTest, ScriptCoverageCreateTest)
374 {
375 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
376 std::unique_ptr<PtJson> array = PtJson::CreateArray();
377 std::unique_ptr<PtJson> object = PtJson::CreateObject();
378 object->Add("functionName", 0);
379 array->Push(object);
380 ptJson->Add("functions", array);
381 std::unique_ptr<ScriptCoverage> coverage = ScriptCoverage::Create(*ptJson);
382 ASSERT_TRUE(!coverage);
383 }
384
HWTEST_F_L0(PtTypesTest,FunctionCoverageCreateTest)385 HWTEST_F_L0(PtTypesTest, FunctionCoverageCreateTest)
386 {
387 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
388 std::unique_ptr<PtJson> array = PtJson::CreateArray();
389 std::unique_ptr<PtJson> object = PtJson::CreateObject();
390 object->Add("functionName", 0);
391 array->Push(object);
392 ptJson->Add("ranges", array);
393 std::unique_ptr<FunctionCoverage> coverage = FunctionCoverage::Create(*ptJson);
394 ASSERT_TRUE(!coverage);
395 }
396
HWTEST_F_L0(PtTypesTest,SymbolRemoteObjectTest)397 HWTEST_F_L0(PtTypesTest, SymbolRemoteObjectTest)
398 {
399 size_t size = 10;
400 const char *data = "SymbolRemoteObjectTest";
401 SymbolRemoteObject obj(ecmaVm, SymbolRef::New(ecmaVm, StringRef::NewFromUtf8(ecmaVm, (const char*)data, size)));
402 }
403
HWTEST_F_L0(PtTypesTest,DescriptionForObjectForDate)404 HWTEST_F_L0(PtTypesTest, DescriptionForObjectForDate)
405 {
406 double input = 123456789.0;
407 Local<DateRef> date = DateRef::New(ecmaVm, input);
408 std::string description = ObjectRemoteObject::DescriptionForObject(ecmaVm, date);
409 ASSERT_TRUE(description.find("GMT") != std::string::npos);
410 }
411
HWTEST_F_L0(PtTypesTest,DescriptionForObjectForPromise)412 HWTEST_F_L0(PtTypesTest, DescriptionForObjectForPromise)
413 {
414 Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(ecmaVm);
415 Local<PromiseRef> promise = capability->GetPromise(ecmaVm);
416 std::string description = ObjectRemoteObject::DescriptionForObject(ecmaVm, promise);
417 ASSERT_TRUE(description == "Promise");
418 }
419
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptorCreate)420 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptorCreate)
421 {
422 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
423 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
424 bool value = true;
425 ptJson->Add("userCode", value);
426 std::unique_ptr<PrivatePropertyDescriptor> result = PrivatePropertyDescriptor::Create(*ptJson);
427 ASSERT_TRUE(result == nullptr);
428 value = false;
429 ptJson->Add("userCode1", value);
430 result = PrivatePropertyDescriptor::Create(*ptJson);
431 ASSERT_TRUE(result == nullptr);
432 int32_t value1 = 100;
433 ptJson1->Add("test", value1);
434 std::unique_ptr<PrivatePropertyDescriptor> result1 = PrivatePropertyDescriptor::Create(*ptJson1);
435 ASSERT_TRUE(result == nullptr);
436 int64_t value2 = 0;
437 ptJson1->Add("", value2);
438 result1 = PrivatePropertyDescriptor::Create(*ptJson1);
439 ASSERT_TRUE(result == nullptr);
440 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
441 ptJson2->Add("name", value);
442 std::unique_ptr<PrivatePropertyDescriptor> result2 = PrivatePropertyDescriptor::Create(*ptJson2);
443 ASSERT_TRUE(result == nullptr);
444 ptJson2->Add("value", value1);
445 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
446 ASSERT_TRUE(result == nullptr);
447 ptJson2->Add("get", value2);
448 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
449 ASSERT_TRUE(result == nullptr);
450 value2 = 10;
451 ptJson2->Add("set", value2);
452 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
453 ASSERT_TRUE(result == nullptr);
454 std::unique_ptr<PtJson> ptJson3 = PtJson::CreateObject();
455 ptJson3->Add("name", "test");
456 ptJson3->Add("value", ptJson);
457 ptJson3->Add("get", ptJson1);
458 ptJson3->Add("set", ptJson2);
459 std::unique_ptr<PrivatePropertyDescriptor> result3 = PrivatePropertyDescriptor::Create(*ptJson3);
460 ASSERT_TRUE(result == nullptr);
461 }
462
HWTEST_F_L0(PtTypesTest,CallArgumentToJson)463 HWTEST_F_L0(PtTypesTest, CallArgumentToJson)
464 {
465 CallArgument callArgument;
466 Local<JSValueRef> name = StringRef::NewFromUtf8(ecmaVm, "name");
467 callArgument.SetValue(name);
468 RemoteObjectId objectId(10);
469 callArgument.SetObjectId(objectId);
470 UnserializableValue value("test");
471 callArgument.SetUnserializableValue(value);
472 std::unique_ptr<PtJson> ret = callArgument.ToJson();
473 ASSERT_TRUE(ret != nullptr);
474 CallArgument callArgument1;
475 RemoteObjectId objectId1(20);
476 callArgument1.SetObjectId(objectId1);
477 ret = callArgument1.ToJson();
478 ASSERT_TRUE(ret != nullptr);
479 CallArgument callArgument2;
480 UnserializableValue value1("CallArgumentToJson");
481 callArgument2.ToJson();
482 ASSERT_TRUE(ret != nullptr);
483 }
484
HWTEST_F_L0(PtTypesTest,ScriptPositionToJson)485 HWTEST_F_L0(PtTypesTest, ScriptPositionToJson)
486 {
487 ScriptPosition position;
488 int32_t line = 10;
489 position.SetLine(line);
490 int32_t column = 8;
491 position.SetColumn(column);
492 std::unique_ptr<PtJson> ret = position.ToJson();
493 ASSERT_TRUE(ret != nullptr);
494 }
495
HWTEST_F_L0(PtTypesTest,SearchMatchCreate)496 HWTEST_F_L0(PtTypesTest, SearchMatchCreate)
497 {
498 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
499 int32_t lineNumber = 10;
500 ptJson->Add("lineNumber", lineNumber);
501 std::string lineContent = "12";
502 ptJson->Add("lineContent", lineContent.c_str());
503 std::unique_ptr<SearchMatch> ret = SearchMatch::Create(*ptJson);
504 ASSERT_TRUE(ret != nullptr);
505
506 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
507 std::string attribute = "32";
508 ptJson1->Add("lineNumber", attribute.c_str());
509 std::unique_ptr<SearchMatch> ret1 = SearchMatch::Create(*ptJson1);
510 ASSERT_TRUE(ret1 == nullptr);
511
512 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
513 int32_t lineNumber2 = 14;
514 std::string lineContent2 = "12";
515 ptJson1->Add("lineNumber1", lineNumber2);
516 ptJson1->Add("lineContent2", lineContent2.c_str());
517 std::unique_ptr<SearchMatch> ret2 = SearchMatch::Create(*ptJson1);
518 ASSERT_TRUE(ret2 == nullptr);
519 }
520
HWTEST_F_L0(PtTypesTest,LocationRangeCreate)521 HWTEST_F_L0(PtTypesTest, LocationRangeCreate)
522 {
523 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
524 std::unique_ptr<PtJson> start = PtJson::CreateObject();
525 std::unique_ptr<PtJson> end = PtJson::CreateObject();
526 std::string scriptId = "12";
527 int32_t lineNumber = 1;
528 int32_t columnNumber = 2;
529 start->Add("lineNumber", lineNumber);
530 start->Add("columnNumber", columnNumber);
531 end->Add("lineNumber", lineNumber + 1);
532 end->Add("columnNumber", columnNumber + 2);
533 ptJson->Add("scriptId", scriptId.c_str());
534 ptJson->Add("start", start);
535 ptJson->Add("end", end);
536 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
537 ASSERT_TRUE(ret != nullptr);
538 }
539
HWTEST_F_L0(PtTypesTest,LocationRangeCreateSwitch)540 HWTEST_F_L0(PtTypesTest, LocationRangeCreateSwitch)
541 {
542 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
543 std::unique_ptr<PtJson> start = PtJson::CreateObject();
544 std::unique_ptr<PtJson> end = PtJson::CreateObject();
545 std::string scriptId = "12";
546 int32_t lineNumber = 1;
547 int32_t columnNumber = 2;
548 start->Add("lineNumber1", lineNumber);
549 start->Add("columnNumber1", columnNumber);
550 end->Add("lineNumber1", lineNumber + 1);
551 end->Add("columnNumber1", columnNumber + 2);
552 ptJson->Add("scriptId1", scriptId.c_str());
553 ptJson->Add("start", start);
554 ptJson->Add("end", end);
555 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
556 ASSERT_TRUE(ret == nullptr);
557 }
558
HWTEST_F_L0(PtTypesTest,LocationRangeCreateSwitchFail)559 HWTEST_F_L0(PtTypesTest, LocationRangeCreateSwitchFail)
560 {
561 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
562 std::unique_ptr<PtJson> start = PtJson::CreateObject();
563 std::unique_ptr<PtJson> end = PtJson::CreateObject();
564 std::string scriptId = "12";
565 int32_t lineNumber = 1;
566 int32_t columnNumber = 2;
567 start->Add("lineNumber1", lineNumber);
568 start->Add("columnNumber1", columnNumber);
569 end->Add("lineNumber1", lineNumber + 1);
570 end->Add("columnNumber1", columnNumber + 2);
571 ptJson->Add("scriptId1", scriptId.c_str());
572 ptJson->Add("start1", start);
573 ptJson->Add("end1", end);
574 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
575 ASSERT_TRUE(ret == nullptr);
576 }
577
HWTEST_F_L0(PtTypesTest,RuntimeCallFrameCreate)578 HWTEST_F_L0(PtTypesTest, RuntimeCallFrameCreate)
579 {
580 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
581 std::string functionName = "RuntimeCallFrameCreate";
582 std::string scriptId = "scriptId";
583 std::string url = "url";
584 int32_t lineNumber = 2;
585 int32_t columnNumber = 10;
586 ptJson->Add("functionName", functionName.c_str());
587 ptJson->Add("moduleName", functionName.c_str());
588 ptJson->Add("scriptId", scriptId.c_str());
589 ptJson->Add("url", url.c_str());
590 ptJson->Add("lineNumber", lineNumber);
591 ptJson->Add("columnNumber", columnNumber);
592 std::unique_ptr<RuntimeCallFrame> ret = RuntimeCallFrame::Create(*ptJson);
593 ASSERT_TRUE(ret != nullptr);
594 }
595
HWTEST_F_L0(PtTypesTest,RuntimeCallFrameCreateFailSwitch)596 HWTEST_F_L0(PtTypesTest, RuntimeCallFrameCreateFailSwitch)
597 {
598 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
599 std::string functionName = "RuntimeCallFrameCreate";
600 std::string scriptId = "scriptId";
601 std::string url = "url";
602 int32_t lineNumber = 2;
603 int32_t columnNumber = 10;
604 ptJson->Add("functionName1", functionName.c_str());
605 ptJson->Add("moduleName1", functionName.c_str());
606 ptJson->Add("scriptId1", scriptId.c_str());
607 ptJson->Add("url1", url.c_str());
608 ptJson->Add("lineNumber1", lineNumber);
609 ptJson->Add("columnNumber1", columnNumber);
610 std::unique_ptr<RuntimeCallFrame> ret = RuntimeCallFrame::Create(*ptJson);
611 ASSERT_TRUE(ret == nullptr);
612 }
613
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileNodeCreate)614 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileNodeCreate)
615 {
616 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
617 std::unique_ptr<PtJson> callFrame = PtJson::CreateObject();
618 std::unique_ptr<PtJson> array = PtJson::CreateArray();
619 std::unique_ptr<PtJson> object = PtJson::CreateObject();
620 callFrame->Add("callFrame", "test");
621 ptJson->Add("callFrame", callFrame);
622 object->Add("test", 0);
623 array->Push(object);
624 ptJson->Add("children", array);
625 std::unique_ptr<SamplingHeapProfileNode> ret = SamplingHeapProfileNode::Create(*ptJson);
626 ASSERT_TRUE(ret == nullptr);
627 }
628
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileCreate)629 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileCreate)
630 {
631 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
632 std::unique_ptr<PtJson> head = PtJson::CreateObject();
633 std::unique_ptr<PtJson> array = PtJson::CreateArray();
634 std::unique_ptr<PtJson> object = PtJson::CreateObject();
635 head->Add("head", "headTest");
636 ptJson->Add("head", head);
637 object->Add("test", 0);
638 array->Push(object);
639 ptJson->Add("samples", array);
640 std::unique_ptr<SamplingHeapProfile> ret = SamplingHeapProfile::Create(*ptJson);
641 ASSERT_TRUE(ret == nullptr);
642 }
643
HWTEST_F_L0(PtTypesTest,ExceptionDetailsCreate)644 HWTEST_F_L0(PtTypesTest, ExceptionDetailsCreate)
645 {
646 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
647 std::unique_ptr<PtJson> exception = PtJson::CreateObject();
648 exception->Add("exception", "test");
649 ptJson->Add("exception", exception);
650 std::unique_ptr<ExceptionDetails> ret = ExceptionDetails::Create(*ptJson);
651 ASSERT_TRUE(ret == nullptr);
652 }
653
654
HWTEST_F_L0(PtTypesTest,ProfileToJsonTest)655 HWTEST_F_L0(PtTypesTest, ProfileToJsonTest)
656 {
657 Profile profile;
658 std::vector<int32_t> samples {1, 2, 3}; // 1:element 2:element 3:element
659 std::vector<int32_t> timeDeltas {1, 2, 3}; // 1:element 2:element 3:element
660 profile.SetSamples(samples);
661 profile.SetTimeDeltas(timeDeltas);
662 std::unique_ptr<PtJson> result = profile.ToJson();
663 ASSERT_TRUE(result);
664 }
665
HWTEST_F_L0(PtTypesTest,ProfileFromProfileInfoTest)666 HWTEST_F_L0(PtTypesTest, ProfileFromProfileInfoTest)
667 {
668 ProfileInfo info;
669 info.samples.push_back(1);
670 info.samples.push_back(2);
671 info.timeDeltas.push_back(1);
672 info.timeDeltas.push_back(2);
673 std::unique_ptr<Profile> result = Profile::FromProfileInfo(info);
674 ASSERT_TRUE(result);
675 }
676
HWTEST_F_L0(PtTypesTest,ProfileNodeToJsonTest)677 HWTEST_F_L0(PtTypesTest, ProfileNodeToJsonTest)
678 {
679 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
680 ProfileNode node;
681 std::vector<int32_t> children {0, 1, 2, 3}; // 1: element 2:element, 3:element
682 std::unique_ptr<PositionTickInfo> positionTickInfo = std::make_unique<PositionTickInfo>();
683 std::unique_ptr<RuntimeCallFrame> callFrame = std::make_unique<RuntimeCallFrame>();
684 positionTickInfo->SetLine(123);
685 positionTickInfo->SetTicks(123);
686 std::vector<std::unique_ptr<PositionTickInfo>> positionTicks;
687 positionTicks.push_back(std::move(positionTickInfo));
688 node.SetChildren(children);
689 node.SetPositionTicks(std::move(positionTicks));
690 node.SetCallFrame(std::move(callFrame));
691 std::unique_ptr<PtJson> result = node.ToJson();
692 ASSERT_TRUE(result);
693 ProfileNode node1;
694 std::unique_ptr<RuntimeCallFrame> callFrame1 = std::make_unique<RuntimeCallFrame>();
695 node1.SetCallFrame(std::move(callFrame1));
696 std::unique_ptr<PtJson> result1 = node1.ToJson();
697 ASSERT_TRUE(result1);
698 }
699
HWTEST_F_L0(PtTypesTest,ProfileNodeFromCpuProfileNodeTest)700 HWTEST_F_L0(PtTypesTest, ProfileNodeFromCpuProfileNodeTest)
701 {
702 CpuProfileNode cpuProfileNode;
703 cpuProfileNode.children.push_back(0);
704 cpuProfileNode.children.push_back(1); // 1: element
705 std::unique_ptr<ProfileNode> result = ProfileNode::FromCpuProfileNode(cpuProfileNode);
706 ASSERT_TRUE(result);
707 }
708
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptorToJsonTest)709 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptorToJsonTest)
710 {
711 PrivatePropertyDescriptor descriptor;
712 std::unique_ptr<RemoteObject> value = std::make_unique<RemoteObject>();
713 std::unique_ptr<RemoteObject> get = std::make_unique<RemoteObject>();
714 std::unique_ptr<RemoteObject> set = std::make_unique<RemoteObject>();
715 descriptor.SetValue(std::move(value));
716 descriptor.SetGet(std::move(get));
717 descriptor.SetSet(std::move(set));
718 std::unique_ptr<PtJson> result = descriptor.ToJson();
719 ASSERT_TRUE(result);
720 }
721
HWTEST_F_L0(PtTypesTest,PropertyDescriptorFromPropertyTest)722 HWTEST_F_L0(PtTypesTest, PropertyDescriptorFromPropertyTest)
723 {
724 Local<SymbolRef> symbol = SymbolRef::New(ecmaVm, StringRef::NewFromUtf8(ecmaVm, "test"));
725 PropertyAttribute property;
726 property.SetGetter(StringRef::NewFromUtf8(ecmaVm, "test"));
727 property.SetSetter(StringRef::NewFromUtf8(ecmaVm, "test"));
728 std::unique_ptr<panda::ecmascript::tooling::PropertyDescriptor> result =
729 panda::ecmascript::tooling::PropertyDescriptor::FromProperty(ecmaVm, symbol, property);
730 ASSERT_TRUE(result);
731 }
732
HWTEST_F_L0(PtTypesTest,PtEventsGetReasonString)733 HWTEST_F_L0(PtTypesTest, PtEventsGetReasonString)
734 {
735 PauseReason reason = static_cast<PauseReason>(50);
736 std::string ret = Paused::GetReasonString(reason);
737 ASSERT_TRUE(ret.empty());
738 }
739
HWTEST_F_L0(PtTypesTest,LocationRangeToJsonTest)740 HWTEST_F_L0(PtTypesTest, LocationRangeToJsonTest)
741 {
742 LocationRange range;
743 std::unique_ptr<ScriptPosition> start = std::make_unique<ScriptPosition>();
744 std::unique_ptr<ScriptPosition> end = std::make_unique<ScriptPosition>();
745 range.SetStart(std::move(start));
746 range.SetEnd(std::move(end));
747 std::unique_ptr<PtJson> result = range.ToJson();
748 ASSERT_TRUE(result);
749 }
750
HWTEST_F_L0(PtTypesTest,BreakLocationToJsonTest)751 HWTEST_F_L0(PtTypesTest, BreakLocationToJsonTest)
752 {
753 BreakLocation location;
754 location.SetColumn(0);
755 location.SetType("test");
756 std::unique_ptr<PtJson> result = location.ToJson();
757 ASSERT_TRUE(result);
758 }
759
HWTEST_F_L0(PtTypesTest,CallFrameCreateTest)760 HWTEST_F_L0(PtTypesTest, CallFrameCreateTest)
761 {
762 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
763 std::unique_ptr<PtJson> array = PtJson::CreateArray();
764 std::unique_ptr<PtJson> object = PtJson::CreateObject();
765 object->Add("type", 0);
766 array->Push(object);
767 ptJson->Add("scopeChain", array);
768 std::unique_ptr<CallFrame> result = CallFrame::Create(*ptJson);
769 ASSERT_TRUE(!result);
770 }
771
HWTEST_F_L0(PtTypesTest,ProfileCreate)772 HWTEST_F_L0(PtTypesTest, ProfileCreate)
773 {
774 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
775 std::unique_ptr<PtJson> array = PtJson::CreateArray();
776 std::unique_ptr<PtJson> object = PtJson::CreateObject();
777 std::unique_ptr<PtJson> object1 = PtJson::CreateObject();
778 object->Add("type", 0);
779 array->Push(object);
780 ptJson->Add("nodes", array);
781 std::unique_ptr<Profile> result = Profile::Create(*ptJson);
782 ASSERT_TRUE(!result);
783 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
784 ptJson1->Add("samples", 0);
785 ptJson1->Add("timeDeltas", 0);
786 std::unique_ptr<Profile> result1 = Profile::Create(*ptJson1);
787 ASSERT_TRUE(!result1);
788 }
789
HWTEST_F_L0(PtTypesTest,ProfileNodeCreate)790 HWTEST_F_L0(PtTypesTest, ProfileNodeCreate)
791 {
792 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
793 std::string attribute = "test";
794 std::unique_ptr<PtJson> object = PtJson::CreateObject();
795 object->Add("functionName", 0);
796 ptJson->Add("callFrame", object);
797 ptJson->Add("hitCount", attribute.c_str());
798 ptJson->Add("children", attribute.c_str());
799 ptJson->Add("positionTicks", attribute.c_str());
800 ptJson->Add("deoptReason", 0);
801 std::unique_ptr<ProfileNode> result = ProfileNode::Create(*ptJson);
802 ASSERT_TRUE(!result);
803 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
804 std::unique_ptr<PtJson> object1 = PtJson::CreateObject();
805 std::unique_ptr<PtJson> array = PtJson::CreateArray();
806 object1->Add("attribute", attribute.c_str());
807 array->Push(object1);
808 ptJson1->Add("positionTicks", array);
809 std::unique_ptr<ProfileNode> result1 = ProfileNode::Create(*ptJson1);
810 ASSERT_TRUE(!result1);
811 }
812
HWTEST_F_L0(PtTypesTest,LocationCreate)813 HWTEST_F_L0(PtTypesTest, LocationCreate)
814 {
815 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
816 std::string attribute = "test";
817 ptJson->Add("columnNumber", attribute.c_str());
818 std::unique_ptr<Location> result = Location::Create(*ptJson);
819 ASSERT_TRUE(!result);
820 }
821
HWTEST_F_L0(PtTypesTest,PropertyDescriptorCreate)822 HWTEST_F_L0(PtTypesTest, PropertyDescriptorCreate)
823 {
824 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
825 std::unique_ptr<PtJson> get = PtJson::CreateObject();
826 std::unique_ptr<PtJson> set = PtJson::CreateObject();
827 std::unique_ptr<PtJson> symbol = PtJson::CreateObject();
828 std::string attribute = "test";
829 get->Add("type", attribute.c_str());
830 set->Add("type", attribute.c_str());
831 symbol->Add("type", attribute.c_str());
832 ptJson->Add("get", get);
833 ptJson->Add("set", set);
834 ptJson->Add("symbol", symbol);
835 std::unique_ptr<tooling::PropertyDescriptor> result = tooling::PropertyDescriptor::Create(*ptJson);
836 ASSERT_TRUE(!result);
837 }
838
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileFromSamplingInfo)839 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileFromSamplingInfo)
840 {
841 SamplingInfo samplingInfo;
842 samplingInfo.head_.selfSize_ = 0;
843 samplingInfo.head_.id_ = 0;
844 samplingInfo.head_.callFrameInfo_.codeType_ = "codetype";
845 samplingInfo.head_.callFrameInfo_.columnNumber_ = 21;
846 samplingInfo.head_.callFrameInfo_.functionName_ = "TestSampling";
847 samplingInfo.head_.callFrameInfo_.lineNumber_ = 221;
848 samplingInfo.head_.callFrameInfo_.scriptId_ = 1;
849 samplingInfo.head_.callFrameInfo_.url_ = "url";
850 Sample sampleInfo(12024, 19, 1, 2);
851 samplingInfo.samples_.push_back(sampleInfo);
852 std::unique_ptr<SamplingHeapProfile> profile = SamplingHeapProfile::FromSamplingInfo(&samplingInfo);
853 ASSERT_TRUE(profile != nullptr);
854 SamplingHeapProfileNode *nodeInfo = profile->GetHead();
855 int32_t selfSize = nodeInfo->GetSelfSize();
856 ASSERT_TRUE(selfSize == samplingInfo.head_.selfSize_);
857 int32_t id = nodeInfo->GetId();
858 ASSERT_TRUE(id == samplingInfo.head_.id_);
859 RuntimeCallFrame *callFrame = nodeInfo->GetCallFrame();
860 const std::string functionName = callFrame->GetFunctionName();
861 ASSERT_TRUE(functionName == samplingInfo.head_.callFrameInfo_.functionName_);
862 const std::string url = callFrame->GetUrl();
863 ASSERT_TRUE(url == samplingInfo.head_.callFrameInfo_.url_);
864 int32_t lineNumber = callFrame->GetLineNumber();
865 ASSERT_TRUE(lineNumber == samplingInfo.head_.callFrameInfo_.lineNumber_);
866 int32_t columnNumber = callFrame->GetColumnNumber();
867 ASSERT_TRUE(columnNumber == samplingInfo.head_.callFrameInfo_.columnNumber_);
868 const std::vector<std::unique_ptr<SamplingHeapProfileSample>> *sampData = profile->GetSamples();
869 int32_t nodeId = sampData->data()->get()->GetNodeId();
870 ASSERT_TRUE(nodeId == sampleInfo.nodeId_);
871 int32_t size = sampData->data()->get()->GetSize();
872 ASSERT_TRUE(size == (sampleInfo.size_ * sampleInfo.count_));
873 int64_t ordinal = sampData->data()->get()->GetOrdinal();
874 ASSERT_TRUE(ordinal == sampleInfo.ordinal_);
875 }
876
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileTransferHead)877 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileTransferHead)
878 {
879 SamplingNode allocationNode;
880 allocationNode.selfSize_ = 0;
881 allocationNode.id_ = 0;
882 allocationNode.callFrameInfo_.codeType_ = "codetype";
883 allocationNode.callFrameInfo_.columnNumber_ = 21;
884 allocationNode.callFrameInfo_.functionName_ = "TestSampling";
885 allocationNode.callFrameInfo_.lineNumber_ = 221;
886 allocationNode.callFrameInfo_.scriptId_ = 1;
887 allocationNode.callFrameInfo_.url_ = "url";
888 std::unique_ptr<SamplingHeapProfileNode> headInfo = SamplingHeapProfile::TransferHead(&allocationNode);
889 ASSERT_TRUE(headInfo != nullptr);
890 int32_t selfSize = headInfo->GetSelfSize();
891 ASSERT_TRUE(selfSize == allocationNode.selfSize_);
892 int32_t id = headInfo->GetId();
893 ASSERT_TRUE(id == allocationNode.id_);
894 RuntimeCallFrame *callFrame = headInfo->GetCallFrame();
895 const std::string functionName = callFrame->GetFunctionName();
896 ASSERT_TRUE(functionName == allocationNode.callFrameInfo_.functionName_);
897 const std::string url = callFrame->GetUrl();
898 ASSERT_TRUE(url == allocationNode.callFrameInfo_.url_);
899 int32_t lineNumber = callFrame->GetLineNumber();
900 ASSERT_TRUE(lineNumber == allocationNode.callFrameInfo_.lineNumber_);
901 int32_t columnNumber = callFrame->GetColumnNumber();
902 ASSERT_TRUE(columnNumber == allocationNode.callFrameInfo_.columnNumber_);
903 }
904 } // namespace panda::test
905