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 "tooling/dynamic/base/pt_types.h"
17 #include "common_components/base/utf_helper.h"
18 #include "ecmascript/js_regexp.h"
19 #include "ecmascript/object_factory.h"
20
21 #include "ecmascript/tests/test_helper.h"
22 #include "protocol_handler.h"
23
24 using namespace panda::ecmascript;
25 using namespace panda::ecmascript::tooling;
26
27 namespace panda::test {
28 class PtTypesTest : public testing::Test {
29 public:
SetUpTestCase()30 static void SetUpTestCase()
31 {
32 GTEST_LOG_(INFO) << "SetUpTestCase";
33 }
34
TearDownTestCase()35 static void TearDownTestCase()
36 {
37 GTEST_LOG_(INFO) << "TearDownCase";
38 }
39
SetUp()40 void SetUp() override
41 {
42 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
43 }
44
TearDown()45 void TearDown() override
46 {
47 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
48 }
49
50 protected:
51 EcmaVM *ecmaVm {nullptr};
52 EcmaHandleScope *scope {nullptr};
53 JSThread *thread {nullptr};
54 };
55
HWTEST_F_L0(PtTypesTest,BreakpointDetailsToString)56 HWTEST_F_L0(PtTypesTest, BreakpointDetailsToString)
57 {
58 BreakpointDetails input;
59 BreakpointId result = BreakpointDetails::ToString(input);
60 ASSERT_TRUE(result == "id:0:0:");
61 }
62
HWTEST_F_L0(PtTypesTest,BreakpointDetailsParseBreakpointId)63 HWTEST_F_L0(PtTypesTest, BreakpointDetailsParseBreakpointId)
64 {
65 BreakpointId input = "";
66 BreakpointDetails detail;
67 bool result = BreakpointDetails::ParseBreakpointId(input, &detail);
68 ASSERT_TRUE(!result);
69 input = "id:0";
70 result = BreakpointDetails::ParseBreakpointId(input, &detail);
71 ASSERT_TRUE(!result);
72 input = "id:0:0";
73 result = BreakpointDetails::ParseBreakpointId(input, &detail);
74 ASSERT_TRUE(!result);
75 input = "id:0:0:url";
76 result = BreakpointDetails::ParseBreakpointId(input, &detail);
77 ASSERT_TRUE(result);
78 }
79
HWTEST_F_L0(PtTypesTest,TypeNameValid)80 HWTEST_F_L0(PtTypesTest, TypeNameValid)
81 {
82 std::string type = "object";
83 bool result = RemoteObject::TypeName::Valid(type);
84 ASSERT_TRUE(result);
85 type = "function";
86 result = RemoteObject::TypeName::Valid(type);
87 ASSERT_TRUE(result);
88 type = "undefined";
89 result = RemoteObject::TypeName::Valid(type);
90 ASSERT_TRUE(result);
91 type = "string";
92 result = RemoteObject::TypeName::Valid(type);
93 ASSERT_TRUE(result);
94 type = "number";
95 result = RemoteObject::TypeName::Valid(type);
96 ASSERT_TRUE(result);
97 type = "boolean";
98 result = RemoteObject::TypeName::Valid(type);
99 ASSERT_TRUE(result);
100 type = "symbol";
101 result = RemoteObject::TypeName::Valid(type);
102 ASSERT_TRUE(result);
103 type = "bigint";
104 result = RemoteObject::TypeName::Valid(type);
105 ASSERT_TRUE(result);
106 type = "wasm";
107 result = RemoteObject::TypeName::Valid(type);
108 ASSERT_TRUE(result);
109 type = "int";
110 result = RemoteObject::TypeName::Valid(type);
111 ASSERT_TRUE(!result);
112 }
113
HWTEST_F_L0(PtTypesTest,SubTypeNameValid)114 HWTEST_F_L0(PtTypesTest, SubTypeNameValid)
115 {
116 std::string type = "array";
117 bool result = RemoteObject::SubTypeName::Valid(type);
118 ASSERT_TRUE(result);
119 type = "null";
120 result = RemoteObject::SubTypeName::Valid(type);
121 ASSERT_TRUE(result);
122 type = "node";
123 result = RemoteObject::SubTypeName::Valid(type);
124 ASSERT_TRUE(result);
125 type = "regexp";
126 result = RemoteObject::SubTypeName::Valid(type);
127 ASSERT_TRUE(result);
128 type = "map";
129 result = RemoteObject::SubTypeName::Valid(type);
130 ASSERT_TRUE(result);
131 type = "set";
132 result = RemoteObject::SubTypeName::Valid(type);
133 ASSERT_TRUE(result);
134 type = "weakmap";
135 result = RemoteObject::SubTypeName::Valid(type);
136 ASSERT_TRUE(result);
137 type = "iterator";
138 result = RemoteObject::SubTypeName::Valid(type);
139 ASSERT_TRUE(result);
140 type = "generator";
141 result = RemoteObject::SubTypeName::Valid(type);
142 ASSERT_TRUE(result);
143 type = "error";
144 result = RemoteObject::SubTypeName::Valid(type);
145 ASSERT_TRUE(result);
146 type = "proxy";
147 result = RemoteObject::SubTypeName::Valid(type);
148 ASSERT_TRUE(result);
149 type = "promise";
150 result = RemoteObject::SubTypeName::Valid(type);
151 ASSERT_TRUE(result);
152 type = "typedarray";
153 result = RemoteObject::SubTypeName::Valid(type);
154 ASSERT_TRUE(result);
155 type = "arraybuffer";
156 result = RemoteObject::SubTypeName::Valid(type);
157 ASSERT_TRUE(result);
158 type = "dataview";
159 result = RemoteObject::SubTypeName::Valid(type);
160 ASSERT_TRUE(result);
161 type = "i32";
162 result = RemoteObject::SubTypeName::Valid(type);
163 ASSERT_TRUE(result);
164 type = "i64";
165 result = RemoteObject::SubTypeName::Valid(type);
166 ASSERT_TRUE(result);
167 type = "f32";
168 result = RemoteObject::SubTypeName::Valid(type);
169 ASSERT_TRUE(result);
170 type = "f64";
171 result = RemoteObject::SubTypeName::Valid(type);
172 ASSERT_TRUE(result);
173 type = "v128";
174 result = RemoteObject::SubTypeName::Valid(type);
175 ASSERT_TRUE(result);
176 type = "externref";
177 result = RemoteObject::SubTypeName::Valid(type);
178 ASSERT_TRUE(result);
179 type = "int";
180 result = RemoteObject::SubTypeName::Valid(type);
181 ASSERT_TRUE(!result);
182 }
183
HWTEST_F_L0(PtTypesTest,ExceptionDetailsGetException)184 HWTEST_F_L0(PtTypesTest, ExceptionDetailsGetException)
185 {
186 ExceptionDetails exception;
187 RemoteObject* result = exception.GetException();
188 ASSERT_TRUE(result == nullptr);
189 }
190
HWTEST_F_L0(PtTypesTest,InternalPropertyDescriptorGetValue)191 HWTEST_F_L0(PtTypesTest, InternalPropertyDescriptorGetValue)
192 {
193 InternalPropertyDescriptor descriptor;
194 RemoteObject* result = descriptor.GetValue();
195 ASSERT_TRUE(result == nullptr);
196 }
197
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptor)198 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptor)
199 {
200 PrivatePropertyDescriptor descriptor;
201 ASSERT_TRUE(descriptor.GetName() == "");
202 }
203
HWTEST_F_L0(PtTypesTest,PropertyDescriptorGetValue)204 HWTEST_F_L0(PtTypesTest, PropertyDescriptorGetValue)
205 {
206 panda::ecmascript::tooling::PropertyDescriptor descriptor;
207 RemoteObject* result = descriptor.GetValue();
208 ASSERT_TRUE(result == nullptr);
209 result = descriptor.GetGet();
210 ASSERT_TRUE(result == nullptr);
211 result = descriptor.GetSet();
212 ASSERT_TRUE(result == nullptr);
213 result = descriptor.GetSymbol();
214 ASSERT_TRUE(result == nullptr);
215 bool res = descriptor.HasSymbol();
216 ASSERT_TRUE(!res);
217 }
218
HWTEST_F_L0(PtTypesTest,BreakLocationTypeValid)219 HWTEST_F_L0(PtTypesTest, BreakLocationTypeValid)
220 {
221 BreakLocation::Type type;
222 bool result = type.Valid("debuggerStatement");
223 ASSERT_TRUE(result);
224 result = type.Valid("call");
225 ASSERT_TRUE(result);
226 result = type.Valid("return");
227 ASSERT_TRUE(result);
228 result = type.Valid("test");
229 ASSERT_TRUE(!result);
230 }
231
HWTEST_F_L0(PtTypesTest,ScopeTypeValid)232 HWTEST_F_L0(PtTypesTest, ScopeTypeValid)
233 {
234 Scope::Type type;
235 bool result = type.Valid("global");
236 ASSERT_TRUE(result);
237 result = type.Valid("local");
238 ASSERT_TRUE(result);
239 result = type.Valid("with");
240 ASSERT_TRUE(result);
241 result = type.Valid("closure");
242 ASSERT_TRUE(result);
243 result = type.Valid("catch");
244 ASSERT_TRUE(result);
245 result = type.Valid("block");
246 ASSERT_TRUE(result);
247 result = type.Valid("script");
248 ASSERT_TRUE(result);
249 result = type.Valid("eval");
250 ASSERT_TRUE(result);
251 result = type.Valid("module");
252 ASSERT_TRUE(result);
253 result = type.Valid("wasm-expression-stack");
254 ASSERT_TRUE(result);
255 result = type.Valid("test");
256 ASSERT_TRUE(!result);
257 }
258
HWTEST_F_L0(PtTypesTest,CallFrameGetFunctionLocation)259 HWTEST_F_L0(PtTypesTest, CallFrameGetFunctionLocation)
260 {
261 CallFrame callFrame;
262 Location *location = callFrame.GetFunctionLocation();
263 ASSERT_TRUE(location == nullptr);
264 RemoteObject *result = callFrame.GetReturnValue();
265 ASSERT_TRUE(result == nullptr);
266 }
267
HWTEST_F_L0(PtTypesTest,MemoryDumpLevelOfDetailValuesValid)268 HWTEST_F_L0(PtTypesTest, MemoryDumpLevelOfDetailValuesValid)
269 {
270 bool result = MemoryDumpLevelOfDetailValues::Valid("background");
271 ASSERT_TRUE(result);
272 result = MemoryDumpLevelOfDetailValues::Valid("light");
273 ASSERT_TRUE(result);
274 result = MemoryDumpLevelOfDetailValues::Valid("detailed");
275 ASSERT_TRUE(result);
276 result = MemoryDumpLevelOfDetailValues::Valid("test");
277 ASSERT_TRUE(!result);
278 }
279
HWTEST_F_L0(PtTypesTest,TraceConfigRecordModeValuesValid)280 HWTEST_F_L0(PtTypesTest, TraceConfigRecordModeValuesValid)
281 {
282 bool result = TraceConfig::RecordModeValues::Valid("recordUntilFull");
283 ASSERT_TRUE(result);
284 result = TraceConfig::RecordModeValues::Valid("recordContinuously");
285 ASSERT_TRUE(result);
286 result = TraceConfig::RecordModeValues::Valid("recordAsMuchAsPossible");
287 ASSERT_TRUE(result);
288 result = TraceConfig::RecordModeValues::Valid("echoToConsole");
289 ASSERT_TRUE(result);
290 result = TraceConfig::RecordModeValues::Valid("test");
291 ASSERT_TRUE(!result);
292 }
293
HWTEST_F_L0(PtTypesTest,TracingBackendValues)294 HWTEST_F_L0(PtTypesTest, TracingBackendValues)
295 {
296 bool result = TracingBackendValues::Valid("auto");
297 ASSERT_TRUE(result);
298 result = TracingBackendValues::Valid("chrome");
299 ASSERT_TRUE(result);
300 result = TracingBackendValues::Valid("system");
301 ASSERT_TRUE(result);
302 result = TracingBackendValues::Valid("test");
303 ASSERT_TRUE(!result);
304 }
305
HWTEST_F_L0(PtTypesTest,TraceConfigToJsonTest)306 HWTEST_F_L0(PtTypesTest, TraceConfigToJsonTest)
307 {
308 TraceConfig treceConfig;
309 std::vector<std::string> vct {"one", "two"};
310 treceConfig.SetIncludedCategories(vct);
311 treceConfig.SetExcludedCategories(vct);
312 treceConfig.SetSyntheticDelays(vct);
313 std::unique_ptr<PtJson> result = treceConfig.ToJson();
314 ASSERT_TRUE(result);
315 }
316
HWTEST_F_L0(PtTypesTest,TraceConfigCreateTest)317 HWTEST_F_L0(PtTypesTest, TraceConfigCreateTest)
318 {
319 std::string attribute = "test";
320 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
321 ptJson->Add("recordMode", 0);
322 ptJson->Add("enableSampling", attribute.c_str());
323 ptJson->Add("enableSystrace", attribute.c_str());
324 ptJson->Add("enableArgumentFilter", attribute.c_str());
325 ptJson->Add("includedCategories", attribute.c_str());
326 ptJson->Add("excludedCategories", attribute.c_str());
327 ptJson->Add("syntheticDelays", attribute.c_str());
328 ptJson->Add("memoryDumpConfig", attribute.c_str());
329 std::unique_ptr<TraceConfig> traceConfig = TraceConfig::Create(*ptJson);
330 ASSERT_TRUE(!traceConfig);
331
332 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
333 ptJson1->Add("recordMode", "test");
334 std::unique_ptr<TraceConfig> traceConfig1 = TraceConfig::Create(*ptJson1);
335 ASSERT_TRUE(!traceConfig1);
336
337 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
338 std::unique_ptr<TraceConfig> traceConfig2 = TraceConfig::Create(*ptJson2);
339 ASSERT_TRUE(traceConfig2);
340
341 std::unique_ptr<PtJson> includedCategoriesArray = PtJson::CreateArray();
342 includedCategoriesArray->Push("includedCategory");
343 std::unique_ptr<PtJson> excludedCategoriesArray = PtJson::CreateArray();
344 excludedCategoriesArray->Push("excludedCategory1");
345 excludedCategoriesArray->Push("excludedCategory2");
346 std::unique_ptr<PtJson> syntheticDelaysArray = PtJson::CreateArray();
347 syntheticDelaysArray->Push("syntheticDelay1");
348 syntheticDelaysArray->Push("syntheticDelay2");
349 syntheticDelaysArray->Push("syntheticDelay3");
350 std::unique_ptr<PtJson> ptJson3 = PtJson::CreateObject();
351 ptJson3->Add("recordMode", "recordUntilFull");
352 ptJson3->Add("enableSampling", true);
353 ptJson3->Add("enableSystrace", true);
354 ptJson3->Add("enableArgumentFilter", true);
355 ptJson3->Add("includedCategories", includedCategoriesArray);
356 ptJson3->Add("excludedCategories", excludedCategoriesArray);
357 ptJson3->Add("syntheticDelays", syntheticDelaysArray);
358 ptJson3->Add("memoryDumpConfig", PtJson::CreateObject());
359 std::unique_ptr<TraceConfig> traceConfig3 = TraceConfig::Create(*ptJson3);
360 ASSERT_TRUE(traceConfig3);
361 ASSERT_TRUE(traceConfig3->GetEnableSampling());
362 ASSERT_TRUE(traceConfig3->GetEnableSystrace());
363 ASSERT_TRUE(traceConfig3->GetEnableArgumentFilter());
364 ASSERT_EQ(traceConfig3->GetIncludedCategories()->size(), 1);
365 ASSERT_EQ(traceConfig3->GetExcludedCategories()->size(), 2);
366 ASSERT_EQ(traceConfig3->GetSyntheticDelays()->size(), 3);
367 }
368
HWTEST_F_L0(PtTypesTest,ScriptTypeProfileCreateTest)369 HWTEST_F_L0(PtTypesTest, ScriptTypeProfileCreateTest)
370 {
371 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
372 std::unique_ptr<PtJson> array = PtJson::CreateArray();
373 std::unique_ptr<PtJson> object = PtJson::CreateObject();
374 object->Add("offset", "test");
375 array->Push(object);
376 ptJson->Add("entries", array);
377 std::unique_ptr<ScriptTypeProfile> profile = ScriptTypeProfile::Create(*ptJson);
378 ASSERT_TRUE(!profile);
379 }
380
HWTEST_F_L0(PtTypesTest,TypeProfileEntryCreateTest)381 HWTEST_F_L0(PtTypesTest, TypeProfileEntryCreateTest)
382 {
383 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
384 std::unique_ptr<PtJson> array = PtJson::CreateArray();
385 std::unique_ptr<PtJson> object = PtJson::CreateObject();
386 object->Add("name", 0);
387 array->Push(object);
388 ptJson->Add("types", array);
389 std::unique_ptr<TypeProfileEntry> entry = TypeProfileEntry::Create(*ptJson);
390 ASSERT_TRUE(!entry);
391 }
392
HWTEST_F_L0(PtTypesTest,ScriptCoverageCreateTest)393 HWTEST_F_L0(PtTypesTest, ScriptCoverageCreateTest)
394 {
395 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
396 std::unique_ptr<PtJson> array = PtJson::CreateArray();
397 std::unique_ptr<PtJson> object = PtJson::CreateObject();
398 object->Add("functionName", 0);
399 array->Push(object);
400 ptJson->Add("functions", array);
401 std::unique_ptr<ScriptCoverage> coverage = ScriptCoverage::Create(*ptJson);
402 ASSERT_TRUE(!coverage);
403 }
404
HWTEST_F_L0(PtTypesTest,FunctionCoverageCreateTest)405 HWTEST_F_L0(PtTypesTest, FunctionCoverageCreateTest)
406 {
407 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
408 std::unique_ptr<PtJson> array = PtJson::CreateArray();
409 std::unique_ptr<PtJson> object = PtJson::CreateObject();
410 object->Add("functionName", 0);
411 array->Push(object);
412 ptJson->Add("ranges", array);
413 std::unique_ptr<FunctionCoverage> coverage = FunctionCoverage::Create(*ptJson);
414 ASSERT_TRUE(!coverage);
415 }
416
HWTEST_F_L0(PtTypesTest,SymbolRemoteObjectTest)417 HWTEST_F_L0(PtTypesTest, SymbolRemoteObjectTest)
418 {
419 size_t size = 10;
420 const char *data = "SymbolRemoteObjectTest";
421 SymbolRemoteObject obj(ecmaVm, SymbolRef::New(ecmaVm, StringRef::NewFromUtf8(ecmaVm, (const char*)data, size)));
422 }
423
HWTEST_F_L0(PtTypesTest,DescriptionForObjectForDate)424 HWTEST_F_L0(PtTypesTest, DescriptionForObjectForDate)
425 {
426 double input = 123456789.0;
427 Local<DateRef> date = DateRef::New(ecmaVm, input);
428 std::string description = ObjectRemoteObject::DescriptionForObject(ecmaVm, date);
429 ASSERT_TRUE(description.find("GMT") != std::string::npos);
430 }
431
HWTEST_F_L0(PtTypesTest,DescriptionForObjectForPromise)432 HWTEST_F_L0(PtTypesTest, DescriptionForObjectForPromise)
433 {
434 Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(ecmaVm);
435 Local<PromiseRef> promise = capability->GetPromise(ecmaVm);
436 std::string description = ObjectRemoteObject::DescriptionForObject(ecmaVm, promise);
437 ASSERT_TRUE(description == "Promise");
438 }
439
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptorCreate)440 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptorCreate)
441 {
442 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
443 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
444 bool value = true;
445 ptJson->Add("userCode", value);
446 std::unique_ptr<PrivatePropertyDescriptor> result = PrivatePropertyDescriptor::Create(*ptJson);
447 ASSERT_TRUE(result == nullptr);
448 value = false;
449 ptJson->Add("userCode1", value);
450 result = PrivatePropertyDescriptor::Create(*ptJson);
451 ASSERT_TRUE(result == nullptr);
452 int32_t value1 = 100;
453 ptJson1->Add("test", value1);
454 std::unique_ptr<PrivatePropertyDescriptor> result1 = PrivatePropertyDescriptor::Create(*ptJson1);
455 ASSERT_TRUE(result == nullptr);
456 int64_t value2 = 0;
457 ptJson1->Add("", value2);
458 result1 = PrivatePropertyDescriptor::Create(*ptJson1);
459 ASSERT_TRUE(result == nullptr);
460 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
461 ptJson2->Add("name", value);
462 std::unique_ptr<PrivatePropertyDescriptor> result2 = PrivatePropertyDescriptor::Create(*ptJson2);
463 ASSERT_TRUE(result == nullptr);
464 ptJson2->Add("value", value1);
465 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
466 ASSERT_TRUE(result == nullptr);
467 ptJson2->Add("get", value2);
468 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
469 ASSERT_TRUE(result == nullptr);
470 value2 = 10;
471 ptJson2->Add("set", value2);
472 result2 = PrivatePropertyDescriptor::Create(*ptJson2);
473 ASSERT_TRUE(result == nullptr);
474 std::unique_ptr<PtJson> ptJson3 = PtJson::CreateObject();
475 ptJson3->Add("name", "test");
476 ptJson3->Add("value", ptJson);
477 ptJson3->Add("get", ptJson1);
478 ptJson3->Add("set", ptJson2);
479 std::unique_ptr<PrivatePropertyDescriptor> result3 = PrivatePropertyDescriptor::Create(*ptJson3);
480 ASSERT_TRUE(result == nullptr);
481 }
482
HWTEST_F_L0(PtTypesTest,CallArgumentToJson)483 HWTEST_F_L0(PtTypesTest, CallArgumentToJson)
484 {
485 CallArgument callArgument;
486 Local<JSValueRef> name = StringRef::NewFromUtf8(ecmaVm, "name");
487 callArgument.SetValue(name);
488 RemoteObjectId objectId(10);
489 callArgument.SetObjectId(objectId);
490 UnserializableValue value("test");
491 callArgument.SetUnserializableValue(value);
492 std::unique_ptr<PtJson> ret = callArgument.ToJson();
493 ASSERT_TRUE(ret != nullptr);
494 CallArgument callArgument1;
495 RemoteObjectId objectId1(20);
496 callArgument1.SetObjectId(objectId1);
497 ret = callArgument1.ToJson();
498 ASSERT_TRUE(ret != nullptr);
499 CallArgument callArgument2;
500 UnserializableValue value1("CallArgumentToJson");
501 callArgument2.ToJson();
502 ASSERT_TRUE(ret != nullptr);
503 }
504
HWTEST_F_L0(PtTypesTest,ScriptPositionToJson)505 HWTEST_F_L0(PtTypesTest, ScriptPositionToJson)
506 {
507 ScriptPosition position;
508 int32_t line = 10;
509 position.SetLine(line);
510 int32_t column = 8;
511 position.SetColumn(column);
512 std::unique_ptr<PtJson> ret = position.ToJson();
513 ASSERT_TRUE(ret != nullptr);
514 }
515
HWTEST_F_L0(PtTypesTest,SearchMatchCreate)516 HWTEST_F_L0(PtTypesTest, SearchMatchCreate)
517 {
518 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
519 int32_t lineNumber = 10;
520 ptJson->Add("lineNumber", lineNumber);
521 std::string lineContent = "12";
522 ptJson->Add("lineContent", lineContent.c_str());
523 std::unique_ptr<SearchMatch> ret = SearchMatch::Create(*ptJson);
524 ASSERT_TRUE(ret != nullptr);
525
526 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
527 std::string attribute = "32";
528 ptJson1->Add("lineNumber", attribute.c_str());
529 std::unique_ptr<SearchMatch> ret1 = SearchMatch::Create(*ptJson1);
530 ASSERT_TRUE(ret1 == nullptr);
531
532 std::unique_ptr<PtJson> ptJson2 = PtJson::CreateObject();
533 int32_t lineNumber2 = 14;
534 std::string lineContent2 = "12";
535 ptJson1->Add("lineNumber1", lineNumber2);
536 ptJson1->Add("lineContent2", lineContent2.c_str());
537 std::unique_ptr<SearchMatch> ret2 = SearchMatch::Create(*ptJson1);
538 ASSERT_TRUE(ret2 == nullptr);
539 }
540
HWTEST_F_L0(PtTypesTest,LocationRangeCreate)541 HWTEST_F_L0(PtTypesTest, LocationRangeCreate)
542 {
543 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
544 std::unique_ptr<PtJson> start = PtJson::CreateObject();
545 std::unique_ptr<PtJson> end = PtJson::CreateObject();
546 std::string scriptId = "12";
547 int32_t lineNumber = 1;
548 int32_t columnNumber = 2;
549 start->Add("lineNumber", lineNumber);
550 start->Add("columnNumber", columnNumber);
551 end->Add("lineNumber", lineNumber + 1);
552 end->Add("columnNumber", columnNumber + 2);
553 ptJson->Add("scriptId", scriptId.c_str());
554 ptJson->Add("start", start);
555 ptJson->Add("end", end);
556 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
557 ASSERT_TRUE(ret != nullptr);
558 }
559
HWTEST_F_L0(PtTypesTest,LocationRangeCreateSwitch)560 HWTEST_F_L0(PtTypesTest, LocationRangeCreateSwitch)
561 {
562 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
563 std::unique_ptr<PtJson> start = PtJson::CreateObject();
564 std::unique_ptr<PtJson> end = PtJson::CreateObject();
565 std::string scriptId = "12";
566 int32_t lineNumber = 1;
567 int32_t columnNumber = 2;
568 start->Add("lineNumber1", lineNumber);
569 start->Add("columnNumber1", columnNumber);
570 end->Add("lineNumber1", lineNumber + 1);
571 end->Add("columnNumber1", columnNumber + 2);
572 ptJson->Add("scriptId1", scriptId.c_str());
573 ptJson->Add("start", start);
574 ptJson->Add("end", end);
575 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
576 ASSERT_TRUE(ret == nullptr);
577 }
578
HWTEST_F_L0(PtTypesTest,LocationRangeCreateSwitchFail)579 HWTEST_F_L0(PtTypesTest, LocationRangeCreateSwitchFail)
580 {
581 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
582 std::unique_ptr<PtJson> start = PtJson::CreateObject();
583 std::unique_ptr<PtJson> end = PtJson::CreateObject();
584 std::string scriptId = "12";
585 int32_t lineNumber = 1;
586 int32_t columnNumber = 2;
587 start->Add("lineNumber1", lineNumber);
588 start->Add("columnNumber1", columnNumber);
589 end->Add("lineNumber1", lineNumber + 1);
590 end->Add("columnNumber1", columnNumber + 2);
591 ptJson->Add("scriptId1", scriptId.c_str());
592 ptJson->Add("start1", start);
593 ptJson->Add("end1", end);
594 std::unique_ptr<LocationRange> ret = LocationRange::Create(*ptJson);
595 ASSERT_TRUE(ret == nullptr);
596 }
597
HWTEST_F_L0(PtTypesTest,RuntimeCallFrameCreate)598 HWTEST_F_L0(PtTypesTest, RuntimeCallFrameCreate)
599 {
600 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
601 std::string functionName = "RuntimeCallFrameCreate";
602 std::string scriptId = "scriptId";
603 std::string url = "url";
604 int32_t lineNumber = 2;
605 int32_t columnNumber = 10;
606 ptJson->Add("functionName", functionName.c_str());
607 ptJson->Add("moduleName", functionName.c_str());
608 ptJson->Add("scriptId", scriptId.c_str());
609 ptJson->Add("url", url.c_str());
610 ptJson->Add("lineNumber", lineNumber);
611 ptJson->Add("columnNumber", columnNumber);
612 std::unique_ptr<RuntimeCallFrame> ret = RuntimeCallFrame::Create(*ptJson);
613 ASSERT_TRUE(ret != nullptr);
614 }
615
HWTEST_F_L0(PtTypesTest,RuntimeCallFrameCreateFailSwitch)616 HWTEST_F_L0(PtTypesTest, RuntimeCallFrameCreateFailSwitch)
617 {
618 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
619 std::string functionName = "RuntimeCallFrameCreate";
620 std::string scriptId = "scriptId";
621 std::string url = "url";
622 int32_t lineNumber = 2;
623 int32_t columnNumber = 10;
624 ptJson->Add("functionName1", functionName.c_str());
625 ptJson->Add("moduleName1", functionName.c_str());
626 ptJson->Add("scriptId1", scriptId.c_str());
627 ptJson->Add("url1", url.c_str());
628 ptJson->Add("lineNumber1", lineNumber);
629 ptJson->Add("columnNumber1", columnNumber);
630 std::unique_ptr<RuntimeCallFrame> ret = RuntimeCallFrame::Create(*ptJson);
631 ASSERT_TRUE(ret == nullptr);
632 }
633
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileNodeCreate)634 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileNodeCreate)
635 {
636 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
637 std::unique_ptr<PtJson> callFrame = PtJson::CreateObject();
638 std::unique_ptr<PtJson> array = PtJson::CreateArray();
639 std::unique_ptr<PtJson> object = PtJson::CreateObject();
640 callFrame->Add("callFrame", "test");
641 ptJson->Add("callFrame", callFrame);
642 object->Add("test", 0);
643 array->Push(object);
644 ptJson->Add("children", array);
645 std::unique_ptr<SamplingHeapProfileNode> ret = SamplingHeapProfileNode::Create(*ptJson);
646 ASSERT_TRUE(ret == nullptr);
647 }
648
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileCreate)649 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileCreate)
650 {
651 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
652 std::unique_ptr<PtJson> head = PtJson::CreateObject();
653 std::unique_ptr<PtJson> array = PtJson::CreateArray();
654 std::unique_ptr<PtJson> object = PtJson::CreateObject();
655 head->Add("head", "headTest");
656 ptJson->Add("head", head);
657 object->Add("test", 0);
658 array->Push(object);
659 ptJson->Add("samples", array);
660 std::unique_ptr<SamplingHeapProfile> ret = SamplingHeapProfile::Create(*ptJson);
661 ASSERT_TRUE(ret == nullptr);
662 }
663
HWTEST_F_L0(PtTypesTest,ExceptionDetailsCreate)664 HWTEST_F_L0(PtTypesTest, ExceptionDetailsCreate)
665 {
666 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
667 std::unique_ptr<PtJson> exception = PtJson::CreateObject();
668 exception->Add("exception", "test");
669 ptJson->Add("exception", exception);
670 std::unique_ptr<ExceptionDetails> ret = ExceptionDetails::Create(*ptJson);
671 ASSERT_TRUE(ret == nullptr);
672 }
673
674
HWTEST_F_L0(PtTypesTest,ProfileToJsonTest)675 HWTEST_F_L0(PtTypesTest, ProfileToJsonTest)
676 {
677 Profile profile;
678 std::vector<int32_t> samples {1, 2, 3}; // 1:element 2:element 3:element
679 std::vector<int32_t> timeDeltas {1, 2, 3}; // 1:element 2:element 3:element
680 profile.SetSamples(samples);
681 profile.SetTimeDeltas(timeDeltas);
682 std::unique_ptr<PtJson> result = profile.ToJson();
683 ASSERT_TRUE(result);
684 }
685
HWTEST_F_L0(PtTypesTest,ProfileFromProfileInfoTest)686 HWTEST_F_L0(PtTypesTest, ProfileFromProfileInfoTest)
687 {
688 ProfileInfo info;
689 info.samples.push_back(1);
690 info.samples.push_back(2);
691 info.timeDeltas.push_back(1);
692 info.timeDeltas.push_back(2);
693 std::unique_ptr<Profile> result = Profile::FromProfileInfo(info);
694 ASSERT_TRUE(result);
695 }
696
HWTEST_F_L0(PtTypesTest,ProfileNodeToJsonTest)697 HWTEST_F_L0(PtTypesTest, ProfileNodeToJsonTest)
698 {
699 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
700 ProfileNode node;
701 std::vector<int32_t> children {0, 1, 2, 3}; // 1: element 2:element, 3:element
702 std::unique_ptr<PositionTickInfo> positionTickInfo = std::make_unique<PositionTickInfo>();
703 std::unique_ptr<RuntimeCallFrame> callFrame = std::make_unique<RuntimeCallFrame>();
704 positionTickInfo->SetLine(123);
705 positionTickInfo->SetTicks(123);
706 std::vector<std::unique_ptr<PositionTickInfo>> positionTicks;
707 positionTicks.push_back(std::move(positionTickInfo));
708 node.SetChildren(children);
709 node.SetPositionTicks(std::move(positionTicks));
710 node.SetCallFrame(std::move(callFrame));
711 std::unique_ptr<PtJson> result = node.ToJson();
712 ASSERT_TRUE(result);
713 ProfileNode node1;
714 std::unique_ptr<RuntimeCallFrame> callFrame1 = std::make_unique<RuntimeCallFrame>();
715 node1.SetCallFrame(std::move(callFrame1));
716 std::unique_ptr<PtJson> result1 = node1.ToJson();
717 ASSERT_TRUE(result1);
718 }
719
HWTEST_F_L0(PtTypesTest,ProfileNodeFromCpuProfileNodeTest)720 HWTEST_F_L0(PtTypesTest, ProfileNodeFromCpuProfileNodeTest)
721 {
722 CpuProfileNode cpuProfileNode;
723 cpuProfileNode.children.push_back(0);
724 cpuProfileNode.children.push_back(1); // 1: element
725 std::unique_ptr<ProfileNode> result = ProfileNode::FromCpuProfileNode(cpuProfileNode);
726 ASSERT_TRUE(result);
727 }
728
HWTEST_F_L0(PtTypesTest,PrivatePropertyDescriptorToJsonTest)729 HWTEST_F_L0(PtTypesTest, PrivatePropertyDescriptorToJsonTest)
730 {
731 PrivatePropertyDescriptor descriptor;
732 std::unique_ptr<RemoteObject> value = std::make_unique<RemoteObject>();
733 std::unique_ptr<RemoteObject> get = std::make_unique<RemoteObject>();
734 std::unique_ptr<RemoteObject> set = std::make_unique<RemoteObject>();
735 descriptor.SetValue(std::move(value));
736 descriptor.SetGet(std::move(get));
737 descriptor.SetSet(std::move(set));
738 std::unique_ptr<PtJson> result = descriptor.ToJson();
739 ASSERT_TRUE(result);
740 }
741
HWTEST_F_L0(PtTypesTest,PropertyDescriptorFromPropertyTest)742 HWTEST_F_L0(PtTypesTest, PropertyDescriptorFromPropertyTest)
743 {
744 Local<SymbolRef> symbol = SymbolRef::New(ecmaVm, StringRef::NewFromUtf8(ecmaVm, "test"));
745 PropertyAttribute property;
746 property.SetGetter(StringRef::NewFromUtf8(ecmaVm, "test"));
747 property.SetSetter(StringRef::NewFromUtf8(ecmaVm, "test"));
748 std::unique_ptr<panda::ecmascript::tooling::PropertyDescriptor> result =
749 panda::ecmascript::tooling::PropertyDescriptor::FromProperty(ecmaVm, symbol, property);
750 ASSERT_TRUE(result);
751 }
752
HWTEST_F_L0(PtTypesTest,PtEventsGetReasonString)753 HWTEST_F_L0(PtTypesTest, PtEventsGetReasonString)
754 {
755 PauseReason reason = static_cast<PauseReason>(50);
756 std::string ret = Paused::GetReasonString(reason);
757 ASSERT_TRUE(ret.empty());
758 }
759
HWTEST_F_L0(PtTypesTest,LocationRangeToJsonTest)760 HWTEST_F_L0(PtTypesTest, LocationRangeToJsonTest)
761 {
762 LocationRange range;
763 std::unique_ptr<ScriptPosition> start = std::make_unique<ScriptPosition>();
764 std::unique_ptr<ScriptPosition> end = std::make_unique<ScriptPosition>();
765 range.SetStart(std::move(start));
766 range.SetEnd(std::move(end));
767 std::unique_ptr<PtJson> result = range.ToJson();
768 ASSERT_TRUE(result);
769 }
770
HWTEST_F_L0(PtTypesTest,BreakLocationToJsonTest)771 HWTEST_F_L0(PtTypesTest, BreakLocationToJsonTest)
772 {
773 BreakLocation location;
774 location.SetColumn(0);
775 location.SetType("test");
776 std::unique_ptr<PtJson> result = location.ToJson();
777 ASSERT_TRUE(result);
778 }
779
HWTEST_F_L0(PtTypesTest,CallFrameCreateTest)780 HWTEST_F_L0(PtTypesTest, CallFrameCreateTest)
781 {
782 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
783 std::unique_ptr<PtJson> array = PtJson::CreateArray();
784 std::unique_ptr<PtJson> object = PtJson::CreateObject();
785 object->Add("type", 0);
786 array->Push(object);
787 ptJson->Add("scopeChain", array);
788 std::unique_ptr<CallFrame> result = CallFrame::Create(*ptJson);
789 ASSERT_TRUE(!result);
790 }
791
HWTEST_F_L0(PtTypesTest,ProfileCreate)792 HWTEST_F_L0(PtTypesTest, ProfileCreate)
793 {
794 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
795 std::unique_ptr<PtJson> array = PtJson::CreateArray();
796 std::unique_ptr<PtJson> object = PtJson::CreateObject();
797 std::unique_ptr<PtJson> object1 = PtJson::CreateObject();
798 object->Add("type", 0);
799 array->Push(object);
800 ptJson->Add("nodes", array);
801 std::unique_ptr<Profile> result = Profile::Create(*ptJson);
802 ASSERT_TRUE(!result);
803 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
804 ptJson1->Add("samples", 0);
805 ptJson1->Add("timeDeltas", 0);
806 std::unique_ptr<Profile> result1 = Profile::Create(*ptJson1);
807 ASSERT_TRUE(!result1);
808 }
809
HWTEST_F_L0(PtTypesTest,ProfileNodeCreate)810 HWTEST_F_L0(PtTypesTest, ProfileNodeCreate)
811 {
812 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
813 std::string attribute = "test";
814 std::unique_ptr<PtJson> object = PtJson::CreateObject();
815 object->Add("functionName", 0);
816 ptJson->Add("callFrame", object);
817 ptJson->Add("hitCount", attribute.c_str());
818 ptJson->Add("children", attribute.c_str());
819 ptJson->Add("positionTicks", attribute.c_str());
820 ptJson->Add("deoptReason", 0);
821 std::unique_ptr<ProfileNode> result = ProfileNode::Create(*ptJson);
822 ASSERT_TRUE(!result);
823 std::unique_ptr<PtJson> ptJson1 = PtJson::CreateObject();
824 std::unique_ptr<PtJson> object1 = PtJson::CreateObject();
825 std::unique_ptr<PtJson> array = PtJson::CreateArray();
826 object1->Add("attribute", attribute.c_str());
827 array->Push(object1);
828 ptJson1->Add("positionTicks", array);
829 std::unique_ptr<ProfileNode> result1 = ProfileNode::Create(*ptJson1);
830 ASSERT_TRUE(!result1);
831 }
832
HWTEST_F_L0(PtTypesTest,LocationCreate)833 HWTEST_F_L0(PtTypesTest, LocationCreate)
834 {
835 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
836 std::string attribute = "test";
837 ptJson->Add("columnNumber", attribute.c_str());
838 std::unique_ptr<Location> result = Location::Create(*ptJson);
839 ASSERT_TRUE(!result);
840 }
841
HWTEST_F_L0(PtTypesTest,PropertyDescriptorCreate)842 HWTEST_F_L0(PtTypesTest, PropertyDescriptorCreate)
843 {
844 std::unique_ptr<PtJson> ptJson = PtJson::CreateObject();
845 std::unique_ptr<PtJson> get = PtJson::CreateObject();
846 std::unique_ptr<PtJson> set = PtJson::CreateObject();
847 std::unique_ptr<PtJson> symbol = PtJson::CreateObject();
848 std::string attribute = "test";
849 get->Add("type", attribute.c_str());
850 set->Add("type", attribute.c_str());
851 symbol->Add("type", attribute.c_str());
852 ptJson->Add("get", get);
853 ptJson->Add("set", set);
854 ptJson->Add("symbol", symbol);
855 std::unique_ptr<tooling::PropertyDescriptor> result = tooling::PropertyDescriptor::Create(*ptJson);
856 ASSERT_TRUE(!result);
857 }
858
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileFromSamplingInfo)859 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileFromSamplingInfo)
860 {
861 SamplingInfo samplingInfo;
862 samplingInfo.head_.selfSize_ = 0;
863 samplingInfo.head_.id_ = 0;
864 samplingInfo.head_.callFrameInfo_.codeType_ = "codetype";
865 samplingInfo.head_.callFrameInfo_.columnNumber_ = 21;
866 samplingInfo.head_.callFrameInfo_.functionName_ = "TestSampling";
867 samplingInfo.head_.callFrameInfo_.lineNumber_ = 221;
868 samplingInfo.head_.callFrameInfo_.scriptId_ = 1;
869 samplingInfo.head_.callFrameInfo_.url_ = "url";
870 Sample sampleInfo(12024, 19, 1, 2);
871 samplingInfo.samples_.push_back(sampleInfo);
872 std::unique_ptr<SamplingHeapProfile> profile = SamplingHeapProfile::FromSamplingInfo(&samplingInfo);
873 ASSERT_TRUE(profile != nullptr);
874 SamplingHeapProfileNode *nodeInfo = profile->GetHead();
875 int32_t selfSize = nodeInfo->GetSelfSize();
876 ASSERT_TRUE(selfSize == samplingInfo.head_.selfSize_);
877 int32_t id = nodeInfo->GetId();
878 ASSERT_TRUE(id == samplingInfo.head_.id_);
879 RuntimeCallFrame *callFrame = nodeInfo->GetCallFrame();
880 const std::string functionName = callFrame->GetFunctionName();
881 ASSERT_TRUE(functionName == samplingInfo.head_.callFrameInfo_.functionName_);
882 const std::string url = callFrame->GetUrl();
883 ASSERT_TRUE(url == samplingInfo.head_.callFrameInfo_.url_);
884 int32_t lineNumber = callFrame->GetLineNumber();
885 ASSERT_TRUE(lineNumber == samplingInfo.head_.callFrameInfo_.lineNumber_);
886 int32_t columnNumber = callFrame->GetColumnNumber();
887 ASSERT_TRUE(columnNumber == samplingInfo.head_.callFrameInfo_.columnNumber_);
888 const std::vector<std::unique_ptr<SamplingHeapProfileSample>> *sampData = profile->GetSamples();
889 int32_t nodeId = sampData->data()->get()->GetNodeId();
890 ASSERT_TRUE(nodeId == sampleInfo.nodeId_);
891 int32_t size = sampData->data()->get()->GetSize();
892 ASSERT_TRUE(size == (sampleInfo.size_ * sampleInfo.count_));
893 int64_t ordinal = sampData->data()->get()->GetOrdinal();
894 ASSERT_TRUE(ordinal == sampleInfo.ordinal_);
895 }
896
HWTEST_F_L0(PtTypesTest,SamplingHeapProfileTransferHead)897 HWTEST_F_L0(PtTypesTest, SamplingHeapProfileTransferHead)
898 {
899 SamplingNode allocationNode;
900 allocationNode.selfSize_ = 0;
901 allocationNode.id_ = 0;
902 allocationNode.callFrameInfo_.codeType_ = "codetype";
903 allocationNode.callFrameInfo_.columnNumber_ = 21;
904 allocationNode.callFrameInfo_.functionName_ = "TestSampling";
905 allocationNode.callFrameInfo_.lineNumber_ = 221;
906 allocationNode.callFrameInfo_.scriptId_ = 1;
907 allocationNode.callFrameInfo_.url_ = "url";
908 std::unique_ptr<SamplingHeapProfileNode> headInfo = SamplingHeapProfile::TransferHead(&allocationNode);
909 ASSERT_TRUE(headInfo != nullptr);
910 int32_t selfSize = headInfo->GetSelfSize();
911 ASSERT_TRUE(selfSize == allocationNode.selfSize_);
912 int32_t id = headInfo->GetId();
913 ASSERT_TRUE(id == allocationNode.id_);
914 RuntimeCallFrame *callFrame = headInfo->GetCallFrame();
915 const std::string functionName = callFrame->GetFunctionName();
916 ASSERT_TRUE(functionName == allocationNode.callFrameInfo_.functionName_);
917 const std::string url = callFrame->GetUrl();
918 ASSERT_TRUE(url == allocationNode.callFrameInfo_.url_);
919 int32_t lineNumber = callFrame->GetLineNumber();
920 ASSERT_TRUE(lineNumber == allocationNode.callFrameInfo_.lineNumber_);
921 int32_t columnNumber = callFrame->GetColumnNumber();
922 ASSERT_TRUE(columnNumber == allocationNode.callFrameInfo_.columnNumber_);
923 }
924
HWTEST_F_L0(PtTypesTest,DescriptionForObjectTest)925 HWTEST_F_L0(PtTypesTest, DescriptionForObjectTest)
926 {
927 Local<WeakMapRef> weakmap = WeakMapRef::New(ecmaVm);
928 std::string description = ObjectRemoteObject::DescriptionForObject(ecmaVm, weakmap);
929 ASSERT_TRUE(description.find("WeakMap") != std::string::npos);
930
931 Local<MapRef> map = MapRef::New(ecmaVm);
932 Local<MapIteratorRef> mapiterator = MapIteratorRef::New(ecmaVm, map);
933 description = ObjectRemoteObject::DescriptionForObject(ecmaVm, mapiterator);
934 ASSERT_TRUE(description.find("MapIterator") != std::string::npos);
935
936 Local<SetRef> set = SetRef::New(ecmaVm);
937 Local<SetIteratorRef> setiterator = SetIteratorRef::New(ecmaVm, set);
938 description = ObjectRemoteObject::DescriptionForObject(ecmaVm, setiterator);
939 ASSERT_TRUE(description.find("SetIterator") != std::string::npos);
940
941 size_t size = 10;
942 Local<NativePointerRef> nativepointer = NativePointerRef::New(ecmaVm, ecmaVm, size);
943 description = ObjectRemoteObject::DescriptionForObject(ecmaVm, nativepointer);
944 std::cout << description << std::endl;
945 ASSERT_TRUE(description.find("External") != std::string::npos);
946 }
947
HWTEST_F_L0(PtTypesTest,FromTaggedTest)948 HWTEST_F_L0(PtTypesTest, FromTaggedTest)
949 {
950 double input = 123456789.0;
951 Local<DateRef> date = DateRef::New(ecmaVm, input);
952 std::unique_ptr<RemoteObject> remoteObject = RemoteObject::FromTagged(ecmaVm, date);
953 std::string description = remoteObject->GetDescription();
954 ASSERT_TRUE(description.find("GMT") != std::string::npos);
955
956 Local<WeakMapRef> weakmap = WeakMapRef::New(ecmaVm);
957 remoteObject = RemoteObject::FromTagged(ecmaVm, weakmap);
958 description = remoteObject->GetDescription();
959 ASSERT_TRUE(description.find("WeakMap") != std::string::npos);
960
961 Local<MapRef> map = MapRef::New(ecmaVm);
962 Local<MapIteratorRef> mapiterator = MapIteratorRef::New(ecmaVm, map);
963 remoteObject = RemoteObject::FromTagged(ecmaVm, mapiterator);
964 description = remoteObject->GetDescription();
965 ASSERT_TRUE(description.find("MapIterator") != std::string::npos);
966
967 Local<SetRef> set = SetRef::New(ecmaVm);
968 Local<SetIteratorRef> setiterator = SetIteratorRef::New(ecmaVm, set);
969 remoteObject = RemoteObject::FromTagged(ecmaVm, setiterator);
970 description = remoteObject->GetDescription();
971 ASSERT_TRUE(description.find("SetIterator") != std::string::npos);
972
973 Local<PromiseCapabilityRef> capability = PromiseCapabilityRef::New(ecmaVm);
974 Local<PromiseRef> promise = capability->GetPromise(ecmaVm);
975 remoteObject = RemoteObject::FromTagged(ecmaVm, promise);
976 description = remoteObject->GetDescription();
977 ASSERT_TRUE(description.find("Promise") != std::string::npos);
978
979 size_t size = 10;
980 Local<NativePointerRef> nativepointer = NativePointerRef::New(ecmaVm, ecmaVm, size);
981 remoteObject = RemoteObject::FromTagged(ecmaVm, nativepointer);
982 description = remoteObject->GetDescription();
983 ASSERT_TRUE(description.find("External") != std::string::npos);
984 }
985
HWTEST_F_L0(PtTypesTest,NativeRangeCreateTest)986 HWTEST_F_L0(PtTypesTest, NativeRangeCreateTest)
987 {
988 std::string msg;
989 std::unique_ptr<NativeRange> nativeRange;
990
991 msg = std::string() + R"({})";
992 nativeRange = NativeRange::Create(DispatchRequest(msg).GetParams());
993 EXPECT_EQ(nativeRange, nullptr);
994
995 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"start":20,"end":40}})";
996 nativeRange = NativeRange::Create(DispatchRequest(msg).GetParams());
997 ASSERT_TRUE(nativeRange != nullptr);
998 }
999
HWTEST_F_L0(PtTypesTest,BreakpointInfoCreateTest)1000 HWTEST_F_L0(PtTypesTest, BreakpointInfoCreateTest)
1001 {
1002 std::string msg;
1003 std::unique_ptr<BreakpointInfo> breakpointInfo;
1004
1005 msg = std::string() + R"({})";
1006 breakpointInfo = BreakpointInfo::Create(DispatchRequest(msg).GetParams());
1007 EXPECT_EQ(breakpointInfo, nullptr);
1008
1009 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"condition":"Test","urlRegex":"Test"}})";
1010 breakpointInfo = BreakpointInfo::Create(DispatchRequest(msg).GetParams());
1011 EXPECT_EQ(breakpointInfo, nullptr);
1012
1013 msg = std::string() + R"({"id":0,"method":"Debugger.Test",
1014 "params":{"scriptHash":"Test", "restrictToFunction": true}})";
1015 breakpointInfo = BreakpointInfo::Create(DispatchRequest(msg).GetParams());
1016 EXPECT_EQ(breakpointInfo, nullptr);
1017
1018 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"lineNumber":1,"columnNumber":2,"url":"Test",
1019 "condition":"Test", "urlRegex":"Test", "scriptHash":"Test", "restrictToFunction": true}})";
1020 breakpointInfo = BreakpointInfo::Create(DispatchRequest(msg).GetParams());
1021 std::unique_ptr<PtJson> resultJson = breakpointInfo->ToJson();
1022 ASSERT_TRUE(resultJson != nullptr);
1023
1024 msg = std::string() + R"({"id":0,"method":"Debugger.Test",
1025 "params":{"lineNumber":1, "columnNumber":2, "url":"Test"}})";
1026 breakpointInfo = BreakpointInfo::Create(DispatchRequest(msg).GetParams());
1027 resultJson = breakpointInfo->ToJson();
1028 ASSERT_TRUE(resultJson != nullptr);
1029 }
1030
HWTEST_F_L0(PtTypesTest,BreakpointReturnInfoCreateTest)1031 HWTEST_F_L0(PtTypesTest, BreakpointReturnInfoCreateTest)
1032 {
1033 std::string msg;
1034 std::unique_ptr<BreakpointReturnInfo> breakpointReturnInfo;
1035
1036 msg = std::string() + R"({})";
1037 breakpointReturnInfo = BreakpointReturnInfo::Create(DispatchRequest(msg).GetParams());
1038 EXPECT_EQ(breakpointReturnInfo, nullptr);
1039
1040 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"lineNumber":20,"columnNumber":40}})";
1041 breakpointReturnInfo = BreakpointReturnInfo::Create(DispatchRequest(msg).GetParams());
1042 ASSERT_TRUE(breakpointReturnInfo != nullptr);
1043
1044 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"id":"Test","scriptId":5}})";
1045 breakpointReturnInfo = BreakpointReturnInfo::Create(DispatchRequest(msg).GetParams());
1046 EXPECT_EQ(breakpointReturnInfo, nullptr);
1047 }
1048
HWTEST_F_L0(PtTypesTest,InvalidUtf16Test)1049 HWTEST_F_L0(PtTypesTest, InvalidUtf16Test)
1050 {
1051 const char16_t invalidUtf16[] = { 0xD800 };
1052 Local<StringRef> invalidStrRef = StringRef::NewFromUtf16(ecmaVm, invalidUtf16);
1053 std::string invalidStrInDebugger = invalidStrRef->DebuggerToString(ecmaVm);
1054
1055 auto utf8 = reinterpret_cast<const uint8_t*>(invalidStrInDebugger.c_str());
1056 size_t utf8Len = invalidStrInDebugger.size();
1057 for (size_t i = 0; i < utf8Len;) {
1058 std::pair<int32_t, size_t> result =
1059 common::utf_helper::ConvertUtf8ToUnicodeChar(utf8 + i, utf8Len - i);
1060 // -1: invalid utf8
1061 ASSERT_TRUE(result.first != -1);
1062 i += result.second;
1063 }
1064 }
1065
HWTEST_F_L0(PtTypesTest,InvalidUtf16DescriptionTest)1066 HWTEST_F_L0(PtTypesTest, InvalidUtf16DescriptionTest)
1067 {
1068 const char16_t invalidUtf16[] = { 0xD800 };
1069 Local<StringRef> invalidStrRef = StringRef::NewFromUtf16(ecmaVm, invalidUtf16);
1070 JSHandle<JSTaggedValue> invalidStrHandle(thread, JSNApiHelper::ToJSTaggedValue(*invalidStrRef));
1071 std::string invalidStrInDebugger = invalidStrRef->DebuggerToString(ecmaVm);
1072 std::string invalidStr = invalidStrRef->ToString(ecmaVm);
1073 ASSERT_TRUE(invalidStrInDebugger != invalidStr);
1074
1075 // String
1076 StringRemoteObject strObj(ecmaVm, invalidStrRef);
1077 EXPECT_EQ(strObj.GetDescription(), invalidStrInDebugger);
1078
1079 // Primitive String
1080 ObjectFactory *factory = ecmaVm->GetFactory();
1081 JSHandle<JSTaggedValue> primitiveStrHandle =
1082 JSHandle<JSTaggedValue>::Cast(factory->NewJSPrimitiveRef(PrimitiveType::PRIMITIVE_STRING, invalidStrHandle));
1083 Local<JSValueRef> primitiveStr = JSNApiHelper::ToLocal<JSValueRef>(primitiveStrHandle);
1084 std::string primitiveStrDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, primitiveStr);
1085 ASSERT_TRUE(primitiveStrDesc.find(invalidStrInDebugger) != std::string::npos);
1086
1087 // RegExp
1088 JSHandle<JSHClass> jsRegExpClass = factory->NewEcmaHClass(JSRegExp::SIZE, JSType::JS_REG_EXP);
1089 JSHandle<JSRegExp> jsRegExp = JSHandle<JSRegExp>::Cast(factory->NewJSObject(jsRegExpClass));
1090 jsRegExp->SetOriginalSource(thread, invalidStrHandle.GetTaggedValue());
1091 JSHandle<JSTaggedValue> jsRegExpHandle = JSHandle<JSTaggedValue>::Cast(jsRegExp);
1092 Local<RegExpRef> regExpRef = JSNApiHelper::ToLocal<RegExpRef>(jsRegExpHandle);
1093 std::string regExpDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, regExpRef);
1094 ASSERT_TRUE(regExpDesc.find(invalidStrInDebugger) != std::string::npos);
1095
1096 // Map
1097 Local<MapRef> mapRef = MapRef::New(ecmaVm);
1098 mapRef->Set(ecmaVm, invalidStrRef, invalidStrRef);
1099 std::string mapDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, mapRef);
1100 ASSERT_TRUE(mapDesc.find(invalidStrInDebugger) != std::string::npos);
1101
1102 // WeakMap
1103 Local<WeakMapRef> weakMapRef = WeakMapRef::New(ecmaVm);
1104 weakMapRef->Set(ecmaVm, primitiveStr, invalidStrRef);
1105 std::string weakMapDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, weakMapRef);
1106 ASSERT_TRUE(weakMapDesc.find(invalidStrInDebugger) != std::string::npos);
1107
1108 // Set
1109 Local<SetRef> setRef = SetRef::New(ecmaVm);
1110 setRef->Add(ecmaVm, invalidStrRef);
1111 std::string setDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, setRef);
1112 ASSERT_TRUE(setDesc.find(invalidStrInDebugger) != std::string::npos);
1113
1114 // WeakSet
1115 Local<WeakSetRef> weakSetRef = WeakSetRef::New(ecmaVm);
1116 weakSetRef->Add(ecmaVm, invalidStrRef);
1117 std::string weakSetDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, weakSetRef);
1118 ASSERT_TRUE(weakSetDesc.find(invalidStrInDebugger) != std::string::npos);
1119
1120 // Error
1121 Local<JSValueRef> error = Exception::Error(ecmaVm, invalidStrRef);
1122 std::string errorDesc = ObjectRemoteObject::DescriptionForObject(ecmaVm, error);
1123 ASSERT_TRUE(errorDesc.find(invalidStrInDebugger) != std::string::npos);
1124
1125 // Symbol
1126 Local<SymbolRef> symbol = SymbolRef::New(ecmaVm, invalidStrRef);
1127 SymbolRemoteObject symbolObj(ecmaVm, symbol);
1128 std::string symbolDesc = symbolObj.GetDescription();
1129 ASSERT_TRUE(symbolDesc.find(invalidStrInDebugger) != std::string::npos);
1130 }
1131 } // namespace panda::test
1132