• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022, The Android Open Source Project
2 //
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 #![no_main]
16 
17 use libfuzzer_sys::{arbitrary::Arbitrary, fuzz_target};
18 use uwb_core::service::{
19     default_runtime, NopUwbServiceCallback, ProtoUwbService, UwbServiceBuilder,
20     UwbServiceCallbackSendBuilder,
21 };
22 use uwb_core::uci::{NopUciHal, NopUciLoggerFactory};
23 
24 /// The list of the ProtoUwbService's methods that take the argument.
25 #[derive(Arbitrary, Debug)]
26 enum Command {
27     SetLoggerMode,
28     InitSession,
29     DeinitSession,
30     StartRanging,
31     StopRanging,
32     Reconfigure,
33     UpdateControllerMulticastList,
34     AndroidSetCountryCode,
35     SendVendorCmd,
36     SessionParams,
37 }
38 
39 fuzz_target!(|methods: Vec<(Command, &[u8])>| {
40     // Setup the ProtoUwbService.
41     let runtime = default_runtime().unwrap();
42     let service = UwbServiceBuilder::new()
43         .runtime_handle(runtime.handle().to_owned())
44         .callback_builder(UwbServiceCallbackSendBuilder::new(NopUwbServiceCallback {}))
45         .uci_hal(NopUciHal {})
46         .uci_logger_factory(NopUciLoggerFactory {})
47         .build()
48         .unwrap();
49     let mut proto_service = ProtoUwbService::new(service);
50     let _ = proto_service.enable();
51 
52     // Call the methods of ProtoUwbService that takes the argument.
53     for (command, bytes) in methods.into_iter() {
54         match command {
55             Command::SetLoggerMode => {
56                 let _ = proto_service.set_logger_mode(bytes);
57             }
58             Command::InitSession => {
59                 let _ = proto_service.init_session(bytes);
60             }
61             Command::DeinitSession => {
62                 let _ = proto_service.deinit_session(bytes);
63             }
64             Command::StartRanging => {
65                 let _ = proto_service.start_ranging(bytes);
66             }
67             Command::StopRanging => {
68                 let _ = proto_service.stop_ranging(bytes);
69             }
70             Command::Reconfigure => {
71                 let _ = proto_service.reconfigure(bytes);
72             }
73             Command::UpdateControllerMulticastList => {
74                 let _ = proto_service.update_controller_multicast_list(bytes);
75             }
76             Command::AndroidSetCountryCode => {
77                 let _ = proto_service.android_set_country_code(bytes);
78             }
79             Command::SendVendorCmd => {
80                 let _ = proto_service.raw_uci_cmd(bytes);
81             }
82             Command::SessionParams => {
83                 let _ = proto_service.session_params(bytes);
84             }
85         }
86     }
87 });
88