1 /*
2  * Copyright (c) 2021 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 #include <gtest/gtest.h>
16 #include <iservice_registry.h>
17 #include <surface.h>
18 #include <buffer_extra_data_impl.h>
19 #include <buffer_queue_producer.h>
20 #include "buffer_consumer_listener.h"
21 #include "sync_fence.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS::Rosen {
30 class BufferQueueProducerRemoteTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34 
35     static inline BufferRequestConfig requestConfig = {
36         .width = 0x100,
37         .height = 0x100,
38         .strideAlignment = 0x8,
39         .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
40         .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
41         .timeout = 0,
42     };
43     static inline BufferFlushConfigWithDamages flushConfig = {
44         .damages = {
45             {
46                 .w = 0x100,
47                 .h = 0x100,
48             }
49         },
50     };
51     static inline std::vector<uint32_t> deletingBuffers;
52     static inline int64_t timestamp = 0;
53     static inline std::vector<Rect> damages = {};
54     static inline sptr<IRemoteObject> robj = nullptr;
55     static inline sptr<IBufferProducer> bp = nullptr;
56     static inline sptr<BufferQueue> bq = nullptr;
57     static inline sptr<BufferQueueProducer> bqp = nullptr;
58     static inline sptr<BufferExtraData> bedata = nullptr;
59     static inline int32_t systemAbilityID = 345154;
60 };
61 
SetUpTestCase()62 void BufferQueueProducerRemoteTest::SetUpTestCase()
63 {
64     uint64_t tokenId;
65     const char *perms[2];
66     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
67     perms[1] = "ohos.permission.CAMERA";
68     NativeTokenInfoParams infoInstance = {
69         .dcapsNum = 0,
70         .permsNum = 2,
71         .aclsNum = 0,
72         .dcaps = NULL,
73         .perms = perms,
74         .acls = NULL,
75         .processName = "dcamera_client_demo",
76         .aplStr = "system_basic",
77     };
78     tokenId = GetAccessTokenId(&infoInstance);
79     SetSelfTokenID(tokenId);
80     int32_t ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
81     ASSERT_EQ(ret, Security::AccessToken::RET_SUCCESS);
82 
83     bq = new BufferQueue("test");
84     bqp = new BufferQueueProducer(bq);
85     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
86     bq->RegisterConsumerListener(listener);
87 
88     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
89     sm->AddSystemAbility(systemAbilityID, bqp);
90 
91     robj = sm->GetSystemAbility(systemAbilityID);
92     bp = iface_cast<IBufferProducer>(robj);
93 
94     bedata = new OHOS::BufferExtraDataImpl;
95 }
96 
TearDownTestCase()97 void BufferQueueProducerRemoteTest::TearDownTestCase()
98 {
99     bp = nullptr;
100     robj = nullptr;
101 
102     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103     sm->RemoveSystemAbility(systemAbilityID);
104 
105     bqp = nullptr;
106     bq = nullptr;
107 }
108 
109 /*
110 * Function: IsProxyObject
111 * Type: Function
112 * Rank: Important(2)
113 * EnvConditions: N/A
114 * CaseDescription: 1. call IsProxyObject and check ret
115  */
116 HWTEST_F(BufferQueueProducerRemoteTest, IsProxy001, Function | MediumTest | Level2)
117 {
118     ASSERT_FALSE(robj->IsProxyObject());
119 }
120 
121 /*
122 * Function: SetQueueSize and GetQueueSize
123 * Type: Function
124 * Rank: Important(2)
125 * EnvConditions: N/A
126 * CaseDescription: 1. call SetQueueSize
127 *                  2. call SetQueueSize again with abnormal input
128 *                  3. check ret and call GetQueueSize
129  */
130 HWTEST_F(BufferQueueProducerRemoteTest, QueueSize001, Function | MediumTest | Level2)
131 {
132     GSError ret = bp->SetQueueSize(2);
133     ASSERT_EQ(ret, OHOS::GSERROR_OK);
134 
135     ret = bp->SetQueueSize(SURFACE_MAX_QUEUE_SIZE + 1);
136     ASSERT_NE(ret, OHOS::GSERROR_OK);
137 
138     ASSERT_EQ(bp->GetQueueSize(), 2u);
139 }
140 
141 /*
142 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
143 * Type: Function
144 * Rank: Important(2)
145 * EnvConditions: N/A
146 * CaseDescription: 1. call RequestBuffer
147 *                  2. call CancelBuffer
148 *                  3. call AcquireBuffer and check ret
149  */
150 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan001, Function | MediumTest | Level2)
151 {
152     IBufferProducer::RequestBufferReturnValue retval;
153     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
154     ASSERT_EQ(ret, OHOS::GSERROR_OK);
155 
156     ret = bp->CancelBuffer(retval.sequence, bedata);
157     ASSERT_EQ(ret, OHOS::GSERROR_OK);
158 
159     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
160     ASSERT_NE(ret, OHOS::GSERROR_OK);
161 }
162 
163 /*
164 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
165 * Type: Function
166 * Rank: Important(2)
167 * EnvConditions: N/A
168 * CaseDescription: 1. call RequestBuffer
169 *                  2. call CancelBuffer
170 *                  3. call CancelBuffer again
171 *                  4. call AcquireBuffer and check ret
172  */
173 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan002, Function | MediumTest | Level2)
174 {
175     IBufferProducer::RequestBufferReturnValue retval;
176     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
177     ASSERT_EQ(ret, OHOS::GSERROR_OK);
178 
179     ret = bp->CancelBuffer(retval.sequence, bedata);
180     ASSERT_EQ(ret, OHOS::GSERROR_OK);
181 
182     ret = bp->CancelBuffer(retval.sequence, bedata);
183     ASSERT_NE(ret, OHOS::GSERROR_OK);
184 
185     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
186     ASSERT_NE(ret, OHOS::GSERROR_OK);
187 }
188 
189 /*
190 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
191 * Type: Function
192 * Rank: Important(2)
193 * EnvConditions: N/A
194 * CaseDescription: 1. call RequestBuffer and CancelBuffer by different retval
195 *                  2. call AcquireBuffer and check ret
196  */
197 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan003, Function | MediumTest | Level2)
198 {
199     IBufferProducer::RequestBufferReturnValue retval1;
200     IBufferProducer::RequestBufferReturnValue retval2;
201     IBufferProducer::RequestBufferReturnValue retval3;
202     GSError ret;
203 
204     ret = bp->RequestBuffer(requestConfig, bedata, retval1);
205     ASSERT_EQ(ret, OHOS::GSERROR_OK);
206     ASSERT_EQ(retval1.buffer, nullptr);
207 
208     ret = bp->RequestBuffer(requestConfig, bedata, retval2);
209     ASSERT_EQ(ret, OHOS::GSERROR_OK);
210     ASSERT_NE(retval2.buffer, nullptr);
211 
212     ret = bp->RequestBuffer(requestConfig, bedata, retval3);
213     ASSERT_NE(ret, OHOS::GSERROR_OK);
214     ASSERT_EQ(retval3.buffer, nullptr);
215 
216     ret = bp->CancelBuffer(retval1.sequence, bedata);
217     ASSERT_EQ(ret, OHOS::GSERROR_OK);
218 
219     ret = bp->CancelBuffer(retval2.sequence, bedata);
220     ASSERT_EQ(ret, OHOS::GSERROR_OK);
221 
222     ret = bp->CancelBuffer(retval3.sequence, bedata);
223     ASSERT_NE(ret, OHOS::GSERROR_OK);
224 
225     ret = bq->AcquireBuffer(retval1.buffer, retval1.fence, timestamp, damages);
226     ASSERT_NE(ret, OHOS::GSERROR_OK);
227 }
228 
229 /*
230 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
231 * Type: Function
232 * Rank: Important(2)
233 * EnvConditions: N/A
234 * CaseDescription: 1. call RequestBuffer and FlushBuffer
235 *                  2. call AcquireBuffer and ReleaseBuffer
236 *                  3. call AcquireBuffer again
237 *                  4. check ret
238  */
239 HWTEST_F(BufferQueueProducerRemoteTest, ReqFlu001, Function | MediumTest | Level2)
240 {
241     IBufferProducer::RequestBufferReturnValue retval;
242     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
243     ASSERT_EQ(ret, OHOS::GSERROR_OK);
244 
245     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
246     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
247     ASSERT_EQ(ret, OHOS::GSERROR_OK);
248 
249     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
250     ASSERT_EQ(ret, OHOS::GSERROR_OK);
251 
252     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
253     ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
254     ASSERT_EQ(ret, OHOS::GSERROR_OK);
255 
256     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
257     ASSERT_NE(ret, OHOS::GSERROR_OK);
258 }
259 
260 /*
261 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
262 * Type: Function
263 * Rank: Important(2)
264 * EnvConditions: N/A
265 * CaseDescription: 1. call RequestBuffer and FlushBuffer
266 *                  2. call FlushBuffer again
267 *                  3. call AcquireBuffer and ReleaseBuffer
268 *                  4. call AcquireBuffer again
269 *                  5. check ret
270  */
271 HWTEST_F(BufferQueueProducerRemoteTest, ReqFlu002, Function | MediumTest | Level2)
272 {
273     IBufferProducer::RequestBufferReturnValue retval;
274     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
275     ASSERT_EQ(ret, OHOS::GSERROR_OK);
276 
277     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
278     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
279     ASSERT_EQ(ret, OHOS::GSERROR_OK);
280 
281     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
282     ASSERT_NE(ret, OHOS::GSERROR_OK);
283 
284     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
285     ASSERT_EQ(ret, OHOS::GSERROR_OK);
286 
287     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
288     ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
289     ASSERT_EQ(ret, OHOS::GSERROR_OK);
290 
291     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
292     ASSERT_NE(ret, OHOS::GSERROR_OK);
293 }
294 }
295