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