1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 <gtest/gtest.h> 17 #include "hook_service.h" 18 #include "hook_manager.h" 19 #include "hook_socket_client.h" 20 #include "socket_context.h" 21 #include "share_memory_allocator.h" 22 #include "event_notifier.h" 23 #include "command_poller.h" 24 #include "native_hook_config.pb.h" 25 26 using namespace testing::ext; 27 using namespace OHOS::Developtools::NativeDaemon; 28 29 namespace { 30 class HookServiceTest : public ::testing::Test { 31 public: SetUpTestCase()32 static void SetUpTestCase() {} TearDownTestCase()33 static void TearDownTestCase() {} SetUp()34 void SetUp() {} TearDown()35 void TearDown() {} 36 }; 37 38 /* 39 * @tc.name: ProtocolProc 40 * @tc.desc: test HookService::ProtocolProc with normal case. 41 * @tc.type: FUNC 42 */ 43 HWTEST_F(HookServiceTest, ProtocolProc, TestSize.Level1) 44 { 45 std::shared_ptr<HookManager> hookManager = std::make_shared<HookManager>(); 46 ASSERT_TRUE(hookManager != nullptr); 47 48 ProfilerPluginConfig config; 49 config.set_name("nativehook"); 50 config.set_plugin_sha256(""); 51 config.set_sample_interval(20); 52 53 NativeHookConfig hookConfig; 54 hookConfig.set_pid(1); 55 hookConfig.set_smb_pages(16384); 56 hookConfig.set_startup_mode(true); 57 std::vector<uint8_t> buffer(hookConfig.ByteSizeLong()); 58 hookConfig.SerializeToArray(buffer.data(), hookConfig.ByteSizeLong()); 59 config.set_config_data(buffer.data(), buffer.size()); 60 61 PluginResult result; 62 std::vector<ProfilerPluginConfig> configVec; 63 configVec.push_back(config); 64 EXPECT_TRUE(hookManager->CreatePluginSession(configVec)); 65 66 SocketContext socketContext; 67 int pid = 1; 68 const int8_t* pidPtr = reinterpret_cast<const int8_t*>(&pid); 69 ClientConfig clientConfig; 70 auto hookService = std::make_shared<HookService>(clientConfig, hookManager); 71 ASSERT_TRUE(hookService != nullptr); 72 ASSERT_TRUE(hookService->ProtocolProc(socketContext, 0, pidPtr, sizeof(pid))); 73 } 74 } // namespace 75