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 <chrono>
16 #include <thread>
17 #include <vector>
18 #include <sys/wait.h>
19 #include <unistd.h>
20 #include <gtest/gtest.h>
21
22 #include <iservice_registry.h>
23 #include <surface.h>
24 #include <buffer_extra_data_impl.h>
25 #include <buffer_client_producer.h>
26 #include <buffer_queue_producer.h>
27 #include "buffer_consumer_listener.h"
28 #include "sync_fence.h"
29 #include "accesstoken_kit.h"
30 #include "nativetoken_kit.h"
31 #include "token_setproc.h"
32
33 using namespace testing;
34 using namespace testing::ext;
35
36 namespace OHOS::Rosen {
37 class BufferClientProducerRemoteTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41
42 static inline BufferRequestConfig requestConfig = {
43 .width = 0x100,
44 .height = 0x100,
45 .strideAlignment = 0x8,
46 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
47 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
48 .timeout = 0,
49 };
50 static inline BufferFlushConfigWithDamages flushConfig = {
51 .damages = {
52 {
53 .w = 0x100,
54 .h = 0x100,
55 }
56 },
57 };
58 static inline sptr<IRemoteObject> robj = nullptr;
59 static inline sptr<IBufferProducer> bp = nullptr;
60 static inline std::vector<int32_t> deletingBuffers;
61 static inline pid_t pid = 0;
62 static inline int pipeFd[2] = {};
63 static inline int pipe1Fd[2] = {};
64 static inline int32_t systemAbilityID = 345135;
65 static inline sptr<BufferExtraData> bedata = new BufferExtraDataImpl;
66 };
67
InitNativeTokenInfo()68 static void InitNativeTokenInfo()
69 {
70 uint64_t tokenId;
71 const char *perms[2];
72 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
73 perms[1] = "ohos.permission.CAMERA";
74 NativeTokenInfoParams infoInstance = {
75 .dcapsNum = 0,
76 .permsNum = 2,
77 .aclsNum = 0,
78 .dcaps = NULL,
79 .perms = perms,
80 .acls = NULL,
81 .processName = "dcamera_client_demo",
82 .aplStr = "system_basic",
83 };
84 tokenId = GetAccessTokenId(&infoInstance);
85 SetSelfTokenID(tokenId);
86 int32_t ret = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
87 ASSERT_EQ(ret, Security::AccessToken::RET_SUCCESS);
88 std::this_thread::sleep_for(std::chrono::milliseconds(50)); // wait 50ms
89 }
90
SetUpTestCase()91 void BufferClientProducerRemoteTest::SetUpTestCase()
92 {
93 if (pipe(pipeFd) < 0) {
94 exit(1);
95 }
96 if (pipe(pipe1Fd) < 0) {
97 exit(0);
98 }
99 pid = fork();
100 if (pid < 0) {
101 exit(1);
102 }
103 if (pid == 0) {
104 InitNativeTokenInfo();
105 sptr<BufferQueue> bq = new BufferQueue("test");
106 ASSERT_NE(bq, nullptr);
107 sptr<BufferQueueProducer> bqp = new BufferQueueProducer(bq);
108 ASSERT_NE(bqp, nullptr);
109 bq->Init();
110 sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
111 bq->RegisterConsumerListener(listener);
112 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
113 sam->AddSystemAbility(systemAbilityID, bqp);
114 close(pipeFd[1]);
115 close(pipe1Fd[0]);
116 char buf[10] = "start";
117 write(pipe1Fd[1], buf, sizeof(buf));
118 sleep(0);
119 read(pipeFd[0], buf, sizeof(buf));
120 sam->RemoveSystemAbility(systemAbilityID);
121 close(pipeFd[0]);
122 close(pipe1Fd[1]);
123 exit(0);
124 } else {
125 close(pipeFd[0]);
126 close(pipe1Fd[1]);
127 char buf[10];
128 read(pipe1Fd[0], buf, sizeof(buf));
129 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
130 robj = sam->GetSystemAbility(systemAbilityID);
131 bp = iface_cast<IBufferProducer>(robj);
132 }
133 }
134
TearDownTestCase()135 void BufferClientProducerRemoteTest::TearDownTestCase()
136 {
137 bp = nullptr;
138 robj = nullptr;
139
140 char buf[10] = "over";
141 write(pipeFd[1], buf, sizeof(buf));
142 close(pipeFd[1]);
143 close(pipe1Fd[0]);
144
145 int32_t ret = 0;
146 do {
147 waitpid(pid, nullptr, 0);
148 } while (ret == -1 && errno == EINTR);
149 }
150
151 /*
152 * Function: IsProxyObject
153 * Type: Function
154 * Rank: Important(2)
155 * EnvConditions: N/A
156 * CaseDescription: 1. check ret for IsProxyObject func
157 */
158 HWTEST_F(BufferClientProducerRemoteTest, IsProxy001, Function | MediumTest | Level2)
159 {
160 ASSERT_TRUE(robj->IsProxyObject());
161 }
162
163 /*
164 * Function: SetQueueSize and GetQueueSize
165 * Type: Function
166 * Rank: Important(2)
167 * EnvConditions: N/A
168 * CaseDescription: 1. call GetQueueSize for default
169 * 2. call SetQueueSize and check the ret of GetQueueSize
170 */
171 HWTEST_F(BufferClientProducerRemoteTest, QueueSize001, Function | MediumTest | Level2)
172 {
173 ASSERT_EQ(bp->GetQueueSize(), (uint32_t)SURFACE_DEFAULT_QUEUE_SIZE);
174
175 GSError ret = bp->SetQueueSize(2);
176 ASSERT_EQ(ret, OHOS::GSERROR_OK);
177
178 ret = bp->SetQueueSize(SURFACE_MAX_QUEUE_SIZE + 1);
179 ASSERT_NE(ret, OHOS::GSERROR_OK);
180
181 ASSERT_EQ(bp->GetQueueSize(), 2u);
182 }
183
184 /*
185 * Function: SetQueueSize and GetQueueSize
186 * Type: Function
187 * Rank: Important(2)
188 * EnvConditions: N/A
189 * CaseDescription: 1. call GetQueueSize for default
190 * 2. call SetQueueSize and check the ret of GetQueueSize
191 */
192 HWTEST_F(BufferClientProducerRemoteTest, ReqCan001, Function | MediumTest | Level2)
193 {
194 IBufferProducer::RequestBufferReturnValue retval;
195 GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
196 ASSERT_EQ(ret, OHOS::GSERROR_OK);
197 ASSERT_NE(retval.buffer, nullptr);
198
199 ret = bp->CancelBuffer(retval.sequence, bedata);
200 ASSERT_EQ(ret, OHOS::GSERROR_OK);
201 }
202
203 /*
204 * Function: RequestBuffer and CancelBuffer
205 * Type: Function
206 * Rank: Important(2)
207 * EnvConditions: N/A
208 * CaseDescription: 1. call RequestBuffer
209 * 2. call CancelBuffer 2 times
210 */
211 HWTEST_F(BufferClientProducerRemoteTest, ReqCan002, Function | MediumTest | Level2)
212 {
213 IBufferProducer::RequestBufferReturnValue retval;
214 GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
215 ASSERT_EQ(ret, OHOS::GSERROR_OK);
216 ASSERT_EQ(retval.buffer, nullptr);
217
218 ret = bp->CancelBuffer(retval.sequence, bedata);
219 ASSERT_EQ(ret, OHOS::GSERROR_OK);
220
221 ret = bp->CancelBuffer(retval.sequence, bedata);
222 ASSERT_NE(ret, OHOS::GSERROR_OK);
223 }
224
225 /*
226 * Function: RequestBuffer and CancelBuffer
227 * Type: Function
228 * Rank: Important(2)
229 * EnvConditions: N/A
230 * CaseDescription: 1. call RequestBuffer and CancelBuffer 3 times
231 */
232 HWTEST_F(BufferClientProducerRemoteTest, ReqCan003, Function | MediumTest | Level2)
233 {
234 IBufferProducer::RequestBufferReturnValue retval1;
235 IBufferProducer::RequestBufferReturnValue retval2;
236 IBufferProducer::RequestBufferReturnValue retval3;
237 GSError ret;
238
239 ret = bp->RequestBuffer(requestConfig, bedata, retval1);
240 ASSERT_EQ(ret, OHOS::GSERROR_OK);
241 ASSERT_EQ(retval1.buffer, nullptr);
242
243 ret = bp->RequestBuffer(requestConfig, bedata, retval2);
244 ASSERT_EQ(ret, OHOS::GSERROR_OK);
245 ASSERT_NE(retval2.buffer, nullptr);
246
247 ret = bp->RequestBuffer(requestConfig, bedata, retval3);
248 ASSERT_NE(ret, OHOS::GSERROR_OK);
249 ASSERT_EQ(retval3.buffer, nullptr);
250
251 ret = bp->CancelBuffer(retval1.sequence, bedata);
252 ASSERT_EQ(ret, OHOS::GSERROR_OK);
253
254 ret = bp->CancelBuffer(retval2.sequence, bedata);
255 ASSERT_EQ(ret, OHOS::GSERROR_OK);
256
257 ret = bp->CancelBuffer(retval3.sequence, bedata);
258 ASSERT_NE(ret, OHOS::GSERROR_OK);
259 }
260
261 /*
262 * Function: SetQueueSize, RequestBuffer and CancelBuffer
263 * Type: Function
264 * Rank: Important(2)
265 * EnvConditions: N/A
266 * CaseDescription: 1. call SetQueueSize
267 * 2. call RequestBuffer and CancelBuffer
268 * 3. call SetQueueSize again
269 */
270 HWTEST_F(BufferClientProducerRemoteTest, SetQueueSizeDeleting001, Function | MediumTest | Level2)
271 {
272 GSError ret = bp->SetQueueSize(1);
273 ASSERT_EQ(ret, OHOS::GSERROR_OK);
274
275 IBufferProducer::RequestBufferReturnValue retval;
276 ret = bp->RequestBuffer(requestConfig, bedata, retval);
277 ASSERT_EQ(ret, OHOS::GSERROR_OK);
278 ASSERT_EQ(retval.buffer, nullptr);
279
280 ret = bp->CancelBuffer(retval.sequence, bedata);
281 ASSERT_EQ(ret, OHOS::GSERROR_OK);
282
283 ret = bp->SetQueueSize(2);
284 ASSERT_EQ(ret, OHOS::GSERROR_OK);
285 }
286
287 /*
288 * Function: RequestBuffer and FlushBuffer
289 * Type: Function
290 * Rank: Important(2)
291 * EnvConditions: N/A
292 * CaseDescription: 1. call RequestBuffer
293 * 2. call FlushBuffer
294 */
295 HWTEST_F(BufferClientProducerRemoteTest, ReqFlu001, Function | MediumTest | Level2)
296 {
297 IBufferProducer::RequestBufferReturnValue retval;
298 GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
299 ASSERT_EQ(ret, OHOS::GSERROR_OK);
300
301 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
302 ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
303 ASSERT_EQ(ret, OHOS::GSERROR_OK);
304 }
305
306 /*
307 * Function: RequestBuffer and FlushBuffer
308 * Type: Function
309 * Rank: Important(2)
310 * EnvConditions: N/A
311 * CaseDescription: 1. call RequestBuffer
312 * 2. call FlushBuffer 2 times
313 */
314 HWTEST_F(BufferClientProducerRemoteTest, ReqFlu002, Function | MediumTest | Level2)
315 {
316 IBufferProducer::RequestBufferReturnValue retval;
317 GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
318 ASSERT_EQ(ret, OHOS::GSERROR_OK);
319
320 sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
321 ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
322 ASSERT_EQ(ret, OHOS::GSERROR_OK);
323
324 ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
325 ASSERT_NE(ret, OHOS::GSERROR_OK);
326 }
327
328 /*
329 * Function: AttachBuffer and DetachBuffer
330 * Type: Function
331 * Rank: Important(2)
332 * EnvConditions: N/A
333 * CaseDescription: 1. call AttachBuffer
334 * 2. call DetachBuffer
335 */
336 HWTEST_F(BufferClientProducerRemoteTest, AttachDetach001, Function | MediumTest | Level2)
337 {
338 sptr<OHOS::SurfaceBuffer> buffer = new SurfaceBufferImpl(0);
339 GSError ret = bp->AttachBuffer(buffer);
340 ASSERT_EQ(ret, OHOS::GSERROR_NOT_SUPPORT);
341
342 ret = bp->DetachBuffer(buffer);
343 ASSERT_EQ(ret, OHOS::GSERROR_NOT_SUPPORT);
344 }
345
346 /*
347 * Function: RegisterReleaseListener
348 * Type: Function
349 * Rank: Important(2)
350 * EnvConditions: N/A
351 * CaseDescription: 1. call RegisterReleaseListener
352 */
353 HWTEST_F(BufferClientProducerRemoteTest, RegisterReleaseListener001, Function | MediumTest | Level2)
354 {
355 OnReleaseFunc onBufferRelease = nullptr;
356 sptr<IProducerListener> listener = new BufferReleaseProducerListener(onBufferRelease);
357 GSError ret = bp->RegisterReleaseListener(listener);
358 ASSERT_EQ(ret, OHOS::GSERROR_OK);
359 }
360
361 /*
362 * Function: GetName
363 * Type: Function
364 * Rank: Important(2)
365 * EnvConditions: N/A
366 * CaseDescription: 1. call GetName
367 */
368 HWTEST_F(BufferClientProducerRemoteTest, GetName001, Function | MediumTest | Level2)
369 {
370 std::string name;
371 GSError ret = bp->GetName(name);
372 ASSERT_EQ(ret, OHOS::GSERROR_OK);
373 }
374
375 /*
376 * Function: GetUniqueId
377 * Type: Function
378 * Rank: Important(2)
379 * EnvConditions: N/A
380 * CaseDescription: 1. call GetUniqueId
381 */
382 HWTEST_F(BufferClientProducerRemoteTest, GetUniqueId001, Function | MediumTest | Level2)
383 {
384 uint64_t bpid = bp->GetUniqueId();
385 ASSERT_NE(bpid, 0);
386 }
387
388 /*
389 * Function: GetDefaultUsage
390 * Type: Function
391 * Rank: Important(2)
392 * EnvConditions: N/A
393 * CaseDescription: 1. call GetDefaultUsage
394 */
395 HWTEST_F(BufferClientProducerRemoteTest, GetDefaultUsage001, Function | MediumTest | Level2)
396 {
397 uint32_t usage = bp->GetDefaultUsage();
398 ASSERT_EQ(usage, 0);
399 }
400
401 /*
402 * Function: SetTransform
403 * Type: Function
404 * Rank: Important(2)
405 * EnvConditions: N/A
406 * CaseDescription: 1. call SetTransform
407 */
408 HWTEST_F(BufferClientProducerRemoteTest, SetTransform001, Function | MediumTest | Level2)
409 {
410 GraphicTransformType transform = GraphicTransformType::GRAPHIC_ROTATE_90;
411 GSError ret = bp->SetTransform(transform);
412 ASSERT_EQ(ret, OHOS::GSERROR_OK);
413 }
414
415 /*
416 * Function: IsSupportedAlloc
417 * Type: Function
418 * Rank: Important(2)
419 * EnvConditions: N/A
420 * CaseDescription: 1. call IsSupportedAlloc with abnormal parameters and check ret
421 */
422 HWTEST_F(BufferClientProducerRemoteTest, isSupportedAlloc001, Function | MediumTest | Level2)
423 {
424 std::vector<BufferVerifyAllocInfo> infos;
425 std::vector<bool> supporteds;
426 GSError ret = bp->IsSupportedAlloc(infos, supporteds);
427 ASSERT_EQ(ret, OHOS::GSERROR_OK);
428 }
429
430 /*
431 * Function: IsSupportedAlloc
432 * Type: Function
433 * Rank: Important(2)
434 * EnvConditions: N/A
435 * CaseDescription: 1. call IsSupportedAlloc with abnormal parameters and check ret
436 */
437 HWTEST_F(BufferClientProducerRemoteTest, isSupportedAlloc002, Function | MediumTest | Level2)
438 {
439 std::vector<BufferVerifyAllocInfo> infos;
440 std::vector<bool> supporteds;
441 GSError ret = bp->IsSupportedAlloc(infos, supporteds);
442 ASSERT_EQ(ret, OHOS::GSERROR_OK);
443
444 BufferVerifyAllocInfo info = {
445 .width = 0x100,
446 .height = 0x100,
447 .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA,
448 .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
449 };
450 infos.push_back(info);
451 info.format = GRAPHIC_PIXEL_FMT_YCRCB_420_SP;
452 infos.push_back(info);
453 info.format = GRAPHIC_PIXEL_FMT_YUV_422_I;
454 infos.push_back(info);
455
456 ret = bp->IsSupportedAlloc(infos, supporteds);
457 ASSERT_EQ(ret, OHOS::GSERROR_OK);
458 }
459
460 /*
461 * Function: SetScalingMode
462 * Type: Function
463 * Rank: Important(2)
464 * EnvConditions: N/A
465 * CaseDescription: 1. call SetScalingMode with abnormal parameters and check ret
466 */
467 HWTEST_F(BufferClientProducerRemoteTest, SetScalingMode001, Function | MediumTest | Level2)
468 {
469 ScalingMode scalingMode = ScalingMode::SCALING_MODE_SCALE_TO_WINDOW;
470 GSError ret = bp->SetScalingMode(-1, scalingMode);
471 ASSERT_EQ(ret, OHOS::GSERROR_NO_ENTRY);
472 }
473
474 /*
475 * Function: SetMetaData
476 * Type: Function
477 * Rank: Important(2)
478 * EnvConditions: N/A
479 * CaseDescription: 1. call SetMetaData with abnormal parameters and check ret
480 */
481 HWTEST_F(BufferClientProducerRemoteTest, SetMetaData001, Function | MediumTest | Level2)
482 {
483 uint32_t sequence = 0;
484 std::vector<GraphicHDRMetaData> metaData;
485 GSError ret = bp->SetMetaData(sequence, metaData);
486 ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
487 }
488
489 /*
490 * Function: SetMetaDataSet
491 * Type: Function
492 * Rank: Important(2)
493 * EnvConditions: N/A
494 * CaseDescription: 1. call SetMetaDataSet with abnormal parameters and check ret
495 */
496 HWTEST_F(BufferClientProducerRemoteTest, SetMetaDataSet001, Function | MediumTest | Level2)
497 {
498 GraphicHDRMetadataKey key = GraphicHDRMetadataKey::GRAPHIC_MATAKEY_HDR10_PLUS;
499 std::vector<uint8_t> metaData;
500
501 uint32_t sequence = 0;
502 GSError ret = bp->SetMetaDataSet(sequence, key, metaData);
503 ASSERT_EQ(ret, OHOS::GSERROR_INVALID_ARGUMENTS);
504 }
505
506 /*
507 * Function: GoBackground
508 * Type: Function
509 * Rank: Important(2)
510 * EnvConditions: N/A
511 * CaseDescription: 1. call GoBackground
512 */
513 HWTEST_F(BufferClientProducerRemoteTest, GoBackground001, Function | MediumTest | Level2)
514 {
515 GSError ret = bp->GoBackground();
516 ASSERT_EQ(ret, OHOS::GSERROR_OK);
517 }
518 }
519