• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     template <typename T>
fillParameter(size_t size,std::vector<T> & data)102     void fillParameter(size_t size, std::vector<T>& data) {
103         for (size_t i = 0; i < size; ++i) {
104             data.push_back(mFuzzedDataProvider->ConsumeIntegral<T>());
105         }
106     }
107 
108   private:
109     FuzzedDataProvider* mFuzzedDataProvider = nullptr;
110     VehiclePropValue mActualValue = VehiclePropValue{};
111     StatusCode mActualStatusCode = StatusCode::OK;
112 
113     VehiclePropValuePool* mObjectPool = nullptr;
114     std::unique_ptr<MockedVehicleHal> mHal;
115     std::unique_ptr<VehicleHalManager> mManager;
116 
117     void invokeDebug();
118     void initValue();
119     void invokePropConfigs();
120     void invokeSubscribe();
121     void invokeSetAndGetValues();
122     void invokeObd2SensorStore();
123     void invokeVmsUtils();
124     void invokeVehiclePropStore();
125     void invokeWatchDogClient();
126     void invokeGetSubscribedLayers(VmsMessageType type);
127     void invokeGet(int32_t property, int32_t areaId);
128 };
129 
130 }  // namespace android::hardware::automotive::vehicle::V2_0::fuzzer
131 
132 #endif  // __VEHICLE_MANAGER_FUZZER_H__
133