• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "camera_timer_fuzzer.h"
17 #include "camera_timer.h"
18 #include "hstream_capture.h"
19 #include "message_parcel.h"
20 #include "nativetoken_kit.h"
21 #include "token_setproc.h"
22 #include "accesstoken_kit.h"
23 #include "camera_metadata_info.h"
24 #include "metadata_utils.h"
25 
26 namespace {
27 
28 const int32_t LIMITSIZE = 5;
29 
30 }
31 
32 namespace OHOS {
33 namespace CameraStandard {
34 
35 bool CameraTimerFuzzer::hasPermission = false;
36 
37 std::shared_ptr<CameraTimer> CameraTimerFuzzer::fuzz_{nullptr};
38 
CheckPermission()39 void CameraTimerFuzzer::CheckPermission()
40 {
41     if (!hasPermission) {
42         uint64_t tokenId;
43         const char *perms[0];
44         perms[0] = "ohos.permission.CAMERA";
45         NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = NULL,
46             .perms = perms, .acls = NULL, .processName = "camera_capture", .aplStr = "system_basic",
47         };
48         tokenId = GetAccessTokenId(&infoInstance);
49         SetSelfTokenID(tokenId);
50         OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
51         hasPermission = true;
52     }
53 }
54 
Test(uint8_t * rawData,size_t size)55 void CameraTimerFuzzer::Test(uint8_t *rawData, size_t size)
56 {
57     if (rawData == nullptr || size < LIMITSIZE) {
58         return;
59     }
60     CheckPermission();
61 
62     fuzz_ = CameraTimer::GetInstance();
63     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
64     MessageParcel data;
65     data.WriteRawData(rawData, size);
66 
67     data.RewindRead(0);
68 
69     fuzz_->IncreaseUserCount();
70     fuzz_->DecreaseUserCount();
71 
72     TimerCallback callback = [] {
73         // do nothing
74     };
75 
76     data.RewindRead(0);
77     uint32_t interval = data.ReadUint32();
78 
79     data.RewindRead(0);
80     bool once = data.ReadBool();
81 
82     uint32_t timerId = fuzz_->Register(callback, interval, once);
83 
84     fuzz_->Unregister(timerId);
85 }
86 
87 } // namespace CameraStandard
88 } // namespace OHOS
89 
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
92 {
93     /* Run your code on data */
94     OHOS::CameraStandard::CameraTimerFuzzer::Test(data, size);
95     return 0;
96 }