• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "plugin_buffer_unittest.h"
17 
18 using namespace testing;
19 using namespace testing::ext;
20 using namespace OHOS::Media;
21 using namespace OHOS::Media::Plugins;
22 
23 const static uint8_t TEST_IN = 100;
24 constexpr size_t MEM_VALID_POSITION = 0;
25 constexpr size_t TEST_WRITE_SIZE = 100;
26 constexpr size_t TEST_READ_SIZE = 100;
27 
28 /**
29  * @tc.name  : Test Memory
30  * @tc.number: Memory_001
31  * @tc.desc  : Test Memory !allocMem
32  */
33 HWTEST_F(PluginBufferUnitTest, Memory_001, TestSize.Level1)
34 {
35     bool allocMem = false;
36     Memory memory(capacity_, nullptr, align_, type_, allocMem);
37     EXPECT_TRUE(memory.addr == nullptr);
38 }
39 
40 /**
41  * @tc.name  : Test Write
42  * @tc.number: Write_001
43  * @tc.desc  : Test Write position == MEM_INVALID_POSITION
44  */
45 HWTEST_F(PluginBufferUnitTest, Write_001, TestSize.Level1)
46 {
47     const uint8_t in = TEST_IN;
48     size_t writeSize = TEST_WRITE_SIZE;
49     size_t position = MEM_INVALID_POSITION;
50     size_t result = memory_.Write(&in, writeSize, position);
51     EXPECT_EQ(result, writeSize);
52 }
53 
54 /**
55  * @tc.name  : Test Read
56  * @tc.number: Read_001
57  * @tc.desc  : Test Read position != MEM_INVALID_POSITION
58  */
59 HWTEST_F(PluginBufferUnitTest, Read_001, TestSize.Level1)
60 {
61     size_t position = MEM_VALID_POSITION;
62     const uint8_t in = TEST_IN;
63     size_t writeSize = TEST_WRITE_SIZE;
64     memory_.Write(&in, writeSize, position);
65     uint8_t out[100] = {0};
66     size_t readSize = TEST_READ_SIZE;
67     size_t result = memory_.Read(out, readSize, position);
68     EXPECT_EQ(result, readSize);
69 }
70 
71 /**
72  * @tc.name  : Test Read
73  * @tc.number: Read_002
74  * @tc.desc  : Test Read position == MEM_INVALID_POSITION
75  */
76 HWTEST_F(PluginBufferUnitTest, Read_002, TestSize.Level1)
77 {
78     size_t position = MEM_VALID_POSITION;
79     const uint8_t in = TEST_IN;
80     size_t writeSize = TEST_WRITE_SIZE;
81     memory_.Write(&in, writeSize, position);
82     uint8_t out[100] = {0};
83     size_t readSize = TEST_READ_SIZE;
84     position = MEM_INVALID_POSITION;
85     size_t result = memory_.Read(out, readSize, position);
86     EXPECT_EQ(result, readSize);
87 }
88 
89 /**
90  * @tc.name  : Test GetReadOnlyData
91  * @tc.number: GetReadOnlyData_001
92  * @tc.desc  : Test GetReadOnlyData position > capacity_
93  */
94 HWTEST_F(PluginBufferUnitTest, GetReadOnlyData_001, TestSize.Level1)
95 {
96     size_t position = capacity_ + 1;
97     const uint8_t* data = memory_.GetReadOnlyData(position);
98     EXPECT_EQ(data, nullptr);
99 }
100 
101 /**
102  * @tc.name  : Test GetWritableAddr
103  * @tc.number: GetWritableAddr_001
104  * @tc.desc  : Test GetWritableAddr position + estimatedWriteSize > capacity_
105  */
106 HWTEST_F(PluginBufferUnitTest, GetWritableAddr_001, TestSize.Level1)
107 {
108     size_t estimatedWriteSize = capacity_ + 1;
109     size_t position = MEM_VALID_POSITION;
110     uint8_t* writableAddr = memory_.GetWritableAddr(estimatedWriteSize, position);
111     EXPECT_EQ(writableAddr, nullptr);
112 }
113 
114 /**
115  * @tc.name  : Test UpdateDataSize
116  * @tc.number: UpdateDataSize_001
117  * @tc.desc  : Test UpdateDataSize position + realWriteSize > capacity_
118  */
119 HWTEST_F(PluginBufferUnitTest, UpdateDataSize_001, TestSize.Level1)
120 {
121     size_t realWriteSize = capacity_ + 1;
122     size_t position = MEM_VALID_POSITION;
123     memory_.UpdateDataSize(realWriteSize, position);
124     EXPECT_EQ(memory_.GetSize(), 0);
125 }