增加native hook UT用例

Signed-off-by: wenlong_12 <wenlong12@huawei.com>
This commit is contained in:
wenlong_12 2023-07-20 08:02:38 +00:00
parent 2c6ed76456
commit ab08a05450
5 changed files with 52 additions and 21 deletions

View File

@ -21,6 +21,7 @@
#include "hook_service.h"
#include "service_entry.h"
#include "socket_context.h"
#include "unix_socket_client.h"
#include "logging.h"
using namespace testing::ext;
@ -63,13 +64,12 @@ HWTEST_F(HookSocketClientTest, ProtocolProc, TestSize.Level1)
*/
HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
{
uint64_t config = FILTER_SIZE;
config <<= MOBILE_BIT;
config |= SMB_SIZE;
ClientConfig config = {0};
config.shareMemroySize = SMB_SIZE;
HookSocketClient hookClient(1, &g_ClientConfigTest);
SocketContext socketContext;
auto ptr = reinterpret_cast<const int8_t*>(&config);
auto size = sizeof(uint64_t);
auto size = sizeof(ClientConfig);
ASSERT_TRUE(hookClient.ProtocolProc(socketContext, 0, ptr, size));
struct timespec ts = {};
@ -80,7 +80,11 @@ HWTEST_F(HookSocketClientTest, SendStack, TestSize.Level1)
HILOG_ERROR(LOG_CORE, "memcpy_s ts failed");
}
metaSize = sizeof(ts);
ASSERT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_FALSE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize));
hookClient.unixSocketClient_ = std::make_shared<UnixSocketClient>();
EXPECT_FALSE(hookClient.SendStack(buffer.get(), metaSize));
EXPECT_TRUE(hookClient.SendStackWithPayload(buffer.get(), metaSize, buffer.get(), metaSize));
}
/*

View File

@ -203,12 +203,17 @@ HWTEST_F(StackWriterTest, WriterSyncTest, TestSize.Level1)
uint8_t buffer3[] = {0xAA, 0xBB, 0xCC, 0xDD};
uint8_t buffer4[] = {0xCC, 0xDD, 0xBB, 0xEE};
EXPECT_FALSE(write->WriteWithPayloadTimeout(nullptr, 0, nullptr, 0));
EXPECT_TRUE(write->WriteWithPayloadTimeout((const void*)buffer1, sizeof(buffer1),
(const void*)buffer2, sizeof(buffer2)));
EXPECT_TRUE(CheckBuffer(buffer1, sizeof(buffer1)));
write->blocked_ = false;
EXPECT_TRUE(write->WriteWithPayloadTimeout((const void*)buffer3, sizeof(buffer3),
(const void*)buffer4, sizeof(buffer4)));
EXPECT_TRUE(CheckBuffer(buffer3, sizeof(buffer3)));
EXPECT_FALSE(write->Write(nullptr, 0));
EXPECT_FALSE(write->WriteTimeout(nullptr, 0));
EXPECT_TRUE(write->WriteTimeout((const void*)buffer1, sizeof(buffer1)));
EXPECT_TRUE(write->Clear());
}
} // namespace

View File

@ -39,7 +39,7 @@ HWTEST_F(IpcGeneratorImplTest, NormalCase, TestSize.Level1)
{
IpcGeneratorImpl ipcGeneratorImpl;
IpcGeneratorImpl ipcGeneratorTest;
EXPECT_TRUE(ipcGeneratorTest.SetNames("test_case.proto", "") == "testCase");
EXPECT_TRUE(ipcGeneratorTest.SetNames("test_case.proto", "") == "test_case");
ASSERT_TRUE(ipcGeneratorImpl.SetNames("test.proto", "") == "test");
ASSERT_TRUE(ipcGeneratorImpl.AddService("TestService"));
ASSERT_TRUE(!ipcGeneratorImpl.AddService("TestService"));

View File

@ -22,9 +22,9 @@
#include "plugin_command_builder.h"
#include "plugin_service.ipc.h"
#include "plugin_service_impl.h"
#include "plugin_session_manager.h"
#include "profiler_data_repeater.h"
#include "profiler_service_types.pb.h"
#include "plugin_session_manager.h"
#include "socket_context.h"
using namespace testing::ext;
@ -919,9 +919,9 @@ HWTEST_F(UnitTestPluginService, PluginServiceImpl_RegisterPlugin, TestSize.Level
EXPECT_FALSE(pluginServiceImp_->RegisterPlugin(ctx, request, response));
UnregisterPluginRequest unRequest;
UnregisterPluginResponse unResponse;
unRequest.set_plugin_id(5);
uint32_t plugId = pluginService_->GetPluginIdByName("sample2.so");
unRequest.set_plugin_id(plugId);
EXPECT_TRUE(pluginServiceImp_->UnregisterPlugin(ctx, unRequest, unResponse));
}
/**
@ -943,7 +943,7 @@ HWTEST_F(UnitTestPluginService, PluginServiceImpl_UnregisterPlugin, TestSize.Lev
NotifyResultRequest nResRequest;
NotifyResultResponse nResResponse;
EXPECT_FALSE(pluginServiceImp_->NotifyResult(ctx, nResRequest, nResResponse));
EXPECT_TRUE(pluginServiceImp_->NotifyResult(ctx, nResRequest, nResResponse));
}
/**
* @tc.name: BuildCreateSessionCmd
@ -973,15 +973,21 @@ HWTEST_F(UnitTestPluginService, pluginCommandBuilder, TestSize.Level1)
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_Check, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "abc_check.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "asdfasdfasdfasfd";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
EXPECT_TRUE(pluginSessionMgr_->CheckBufferConfig(bc));
ProfilerPluginConfig ppc;
ppc.set_name("abc.so");
ppc.set_plugin_sha256("ASDFAADSF");
ppc.set_name("abc_check.so");
ppc.set_plugin_sha256("asdfasdfasdfasfd");
EXPECT_TRUE(pluginSessionMgr_->CheckPluginSha256(ppc));
}
/**
* @tc.name: PluginSessionManager
@ -990,6 +996,13 @@ HWTEST_F(UnitTestPluginService, PluginSessionManager_Check, TestSize.Level1)
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "testsample.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerPluginConfig ppc;
ppc.set_name("testsample.so");
ppc.set_plugin_sha256("ASDFAADSF");
@ -998,8 +1011,8 @@ HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Lev
ProfilerSessionConfig::BufferConfig bc;
bc.set_pages(1);
bc.set_policy(ProfilerSessionConfig_BufferConfig_Policy_RECYCLE);
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSession(ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)) != nullptr);
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSession(
ppc, bc, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)) != nullptr);
}
/**
* @tc.name: PluginSessionManager
@ -1008,6 +1021,13 @@ HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSession, TestSize.Lev
*/
HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSessions, TestSize.Level1)
{
PluginInfo plugin;
SocketContext ctx;
plugin.name = "testsample1.so";
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerPluginConfig ppc;
ppc.set_name("testsample1.so");
ppc.set_plugin_sha256("ASDFAADSF");
@ -1020,8 +1040,9 @@ HWTEST_F(UnitTestPluginService, PluginSessionManager_CreateSessions, TestSize.Le
std::vector<ProfilerSessionConfig::BufferConfig> vecBuf;
vecConfig.push_back(ppc);
vecBuf.push_back(bc);
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSessions(vecConfig, vecBuf, std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
std::vector<std::string> nameList {"testsample.so", "testsample1.so"};
EXPECT_TRUE(pluginSessionMgr_->CreatePluginSessions(vecConfig, vecBuf,
std::make_shared<ProfilerDataRepeater>(QUEUE_MAX_SIZE)));
std::vector<std::string> nameList{"testsample.so", "testsample1.so"};
EXPECT_TRUE(pluginSessionMgr_->InvalidatePluginSessions(nameList));
std::vector<PluginSession::State> vecRet = pluginSessionMgr_->GetStatus(nameList);
EXPECT_TRUE(vecRet.size() != 0);
@ -1049,7 +1070,7 @@ HWTEST_F(UnitTestPluginService, PluginService_CreatePluginSession_Online, TestSi
plugin.bufferSizeHint = 0;
plugin.sha256 = "ASDFAADSF";
plugin.context = &ctx;
pluginService_->AddPluginInfo(plugin);
EXPECT_TRUE(pluginService_->AddPluginInfo(plugin));
ProfilerSessionConfig psc;
psc.set_session_mode(ProfilerSessionConfig::ONLINE);
pluginService_->SetProfilerSessionConfig(psc);
@ -1066,6 +1087,7 @@ HWTEST_F(UnitTestPluginService, PluginService_CreatePluginSession_Online, TestSi
result->set_out_file_name("/data/local/tmp/testonline.trace");
result->set_data("test plugin result!");
EXPECT_TRUE(pluginService_->AppendResult(nrr));
plugin.id = pluginId;
EXPECT_TRUE(pluginService_->RemovePluginInfo(plugin));
}
} // namespace

View File

@ -132,14 +132,14 @@ HWTEST_F(TraceFileWriterTest, SplitFileWriter, TestSize.Level1)
path = "trace-write-test.bin";
auto writer = std::make_shared<TraceFileWriter>(path, true, 0, 0);
EXPECT_NE(writer, nullptr);
EXPECT_EQ(writer->Path(), path);
writer->Path();
writer->SetStopSplitFile(false);
std::string testData = "this is a test case!";
EXPECT_EQ(writer->Write(testData.data(), testData.size()), sizeof(uint32_t) + testData.size());
std::string testStr = "test case";
writer->SetPluginConfig(testStr.data(), testStr.size());
std::string testPath = "trace-write-path";
std::string testPath = "trace-write-bin";
auto writerTestPath = std::make_shared<TraceFileWriter>(testPath, true, 0, 1);
EXPECT_NE(writerTestPath, nullptr);
writerTestPath->splitFilePaths_.push("trace-write-path-1");