1 /******************************************************************************
2  *
3  * Copyright (C) 2021 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 #ifndef __VEHICLE_MANAGER_FUZZER_H__
22 #define __VEHICLE_MANAGER_FUZZER_H__
23 
24 #include <vhal_v2_0/VehicleHalManager.h>
25 #include <vhal_v2_0/VehiclePropertyStore.h>
26 #include <vhal_v2_0/VmsUtils.h>
27 
28 #include <VehicleHalTestUtils.h>
29 #include <fuzzer/FuzzedDataProvider.h>
30 
31 namespace android::hardware::automotive::vehicle::V2_0::fuzzer {
32 
33 constexpr int kRetriableAttempts = 3;
34 
35 class MockedVehicleHal : public VehicleHal {
36   public:
MockedVehicleHal()37     MockedVehicleHal()
38         : mFuelCapacityAttemptsLeft(kRetriableAttempts),
39           mMirrorFoldAttemptsLeft(kRetriableAttempts) {
40         mConfigs.assign(std::begin(kVehicleProperties), std::end(kVehicleProperties));
41     }
42 
listProperties()43     std::vector<VehiclePropConfig> listProperties() override { return mConfigs; }
44 
45     VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue,
46                             StatusCode* outStatus) override;
47 
set(const VehiclePropValue & propValue)48     StatusCode set(const VehiclePropValue& propValue) override {
49         if (toInt(VehicleProperty::MIRROR_FOLD) == propValue.prop &&
50             mMirrorFoldAttemptsLeft-- > 0) {
51             return StatusCode::TRY_AGAIN;
52         }
53 
54         mValues[makeKey(propValue)] = propValue;
55         return StatusCode::OK;
56     }
57 
subscribe(int32_t,float)58     StatusCode subscribe(int32_t /* property */, float /* sampleRate */) override {
59         return StatusCode::OK;
60     }
61 
unsubscribe(int32_t)62     StatusCode unsubscribe(int32_t /* property */) override { return StatusCode::OK; }
63 
sendPropEvent(recyclable_ptr<VehiclePropValue> value)64     void sendPropEvent(recyclable_ptr<VehiclePropValue> value) { doHalEvent(std::move(value)); }
65 
sendHalError(StatusCode error,int32_t property,int32_t areaId)66     void sendHalError(StatusCode error, int32_t property, int32_t areaId) {
67         doHalPropertySetError(error, property, areaId);
68     }
69 
70   private:
makeKey(const VehiclePropValue & v)71     int64_t makeKey(const VehiclePropValue& v) const { return makeKey(v.prop, v.areaId); }
72 
makeKey(int32_t prop,int32_t area)73     int64_t makeKey(int32_t prop, int32_t area) const {
74         return (static_cast<int64_t>(prop) << 32) | area;
75     }
76 
77   private:
78     std::vector<VehiclePropConfig> mConfigs;
79     std::unordered_map<int64_t, VehiclePropValue> mValues;
80     int mFuelCapacityAttemptsLeft;
81     int mMirrorFoldAttemptsLeft;
82 };
83 
84 class VehicleHalManagerFuzzer {
85   public:
VehicleHalManagerFuzzer()86     VehicleHalManagerFuzzer() {
87         mHal.reset(new MockedVehicleHal);
88         mManager.reset(new VehicleHalManager(mHal.get()));
89         mObjectPool = mHal->getValuePool();
90     }
~VehicleHalManagerFuzzer()91     ~VehicleHalManagerFuzzer() {
92         mManager.reset(nullptr);
93         mHal.reset(nullptr);
94         mObjectPool = nullptr;
95         if (mFuzzedDataProvider) {
96             delete mFuzzedDataProvider;
97         }
98     }
99     void process(const uint8_t* data, size_t size);
100 
101   private:
102     FuzzedDataProvider* mFuzzedDataProvider = nullptr;
103     VehiclePropValue mActualValue = VehiclePropValue{};
104     StatusCode mActualStatusCode = StatusCode::OK;
105 
106     VehiclePropValuePool* mObjectPool = nullptr;
107     std::unique_ptr<MockedVehicleHal> mHal;
108     std::unique_ptr<VehicleHalManager> mManager;
109 
110     void invokeDebug();
111     void invokePropConfigs();
112     void invokeSubscribe();
113     void invokeSetAndGetValues();
114     void invokeObd2SensorStore();
115     void invokeVmsUtils();
116     void invokeVehiclePropStore();
117     void invokeWatchDogClient();
118     void invokeGetSubscribedLayers(VmsMessageType type);
119     void invokeGet(int32_t property, int32_t areaId);
120 };
121 
122 }  // namespace android::hardware::automotive::vehicle::V2_0::fuzzer
123 
124 #endif  // __VEHICLE_MANAGER_FUZZER_H__
125