1 /*
2  * Copyright (c) 2023 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 
16 #include "stream_repeat_stub_fuzzer.h"
17 #include "hstream_repeat.h"
18 #include "nativetoken_kit.h"
19 #include "token_setproc.h"
20 #include "accesstoken_kit.h"
21 #include "iconsumer_surface.h"
22 #include "metadata_utils.h"
23 #include "camera_service_ipc_interface_code.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 namespace StreamRepeatStubFuzzer {
28 const int32_t LIMITSIZE = 2;
29 const int32_t PHOTO_WIDTH = 1280;
30 const int32_t PHOTO_HEIGHT = 960;
31 const int32_t PHOTO_FORMAT = 2000;
32 const RepeatStreamType REPEAT_STREAM_TYPE = RepeatStreamType::PREVIEW;
33 
34 bool g_hasPermission = false;
35 HStreamRepeatStub *fuzz = nullptr;
36 
CheckPermission()37 void CheckPermission()
38 {
39     if (!g_hasPermission) {
40         uint64_t tokenId;
41         const char *perms[0];
42         perms[0] = "ohos.permission.CAMERA";
43         NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
44             .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
45         };
46         tokenId = GetAccessTokenId(&infoInstance);
47         SetSelfTokenID(tokenId);
48         OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
49         g_hasPermission = true;
50     }
51 }
52 
Test(uint8_t * rawData,size_t size)53 void Test(uint8_t *rawData, size_t size)
54 {
55     if (rawData == nullptr || size < LIMITSIZE) {
56         return;
57     }
58     CheckPermission();
59     Test_OnRemoteRequest(rawData, size);
60 }
61 
RunCase(MessageParcel & data,uint32_t code)62 void RunCase(MessageParcel &data, uint32_t code)
63 {
64     MessageParcel reply;
65     MessageOption option;
66     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
67     if (photoSurface == nullptr) {
68         return;
69     }
70     sptr<IBufferProducer> producer = photoSurface->GetProducer();
71     sptr<HStreamRepeat> hstreamRepeat = new HStreamRepeat(producer, PHOTO_FORMAT, PHOTO_WIDTH,
72         PHOTO_HEIGHT, REPEAT_STREAM_TYPE);
73     hstreamRepeat->OnRemoteRequest(code, data, reply, option);
74 }
75 
Test_OnRemoteRequest(uint8_t * rawData,size_t size)76 void Test_OnRemoteRequest(uint8_t *rawData, size_t size)
77 {
78     static const int32_t MAX_CODE = 20;
79     for (int32_t i = 0; i < MAX_CODE; i++) {
80         MessageParcel data;
81         data.WriteInterfaceToken(u"IStreamRepeat");
82         data.WriteRawData(rawData, size);
83         data.RewindRead(0);
84         RunCase(data, i);
85     }
86 }
87 
88 } // namespace StreamRepeatStubFuzzer
89 } // namespace CameraStandard
90 } // namespace OHOS
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
94 {
95     /* Run your code on data */
96     OHOS::CameraStandard::StreamRepeatStubFuzzer::Test(data, size);
97     return 0;
98 }