1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
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 "buffer/avsharedmemory.h"
18 #include "buffer/avsharedmemorybase.h"
19 #include "common/status.h"
20 
21 constexpr int SIZE = 15;
22 
23 using namespace std;
24 using namespace testing::ext;
25 using namespace OHOS::Media;
26 
27 namespace OHOS {
28 namespace Media {
29 namespace AvsharedmemorybaseUnitTest {
30 class AvsharedmemorybaseUnitTest : public testing::Test {
31 public:
32     static void SetUpTestCase(void);
33 
34     static void TearDownTestCase(void);
35 
36     void SetUp(void);
37 
38     void TearDown(void);
39 };
40 
SetUpTestCase(void)41 void AvsharedmemorybaseUnitTest::SetUpTestCase(void) {}
42 
TearDownTestCase(void)43 void AvsharedmemorybaseUnitTest::TearDownTestCase(void) {}
44 
SetUp(void)45 void AvsharedmemorybaseUnitTest::SetUp(void) {}
46 
TearDown(void)47 void AvsharedmemorybaseUnitTest::TearDown(void) {}
48 
49 /**
50  * @tc.name: Test AVSharedMemoryBase::Read API
51  * @tc.desc: Test AVSharedMemoryBase::Read interface, set readSize to 0.
52  * @tc.type: FUNC
53  */
54 HWTEST_F(AvsharedmemorybaseUnitTest, Read_Test_001, TestSize.Level0)
55 {
56     std::shared_ptr<AVSharedMemoryBase> memory =
57         std::make_shared<AVSharedMemoryBase>(SIZE, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
58     EXPECT_EQ(memory->Read(nullptr, 0, 0), 0);
59 }
60 
61 /**
62  * @tc.name: Test Read API
63  * @tc.desc: Test Read interface, read data from memory.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(AvsharedmemorybaseUnitTest, Read, TestSize.Level0)
67 {
68     // 1. Set up the test environment
69     std::shared_ptr<AVSharedMemoryBase> memory =
70         std::make_shared<AVSharedMemoryBase>(SIZE, AVSharedMemory::Flags::FLAGS_READ_WRITE, "test");
71     ASSERT_TRUE(memory != nullptr);
72     int32_t ret = memory->Init();
73     ASSERT_TRUE(ret == static_cast<int32_t>(Status::OK));
74 
75     // 2. Call the function to be tested
76     uint8_t buffer[SIZE] = {0};
77     ret = memory->Write(buffer, SIZE);
78     ASSERT_TRUE(ret == SIZE);
79 
80     // 3. Verify the result
81     uint8_t readBuffer[SIZE] = {0};
82     ret = memory->Read(readBuffer, SIZE);
83     ASSERT_TRUE(ret == SIZE);
84     ASSERT_TRUE(memcmp(buffer, readBuffer, SIZE) == 0);
85 }
86 }  // namespace AvsharedmemorybaseUnitTest
87 }  // namespace Media
88 }  // namespace OHOS