From 6ff85bfc1fba23e913ceb9ed3d17abb5f7577283 Mon Sep 17 00:00:00 2001 From: Riachel Date: Wed, 23 Nov 2022 01:47:19 +0000 Subject: [PATCH] Descriptor: add runtime ut tests details: add runtime ut tests issue:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I601LU Signed-off-by: Riachel --- tooling/test/BUILD.gn | 1 + tooling/test/heapprofiler_impl_test.cpp | 36 +++++ tooling/test/runtime_impl_test.cpp | 201 ++++++++++++++++++++++++ 3 files changed, 238 insertions(+) create mode 100644 tooling/test/runtime_impl_test.cpp diff --git a/tooling/test/BUILD.gn b/tooling/test/BUILD.gn index 4fff3eb1..41da3561 100644 --- a/tooling/test/BUILD.gn +++ b/tooling/test/BUILD.gn @@ -173,6 +173,7 @@ host_unittest_action("DebuggerTest") { "js_pt_hooks_test.cpp", "pt_base64_test.cpp", "pt_json_test.cpp", + "runtime_impl_test.cpp", ] configs = [ "//arkcompiler/toolchain:toolchain_test_config" ] diff --git a/tooling/test/heapprofiler_impl_test.cpp b/tooling/test/heapprofiler_impl_test.cpp index 348d21d7..fd3e8550 100644 --- a/tooling/test/heapprofiler_impl_test.cpp +++ b/tooling/test/heapprofiler_impl_test.cpp @@ -204,6 +204,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplCollectGarbage) DispatchRequest request(msg); dispatcherImpl->CollectGarbage(request); ASSERT_TRUE(result.find("CollectGarbage not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplEnable) @@ -218,6 +222,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplEnable) DispatchRequest request(msg); dispatcherImpl->Enable(request); ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDisable) @@ -232,6 +240,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDisable) DispatchRequest request(msg); dispatcherImpl->Disable(request); ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetHeapObjectId) @@ -250,6 +262,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetHeapObjectId) DispatchRequest request1(msg); dispatcherImpl->GetHeapObjectId(request1); ASSERT_TRUE(result.find("GetHeapObjectId not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetObjectByHeapObjectId) @@ -270,6 +286,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetObjectByHeapObjectId) DispatchRequest request1(msg); dispatcherImpl->GetObjectByHeapObjectId(request1); ASSERT_TRUE(result.find("GetObjectByHeapObjectId not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetSamplingProfile) @@ -284,6 +304,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetSamplingProfile) DispatchRequest request(msg); dispatcherImpl->GetSamplingProfile(request); ASSERT_TRUE(result.find("GetSamplingProfile not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStartSampling) @@ -303,6 +327,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStartSampling) DispatchRequest request1(msg); dispatcherImpl->StartSampling(request1); ASSERT_TRUE(result.find("StartSampling not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStopSampling) @@ -317,6 +345,10 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStopSampling) DispatchRequest request(msg); dispatcherImpl->StopSampling(request); ASSERT_TRUE(result.find("StopSampling not support now") != std::string::npos); + if (channel) { + delete channel; + channel = nullptr; + } } HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplTakeHeapSnapshot) @@ -341,5 +373,9 @@ HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplTakeHeapSnapshot) DispatchRequest request1(msg); dispatcherImpl->TakeHeapSnapshot(request1); ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel) { + delete channel; + channel = nullptr; + } } } // namespace panda::test diff --git a/tooling/test/runtime_impl_test.cpp b/tooling/test/runtime_impl_test.cpp new file mode 100644 index 00000000..587e6bfd --- /dev/null +++ b/tooling/test/runtime_impl_test.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2022 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 "agent/runtime_impl.h" +#include "ecmascript/tests/test_helper.h" +#include "tooling/protocol_handler.h" + +using namespace panda::ecmascript; +using namespace panda::ecmascript::tooling; + + +namespace panda::test { +class RuntimeImplTest : 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(RuntimeImplTest, DispatcherImplDispatch) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.test","params":{}})"; + DispatchRequest request(msg); + dispatcherImpl->Dispatch(request); + ASSERT_TRUE(result.find("unknown method: test") != std::string::npos); + + msg = std::string() + R"({"id":0,"method":"Rumtime.enable","params":{}})"; + DispatchRequest request1(msg); + dispatcherImpl->Dispatch(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplEnable) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.enable","params":{}})"; + DispatchRequest request1(msg); + dispatcherImpl->Enable(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplDisable) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.disable","params":{}})"; + DispatchRequest request1(msg); + dispatcherImpl->Disable(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplRunIfWaitingForDebugger) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.runIfWaitingForDebugger","params":{}})"; + DispatchRequest request1(msg); + dispatcherImpl->RunIfWaitingForDebugger(request1); + ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}"); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetProperties) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.getProperties","params":{"objectId":0}})"; + DispatchRequest request(msg); + dispatcherImpl->GetProperties(request); + ASSERT_TRUE(result.find("wrong params") != std::string::npos); + + msg = std::string() + R"({"id":0,"method":"Rumtime.getProperties","params":{"objectId":"0"}})"; + DispatchRequest request1(msg); + dispatcherImpl->GetProperties(request1); + ASSERT_TRUE(result.find("Unknown object id") != std::string::npos); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplCallFunctionOn) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.callFunctionOn","params":{ + "functionDeclaration":0}})"; + DispatchRequest request(msg); + dispatcherImpl->GetProperties(request); + ASSERT_TRUE(result.find("wrong params") != std::string::npos); + + msg = std::string() + R"({"id":0,"method":"Rumtime.getProperties","params":{"functionDeclaration":"test"}})"; + DispatchRequest request1(msg); + dispatcherImpl->CallFunctionOn(request1); + ASSERT_TRUE(result.find("Unsupport eval now") != std::string::npos); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} + +HWTEST_F_L0(RuntimeImplTest, DispatcherImplGetHeapUsage) +{ + 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 runtimeImpl = std::make_unique(ecmaVm, channel); + auto dispatcherImpl = std::make_unique(channel, std::move(runtimeImpl)); + + std::string msg = std::string() + R"({"id":0,"method":"Rumtime.getHeapUsage ","params":{}})"; + DispatchRequest request(msg); + dispatcherImpl->GetHeapUsage(request); + ASSERT_TRUE(result.find("usedSize") != std::string::npos); + ASSERT_TRUE(result.find("totalSize") != std::string::npos); + if (channel != nullptr) { + delete channel; + channel = nullptr; + } +} +} // namespace panda::test -- Gitee