1/*
2 * Copyright (c) 2024 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 "cloudsynccallbackstub_fuzzer.h"
16
17#include <cstddef>
18#include <cstdint>
19
20#include "cloud_fuzzer_helper.h"
21#include "cloud_sync_callback_client.h"
22#include "cloud_sync_callback_stub.h"
23
24#include "message_parcel.h"
25#include "utils_log.h"
26
27namespace OHOS {
28constexpr size_t U32_AT_SIZE = 4;
29
30using namespace OHOS::FileManagement::CloudSync;
31using namespace std;
32
33class CloudSyncCallbackTest : public CloudSyncCallbackStub {
34public:
35    void OnSyncStateChanged(SyncType type, SyncPromptState state) override {}
36    void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {}
37};
38
39bool WriteInterfaceToken(MessageParcel &data)
40{
41    if (!data.WriteInterfaceToken(CloudSyncCallbackStub::GetDescriptor())) {
42        LOGE("Write token failed.");
43        return false;
44    }
45    return true;
46}
47
48void HandleOnSyncStateChangedFuzzTest(std::shared_ptr<CloudSyncCallbackStub> cloudSyncCallbackStubStr,
49                                      const uint8_t *data,
50                                      size_t size)
51{
52    FuzzData fuzzData(data, size);
53    MessageParcel datas;
54    if (!WriteInterfaceToken(datas)) {
55        return;
56    }
57    int32_t state = fuzzData.GetData<int32_t>();
58    datas.WriteInt32(state);
59    int32_t error = fuzzData.GetData<int32_t>();
60    datas.WriteInt32(error);
61    datas.RewindRead(0);
62    // SERVICE_CMD_ON_SYNC_STATE_CHANGED
63    uint32_t code = 0;
64    MessageParcel reply;
65    MessageOption option;
66
67    cloudSyncCallbackStubStr->OnRemoteRequest(code, datas, reply, option);
68}
69} // namespace OHOS
70
71/* Fuzzer entry point */
72extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73{
74    /* Run your code on data */
75    if (data == nullptr || size < OHOS::U32_AT_SIZE) {
76        return 0;
77    }
78
79    auto cloudSyncCallackPtr = std::make_shared<OHOS::CloudSyncCallbackTest>();
80    auto cloudSyncCallbackStubStr =
81        std::make_shared<OHOS::FileManagement::CloudSync::CloudSyncCallbackClient>(cloudSyncCallackPtr);
82    OHOS::HandleOnSyncStateChangedFuzzTest(cloudSyncCallbackStubStr, data, size);
83    return 0;
84}
85