diff --git a/tooling/agent/debugger_impl.h b/tooling/agent/debugger_impl.h index 9c84217347e5faa806b2e343c7ffd56239a5bfd7..6f304de74e32fec9c8ef925225fe421216300796 100644 --- a/tooling/agent/debugger_impl.h +++ b/tooling/agent/debugger_impl.h @@ -338,6 +338,26 @@ private: friend class JSPtHooks; friend class test::TestHooks; + friend class DebuggerImplFriendTest; }; + +class DebuggerImplFriendTest { +public: + DebuggerImplFriendTest(std::unique_ptr &debuggerImpl) { + debuggerImpl_ = std::move(debuggerImpl); + } + + void CheckAndGenerateCondFunc(const std::optional condition){ + debuggerImpl_->CheckAndGenerateCondFunc(condition); + } + + Local ConvertToLocal(const std::string &varValue){ + return debuggerImpl_->ConvertToLocal(varValue); + } + +private: + std::unique_ptr debuggerImpl_; +}; + } // namespace panda::ecmascript::tooling #endif \ No newline at end of file diff --git a/tooling/agent/heapprofiler_impl.h b/tooling/agent/heapprofiler_impl.h index b3cd4695db1d52c476a32a0e45fc1a49fff10bff..bbcd9f51dd4dd37bfbb535cbb20315a960f10b4a 100644 --- a/tooling/agent/heapprofiler_impl.h +++ b/tooling/agent/heapprofiler_impl.h @@ -197,6 +197,38 @@ private: #if defined(ECMASCRIPT_SUPPORT_HEAPPROFILER) uv_timer_t handle_ {}; #endif + + friend class HeapProfilerImplFriendTest; +}; + +class HeapProfilerImplFriendTest { +public: + HeapProfilerImplFriendTest(std::unique_ptr &heapprofilerImpl) { + heapprofilerImpl_ = std::move(heapprofilerImpl); + } + + void ResetProfiles() { + heapprofilerImpl_->frontend_.ResetProfiles(); + } + + void LastSeenObjectId(int32_t lastSeenObjectId, int64_t timeStampUs) { + heapprofilerImpl_->frontend_.LastSeenObjectId(lastSeenObjectId, timeStampUs); + } + + void HeapStatsUpdate(HeapStat* updateData, int32_t count) { + heapprofilerImpl_->frontend_.HeapStatsUpdate(updateData, count); + } + + void AddHeapSnapshotChunk(char *data, int32_t size) { + heapprofilerImpl_->frontend_.AddHeapSnapshotChunk(data, size); + } + + void ReportHeapSnapshotProgress(int32_t done, int32_t total) { + heapprofilerImpl_->frontend_.ReportHeapSnapshotProgress(done, total); + } +private: + std::unique_ptr heapprofilerImpl_; + }; } // namespace panda::ecmascript::tooling #endif diff --git a/tooling/test/debugger_impl_test.cpp b/tooling/test/debugger_impl_test.cpp index 4aaa03387abf6431c3779f56939d5e245c311b83..bb137d29f09cd9c29070f53f1c3657026622f36d 100644 --- a/tooling/test/debugger_impl_test.cpp +++ b/tooling/test/debugger_impl_test.cpp @@ -74,6 +74,28 @@ HWTEST_F_L0(DebuggerImplTest, NotifyScriptParsed__001) } } +HWTEST_F_L0(DebuggerImplTest, NotifyScriptParsed__002) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + ecmaVm->GetJsDebuggerManager()->SetIsDebugApp(true); + ecmaVm->GetJsDebuggerManager()->SetDebugMode(true); + // DebuggerImpl::NotifyScriptParsed -- fileName.substr(0, DATA_APP_PATH.length()) != DATA_APP_PATH + std::string strFilename = ""; + EXPECT_FALSE(debuggerImpl->NotifyScriptParsed(0, strFilename, "")); + ecmaVm->GetJsDebuggerManager()->SetIsDebugApp(false); + ecmaVm->GetJsDebuggerManager()->SetDebugMode(false); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + HWTEST_F_L0(DebuggerImplTest, GenerateCallFrames__001) { } @@ -259,6 +281,35 @@ HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_EvaluateOnCallFrame__003) } } +HWTEST_F_L0(DebuggerImplTest, EvaluateOnCallFrame__004) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + auto dispatcherImpl = std::make_unique(protocolChannel, std::move(debuggerImpl)); + std::string msg = std::string() + + R"({ + "id":0, + "method":"Debugger.enable", + "params":{ + "maxScriptsCacheSize":"NotIntValue", + "callFrameId":"0", + "expression":"the expression" + } + })"; + DispatchRequest request1(msg); + dispatcherImpl->EvaluateOnCallFrame(request1); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_GetPossibleBreakpoints__001) { std::string outStrForCallbackCheck = ""; @@ -1259,4 +1310,178 @@ HWTEST_F_L0(DebuggerImplTest, NativeOutTest) ASSERT_TRUE(result2); ASSERT_NE(jspthooks, nullptr); } + +HWTEST_F_L0(DebuggerImplTest, NotifyNativeReturn__001) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", false); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyNativeReturn(nullptr); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + +HWTEST_F_L0(DebuggerImplTest, NotifyNativeReturn__002) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", true); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyNativeReturn(nullptr); + //ASSERT_TRUE(debuggerImpl->mixStackEnabled_ == true); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} +HWTEST_F_L0(DebuggerImplTest, NotifyReturnNative__001) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", true); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyReturnNative(); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + +HWTEST_F_L0(DebuggerImplTest, NotifyReturnNative__002) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", false); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyReturnNative(); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + +HWTEST_F_L0(DebuggerImplTest, NotifyNativeCalling__001) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", false); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyNativeCalling(nullptr); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + +HWTEST_F_L0(DebuggerImplTest, NotifyNativeCalling__002) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + std::unique_ptr json = PtJson::CreateObject(); + json->Add("enabled", false); + json->Add("mixedStackEnabled", true); + std::unique_ptr params = SetMixedDebugParams::Create(*json); + debuggerImpl->SetMixedDebugEnabled(*params); + debuggerImpl->NotifyNativeCalling(nullptr); + if (protocolChannel) { + delete protocolChannel; + protocolChannel = nullptr; + } +} + +HWTEST_F_L0(DebuggerImplTest, CheckAndGenerateCondFunc__001) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + auto debugger = std::make_unique(debuggerImpl); + debugger->CheckAndGenerateCondFunc(""); + std::string msg = "UEFOREEAAAAAAAAADAACAEgBAAAAAAAAAAAAAAIAAAA8AAAAAQAA"; + msg += "AEQBAAAAAAARAAAAAEAAABEAAAAkQAAAMQAAAB8AAAASAEAAAIAAABsAAAAAgAAAHQAAAD//////////"; + debugger->CheckAndGenerateCondFunc(msg); +} + +HWTEST_F_L0(DebuggerImplTest, ConvertToLocal__001) +{ + std::string outStrForCallbackCheck = ""; + std::function callback = + [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) { + outStrForCallbackCheck = inStrOfReply;}; + ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm); + auto runtimeImpl = std::make_unique(ecmaVm, protocolChannel); + auto debuggerImpl = std::make_unique(ecmaVm, protocolChannel, runtimeImpl.get()); + auto debugger = std::make_unique(debuggerImpl); + Local taggedValue = debugger->ConvertToLocal(""); + ASSERT_TRUE(!taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("false"); + ASSERT_TRUE(!taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("true"); + ASSERT_TRUE(!taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("undefined"); + ASSERT_TRUE(!taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("\"test\""); + ASSERT_TRUE(!taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("test"); + ASSERT_TRUE(taggedValue.IsEmpty()); + taggedValue = debugger->ConvertToLocal("1"); + ASSERT_TRUE(!taggedValue.IsEmpty()); +} } // namespace panda::test diff --git a/tooling/test/heapprofiler_impl_test.cpp b/tooling/test/heapprofiler_impl_test.cpp index fe021a45acf95dfec9e8aac8be76b4b425d167c1..ead82f53b950b8d7802b7673b27b620b5d6c1dba 100644 --- a/tooling/test/heapprofiler_impl_test.cpp +++ b/tooling/test/heapprofiler_impl_test.cpp @@ -409,4 +409,115 @@ HWTEST_F_L0(HeapProfilerImplTest, StopSamplingSuccessful) DispatchResponse result = heapProfiler->StopSampling(&samplingHeapProfile); ASSERT_TRUE(result.IsOk()); } +HWTEST_F_L0(HeapProfilerImplTest, StartTrackingHeapObjects) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm); + auto tracing = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(tracing)); + std::string msg = ""; + msg = std::string() + R"({"id":0,"method":"HeapProfiler.StartTrackingHeapObjects","params":{"trackAllocations":0}})"; + DispatchRequest request1(msg); + dispatcherImpl->StartTrackingHeapObjects(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{\"code\":1,\"message\":\"wrong params\"}}"); + if (channel) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(HeapProfilerImplTest, StopTrackingHeapObjects) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm); + auto tracing = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(tracing)); + std::string msg = ""; + msg = std::string() + R"({"id":0,"method":"HeapProfiler.StopTrackingHeapObjects","params":{"reportProgress":0}})"; + DispatchRequest request1(msg); + dispatcherImpl->StopTrackingHeapObjects(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{\"code\":1,\"message\":\"wrong params\"}}"); + if (channel) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(HeapProfilerImplTest, ResetProfiles) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm); + auto tracing = std::make_unique(ecmaVm, nullptr); + auto heapprofiler = std::make_unique(tracing); + heapprofiler->ResetProfiles(); + tracing = std::make_unique(ecmaVm, channel); + heapprofiler = std::make_unique(tracing); + heapprofiler->ResetProfiles(); + if (channel) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(HeapProfilerImplTest, LastSeenObjectId) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm); + auto tracing = std::make_unique(ecmaVm, nullptr); + auto heapprofiler = std::make_unique(tracing); + heapprofiler->LastSeenObjectId(0, 0); + tracing = std::make_unique(ecmaVm, channel); + heapprofiler = std::make_unique(tracing); + heapprofiler->LastSeenObjectId(0, 0); + if (channel) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(HeapProfilerImplTest, HeapStatsUpdate) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm); + auto tracing = std::make_unique(ecmaVm, nullptr); + auto heapprofiler = std::make_unique(tracing); + heapprofiler->HeapStatsUpdate(nullptr, 0); + tracing = std::make_unique(ecmaVm, channel); + heapprofiler = std::make_unique(tracing); + heapprofiler->HeapStatsUpdate(nullptr, 0); + if (channel) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(HeapProfilerImplTest, AddHeapSnapshotChunk) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + auto tracing = std::make_unique(ecmaVm, nullptr); + auto heapprofiler = std::make_unique(tracing); + heapprofiler->AddHeapSnapshotChunk(nullptr, 0); +} + +HWTEST_F_L0(HeapProfilerImplTest, ReportHeapSnapshotProgress) +{ + std::string result = ""; + std::function callback = + [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;}; + auto tracing = std::make_unique(ecmaVm, nullptr); + auto heapprofiler = std::make_unique(tracing); + heapprofiler->ReportHeapSnapshotProgress(0, 0); +} } // namespace panda::test