• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "unsubscribeconstraints_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 #include "access_token.h"
21 #include "access_token_error.h"
22 #include "accesstoken_kit.h"
23 #include "account_log_wrapper.h"
24 #include "fuzz_data.h"
25 #include "nativetoken_kit.h"
26 #include "os_account_constants.h"
27 #include "os_account_manager.h"
28 #include "token_setproc.h"
29 
30 using namespace std;
31 using namespace OHOS::AccountSA;
32 using namespace OHOS::Security::AccessToken;
33 class TestOsAccountConstraintSubscriber : public OsAccountConstraintSubscriber {
34 public:
TestOsAccountConstraintSubscriber(const std::set<std::string> & constraintSet)35     explicit TestOsAccountConstraintSubscriber(const std::set<std::string> &constraintSet)
36         : OsAccountConstraintSubscriber(constraintSet) {}
OnConstraintChanged(const OsAccountConstraintStateData & constraintData)37     void OnConstraintChanged(const OsAccountConstraintStateData &constraintData) {}
38 };
39 
40 namespace OHOS {
UnsubscribeConstraintsFuzzTest(const uint8_t * data,size_t size)41     bool UnsubscribeConstraintsFuzzTest(const uint8_t* data, size_t size)
42     {
43         int32_t result = ERR_OK;
44         if ((data != nullptr) && (size != 0)) {
45             FuzzData fuzzData(data, size);
46             std::string testStr = fuzzData.GenerateString();
47             std::set<string> constraints = {testStr};
48             auto subscriber = std::make_shared<TestOsAccountConstraintSubscriber>(constraints);
49             result = OsAccountManager::UnsubscribeOsAccountConstraints(subscriber);
50         }
51         return result == ERR_OK;
52     }
53 }
54 
NativeTokenGet()55 void NativeTokenGet()
56 {
57     uint64_t tokenId;
58     const char **perms = new const char *[1];
59     perms[0] = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
60     NativeTokenInfoParams infoInstance = {
61         .dcapsNum = 0,
62         .permsNum = 1,
63         .aclsNum = 0,
64         .perms = perms,
65         .acls = nullptr,
66         .aplStr = "system_core",
67     };
68     infoInstance.processName = "RegisterInputer";
69     tokenId = GetAccessTokenId(&infoInstance);
70     SetSelfTokenID(tokenId);
71     AccessTokenKit::ReloadNativeTokenInfo();
72     delete[] perms;
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)76 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
77 {
78     NativeTokenGet();
79     return 0;
80 }
81 
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85     /* Run your code on data */
86     OHOS::UnsubscribeConstraintsFuzzTest(data, size);
87     return 0;
88 }
89 
90