1 //
2 // Copyright (C) 2018 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 <stdint.h>
18 
19 #include <memory>
20 #include <set>
21 #include <string>
22 
23 #include <gmock/gmock.h>
24 
25 #include <libsnapshot/cow_writer.h>
26 #include <libsnapshot/snapshot_writer.h>
27 
28 #include "payload_consumer/file_descriptor.h"
29 #include "update_engine/aosp/dynamic_partition_control_android.h"
30 #include "update_engine/common/boot_control_interface.h"
31 #include "update_engine/common/dynamic_partition_control_interface.h"
32 
33 namespace chromeos_update_engine {
34 
35 class MockDynamicPartitionControlAndroid
36     : public DynamicPartitionControlAndroid {
37  public:
MockDynamicPartitionControlAndroid()38   MockDynamicPartitionControlAndroid()
39       : DynamicPartitionControlAndroid(0 /*source slot*/) {}
40   MOCK_METHOD(
41       bool,
42       MapPartitionOnDeviceMapper,
43       (const std::string&, const std::string&, uint32_t, bool, std::string*),
44       (override));
45   MOCK_METHOD(bool,
46               UnmapPartitionOnDeviceMapper,
47               (const std::string&),
48               (override));
49   MOCK_METHOD(void, Cleanup, (), (override));
50   MOCK_METHOD(bool, DeviceExists, (const std::string&), (override));
51   MOCK_METHOD(::android::dm::DmDeviceState,
52               GetState,
53               (const std::string&),
54               (override));
55   MOCK_METHOD(bool,
56               GetDmDevicePathByName,
57               (const std::string&, std::string*),
58               (override));
59   MOCK_METHOD(std::unique_ptr<::android::fs_mgr::MetadataBuilder>,
60               LoadMetadataBuilder,
61               (const std::string&, uint32_t),
62               (override));
63   MOCK_METHOD(std::unique_ptr<::android::fs_mgr::MetadataBuilder>,
64               LoadMetadataBuilder,
65               (const std::string&, uint32_t, uint32_t),
66               (override));
67   MOCK_METHOD(bool,
68               StoreMetadata,
69               (const std::string&, android::fs_mgr::MetadataBuilder*, uint32_t),
70               (override));
71   MOCK_METHOD(bool, GetDeviceDir, (std::string*), (override));
72   MOCK_METHOD(FeatureFlag, GetDynamicPartitionsFeatureFlag, (), (override));
73   MOCK_METHOD(std::string, GetSuperPartitionName, (uint32_t), (override));
74   MOCK_METHOD(FeatureFlag, GetVirtualAbFeatureFlag, (), (override));
75   MOCK_METHOD(FeatureFlag, GetVirtualAbCompressionFeatureFlag, (), (override));
76   MOCK_METHOD(bool, FinishUpdate, (bool), (override));
77   MOCK_METHOD(bool,
78               GetSystemOtherPath,
79               (uint32_t, uint32_t, const std::string&, std::string*, bool*),
80               (override));
81   MOCK_METHOD(bool,
82               EraseSystemOtherAvbFooter,
83               (uint32_t, uint32_t),
84               (override));
85   MOCK_METHOD(std::optional<bool>, IsAvbEnabledOnSystemOther, (), (override));
86   MOCK_METHOD(bool, IsRecovery, (), (override));
87   MOCK_METHOD(bool,
88               PrepareDynamicPartitionsForUpdate,
89               (uint32_t, uint32_t, const DeltaArchiveManifest&, bool),
90               (override));
91   MOCK_METHOD(std::unique_ptr<android::snapshot::ISnapshotWriter>,
92               OpenCowWriter,
93               (const std::string& unsuffixed_partition_name,
94                const std::optional<std::string>& source_path,
95                bool is_append),
96               (override));
97   MOCK_METHOD(FileDescriptorPtr,
98               OpenCowFd,
99               (const std::string& unsuffixed_partition_name,
100                const std::optional<std::string>& source_path,
101                bool is_append),
102               (override));
103   MOCK_METHOD(bool, MapAllPartitions, (), (override));
104   MOCK_METHOD(bool, UnmapAllPartitions, (), (override));
105   MOCK_METHOD(bool,
106               IsDynamicPartition,
107               (const std::string&, uint32_t slot),
108               (override));
109   MOCK_METHOD(bool, UpdateUsesSnapshotCompression, (), (override));
110 
set_fake_mapped_devices(const std::set<std::string> & fake)111   void set_fake_mapped_devices(const std::set<std::string>& fake) override {
112     DynamicPartitionControlAndroid::set_fake_mapped_devices(fake);
113   }
114 
RealGetSystemOtherPath(uint32_t source_slot,uint32_t target_slot,const std::string & partition_name_suffix,std::string * path,bool * should_unmap)115   bool RealGetSystemOtherPath(uint32_t source_slot,
116                               uint32_t target_slot,
117                               const std::string& partition_name_suffix,
118                               std::string* path,
119                               bool* should_unmap) {
120     return DynamicPartitionControlAndroid::GetSystemOtherPath(
121         source_slot, target_slot, partition_name_suffix, path, should_unmap);
122   }
123 
RealEraseSystemOtherAvbFooter(uint32_t source_slot,uint32_t target_slot)124   bool RealEraseSystemOtherAvbFooter(uint32_t source_slot,
125                                      uint32_t target_slot) {
126     return DynamicPartitionControlAndroid::EraseSystemOtherAvbFooter(
127         source_slot, target_slot);
128   }
129 
RealIsAvbEnabledInFstab(const std::string & path)130   std::optional<bool> RealIsAvbEnabledInFstab(const std::string& path) {
131     return DynamicPartitionControlAndroid::IsAvbEnabledInFstab(path);
132   }
133 
RealPrepareDynamicPartitionsForUpdate(uint32_t source_slot,uint32_t target_slot,const DeltaArchiveManifest & manifest,bool delete_source)134   bool RealPrepareDynamicPartitionsForUpdate(
135       uint32_t source_slot,
136       uint32_t target_slot,
137       const DeltaArchiveManifest& manifest,
138       bool delete_source) {
139     return DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
140         source_slot, target_slot, manifest, delete_source);
141   }
142   using DynamicPartitionControlAndroid::SetSourceSlot;
143   using DynamicPartitionControlAndroid::SetTargetSlot;
144 };
145 
146 }  // namespace chromeos_update_engine
147