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