1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <RemoteAccessService.h>
18 #include <fuzzbinder/libbinder_ndk_driver.h>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include <gmock/gmock.h>
21 #include <grpcpp/test/mock_stream.h>
22 #include <wakeup_client.grpc.pb.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace remoteaccess {
28 
29 using ::grpc::ClientAsyncReaderInterface;
30 using ::grpc::ClientAsyncResponseReaderInterface;
31 using ::grpc::ClientContext;
32 using ::grpc::ClientReader;
33 using ::grpc::ClientReaderInterface;
34 using ::grpc::CompletionQueue;
35 using ::grpc::Status;
36 using ::grpc::testing::MockClientReader;
37 using ::testing::_;
38 using ::testing::Return;
39 
40 class MockGrpcClientStub : public WakeupClient::StubInterface {
41   public:
GetRemoteTasksRaw(ClientContext * context,const GetRemoteTasksRequest & request)42     ClientReaderInterface<GetRemoteTasksResponse>* GetRemoteTasksRaw(
43             [[maybe_unused]] ClientContext* context,
44             [[maybe_unused]] const GetRemoteTasksRequest& request) override {
45         MockClientReader<GetRemoteTasksResponse>* mockClientReader =
46                 new MockClientReader<GetRemoteTasksResponse>();
47         ON_CALL(*mockClientReader, Finish()).WillByDefault(Return(Status::OK));
48         ON_CALL(*mockClientReader, Read(_)).WillByDefault(Return(false));
49         return mockClientReader;
50     }
51 
NotifyWakeupRequired(ClientContext * context,const NotifyWakeupRequiredRequest & request,NotifyWakeupRequiredResponse * response)52     Status NotifyWakeupRequired([[maybe_unused]] ClientContext* context,
53                                 [[maybe_unused]] const NotifyWakeupRequiredRequest& request,
54                                 [[maybe_unused]] NotifyWakeupRequiredResponse* response) {
55         return Status::OK;
56     }
57 
ScheduleTask(ClientContext * context,const ScheduleTaskRequest & request,ScheduleTaskResponse * response)58     Status ScheduleTask(ClientContext* context, const ScheduleTaskRequest& request,
59                         ScheduleTaskResponse* response) {
60         return Status::OK;
61     }
62 
UnscheduleTask(ClientContext * context,const UnscheduleTaskRequest & request,UnscheduleTaskResponse * response)63     Status UnscheduleTask(ClientContext* context, const UnscheduleTaskRequest& request,
64                           UnscheduleTaskResponse* response) {
65         return Status::OK;
66     }
67 
UnscheduleAllTasks(ClientContext * context,const UnscheduleAllTasksRequest & request,UnscheduleAllTasksResponse * response)68     Status UnscheduleAllTasks(ClientContext* context, const UnscheduleAllTasksRequest& request,
69                               UnscheduleAllTasksResponse* response) {
70         return Status::OK;
71     }
72 
IsTaskScheduled(ClientContext * context,const IsTaskScheduledRequest & request,IsTaskScheduledResponse * response)73     Status IsTaskScheduled(ClientContext* context, const IsTaskScheduledRequest& request,
74                            IsTaskScheduledResponse* response) {
75         return Status::OK;
76     }
77 
GetAllPendingScheduledTasks(ClientContext * context,const GetAllPendingScheduledTasksRequest & request,GetAllPendingScheduledTasksResponse * response)78     Status GetAllPendingScheduledTasks(ClientContext* context,
79                                        const GetAllPendingScheduledTasksRequest& request,
80                                        GetAllPendingScheduledTasksResponse* response) {
81         return Status::OK;
82     }
83 
84     // Async methods which we do not care.
AsyncGetRemoteTasksRaw(ClientContext * context,const GetRemoteTasksRequest & request,CompletionQueue * cq,void * tag)85     ClientAsyncReaderInterface<GetRemoteTasksResponse>* AsyncGetRemoteTasksRaw(
86             [[maybe_unused]] ClientContext* context,
87             [[maybe_unused]] const GetRemoteTasksRequest& request,
88             [[maybe_unused]] CompletionQueue* cq, [[maybe_unused]] void* tag) {
89         return nullptr;
90     }
91 
PrepareAsyncGetRemoteTasksRaw(ClientContext * context,const GetRemoteTasksRequest & request,CompletionQueue * cq)92     ClientAsyncReaderInterface<GetRemoteTasksResponse>* PrepareAsyncGetRemoteTasksRaw(
93             [[maybe_unused]] ClientContext* context,
94             [[maybe_unused]] const GetRemoteTasksRequest& request,
95             [[maybe_unused]] CompletionQueue* cq) {
96         return nullptr;
97     }
98 
AsyncNotifyWakeupRequiredRaw(ClientContext * context,const NotifyWakeupRequiredRequest & request,CompletionQueue * cq)99     ClientAsyncResponseReaderInterface<NotifyWakeupRequiredResponse>* AsyncNotifyWakeupRequiredRaw(
100             [[maybe_unused]] ClientContext* context,
101             [[maybe_unused]] const NotifyWakeupRequiredRequest& request,
102             [[maybe_unused]] CompletionQueue* cq) {
103         return nullptr;
104     }
105 
106     ClientAsyncResponseReaderInterface<NotifyWakeupRequiredResponse>*
PrepareAsyncNotifyWakeupRequiredRaw(ClientContext * context,const NotifyWakeupRequiredRequest & request,CompletionQueue * c)107     PrepareAsyncNotifyWakeupRequiredRaw([[maybe_unused]] ClientContext* context,
108                                         [[maybe_unused]] const NotifyWakeupRequiredRequest& request,
109                                         [[maybe_unused]] CompletionQueue* c) {
110         return nullptr;
111     }
112 
AsyncScheduleTaskRaw(ClientContext * context,const ScheduleTaskRequest & request,CompletionQueue * cq)113     ClientAsyncResponseReaderInterface<ScheduleTaskResponse>* AsyncScheduleTaskRaw(
114             [[maybe_unused]] ClientContext* context,
115             [[maybe_unused]] const ScheduleTaskRequest& request,
116             [[maybe_unused]] CompletionQueue* cq) {
117         return nullptr;
118     }
119 
PrepareAsyncScheduleTaskRaw(ClientContext * context,const ScheduleTaskRequest & request,CompletionQueue * c)120     ClientAsyncResponseReaderInterface<ScheduleTaskResponse>* PrepareAsyncScheduleTaskRaw(
121             [[maybe_unused]] ClientContext* context,
122             [[maybe_unused]] const ScheduleTaskRequest& request,
123             [[maybe_unused]] CompletionQueue* c) {
124         return nullptr;
125     }
126 
AsyncUnscheduleTaskRaw(ClientContext * context,const UnscheduleTaskRequest & request,CompletionQueue * cq)127     ClientAsyncResponseReaderInterface<UnscheduleTaskResponse>* AsyncUnscheduleTaskRaw(
128             [[maybe_unused]] ClientContext* context,
129             [[maybe_unused]] const UnscheduleTaskRequest& request,
130             [[maybe_unused]] CompletionQueue* cq) {
131         return nullptr;
132     }
133 
PrepareAsyncUnscheduleTaskRaw(ClientContext * context,const UnscheduleTaskRequest & request,CompletionQueue * c)134     ClientAsyncResponseReaderInterface<UnscheduleTaskResponse>* PrepareAsyncUnscheduleTaskRaw(
135             [[maybe_unused]] ClientContext* context,
136             [[maybe_unused]] const UnscheduleTaskRequest& request,
137             [[maybe_unused]] CompletionQueue* c) {
138         return nullptr;
139     }
140 
AsyncUnscheduleAllTasksRaw(ClientContext * context,const UnscheduleAllTasksRequest & request,CompletionQueue * cq)141     ClientAsyncResponseReaderInterface<UnscheduleAllTasksResponse>* AsyncUnscheduleAllTasksRaw(
142             [[maybe_unused]] ClientContext* context,
143             [[maybe_unused]] const UnscheduleAllTasksRequest& request,
144             [[maybe_unused]] CompletionQueue* cq) {
145         return nullptr;
146     }
147 
148     ClientAsyncResponseReaderInterface<UnscheduleAllTasksResponse>*
PrepareAsyncUnscheduleAllTasksRaw(ClientContext * context,const UnscheduleAllTasksRequest & request,CompletionQueue * c)149     PrepareAsyncUnscheduleAllTasksRaw([[maybe_unused]] ClientContext* context,
150                                       [[maybe_unused]] const UnscheduleAllTasksRequest& request,
151                                       [[maybe_unused]] CompletionQueue* c) {
152         return nullptr;
153     }
154 
AsyncIsTaskScheduledRaw(ClientContext * context,const IsTaskScheduledRequest & request,CompletionQueue * cq)155     ClientAsyncResponseReaderInterface<IsTaskScheduledResponse>* AsyncIsTaskScheduledRaw(
156             [[maybe_unused]] ClientContext* context,
157             [[maybe_unused]] const IsTaskScheduledRequest& request,
158             [[maybe_unused]] CompletionQueue* cq) {
159         return nullptr;
160     }
161 
PrepareAsyncIsTaskScheduledRaw(ClientContext * context,const IsTaskScheduledRequest & request,CompletionQueue * c)162     ClientAsyncResponseReaderInterface<IsTaskScheduledResponse>* PrepareAsyncIsTaskScheduledRaw(
163             [[maybe_unused]] ClientContext* context,
164             [[maybe_unused]] const IsTaskScheduledRequest& request,
165             [[maybe_unused]] CompletionQueue* c) {
166         return nullptr;
167     }
168 
169     ClientAsyncResponseReaderInterface<GetAllPendingScheduledTasksResponse>*
AsyncGetAllPendingScheduledTasksRaw(ClientContext * context,const GetAllPendingScheduledTasksRequest & request,CompletionQueue * cq)170     AsyncGetAllPendingScheduledTasksRaw(
171             [[maybe_unused]] ClientContext* context,
172             [[maybe_unused]] const GetAllPendingScheduledTasksRequest& request,
173             [[maybe_unused]] CompletionQueue* cq) {
174         return nullptr;
175     }
176 
177     ClientAsyncResponseReaderInterface<GetAllPendingScheduledTasksResponse>*
PrepareAsyncGetAllPendingScheduledTasksRaw(ClientContext * context,const GetAllPendingScheduledTasksRequest & request,CompletionQueue * c)178     PrepareAsyncGetAllPendingScheduledTasksRaw(
179             [[maybe_unused]] ClientContext* context,
180             [[maybe_unused]] const GetAllPendingScheduledTasksRequest& request,
181             [[maybe_unused]] CompletionQueue* c) {
182         return nullptr;
183     }
184 };
185 
186 }  // namespace remoteaccess
187 }  // namespace automotive
188 }  // namespace hardware
189 }  // namespace android
190 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)191 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
192     android::hardware::automotive::remoteaccess::MockGrpcClientStub stub;
193     std::shared_ptr<android::hardware::automotive::remoteaccess::RemoteAccessService> service =
194             ndk::SharedRefBase::make<
195                     android::hardware::automotive::remoteaccess::RemoteAccessService>(&stub);
196     android::fuzzService(service->asBinder().get(), FuzzedDataProvider(data, size));
197 
198     return 0;
199 }
200