106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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 <fcntl.h> 1706f6ba60Sopenharmony_ci#include <cmath> 1806f6ba60Sopenharmony_ci#include <cstdio> 1906f6ba60Sopenharmony_ci#include <string> 2006f6ba60Sopenharmony_ci#include <vector> 2106f6ba60Sopenharmony_ci 2206f6ba60Sopenharmony_ci#include <gtest/gtest.h> 2306f6ba60Sopenharmony_ci 2406f6ba60Sopenharmony_ci#include "ringbuffer.h" 2506f6ba60Sopenharmony_ci 2606f6ba60Sopenharmony_ciusing namespace testing::ext; 2706f6ba60Sopenharmony_ci 2806f6ba60Sopenharmony_cinamespace { 2906f6ba60Sopenharmony_ciconstexpr int32_t BUFFER_SIZE = 1 * 1024 * 1024; 3006f6ba60Sopenharmony_ciconst std::string READ_FILE_NAME = "/data/local/tmp/hiebpfreadtest.txt"; 3106f6ba60Sopenharmony_ciconst std::string WRITE_FILE_NAME = "/data/local/tmp/hiebpfwritetest.txt"; 3206f6ba60Sopenharmony_ciconstexpr int FILE_MODE = 0777; 3306f6ba60Sopenharmony_ci} // namespace 3406f6ba60Sopenharmony_ci 3506f6ba60Sopenharmony_cinamespace OHOS { 3606f6ba60Sopenharmony_cinamespace Developtools { 3706f6ba60Sopenharmony_cinamespace Hiebpf { 3806f6ba60Sopenharmony_ciclass RingbufferTest : public ::testing::Test { 3906f6ba60Sopenharmony_cipublic: 4006f6ba60Sopenharmony_ci static void SetUpTestCase() {}; 4106f6ba60Sopenharmony_ci static void TearDownTestCase() 4206f6ba60Sopenharmony_ci { 4306f6ba60Sopenharmony_ci if (access(READ_FILE_NAME.c_str(), F_OK) == 0) { 4406f6ba60Sopenharmony_ci std::string cmd = "rm " + READ_FILE_NAME; 4506f6ba60Sopenharmony_ci system(cmd.c_str()); 4606f6ba60Sopenharmony_ci } 4706f6ba60Sopenharmony_ci if (access(WRITE_FILE_NAME.c_str(), F_OK) == 0) { 4806f6ba60Sopenharmony_ci std::string cmd = "rm " + WRITE_FILE_NAME; 4906f6ba60Sopenharmony_ci system(cmd.c_str()); 5006f6ba60Sopenharmony_ci } 5106f6ba60Sopenharmony_ci } 5206f6ba60Sopenharmony_ci 5306f6ba60Sopenharmony_ci void SetUp() {} 5406f6ba60Sopenharmony_ci void TearDown() {} 5506f6ba60Sopenharmony_ci}; 5606f6ba60Sopenharmony_ci 5706f6ba60Sopenharmony_ci/** 5806f6ba60Sopenharmony_ci * @tc.name: RingbufferByte 5906f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer DEFAULT 6006f6ba60Sopenharmony_ci * @tc.type: FUNC 6106f6ba60Sopenharmony_ci */ 6206f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, RingbufferDefault, TestSize.Level1) 6306f6ba60Sopenharmony_ci{ 6406f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 6506f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(0, memAlign); 6606f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 6706f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 6806f6ba60Sopenharmony_ci ASSERT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 6906f6ba60Sopenharmony_ci} 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci/** 7206f6ba60Sopenharmony_ci * @tc.name: RingbufferByte 7306f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer B_ALIGN_SHIFT 7406f6ba60Sopenharmony_ci * @tc.type: FUNC 7506f6ba60Sopenharmony_ci */ 7606f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, RingbufferByte, TestSize.Level1) 7706f6ba60Sopenharmony_ci{ 7806f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 7906f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, memAlign); 8006f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 8106f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 8206f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 8306f6ba60Sopenharmony_ci 8406f6ba60Sopenharmony_ci const size_t size = 263; // 100000111 8506f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(size, memAlign); 8606f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 8706f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 8806f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, size); 8906f6ba60Sopenharmony_ci} 9006f6ba60Sopenharmony_ci 9106f6ba60Sopenharmony_ci/** 9206f6ba60Sopenharmony_ci * @tc.name: RingbufferHalfWord 9306f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer H_ALIGN_SHIFT 9406f6ba60Sopenharmony_ci * @tc.type: FUNC 9506f6ba60Sopenharmony_ci */ 9606f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, RingbufferHalfWord, TestSize.Level1) 9706f6ba60Sopenharmony_ci{ 9806f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::H_ALIGN_SHIFT}; 9906f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, memAlign); 10006f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 10106f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 10206f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 10306f6ba60Sopenharmony_ci 10406f6ba60Sopenharmony_ci const size_t size = 263; // 100000111 10506f6ba60Sopenharmony_ci const int bit = RingBuffer::MemAlignShift::H_ALIGN_SHIFT; 10606f6ba60Sopenharmony_ci const size_t expectSize = (pow(2, bit)) * (size >> bit); // 2*131(10000011) 10706f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(size, memAlign); 10806f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 10906f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 11006f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, expectSize); 11106f6ba60Sopenharmony_ci} 11206f6ba60Sopenharmony_ci 11306f6ba60Sopenharmony_ci/** 11406f6ba60Sopenharmony_ci * @tc.name: RingbufferWord 11506f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer W_ALIGN_SHIFT 11606f6ba60Sopenharmony_ci * @tc.type: FUNC 11706f6ba60Sopenharmony_ci */ 11806f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, RingbufferWord, TestSize.Level1) 11906f6ba60Sopenharmony_ci{ 12006f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::W_ALIGN_SHIFT}; 12106f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, memAlign); 12206f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 12306f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 12406f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 12506f6ba60Sopenharmony_ci 12606f6ba60Sopenharmony_ci const size_t size = 263; // 100000111 12706f6ba60Sopenharmony_ci const int bit = RingBuffer::MemAlignShift::W_ALIGN_SHIFT; 12806f6ba60Sopenharmony_ci const size_t expectSize = (pow(2, bit)) * (size >> bit); // 4*65(10000011) 12906f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(size, memAlign); 13006f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 13106f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 13206f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, expectSize); 13306f6ba60Sopenharmony_ci} 13406f6ba60Sopenharmony_ci 13506f6ba60Sopenharmony_ci/** 13606f6ba60Sopenharmony_ci * @tc.name: RingbufferDoubleWord 13706f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer D_ALIGN_SHIFT 13806f6ba60Sopenharmony_ci * @tc.type: FUNC 13906f6ba60Sopenharmony_ci */ 14006f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, RingbufferDoubleWord, TestSize.Level1) 14106f6ba60Sopenharmony_ci{ 14206f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::D_ALIGN_SHIFT}; 14306f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, memAlign); 14406f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 14506f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 14606f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 14706f6ba60Sopenharmony_ci 14806f6ba60Sopenharmony_ci const size_t size = 263; // 100000111 14906f6ba60Sopenharmony_ci const int bit = RingBuffer::MemAlignShift::D_ALIGN_SHIFT; 15006f6ba60Sopenharmony_ci const size_t expectSize = (pow(2, bit)) * (size >> bit); // 8*32(1000001) 15106f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(size, memAlign); 15206f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 15306f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 15406f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, expectSize); 15506f6ba60Sopenharmony_ci} 15606f6ba60Sopenharmony_ci 15706f6ba60Sopenharmony_ci/** 15806f6ba60Sopenharmony_ci * @tc.name: Capacity 15906f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Capacity 16006f6ba60Sopenharmony_ci * @tc.type: FUNC 16106f6ba60Sopenharmony_ci */ 16206f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, Capacity, TestSize.Level1) 16306f6ba60Sopenharmony_ci{ 16406f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 16506f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, memAlign); 16606f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 16706f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->Capacity(), BUFFER_SIZE); 16806f6ba60Sopenharmony_ci} 16906f6ba60Sopenharmony_ci 17006f6ba60Sopenharmony_ci/** 17106f6ba60Sopenharmony_ci * @tc.name: ReadAndWrite 17206f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Read and Write 17306f6ba60Sopenharmony_ci * @tc.type: FUNC 17406f6ba60Sopenharmony_ci */ 17506f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, ReadAndWrite, TestSize.Level1) 17606f6ba60Sopenharmony_ci{ 17706f6ba60Sopenharmony_ci int readFd = open(READ_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 17806f6ba60Sopenharmony_ci ASSERT_GT(readFd, 0); 17906f6ba60Sopenharmony_ci std::string testStr = "this is hiebpf test file"; 18006f6ba60Sopenharmony_ci for (int i = testStr.size(); i < RingBuffer::BufferSize::DEFAULT_SIZE - 1; i++) { 18106f6ba60Sopenharmony_ci testStr += "1"; 18206f6ba60Sopenharmony_ci } 18306f6ba60Sopenharmony_ci int bytes = write(readFd, testStr.data(), testStr.size()); 18406f6ba60Sopenharmony_ci EXPECT_EQ(bytes, testStr.size()); 18506f6ba60Sopenharmony_ci close(readFd); 18606f6ba60Sopenharmony_ci 18706f6ba60Sopenharmony_ci readFd = open(READ_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 18806f6ba60Sopenharmony_ci ASSERT_GT(readFd, 0); 18906f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 19006f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(1, memAlign); 19106f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 19206f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 19306f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 19406f6ba60Sopenharmony_ci 19506f6ba60Sopenharmony_ci size_t ret = ringBuffer->Read(-1, 1); 19606f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 19706f6ba60Sopenharmony_ci ret = ringBuffer->Read(readFd, 0); 19806f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 19906f6ba60Sopenharmony_ci const int expectBytes = 20; 20006f6ba60Sopenharmony_ci auto len = ringBuffer->Read(readFd, expectBytes); 20106f6ba60Sopenharmony_ci EXPECT_EQ(len, expectBytes); 20206f6ba60Sopenharmony_ci close(readFd); 20306f6ba60Sopenharmony_ci 20406f6ba60Sopenharmony_ci int writeFd = open(WRITE_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 20506f6ba60Sopenharmony_ci ASSERT_GT(writeFd, 0); 20606f6ba60Sopenharmony_ci 20706f6ba60Sopenharmony_ci ret = ringBuffer->Write(-1, 1); 20806f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 20906f6ba60Sopenharmony_ci ret = ringBuffer->Write(writeFd, len); 21006f6ba60Sopenharmony_ci EXPECT_EQ(ret, len); 21106f6ba60Sopenharmony_ci close(writeFd); 21206f6ba60Sopenharmony_ci 21306f6ba60Sopenharmony_ci // The data length is greater than the buffer size, need to splitte read. 21406f6ba60Sopenharmony_ci readFd = open(READ_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 21506f6ba60Sopenharmony_ci ASSERT_GT(readFd, 0); 21606f6ba60Sopenharmony_ci len = ringBuffer->Read(readFd, bytes); 21706f6ba60Sopenharmony_ci EXPECT_EQ(len, bytes); 21806f6ba60Sopenharmony_ci close(readFd); 21906f6ba60Sopenharmony_ci 22006f6ba60Sopenharmony_ci writeFd = open(WRITE_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 22106f6ba60Sopenharmony_ci ASSERT_GT(writeFd, 0); 22206f6ba60Sopenharmony_ci ret = ringBuffer->Write(writeFd, len); 22306f6ba60Sopenharmony_ci EXPECT_EQ(ret, len); 22406f6ba60Sopenharmony_ci close(writeFd); 22506f6ba60Sopenharmony_ci} 22606f6ba60Sopenharmony_ci 22706f6ba60Sopenharmony_ci/** 22806f6ba60Sopenharmony_ci * @tc.name: PutAndWriteChar 22906f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Put and Write(char) 23006f6ba60Sopenharmony_ci * @tc.type: FUNC 23106f6ba60Sopenharmony_ci */ 23206f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, PutAndWriteChar, TestSize.Level1) 23306f6ba60Sopenharmony_ci{ 23406f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 23506f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(1, memAlign); 23606f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 23706f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 23806f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 23906f6ba60Sopenharmony_ci 24006f6ba60Sopenharmony_ci std::string testStr = "this is hiebpf test file"; 24106f6ba60Sopenharmony_ci size_t ret = ringBuffer->Put(nullptr, 1); 24206f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 24306f6ba60Sopenharmony_ci ret = ringBuffer->Put(testStr.c_str(), 0); 24406f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 24506f6ba60Sopenharmony_ci auto len = ringBuffer->Put(testStr.c_str(), testStr.size()); 24606f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 24706f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->tail_, testStr.size()); 24806f6ba60Sopenharmony_ci 24906f6ba60Sopenharmony_ci int writeFd = open(WRITE_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 25006f6ba60Sopenharmony_ci ASSERT_GT(writeFd, 0); 25106f6ba60Sopenharmony_ci 25206f6ba60Sopenharmony_ci ret = ringBuffer->Write(-1, 1); 25306f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 25406f6ba60Sopenharmony_ci ret = ringBuffer->Write(writeFd, len); 25506f6ba60Sopenharmony_ci EXPECT_EQ(ret, len); 25606f6ba60Sopenharmony_ci close(writeFd); 25706f6ba60Sopenharmony_ci} 25806f6ba60Sopenharmony_ci 25906f6ba60Sopenharmony_ci/** 26006f6ba60Sopenharmony_ci * @tc.name: PutAndWriteStr 26106f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Put and Write(str) 26206f6ba60Sopenharmony_ci * @tc.type: FUNC 26306f6ba60Sopenharmony_ci */ 26406f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, PutAndWriteStr, TestSize.Level1) 26506f6ba60Sopenharmony_ci{ 26606f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 26706f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(0, memAlign); 26806f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 26906f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 27006f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 27106f6ba60Sopenharmony_ci 27206f6ba60Sopenharmony_ci size_t ret = ringBuffer->Put(""); 27306f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 27406f6ba60Sopenharmony_ci ret = ringBuffer->Put("\0"); 27506f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 27606f6ba60Sopenharmony_ci std::string testStr = "this is hiebpf test file"; 27706f6ba60Sopenharmony_ci auto len = ringBuffer->Put(testStr); 27806f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 27906f6ba60Sopenharmony_ci 28006f6ba60Sopenharmony_ci int writeFd = open(WRITE_FILE_NAME.c_str(), O_CREAT | O_RDWR, FILE_MODE); 28106f6ba60Sopenharmony_ci ASSERT_GT(writeFd, 0); 28206f6ba60Sopenharmony_ci 28306f6ba60Sopenharmony_ci ret = ringBuffer->Write(-1, 1); 28406f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 28506f6ba60Sopenharmony_ci ret = ringBuffer->Write(writeFd, len); 28606f6ba60Sopenharmony_ci EXPECT_EQ(ret, len); 28706f6ba60Sopenharmony_ci close(writeFd); 28806f6ba60Sopenharmony_ci} 28906f6ba60Sopenharmony_ci 29006f6ba60Sopenharmony_ci/** 29106f6ba60Sopenharmony_ci * @tc.name: PutAndGetStr 29206f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Put and Get(str) 29306f6ba60Sopenharmony_ci * @tc.type: FUNC 29406f6ba60Sopenharmony_ci */ 29506f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, PutAndGetStr, TestSize.Level1) 29606f6ba60Sopenharmony_ci{ 29706f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 29806f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(0, memAlign); 29906f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 30006f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 30106f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 30206f6ba60Sopenharmony_ci 30306f6ba60Sopenharmony_ci std::string testStr = "this is hiebpf test file"; 30406f6ba60Sopenharmony_ci auto len = ringBuffer->Put(testStr); 30506f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 30606f6ba60Sopenharmony_ci 30706f6ba60Sopenharmony_ci std::vector<char> buff; 30806f6ba60Sopenharmony_ci buff.resize(testStr.size() + 1); 30906f6ba60Sopenharmony_ci len = ringBuffer->Get(nullptr, 1); 31006f6ba60Sopenharmony_ci EXPECT_EQ(len, 0); 31106f6ba60Sopenharmony_ci len = ringBuffer->Get(buff.data(), 0); 31206f6ba60Sopenharmony_ci EXPECT_EQ(len, 0); 31306f6ba60Sopenharmony_ci len = ringBuffer->Get(buff.data(), RingBuffer::BufferSize::DEFAULT_SIZE + 1); 31406f6ba60Sopenharmony_ci EXPECT_EQ(len, 0); 31506f6ba60Sopenharmony_ci len = ringBuffer->Get(buff.data(), testStr.size()); 31606f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 31706f6ba60Sopenharmony_ci 31806f6ba60Sopenharmony_ci // need to splitte data 31906f6ba60Sopenharmony_ci for (int i = testStr.size() + 1; i < RingBuffer::BufferSize::DEFAULT_SIZE; i++) { 32006f6ba60Sopenharmony_ci testStr += "1"; 32106f6ba60Sopenharmony_ci } 32206f6ba60Sopenharmony_ci len = ringBuffer->Put(testStr); 32306f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 32406f6ba60Sopenharmony_ci buff.resize(testStr.size() + 1); 32506f6ba60Sopenharmony_ci len = ringBuffer->Get(buff.data(), testStr.size()); 32606f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 32706f6ba60Sopenharmony_ci 32806f6ba60Sopenharmony_ci // The data length is greater than the buffer size, need to resize. 32906f6ba60Sopenharmony_ci testStr += "11111111"; 33006f6ba60Sopenharmony_ci len = ringBuffer->Put(testStr); 33106f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 33206f6ba60Sopenharmony_ci buff.resize(testStr.size() + 1); 33306f6ba60Sopenharmony_ci len = ringBuffer->Get(buff.data(), testStr.size()); 33406f6ba60Sopenharmony_ci EXPECT_EQ(len, testStr.size()); 33506f6ba60Sopenharmony_ci} 33606f6ba60Sopenharmony_ci 33706f6ba60Sopenharmony_ci/** 33806f6ba60Sopenharmony_ci * @tc.name: Allocate 33906f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Allocate 34006f6ba60Sopenharmony_ci * @tc.type: FUNC 34106f6ba60Sopenharmony_ci */ 34206f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, Allocate, TestSize.Level1) 34306f6ba60Sopenharmony_ci{ 34406f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, RingBuffer::MemAlignShift::B_ALIGN_SHIFT); 34506f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 34606f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 34706f6ba60Sopenharmony_ci auto newBuffer = ringBuffer->Allocate(1); 34806f6ba60Sopenharmony_ci ASSERT_TRUE(newBuffer != nullptr); 34906f6ba60Sopenharmony_ci 35006f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, RingBuffer::MemAlignShift::H_ALIGN_SHIFT); 35106f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 35206f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 35306f6ba60Sopenharmony_ci newBuffer = ringBuffer->Allocate(1); 35406f6ba60Sopenharmony_ci ASSERT_TRUE(newBuffer != nullptr); 35506f6ba60Sopenharmony_ci 35606f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, RingBuffer::MemAlignShift::W_ALIGN_SHIFT); 35706f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 35806f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 35906f6ba60Sopenharmony_ci newBuffer = ringBuffer->Allocate(1); 36006f6ba60Sopenharmony_ci ASSERT_TRUE(newBuffer != nullptr); 36106f6ba60Sopenharmony_ci 36206f6ba60Sopenharmony_ci ringBuffer = std::make_unique<RingBuffer>(BUFFER_SIZE, RingBuffer::MemAlignShift::D_ALIGN_SHIFT); 36306f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 36406f6ba60Sopenharmony_ci EXPECT_EQ(ringBuffer->bufSize_, BUFFER_SIZE); 36506f6ba60Sopenharmony_ci newBuffer = ringBuffer->Allocate(1); 36606f6ba60Sopenharmony_ci ASSERT_TRUE(newBuffer != nullptr); 36706f6ba60Sopenharmony_ci} 36806f6ba60Sopenharmony_ci 36906f6ba60Sopenharmony_ci/** 37006f6ba60Sopenharmony_ci * @tc.name: Peek 37106f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Peek 37206f6ba60Sopenharmony_ci * @tc.type: FUNC 37306f6ba60Sopenharmony_ci */ 37406f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, Peek, TestSize.Level1) 37506f6ba60Sopenharmony_ci{ 37606f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 37706f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(1, memAlign); 37806f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 37906f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 38006f6ba60Sopenharmony_ci 38106f6ba60Sopenharmony_ci uint32_t expectNum = 0; 38206f6ba60Sopenharmony_ci auto ret = ringBuffer->Peek(&expectNum); // ringBuffer no data 38306f6ba60Sopenharmony_ci EXPECT_EQ(ret, -1); 38406f6ba60Sopenharmony_ci 38506f6ba60Sopenharmony_ci uint32_t num = 4294967295; 38606f6ba60Sopenharmony_ci char* ptr = reinterpret_cast<char *>(&num); 38706f6ba60Sopenharmony_ci auto len = ringBuffer->Put(ptr, sizeof(ptr)); 38806f6ba60Sopenharmony_ci EXPECT_EQ(len, sizeof(ptr)); 38906f6ba60Sopenharmony_ci 39006f6ba60Sopenharmony_ci ret = ringBuffer->Peek(&expectNum); 39106f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 39206f6ba60Sopenharmony_ci EXPECT_EQ(num, expectNum); 39306f6ba60Sopenharmony_ci 39406f6ba60Sopenharmony_ci // need to splitte data 39506f6ba60Sopenharmony_ci for (int i = sizeof(ptr) + 1; i < RingBuffer::BufferSize::DEFAULT_SIZE - 1; i++) { 39606f6ba60Sopenharmony_ci len = ringBuffer->Put("2", 1); 39706f6ba60Sopenharmony_ci EXPECT_EQ(len, 1); 39806f6ba60Sopenharmony_ci } 39906f6ba60Sopenharmony_ci len = ringBuffer->Put("3"); 40006f6ba60Sopenharmony_ci EXPECT_EQ(len, 1); 40106f6ba60Sopenharmony_ci const int size = RingBuffer::BufferSize::DEFAULT_SIZE - 1; 40206f6ba60Sopenharmony_ci char buff[size + 1] = {0}; 40306f6ba60Sopenharmony_ci ret = ringBuffer->Get(buff, size); 40406f6ba60Sopenharmony_ci EXPECT_STRNE(buff, ""); 40506f6ba60Sopenharmony_ci EXPECT_EQ(ret, size); 40606f6ba60Sopenharmony_ci len = ringBuffer->Put(ptr, sizeof(ptr)); 40706f6ba60Sopenharmony_ci EXPECT_EQ(len, sizeof(ptr)); 40806f6ba60Sopenharmony_ci 40906f6ba60Sopenharmony_ci expectNum = 0; 41006f6ba60Sopenharmony_ci ret = ringBuffer->Peek(&expectNum); 41106f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 41206f6ba60Sopenharmony_ci EXPECT_EQ(num, expectNum); 41306f6ba60Sopenharmony_ci} 41406f6ba60Sopenharmony_ci 41506f6ba60Sopenharmony_ci/** 41606f6ba60Sopenharmony_ci * @tc.name: Resize 41706f6ba60Sopenharmony_ci * @tc.desc: Test Ringbuffer Resize 41806f6ba60Sopenharmony_ci * @tc.type: FUNC 41906f6ba60Sopenharmony_ci */ 42006f6ba60Sopenharmony_ciHWTEST_F(RingbufferTest, Resize, TestSize.Level1) 42106f6ba60Sopenharmony_ci{ 42206f6ba60Sopenharmony_ci constexpr enum RingBuffer::MemAlignShift memAlign {RingBuffer::MemAlignShift::B_ALIGN_SHIFT}; 42306f6ba60Sopenharmony_ci auto ringBuffer = std::make_unique<RingBuffer>(1, memAlign); 42406f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer != nullptr); 42506f6ba60Sopenharmony_ci ASSERT_TRUE(ringBuffer->buffer_ != nullptr); 42606f6ba60Sopenharmony_ci ASSERT_EQ(ringBuffer->bufSize_, RingBuffer::BufferSize::DEFAULT_SIZE); 42706f6ba60Sopenharmony_ci 42806f6ba60Sopenharmony_ci auto oldSize = ringBuffer->bufSize_; 42906f6ba60Sopenharmony_ci auto ret = ringBuffer->Resize(); 43006f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 43106f6ba60Sopenharmony_ci ASSERT_EQ(ringBuffer->bufSize_, oldSize << 1); 43206f6ba60Sopenharmony_ci 43306f6ba60Sopenharmony_ci // data splitted 43406f6ba60Sopenharmony_ci std::string putStr = ""; 43506f6ba60Sopenharmony_ci std::string testStr = "222222"; 43606f6ba60Sopenharmony_ci std::string destStr = "this is hiebpf test file"; 43706f6ba60Sopenharmony_ci const int size = destStr.length(); 43806f6ba60Sopenharmony_ci ret = ringBuffer->Put(destStr); 43906f6ba60Sopenharmony_ci EXPECT_EQ(ret, size); 44006f6ba60Sopenharmony_ci while (putStr.size() < (ringBuffer->bufSize_ - size - 1)) { 44106f6ba60Sopenharmony_ci putStr += "1"; 44206f6ba60Sopenharmony_ci } 44306f6ba60Sopenharmony_ci ret = ringBuffer->Put(putStr.c_str()); 44406f6ba60Sopenharmony_ci EXPECT_EQ(ret, putStr.size()); 44506f6ba60Sopenharmony_ci char buff[size + 1]; 44606f6ba60Sopenharmony_ci memset_s(buff, sizeof(buff), 0, sizeof(buff)); 44706f6ba60Sopenharmony_ci ret = ringBuffer->Get(buff, size); 44806f6ba60Sopenharmony_ci EXPECT_EQ(ret, size); 44906f6ba60Sopenharmony_ci EXPECT_STREQ(buff, destStr.c_str()); 45006f6ba60Sopenharmony_ci ret = ringBuffer->Put(testStr); 45106f6ba60Sopenharmony_ci EXPECT_EQ(ret, testStr.size()); 45206f6ba60Sopenharmony_ci 45306f6ba60Sopenharmony_ci oldSize = ringBuffer->bufSize_; 45406f6ba60Sopenharmony_ci ret = ringBuffer->Resize(); 45506f6ba60Sopenharmony_ci EXPECT_EQ(ret, 0); 45606f6ba60Sopenharmony_ci ASSERT_EQ(ringBuffer->bufSize_, oldSize << 1); 45706f6ba60Sopenharmony_ci} 45806f6ba60Sopenharmony_ci} // namespace Hiebpf 45906f6ba60Sopenharmony_ci} // namespace Developtools 46006f6ba60Sopenharmony_ci} // namespace OHOS 461