106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include <gtest/gtest.h> 1706f6ba60Sopenharmony_ci#include <sys/mman.h> 1806f6ba60Sopenharmony_ci#include <sys/syscall.h> 1906f6ba60Sopenharmony_ci#include "buffer_writer.h" 2006f6ba60Sopenharmony_ci#include "plugin_service_types.pb.h" 2106f6ba60Sopenharmony_ci 2206f6ba60Sopenharmony_ciusing namespace testing::ext; 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_cinamespace { 2506f6ba60Sopenharmony_ciconstexpr uint32_t SMB1_SIZE = 10 * 4096; 2606f6ba60Sopenharmony_ciconstexpr uint32_t SMB2_SIZE = 10 * 4096; 2706f6ba60Sopenharmony_ciconst std::string SMB1_NAME = "testsmb1"; 2806f6ba60Sopenharmony_ciconst std::string SMB2_NAME = "testsmb2"; 2906f6ba60Sopenharmony_ciconst std::string PLUGIN_NAME = "testplugin"; 3006f6ba60Sopenharmony_ciconst std::string PLUGIN_VERSION = "1.01"; 3106f6ba60Sopenharmony_civoid *g_smbAddr1 = nullptr; 3206f6ba60Sopenharmony_civoid *g_smbAddr2 = nullptr; 3306f6ba60Sopenharmony_ciint g_smbFd1 = 0; 3406f6ba60Sopenharmony_ciint g_smbFd2 = 0; 3506f6ba60Sopenharmony_ci 3606f6ba60Sopenharmony_ciint InitShareMemory1() 3706f6ba60Sopenharmony_ci{ 3806f6ba60Sopenharmony_ci int fd = syscall(SYS_memfd_create, SMB1_NAME.c_str(), 0); 3906f6ba60Sopenharmony_ci CHECK_TRUE(fd >= 0, -1, "CreateBlock FAIL SYS_memfd_create"); 4006f6ba60Sopenharmony_ci 4106f6ba60Sopenharmony_ci int check = ftruncate(fd, SMB1_SIZE); 4206f6ba60Sopenharmony_ci if (check < 0) { 4306f6ba60Sopenharmony_ci close(fd); 4406f6ba60Sopenharmony_ci const int bufSize = 256; 4506f6ba60Sopenharmony_ci char buf[bufSize] = { 0 }; 4606f6ba60Sopenharmony_ci strerror_r(errno, buf, bufSize); 4706f6ba60Sopenharmony_ci PROFILER_LOG_ERROR(LOG_CORE, "CreateBlock ftruncate ERR : %s", buf); 4806f6ba60Sopenharmony_ci return -1; 4906f6ba60Sopenharmony_ci } 5006f6ba60Sopenharmony_ci 5106f6ba60Sopenharmony_ci g_smbAddr1 = mmap(nullptr, SMB1_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 5206f6ba60Sopenharmony_ci if (g_smbAddr1 == static_cast<void*>(MAP_FAILED)) { 5306f6ba60Sopenharmony_ci close(fd); 5406f6ba60Sopenharmony_ci const int bufSize = 256; 5506f6ba60Sopenharmony_ci char buf[bufSize] = { 0 }; 5606f6ba60Sopenharmony_ci strerror_r(errno, buf, bufSize); 5706f6ba60Sopenharmony_ci PROFILER_LOG_ERROR(LOG_CORE, "CreateBlock g_smbAddr1 mmap ERR : %s", buf); 5806f6ba60Sopenharmony_ci return -1; 5906f6ba60Sopenharmony_ci } 6006f6ba60Sopenharmony_ci 6106f6ba60Sopenharmony_ci ShareMemoryBlock::BlockHeader* header_ = reinterpret_cast<ShareMemoryBlock::BlockHeader*>(g_smbAddr1); 6206f6ba60Sopenharmony_ci 6306f6ba60Sopenharmony_ci // initialize header infos 6406f6ba60Sopenharmony_ci header_->info.readOffset_ = 0; 6506f6ba60Sopenharmony_ci header_->info.writeOffset_ = 0; 6606f6ba60Sopenharmony_ci header_->info.memorySize_ = SMB1_SIZE - sizeof(ShareMemoryBlock::BlockHeader); 6706f6ba60Sopenharmony_ci header_->info.bytesCount_ = 0; 6806f6ba60Sopenharmony_ci header_->info.chunkCount_ = 0; 6906f6ba60Sopenharmony_ci 7006f6ba60Sopenharmony_ci return fd; 7106f6ba60Sopenharmony_ci} 7206f6ba60Sopenharmony_ci 7306f6ba60Sopenharmony_ciint InitShareMemory2() 7406f6ba60Sopenharmony_ci{ 7506f6ba60Sopenharmony_ci int fd = syscall(SYS_memfd_create, SMB2_NAME.c_str(), 0); 7606f6ba60Sopenharmony_ci CHECK_TRUE(fd >= 0, -1, "CreateBlock FAIL SYS_memfd_create"); 7706f6ba60Sopenharmony_ci 7806f6ba60Sopenharmony_ci int check = ftruncate(fd, SMB2_SIZE); 7906f6ba60Sopenharmony_ci if (check < 0) { 8006f6ba60Sopenharmony_ci close(fd); 8106f6ba60Sopenharmony_ci const int bufSize = 256; 8206f6ba60Sopenharmony_ci char buf[bufSize] = { 0 }; 8306f6ba60Sopenharmony_ci strerror_r(errno, buf, bufSize); 8406f6ba60Sopenharmony_ci PROFILER_LOG_ERROR(LOG_CORE, "CreateBlock ftruncate ERR : %s", buf); 8506f6ba60Sopenharmony_ci return -1; 8606f6ba60Sopenharmony_ci } 8706f6ba60Sopenharmony_ci 8806f6ba60Sopenharmony_ci g_smbAddr2 = mmap(nullptr, SMB2_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 8906f6ba60Sopenharmony_ci if (g_smbAddr2 == static_cast<void*>(MAP_FAILED)) { 9006f6ba60Sopenharmony_ci close(fd); 9106f6ba60Sopenharmony_ci const int bufSize = 256; 9206f6ba60Sopenharmony_ci char buf[bufSize] = { 0 }; 9306f6ba60Sopenharmony_ci strerror_r(errno, buf, bufSize); 9406f6ba60Sopenharmony_ci PROFILER_LOG_ERROR(LOG_CORE, "CreateBlock g_smbAddr2 mmap ERR : %s", buf); 9506f6ba60Sopenharmony_ci return -1; 9606f6ba60Sopenharmony_ci } 9706f6ba60Sopenharmony_ci 9806f6ba60Sopenharmony_ci ShareMemoryBlock::BlockHeader* header_ = reinterpret_cast<ShareMemoryBlock::BlockHeader*>(g_smbAddr2); 9906f6ba60Sopenharmony_ci 10006f6ba60Sopenharmony_ci // initialize header infos 10106f6ba60Sopenharmony_ci header_->info.readOffset_ = 0; 10206f6ba60Sopenharmony_ci header_->info.writeOffset_ = 0; 10306f6ba60Sopenharmony_ci header_->info.memorySize_ = SMB2_SIZE - sizeof(ShareMemoryBlock::BlockHeader); 10406f6ba60Sopenharmony_ci header_->info.bytesCount_ = 0; 10506f6ba60Sopenharmony_ci header_->info.chunkCount_ = 0; 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_ci return fd; 10806f6ba60Sopenharmony_ci} 10906f6ba60Sopenharmony_ci 11006f6ba60Sopenharmony_ciclass BufferWriteTest : public ::testing::Test { 11106f6ba60Sopenharmony_ciprotected: 11206f6ba60Sopenharmony_ci static void SetUpTestCase() 11306f6ba60Sopenharmony_ci { 11406f6ba60Sopenharmony_ci g_smbFd1 = InitShareMemory1(); 11506f6ba60Sopenharmony_ci g_smbFd2 = InitShareMemory2(); 11606f6ba60Sopenharmony_ci } 11706f6ba60Sopenharmony_ci static void TearDownTestCase() {} 11806f6ba60Sopenharmony_ci}; 11906f6ba60Sopenharmony_ci 12006f6ba60Sopenharmony_cibool CheckBuffer(uint8_t *buffer, size_t size) 12106f6ba60Sopenharmony_ci{ 12206f6ba60Sopenharmony_ci ShareMemoryBlock::BlockHeader* header_ = reinterpret_cast<ShareMemoryBlock::BlockHeader*>(g_smbAddr1); 12306f6ba60Sopenharmony_ci uint8_t *cmpaddr = (uint8_t *)g_smbAddr1 + sizeof(ShareMemoryBlock::BlockHeader) + header_->info.readOffset_.load(); 12406f6ba60Sopenharmony_ci uint32_t cmpsize = *(uint32_t*)cmpaddr; 12506f6ba60Sopenharmony_ci cmpaddr = cmpaddr + sizeof(uint32_t); 12606f6ba60Sopenharmony_ci ProfilerPluginData pluginData; 12706f6ba60Sopenharmony_ci pluginData.ParseFromArray(cmpaddr, cmpsize); 12806f6ba60Sopenharmony_ci const char* data = pluginData.data().c_str(); 12906f6ba60Sopenharmony_ci 13006f6ba60Sopenharmony_ci header_->info.readOffset_ = header_->info.writeOffset_.load(); 13106f6ba60Sopenharmony_ci if (memcmp(buffer, data, size) == 0) { 13206f6ba60Sopenharmony_ci return true; 13306f6ba60Sopenharmony_ci } 13406f6ba60Sopenharmony_ci return false; 13506f6ba60Sopenharmony_ci} 13606f6ba60Sopenharmony_ci 13706f6ba60Sopenharmony_ci 13806f6ba60Sopenharmony_cibool CheckMessage(uint8_t *buffer, size_t size) 13906f6ba60Sopenharmony_ci{ 14006f6ba60Sopenharmony_ci ShareMemoryBlock::BlockHeader* header_ = reinterpret_cast<ShareMemoryBlock::BlockHeader*>(g_smbAddr2); 14106f6ba60Sopenharmony_ci uint8_t *cmpaddr = (uint8_t *)g_smbAddr2 + sizeof(ShareMemoryBlock::BlockHeader) + header_->info.readOffset_.load(); 14206f6ba60Sopenharmony_ci cmpaddr = cmpaddr + sizeof(uint32_t); 14306f6ba60Sopenharmony_ci header_->info.readOffset_ = header_->info.writeOffset_.load(); 14406f6ba60Sopenharmony_ci 14506f6ba60Sopenharmony_ci if (memcmp(buffer, cmpaddr, size) == 0) { 14606f6ba60Sopenharmony_ci return true; 14706f6ba60Sopenharmony_ci } 14806f6ba60Sopenharmony_ci return false; 14906f6ba60Sopenharmony_ci} 15006f6ba60Sopenharmony_ci 15106f6ba60Sopenharmony_ci/** 15206f6ba60Sopenharmony_ci * @tc.name: plugin 15306f6ba60Sopenharmony_ci * @tc.desc: Write data to shared memory through writer. 15406f6ba60Sopenharmony_ci * @tc.type: FUNC 15506f6ba60Sopenharmony_ci */ 15606f6ba60Sopenharmony_ciHWTEST_F(BufferWriteTest, WriteTest, TestSize.Level1) 15706f6ba60Sopenharmony_ci{ 15806f6ba60Sopenharmony_ci auto write = std::make_shared<BufferWriter>(PLUGIN_NAME, PLUGIN_VERSION, SMB1_SIZE, g_smbFd1, -1, 0); 15906f6ba60Sopenharmony_ci uint8_t buffer1[] = {0x55, 0xAA, 0x55, 0xAA}; 16006f6ba60Sopenharmony_ci uint8_t buffer2[] = {0x11, 0x22, 0x33, 0x44}; 16106f6ba60Sopenharmony_ci uint8_t buffer3[] = {0xAA, 0xBB, 0xCC, 0xDD}; 16206f6ba60Sopenharmony_ci EXPECT_TRUE(write->Write(buffer1, sizeof(buffer1))); 16306f6ba60Sopenharmony_ci EXPECT_TRUE(CheckBuffer(buffer1, sizeof(buffer1))); 16406f6ba60Sopenharmony_ci 16506f6ba60Sopenharmony_ci EXPECT_TRUE(write->Write(buffer2, sizeof(buffer2))); 16606f6ba60Sopenharmony_ci EXPECT_TRUE(CheckBuffer(buffer2, sizeof(buffer2))); 16706f6ba60Sopenharmony_ci 16806f6ba60Sopenharmony_ci EXPECT_TRUE(write->Write(buffer3, sizeof(buffer3))); 16906f6ba60Sopenharmony_ci EXPECT_TRUE(CheckBuffer(buffer3, sizeof(buffer3))); 17006f6ba60Sopenharmony_ci EXPECT_FALSE(write->Write(nullptr, 0)); 17106f6ba60Sopenharmony_ci} 17206f6ba60Sopenharmony_ci 17306f6ba60Sopenharmony_ci/** 17406f6ba60Sopenharmony_ci * @tc.name: plugin 17506f6ba60Sopenharmony_ci * @tc.desc: Write data to shared memory through writer. 17606f6ba60Sopenharmony_ci * @tc.type: FUNC 17706f6ba60Sopenharmony_ci */ 17806f6ba60Sopenharmony_ciHWTEST_F(BufferWriteTest, WriteMessageTest, TestSize.Level1) 17906f6ba60Sopenharmony_ci{ 18006f6ba60Sopenharmony_ci uint8_t data[1024]; 18106f6ba60Sopenharmony_ci auto write = std::make_shared<BufferWriter>(PLUGIN_NAME, PLUGIN_VERSION, SMB2_SIZE, g_smbFd2, -1, 0); 18206f6ba60Sopenharmony_ci 18306f6ba60Sopenharmony_ci ProfilerPluginConfig configData; 18406f6ba60Sopenharmony_ci configData.set_name("111"); 18506f6ba60Sopenharmony_ci configData.set_plugin_sha256("222"); 18606f6ba60Sopenharmony_ci configData.set_sample_interval(1000); 18706f6ba60Sopenharmony_ci size_t size = configData.ByteSizeLong(); 18806f6ba60Sopenharmony_ci configData.SerializeToArray(data, size); 18906f6ba60Sopenharmony_ci 19006f6ba60Sopenharmony_ci EXPECT_TRUE(write->WriteMessage(configData, "111")); 19106f6ba60Sopenharmony_ci EXPECT_TRUE(CheckMessage(data, size)); 19206f6ba60Sopenharmony_ci 19306f6ba60Sopenharmony_ci ProfilerPluginState stateData; 19406f6ba60Sopenharmony_ci stateData.set_name("st"); 19506f6ba60Sopenharmony_ci stateData.set_state(ProfilerPluginState::IN_SESSION); 19606f6ba60Sopenharmony_ci size = stateData.ByteSizeLong(); 19706f6ba60Sopenharmony_ci stateData.SerializeToArray(data, size); 19806f6ba60Sopenharmony_ci 19906f6ba60Sopenharmony_ci EXPECT_TRUE(write->WriteMessage(stateData, "111")); 20006f6ba60Sopenharmony_ci EXPECT_TRUE(CheckMessage(data, size)); 20106f6ba60Sopenharmony_ci 20206f6ba60Sopenharmony_ci 20306f6ba60Sopenharmony_ci ProfilerPluginData pluginData; 20406f6ba60Sopenharmony_ci pluginData.set_name("test"); 20506f6ba60Sopenharmony_ci pluginData.set_status(1); 20606f6ba60Sopenharmony_ci struct timespec ts; 20706f6ba60Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &ts); 20806f6ba60Sopenharmony_ci pluginData.set_clock_id(ProfilerPluginData::CLOCKID_REALTIME); 20906f6ba60Sopenharmony_ci pluginData.set_tv_sec(ts.tv_sec); 21006f6ba60Sopenharmony_ci pluginData.set_tv_nsec(ts.tv_nsec); 21106f6ba60Sopenharmony_ci size = pluginData.ByteSizeLong(); 21206f6ba60Sopenharmony_ci pluginData.SerializeToArray(data, size); 21306f6ba60Sopenharmony_ci 21406f6ba60Sopenharmony_ci EXPECT_TRUE(write->WriteMessage(pluginData, "111")); 21506f6ba60Sopenharmony_ci EXPECT_TRUE(CheckMessage(data, size)); 21606f6ba60Sopenharmony_ci} 21706f6ba60Sopenharmony_ci} // namespace