• 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 <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<int32_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     bq->Init();
86     sptr<IBufferConsumerListener> listener = new BufferConsumerListener();
87     bq->RegisterConsumerListener(listener);
88 
89     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
90     sm->AddSystemAbility(systemAbilityID, bqp);
91 
92     robj = sm->GetSystemAbility(systemAbilityID);
93     bp = iface_cast<IBufferProducer>(robj);
94 
95     bedata = new OHOS::BufferExtraDataImpl;
96 }
97 
TearDownTestCase()98 void BufferQueueProducerRemoteTest::TearDownTestCase()
99 {
100     bp = nullptr;
101     robj = nullptr;
102 
103     auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104     sm->RemoveSystemAbility(systemAbilityID);
105 
106     bqp = nullptr;
107     bq = nullptr;
108 }
109 
110 /*
111 * Function: IsProxyObject
112 * Type: Function
113 * Rank: Important(2)
114 * EnvConditions: N/A
115 * CaseDescription: 1. call IsProxyObject and check ret
116  */
117 HWTEST_F(BufferQueueProducerRemoteTest, IsProxy001, Function | MediumTest | Level2)
118 {
119     ASSERT_FALSE(robj->IsProxyObject());
120 }
121 
122 /*
123 * Function: SetQueueSize and GetQueueSize
124 * Type: Function
125 * Rank: Important(2)
126 * EnvConditions: N/A
127 * CaseDescription: 1. call SetQueueSize
128 *                  2. call SetQueueSize again with abnormal input
129 *                  3. check ret and call GetQueueSize
130  */
131 HWTEST_F(BufferQueueProducerRemoteTest, QueueSize001, Function | MediumTest | Level2)
132 {
133     GSError ret = bp->SetQueueSize(2);
134     ASSERT_EQ(ret, OHOS::GSERROR_OK);
135 
136     ret = bp->SetQueueSize(SURFACE_MAX_QUEUE_SIZE + 1);
137     ASSERT_NE(ret, OHOS::GSERROR_OK);
138 
139     ASSERT_EQ(bp->GetQueueSize(), 2u);
140 }
141 
142 /*
143 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
144 * Type: Function
145 * Rank: Important(2)
146 * EnvConditions: N/A
147 * CaseDescription: 1. call RequestBuffer
148 *                  2. call CancelBuffer
149 *                  3. call AcquireBuffer and check ret
150  */
151 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan001, Function | MediumTest | Level2)
152 {
153     IBufferProducer::RequestBufferReturnValue retval;
154     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
155     ASSERT_EQ(ret, OHOS::GSERROR_OK);
156 
157     ret = bp->CancelBuffer(retval.sequence, bedata);
158     ASSERT_EQ(ret, OHOS::GSERROR_OK);
159 
160     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
161     ASSERT_NE(ret, OHOS::GSERROR_OK);
162 }
163 
164 /*
165 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
166 * Type: Function
167 * Rank: Important(2)
168 * EnvConditions: N/A
169 * CaseDescription: 1. call RequestBuffer
170 *                  2. call CancelBuffer
171 *                  3. call CancelBuffer again
172 *                  4. call AcquireBuffer and check ret
173  */
174 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan002, Function | MediumTest | Level2)
175 {
176     IBufferProducer::RequestBufferReturnValue retval;
177     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
178     ASSERT_EQ(ret, OHOS::GSERROR_OK);
179 
180     ret = bp->CancelBuffer(retval.sequence, bedata);
181     ASSERT_EQ(ret, OHOS::GSERROR_OK);
182 
183     ret = bp->CancelBuffer(retval.sequence, bedata);
184     ASSERT_NE(ret, OHOS::GSERROR_OK);
185 
186     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
187     ASSERT_NE(ret, OHOS::GSERROR_OK);
188 }
189 
190 /*
191 * Function: RequestBuffer, CancelBuffer and AcquireBuffer
192 * Type: Function
193 * Rank: Important(2)
194 * EnvConditions: N/A
195 * CaseDescription: 1. call RequestBuffer and CancelBuffer by different retval
196 *                  2. call AcquireBuffer and check ret
197  */
198 HWTEST_F(BufferQueueProducerRemoteTest, ReqCan003, Function | MediumTest | Level2)
199 {
200     IBufferProducer::RequestBufferReturnValue retval1;
201     IBufferProducer::RequestBufferReturnValue retval2;
202     IBufferProducer::RequestBufferReturnValue retval3;
203     GSError ret;
204 
205     ret = bp->RequestBuffer(requestConfig, bedata, retval1);
206     ASSERT_EQ(ret, OHOS::GSERROR_OK);
207     ASSERT_EQ(retval1.buffer, nullptr);
208 
209     ret = bp->RequestBuffer(requestConfig, bedata, retval2);
210     ASSERT_EQ(ret, OHOS::GSERROR_OK);
211     ASSERT_NE(retval2.buffer, nullptr);
212 
213     ret = bp->RequestBuffer(requestConfig, bedata, retval3);
214     ASSERT_NE(ret, OHOS::GSERROR_OK);
215     ASSERT_EQ(retval3.buffer, nullptr);
216 
217     ret = bp->CancelBuffer(retval1.sequence, bedata);
218     ASSERT_EQ(ret, OHOS::GSERROR_OK);
219 
220     ret = bp->CancelBuffer(retval2.sequence, bedata);
221     ASSERT_EQ(ret, OHOS::GSERROR_OK);
222 
223     ret = bp->CancelBuffer(retval3.sequence, bedata);
224     ASSERT_NE(ret, OHOS::GSERROR_OK);
225 
226     ret = bq->AcquireBuffer(retval1.buffer, retval1.fence, timestamp, damages);
227     ASSERT_NE(ret, OHOS::GSERROR_OK);
228 }
229 
230 /*
231 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
232 * Type: Function
233 * Rank: Important(2)
234 * EnvConditions: N/A
235 * CaseDescription: 1. call RequestBuffer and FlushBuffer
236 *                  2. call AcquireBuffer and ReleaseBuffer
237 *                  3. call AcquireBuffer again
238 *                  4. check ret
239  */
240 HWTEST_F(BufferQueueProducerRemoteTest, ReqFlu001, Function | MediumTest | Level2)
241 {
242     IBufferProducer::RequestBufferReturnValue retval;
243     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
244     ASSERT_EQ(ret, OHOS::GSERROR_OK);
245 
246     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
247     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
248     ASSERT_EQ(ret, OHOS::GSERROR_OK);
249 
250     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
251     ASSERT_EQ(ret, OHOS::GSERROR_OK);
252 
253     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
254     ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
255     ASSERT_EQ(ret, OHOS::GSERROR_OK);
256 
257     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
258     ASSERT_NE(ret, OHOS::GSERROR_OK);
259 }
260 
261 /*
262 * Function: RequestBuffer, FlushBuffer, AcquireBuffer and ReleaseBuffer
263 * Type: Function
264 * Rank: Important(2)
265 * EnvConditions: N/A
266 * CaseDescription: 1. call RequestBuffer and FlushBuffer
267 *                  2. call FlushBuffer again
268 *                  3. call AcquireBuffer and ReleaseBuffer
269 *                  4. call AcquireBuffer again
270 *                  5. check ret
271  */
272 HWTEST_F(BufferQueueProducerRemoteTest, ReqFlu002, Function | MediumTest | Level2)
273 {
274     IBufferProducer::RequestBufferReturnValue retval;
275     GSError ret = bp->RequestBuffer(requestConfig, bedata, retval);
276     ASSERT_EQ(ret, OHOS::GSERROR_OK);
277 
278     sptr<SyncFence> acquireFence = SyncFence::INVALID_FENCE;
279     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
280     ASSERT_EQ(ret, OHOS::GSERROR_OK);
281 
282     ret = bp->FlushBuffer(retval.sequence, bedata, acquireFence, flushConfig);
283     ASSERT_NE(ret, OHOS::GSERROR_OK);
284 
285     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
286     ASSERT_EQ(ret, OHOS::GSERROR_OK);
287 
288     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
289     ret = bq->ReleaseBuffer(retval.buffer, releaseFence);
290     ASSERT_EQ(ret, OHOS::GSERROR_OK);
291 
292     ret = bq->AcquireBuffer(retval.buffer, retval.fence, timestamp, damages);
293     ASSERT_NE(ret, OHOS::GSERROR_OK);
294 }
295 }
296