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 <unistd.h>
18 #include <gtest/gtest.h>
19 #include <iservice_registry.h>
20 #include <securec.h>
21 #include <native_window.h>
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 #include "external_window.h"
26 #include "iconsumer_surface.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 namespace OHOS::Rosen {
32 class NativeWindowBufferTest : public testing::Test,  public IBufferConsumerListenerClazz {
33 public:
34     static void SetUpTestCase();
35     void OnBufferAvailable() override;
36     int32_t SetData(NativeWindowBuffer *nativeWindowBuffer, NativeWindow *nativeWindow);
37     bool GetData(sptr<SurfaceBuffer> &buffer);
38     pid_t ChildProcessMain();
39     int32_t InitNativeWindowAndBuffer(sptr<IRemoteObject> robj);
40 
41     static inline sptr<OHOS::IConsumerSurface> cSurface = nullptr;
42     static inline int32_t pipeFd[2] = {};
43     static inline int32_t ipcSystemAbilityID = 34156;
44 };
45 
SetUpTestCase()46 void NativeWindowBufferTest::SetUpTestCase() {}
47 
OnBufferAvailable()48 void NativeWindowBufferTest::OnBufferAvailable() {}
49 
SetData(NativeWindowBuffer * nativeWindowBuffer,NativeWindow * nativeWindow)50 int32_t NativeWindowBufferTest::SetData(NativeWindowBuffer *nativeWindowBuffer, NativeWindow *nativeWindow)
51 {
52     nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("123", 0x123);
53     nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("345", (int64_t)0x345);
54     nativeWindowBuffer->sfbuffer->GetExtraData()->ExtraSet("567", "567");
55 
56     // alloc OHExtDataHandle
57     uint32_t reserveInts = 1;
58     size_t handleSize = sizeof(OHExtDataHandle) + (sizeof(int32_t) * reserveInts);
59     OHExtDataHandle *handle = static_cast<OHExtDataHandle *>(malloc(handleSize));
60     if (handle == nullptr) {
61         return -1;
62     }
63     int32_t ret = memset_s(handle, handleSize, 0, handleSize);
64     if (ret != EOK) {
65         return -1;
66     }
67     handle->fd = -1;
68     handle->reserveInts = reserveInts;
69     for (uint32_t i = 0; i < reserveInts; i++) {
70         handle->reserve[i] = 1;
71     }
72 
73     ret = OH_NativeWindow_NativeWindowSetTunnelHandle(nativeWindow, handle);
74     // free OHExtDataHandle
75     if (handle->fd >= 0) {
76         close(handle->fd);
77         handle->fd = -1;
78     }
79     free(handle);
80     handle = nullptr;
81     return ret;
82 }
83 
GetData(sptr<SurfaceBuffer> & buffer)84 bool NativeWindowBufferTest::GetData(sptr<SurfaceBuffer> &buffer)
85 {
86     int32_t int32;
87     int64_t int64;
88     std::string str;
89     buffer->GetExtraData()->ExtraGet("123", int32);
90     buffer->GetExtraData()->ExtraGet("345", int64);
91     buffer->GetExtraData()->ExtraGet("567", str);
92     if ((int32 != 0x123) || (int64 != 0x345) || (str != "567")) {
93         return false;
94     }
95 
96     sptr<SurfaceTunnelHandle> handleGet = nullptr;
97     handleGet = cSurface->GetTunnelHandle();
98     if ((handleGet == nullptr) || (handleGet->GetHandle()->fd != -1) ||
99         (handleGet->GetHandle()->reserveInts != 1) || (handleGet->GetHandle()->reserve[0] != 1)) {
100             return false;
101     }
102     return true;
103 }
104 
InitNativeWindowAndBuffer(sptr<IRemoteObject> robj)105 int32_t NativeWindowBufferTest::InitNativeWindowAndBuffer(sptr<IRemoteObject> robj)
106 {
107     auto producer = iface_cast<IBufferProducer>(robj);
108     sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
109 
110     struct NativeWindow *nativeWindow = OH_NativeWindow_CreateNativeWindow(&pSurface);
111     struct NativeWindowBuffer *nativeWindowBuffer = nullptr;
112 
113     int code = SET_USAGE;
114     uint64_t usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA;
115     OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, usage);
116 
117     code = SET_BUFFER_GEOMETRY;
118     int32_t height = 0x100;
119     int32_t width = 0x100;
120     OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, height, width);
121 
122     code = SET_FORMAT;
123     int32_t format = GRAPHIC_PIXEL_FMT_RGBA_8888;
124     OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, format);
125 
126     code = SET_STRIDE;
127     int32_t stride = 0x8;
128     OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, code, stride);
129 
130     int32_t fenceFd = -1;
131     auto ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &nativeWindowBuffer, &fenceFd);
132     if (ret != OHOS::GSERROR_OK) {
133         return ret;
134     }
135     ret = SetData(nativeWindowBuffer, nativeWindow);
136     if (ret != OHOS::GSERROR_OK) {
137         return ret;
138     }
139 
140     struct Region *region = new Region();
141     struct Region::Rect *rect = new Region::Rect();
142     rect->w = 0x100;
143     rect->h = 0x100;
144     region->rects = rect;
145     region->rectNumber = 1;
146     ret = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, nativeWindowBuffer, -1, *region);
147     if (ret != OHOS::GSERROR_OK) {
148         delete rect;
149         delete region;
150         return ret;
151     }
152     delete rect;
153     delete region;
154     return OHOS::GSERROR_OK;
155 }
156 
ChildProcessMain()157 pid_t NativeWindowBufferTest::ChildProcessMain()
158 {
159     pipe(pipeFd);
160     pid_t pid = fork();
161     if (pid != 0) {
162         return pid;
163     }
164 
165     int64_t data;
166     read(pipeFd[0], &data, sizeof(data));
167 
168     sptr<IRemoteObject> robj = nullptr;
169     while (true) {
170         auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
171         robj = sam->GetSystemAbility(ipcSystemAbilityID);
172         if (robj != nullptr) {
173             break;
174         }
175         sleep(0);
176     }
177     int32_t ret = InitNativeWindowAndBuffer(robj);
178     if (ret != OHOS::GSERROR_OK) {
179         data = ret;
180         write(pipeFd[1], &data, sizeof(data));
181         exit(0);
182         return -1;
183     }
184 
185     data = ret;
186     write(pipeFd[1], &data, sizeof(data));
187     usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
188     read(pipeFd[0], &data, sizeof(data));
189     close(pipeFd[0]);
190     close(pipeFd[1]);
191     exit(0);
192     return 0;
193 }
194 
195 /*
196 * Function: produce and consumer surface of nativewindow
197 * Type: Function
198 * Rank: Important(2)
199 * EnvConditions: N/A
200 * CaseDescription: 1. produce surface by nativewindow interface, fill buffer
201 *                  2. consume surface and check buffer
202 * @tc.require: issueI5GMZN issueI5IWHW
203  */
204 HWTEST_F(NativeWindowBufferTest, Surface001, Function | MediumTest | Level2)
205 {
206     auto pid = ChildProcessMain();
207     ASSERT_GE(pid, 0);
208 
209     uint64_t tokenId;
210     const char *perms[2];
211     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
212     perms[1] = "ohos.permission.CAMERA";
213     NativeTokenInfoParams infoInstance = {
214         .dcapsNum = 0,
215         .permsNum = 2,
216         .aclsNum = 0,
217         .dcaps = NULL,
218         .perms = perms,
219         .acls = NULL,
220         .processName = "dcamera_client_demo",
221         .aplStr = "system_basic",
222     };
223     tokenId = GetAccessTokenId(&infoInstance);
224     SetSelfTokenID(tokenId);
225     int32_t rett = Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
226     ASSERT_EQ(rett, Security::AccessToken::RET_SUCCESS);
227 
228     cSurface = IConsumerSurface::Create("test");
229     cSurface->RegisterConsumerListener(this);
230     auto producer = cSurface->GetProducer();
231     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
232     sam->AddSystemAbility(ipcSystemAbilityID, producer->AsObject());
233 
234     int64_t data = 0;
235     write(pipeFd[1], &data, sizeof(data));
236     usleep(1000); // sleep 1000 microseconds (equals 1 milliseconds)
237     read(pipeFd[0], &data, sizeof(data));
238     EXPECT_EQ(data, OHOS::GSERROR_OK);
239 
240     OHOS::sptr<SurfaceBuffer> buffer = nullptr;
241     int32_t fence = -1;
242     int64_t timestamp;
243     Rect damage;
244     auto ret = cSurface->AcquireBuffer(buffer, fence, timestamp, damage);
245     EXPECT_EQ(ret, OHOS::GSERROR_OK);
246     EXPECT_NE(buffer, nullptr);
247     EXPECT_EQ(GetData(buffer), true);
248 
249     ret = cSurface->ReleaseBuffer(buffer, -1);
250     EXPECT_EQ(ret, OHOS::GSERROR_OK);
251 
252     write(pipeFd[1], &data, sizeof(data));
253     close(pipeFd[0]);
254     close(pipeFd[1]);
255     sam->RemoveSystemAbility(ipcSystemAbilityID);
256     waitpid(pid, nullptr, 0);
257 }
258 }
259