/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "tooling/base/pt_types.h" #include "tooling/base/pt_returns.h" #include "dispatcher.h" #include "ecmascript/js_array.h" #include "ecmascript/js_object-inl.h" #include "ecmascript/js_tagged_value-inl.h" #include "ecmascript/object_factory.h" #include "ecmascript/tests/test_helper.h" using namespace panda::ecmascript; using namespace panda::ecmascript::tooling; namespace panda::test { // Duplicate name of panda::ecmascript::PropertyDescriptor in js_object-inl.h using panda::ecmascript::tooling::PropertyDescriptor; using ObjectType = RemoteObject::TypeName; using ObjectSubType = RemoteObject::SubTypeName; using ObjectClassName = RemoteObject::ClassName; class DebuggerReturnsTest : public testing::Test { public: static void SetUpTestCase() { GTEST_LOG_(INFO) << "SetUpTestCase"; } static void TearDownTestCase() { GTEST_LOG_(INFO) << "TearDownCase"; } void SetUp() override { TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope); } void TearDown() override { TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope); } protected: EcmaVM *ecmaVm {nullptr}; EcmaHandleScope *scope {nullptr}; JSThread *thread {nullptr}; }; HWTEST_F_L0(DebuggerReturnsTest, DebuggerEnableReturnsToJsonTest) { std::vector list {}; std::unique_ptr enableReturns = std::make_unique(100U, list); ASSERT_NE(enableReturns, nullptr); std::string debuggerId; ASSERT_EQ(enableReturns->ToJson()->GetString("debuggerId", &debuggerId), Result::SUCCESS); EXPECT_EQ(debuggerId, "100"); } HWTEST_F_L0(DebuggerReturnsTest, SetBreakpointByUrlReturnsToJsonTest) { auto locations = std::vector>(); std::unique_ptr location = std::make_unique(); location->SetScriptId(1).SetLine(99); locations.emplace_back(std::move(location)); std::unique_ptr setBreakpointByUrlReturns = std::make_unique("11", std::move(locations)); ASSERT_NE(setBreakpointByUrlReturns, nullptr); std::string id; ASSERT_EQ(setBreakpointByUrlReturns->ToJson()->GetString("breakpointId", &id), Result::SUCCESS); EXPECT_EQ(id, "11"); std::unique_ptr locationsJson; ASSERT_EQ(setBreakpointByUrlReturns->ToJson()->GetArray("locations", &locationsJson), Result::SUCCESS); ASSERT_NE(locationsJson, nullptr); EXPECT_EQ(locationsJson->GetSize(), 1); } HWTEST_F_L0(DebuggerReturnsTest, EvaluateOnCallFrameReturnsToJsonTest) { std::unique_ptr result = std::make_unique(); result->SetType("idle"); std::unique_ptr exceptionDetails = std::make_unique(); exceptionDetails->SetExceptionId(12); std::unique_ptr evaluateOnCallFrameReturns = std::make_unique(std::move(result), std::move(exceptionDetails)); ASSERT_NE(evaluateOnCallFrameReturns, nullptr); std::unique_ptr json; ASSERT_EQ(evaluateOnCallFrameReturns->ToJson()->GetObject("result", &json), Result::SUCCESS); std::string type; ASSERT_EQ(json->GetString("type", &type), Result::SUCCESS); EXPECT_EQ(type, "idle"); std::unique_ptr tmpJson; ASSERT_EQ(evaluateOnCallFrameReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::SUCCESS); int32_t exceptionId; ASSERT_EQ(tmpJson->GetInt("exceptionId", &exceptionId), Result::SUCCESS); EXPECT_EQ(exceptionId, 12); } HWTEST_F_L0(DebuggerReturnsTest, GetPossibleBreakpointsReturnsToJsonTest) { auto locations = std::vector>(); std::unique_ptr breakLocation = std::make_unique(); breakLocation->SetScriptId(11).SetLine(1).SetColumn(44).SetType("idel5"); locations.emplace_back(std::move(breakLocation)); std::unique_ptr getPossibleBreakpointsReturns = std::make_unique (std::move(locations)); std::unique_ptr locationsJson; ASSERT_EQ(getPossibleBreakpointsReturns->ToJson()->GetArray("locations", &locationsJson), Result::SUCCESS); ASSERT_NE(locationsJson, nullptr); EXPECT_EQ(locationsJson->GetSize(), 1); } HWTEST_F_L0(DebuggerReturnsTest, GetScriptSourceReturnsToJsonTest) { std::unique_ptr getScriptSourceReturns = std::make_unique ("source_1", "bytecode_1"); ASSERT_NE(getScriptSourceReturns, nullptr); std::string scriptSource; ASSERT_EQ(getScriptSourceReturns->ToJson()->GetString("scriptSource", &scriptSource), Result::SUCCESS); EXPECT_EQ(scriptSource, "source_1"); std::string bytecode; ASSERT_EQ(getScriptSourceReturns->ToJson()->GetString("bytecode", &bytecode), Result::SUCCESS); EXPECT_EQ(bytecode, "bytecode_1"); } HWTEST_F_L0(DebuggerReturnsTest, RestartFrameReturnsToJsonTest) { auto callFrames = std::vector>(); std::unique_ptr callFrame = std::make_unique(); std::unique_ptr location = std::make_unique(); location->SetScriptId(13).SetLine(16); std::unique_ptr res = std::make_unique(); res->SetType("idle2"); callFrame->SetCallFrameId(55); callFrame->SetLocation(std::move(location)); callFrame->SetThis(std::move(res)); callFrames.emplace_back(std::move(callFrame)); std::unique_ptr restartFrameReturns = std::make_unique (std::move(callFrames)); std::unique_ptr json; ASSERT_EQ(restartFrameReturns->ToJson()->GetArray("callFrames", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(DebuggerReturnsTest, SetBreakpointReturnsToJsonTest) { std::unique_ptr location = std::make_unique(); std::unique_ptr setBreakpointReturns = std::make_unique("breakpointId_1", std::move(location)); ASSERT_NE(setBreakpointReturns, nullptr); std::string breakpointId; ASSERT_EQ(setBreakpointReturns->ToJson()->GetString("breakpointId", &breakpointId), Result::SUCCESS); EXPECT_EQ(breakpointId, "breakpointId_1"); std::unique_ptr tmpJson; ASSERT_EQ(setBreakpointReturns->ToJson()->GetObject("actualLocation", &tmpJson), Result::SUCCESS); } HWTEST_F_L0(DebuggerReturnsTest, SetInstrumentationBreakpointReturnsToJsonTest) { std::unique_ptr setInstrumentationBreakpointReturns = std::make_unique("111"); ASSERT_NE(setInstrumentationBreakpointReturns, nullptr); std::string breakpointId; ASSERT_EQ(setInstrumentationBreakpointReturns->ToJson()->GetString("breakpointId", &breakpointId), Result::SUCCESS); EXPECT_EQ(breakpointId, "111"); } HWTEST_F_L0(DebuggerReturnsTest, SetScriptSourceReturnsToJsonTest) { auto callFrames = std::vector>(); std::unique_ptr callFrame = std::make_unique(); std::unique_ptr location = std::make_unique(); location->SetScriptId(3).SetLine(36); std::unique_ptr res = std::make_unique(); res->SetType("idle5"); callFrame->SetCallFrameId(33); callFrame->SetLocation(std::move(location)); callFrame->SetThis(std::move(res)); callFrames.emplace_back(std::move(callFrame)); std::unique_ptr setScriptSourceReturns = std::make_unique (std::move(callFrames)); std::unique_ptr json; ASSERT_EQ(setScriptSourceReturns->ToJson()->GetArray("callFrames", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(DebuggerReturnsTest, GetPropertiesReturnsToJsonTest) { auto descriptor = std::vector>(); std::unique_ptr propertyDescriptor = std::make_unique(); propertyDescriptor->SetName("filename1").SetConfigurable(true).SetEnumerable(true); descriptor.emplace_back(std::move(propertyDescriptor)); std::unique_ptr getPropertiesReturns = std::make_unique (std::move(descriptor)); ASSERT_NE(getPropertiesReturns, nullptr); std::unique_ptr json; ASSERT_EQ(getPropertiesReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 1); } HWTEST_F_L0(DebuggerReturnsTest, CallFunctionOnReturnsToJsonTest) { std::unique_ptr result = std::make_unique(); result->SetType("idle2"); std::unique_ptr exceptionDetails = std::make_unique(); std::unique_ptr callFunctionOnReturns = std::make_unique(std::move(result), std::move(exceptionDetails)); ASSERT_NE(callFunctionOnReturns, nullptr); std::unique_ptr json; ASSERT_EQ(callFunctionOnReturns->ToJson()->GetObject("result", &json), Result::SUCCESS); std::string type; ASSERT_EQ(json->GetString("type", &type), Result::SUCCESS); EXPECT_EQ(type, "idle2"); std::unique_ptr tmpJson; ASSERT_EQ(callFunctionOnReturns->ToJson()->GetObject("exceptionDetails", &tmpJson), Result::SUCCESS); } HWTEST_F_L0(DebuggerReturnsTest, StopSamplingReturnsToJsonTest) { auto res = std::vector>(); std::unique_ptr runtime = std::make_unique(); std::unique_ptr node = std::make_unique(); node->SetCallFrame(std::move(runtime)); std::unique_ptr profile = std::make_unique(); profile->SetHead(std::move(node)); profile->SetSamples(std::move(res)); std::unique_ptr stopSamplingReturns = std::make_unique(std::move(profile)); ASSERT_NE(stopSamplingReturns, nullptr); std::unique_ptr json; ASSERT_EQ(stopSamplingReturns->ToJson()->GetObject("profile", &json), Result::SUCCESS); } HWTEST_F_L0(DebuggerReturnsTest, GetHeapObjectIdReturnsToJsonTest) { std::unique_ptr getHeapObjectIdReturns = std::make_unique(10); ASSERT_NE(getHeapObjectIdReturns, nullptr); std::string heapSnapshotObjectId; ASSERT_EQ(getHeapObjectIdReturns->ToJson()->GetString("heapSnapshotObjectId", &heapSnapshotObjectId), Result::SUCCESS); EXPECT_EQ(heapSnapshotObjectId, "10"); } HWTEST_F_L0(DebuggerReturnsTest, GetObjectByHeapObjectIdReturnsToJsonTest) { std::unique_ptr remoteObjectResult = std::make_unique(); remoteObjectResult->SetType("idle5"); std::unique_ptr getObjectByHeapObjectIdReturns = std::make_unique(std::move(remoteObjectResult)); ASSERT_NE(getObjectByHeapObjectIdReturns, nullptr); std::unique_ptr json; ASSERT_EQ(getObjectByHeapObjectIdReturns->ToJson()->GetObject("result", &json), Result::SUCCESS); std::string type; ASSERT_EQ(json->GetString("type", &type), Result::SUCCESS); EXPECT_EQ(type, "idle5"); } HWTEST_F_L0(DebuggerReturnsTest, StopReturnsToJsonTest) { std::unique_ptr profile = std::make_unique(); std::unique_ptr stopReturns= std::make_unique(std::move(profile)); ASSERT_NE(stopReturns, nullptr); std::unique_ptr json; ASSERT_EQ(stopReturns->ToJson()->GetObject("profile", &json), Result::SUCCESS); } HWTEST_F_L0(DebuggerReturnsTest, GetHeapUsageReturnsToJsonTest) { double usedSize = 1; double totalSize = 1; std::unique_ptr getHeapUsageReturns = std::make_unique(usedSize, totalSize); ASSERT_NE(getHeapUsageReturns, nullptr); double pUsedSize; ASSERT_EQ(getHeapUsageReturns->ToJson()->GetDouble("usedSize", &pUsedSize), Result::SUCCESS); EXPECT_EQ(pUsedSize, 1); double pTotalSize; ASSERT_EQ(getHeapUsageReturns->ToJson()->GetDouble("totalSize", &pTotalSize), Result::SUCCESS); EXPECT_EQ(pTotalSize, 1); } HWTEST_F_L0(DebuggerReturnsTest, GetBestEffortCoverageReturnsToJsonTest) { auto result = std::vector>(); std::unique_ptr scriptCoverage = std::make_unique(); std::unique_ptr getBestEffortCoverageReturns = std::make_unique(std::move(result)); std::unique_ptr json; ASSERT_EQ(getBestEffortCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 0); } HWTEST_F_L0(DebuggerReturnsTest, StartPreciseCoverageReturnsToJsonTest) { std::unique_ptr startPreciseCoverageReturns = std::make_unique(1001); ASSERT_NE(startPreciseCoverageReturns, nullptr); int32_t timestamp; ASSERT_EQ(startPreciseCoverageReturns->ToJson()->GetInt("timestamp", ×tamp), Result::SUCCESS); EXPECT_EQ(timestamp, 1001); } HWTEST_F_L0(DebuggerReturnsTest, TakePreciseCoverageReturnsToJsonTest) { auto coverage = std::vector>(); std::unique_ptr takePreciseCoverageReturns = std::make_unique(std::move(coverage), 1001); ASSERT_NE(takePreciseCoverageReturns, nullptr); std::unique_ptr json; ASSERT_EQ(takePreciseCoverageReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 0); int32_t timestamp; ASSERT_EQ(takePreciseCoverageReturns->ToJson()->GetInt("timestamp", ×tamp), Result::SUCCESS); EXPECT_EQ(timestamp, 1001); } HWTEST_F_L0(DebuggerReturnsTest, TakeTypeProfileturnsToJsonTest) { auto result = std::vector>(); std::unique_ptr scriptTypeProfile = std::make_unique(); std::unique_ptr takeTypeProfileturns = std::make_unique (std::move(result)); std::unique_ptr json; ASSERT_EQ(takeTypeProfileturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 0); } HWTEST_F_L0(DebuggerReturnsTest, GetCategoriesReturnsToJsonTest) { auto result = std::vector(); std::unique_ptr getCategoriesReturns = std::make_unique (std::move(result)); std::unique_ptr json; ASSERT_EQ(getCategoriesReturns->ToJson()->GetArray("categories", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 0); } HWTEST_F_L0(DebuggerReturnsTest, RequestMemoryDumpReturnsToJsonTest) { std::unique_ptr requestMemoryDumpReturns = std::make_unique("123", true); ASSERT_NE(requestMemoryDumpReturns, nullptr); std::string dumpGuid; ASSERT_EQ(requestMemoryDumpReturns->ToJson()->GetString("dumpGuid", &dumpGuid), Result::SUCCESS); EXPECT_EQ(dumpGuid, "123"); bool success; ASSERT_EQ(requestMemoryDumpReturns->ToJson()->GetBool("success", &success), Result::SUCCESS); ASSERT_TRUE(success); } HWTEST_F_L0(DebuggerReturnsTest, SearchInContentReturnsToJsonTest) { auto result = std::vector>(); std::unique_ptr searchMatch = std::make_unique(); std::unique_ptr searchInContentReturns = std::make_unique(std::move(result)); std::unique_ptr json; ASSERT_EQ(searchInContentReturns->ToJson()->GetArray("result", &json), Result::SUCCESS); ASSERT_NE(json, nullptr); EXPECT_EQ(json->GetSize(), 0); } HWTEST_F_L0(DebuggerReturnsTest, GetPossibleAndSetBreakpointByUrlReturnsToJsonTest) { auto locations = std::vector>(); std::unique_ptr breakpointReturnInfo = std::make_unique(); breakpointReturnInfo->SetScriptId(11).SetLineNumber(1).SetColumnNumber(44); locations.emplace_back(std::move(breakpointReturnInfo)); std::unique_ptr getPossibleAndSetBreakpointByUrlReturns = std::make_unique (std::move(locations)); std::unique_ptr locationsJS; ASSERT_EQ(getPossibleAndSetBreakpointByUrlReturns->ToJson()->GetArray("locations", &locationsJS), Result::SUCCESS); ASSERT_NE(locationsJS, nullptr); EXPECT_EQ(locationsJS->GetSize(), 1); } } // namespace panda::test