1 /*
2  * Copyright (c) 2022-2023 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 <hilog/log.h>
18 #include "buffer_handle.h"
19 
20 #include "buffer_handle_utils.h"
21 #include "buffer_handle_parcel.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 class BufferHandleTest : public testing::Test {
27 public:
28     static constexpr HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, 0xD001400, "graphicutils" };
29 
SetUpTestCase()30     static void SetUpTestCase()
31     {}
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {}
35 };
36 
37 /*
38 * Function: BufferHandleTest
39 * Type: Function
40 * Rank: Important(2)
41 * EnvConditions: N/A
42 * CaseDescription: AllocateBufferHandle
43 */
44 HWTEST_F(BufferHandleTest, AllocateBufferHandle, Function | SmallTest | Level2)
45 {
46     uint32_t fds = 1025, ints = 1025;
47     uint32_t buffer_handle_reserve_max_size = 1024;
48     ASSERT_EQ(nullptr, AllocateBufferHandle(fds, ints));
49     ASSERT_EQ(nullptr, AllocateBufferHandle(fds, buffer_handle_reserve_max_size));
50     ASSERT_EQ(nullptr, AllocateBufferHandle(buffer_handle_reserve_max_size, ints));
51     BufferHandle *handle = AllocateBufferHandle(buffer_handle_reserve_max_size, buffer_handle_reserve_max_size);
52     ASSERT_NE(nullptr, handle);
53     ASSERT_EQ(0, FreeBufferHandle(handle));
54 }
55 
56 /*
57 * Function: BufferHandleTest
58 * Type: Function
59 * Rank: Important(2)
60 * EnvConditions: N/A
61 * CaseDescription: AllocateBufferHandle
62 */
63 HWTEST_F(BufferHandleTest, AllocateBufferHandle001, Function | SmallTest | Level2)
64 {
65     uint32_t fds = 1025, ints = 1025;
66     uint32_t testsize = 100;
67     auto test1 = AllocateBufferHandle(fds, ints);
68     ASSERT_EQ(nullptr, test1);
69 
70     auto test2 = AllocateBufferHandle(fds, testsize);
71     ASSERT_EQ(nullptr, test2);
72 
73     auto test3 = AllocateBufferHandle(testsize, ints);
74     ASSERT_EQ(nullptr, test3);
75 
76     auto test4 = AllocateBufferHandle(testsize, testsize);
77     ASSERT_NE(nullptr, test4);
78     ASSERT_EQ(0, FreeBufferHandle(test4));
79 }
80 
81 /*
82 * Function: BufferHandleTest
83 * Type: Function
84 * Rank: Important(2)
85 * EnvConditions: N/A
86 * CaseDescription: FreeBufferHandle
87 */
88 HWTEST_F(BufferHandleTest, FreeBufferHandle, Function | SmallTest | Level2)
89 {
90     uint32_t buffer_handle_reserve_max_size = 1024;
91     ASSERT_EQ(0, FreeBufferHandle(nullptr));
92     BufferHandle *handle = AllocateBufferHandle(buffer_handle_reserve_max_size, buffer_handle_reserve_max_size);
93     ASSERT_NE(nullptr, handle);
94     ASSERT_EQ(0, FreeBufferHandle(handle));
95 }
96 
97 /*
98 * Function: BufferHandleTest
99 * Type: Function
100 * Rank: Important(2)
101 * EnvConditions: N/A
102 * CaseDescription: FreeBufferHandle
103 */
104 HWTEST_F(BufferHandleTest, FreeBufferHandle001, Function | SmallTest | Level2)
105 {
106     uint32_t testsize = 100;
107     auto test1 = FreeBufferHandle(nullptr);
108     ASSERT_EQ(0, test1);
109 
110     BufferHandle *handle = AllocateBufferHandle(testsize, testsize);
111     ASSERT_NE(nullptr, handle);
112     handle->fd = 0;
113     handle->reserve[0] = 0;
114     auto test2 = FreeBufferHandle(handle);
115     ASSERT_EQ(0, test2);
116 }
117 
118 /*
119 * Function: BufferHandleTest
120 * Type: Function
121 * Rank: Important(2)
122 * EnvConditions: N/A
123 * CaseDescription: OHOS::WriteBufferHandle
124 */
125 HWTEST_F(BufferHandleTest, WriteBufferHandle, Function | SmallTest | Level2)
126 {
127     MessageParcel parcel;
128     uint32_t buffer_handle_reserve_max_size = 1024;
129     BufferHandle *handle = AllocateBufferHandle(buffer_handle_reserve_max_size, buffer_handle_reserve_max_size);
130     ASSERT_NE(nullptr, handle);
131 
132     ASSERT_EQ(false, WriteBufferHandle(parcel, *handle));
133 
134     ASSERT_EQ(0, FreeBufferHandle(handle));
135 }
136 
137 /*
138 * Function: BufferHandleTest
139 * Type: Function
140 * Rank: Important(2)
141 * EnvConditions: N/A
142 * CaseDescription: OHOS::ReadBufferHandle
143 */
144 HWTEST_F(BufferHandleTest, ReadBufferHandle, Function | SmallTest | Level2)
145 {
146     MessageParcel parcel;
147 
148     EXPECT_EQ(nullptr, ReadBufferHandle(parcel));
149 }
150 }