| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2015 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 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ | 
|  | 18 | #define UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 19 |  | 
|  | 20 | #include <map> | 
|  | 21 | #include <string> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <base/time/time.h> | 
|  | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/boot_control_interface.h" | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 27 |  | 
|  | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
|  | 30 | // Implements a fake bootloader control interface used for testing. | 
|  | 31 | class FakeBootControl : public BootControlInterface { | 
|  | 32 | public: | 
|  | 33 | FakeBootControl() { | 
|  | 34 | SetNumSlots(num_slots_); | 
|  | 35 | // The current slot should be bootable. | 
|  | 36 | is_bootable_[current_slot_] = true; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | // BootControlInterface overrides. | 
|  | 40 | unsigned int GetNumSlots() const override { return num_slots_; } | 
|  | 41 | BootControlInterface::Slot GetCurrentSlot() const override { | 
|  | 42 | return current_slot_; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | bool GetPartitionDevice(const std::string& partition_name, | 
|  | 46 | BootControlInterface::Slot slot, | 
|  | 47 | std::string* device) const override { | 
|  | 48 | if (slot >= num_slots_) | 
|  | 49 | return false; | 
|  | 50 | auto part_it = devices_[slot].find(partition_name); | 
|  | 51 | if (part_it == devices_[slot].end()) | 
|  | 52 | return false; | 
|  | 53 | *device = part_it->second; | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | bool IsSlotBootable(BootControlInterface::Slot slot) const override { | 
|  | 58 | return slot < num_slots_ && is_bootable_[slot]; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | bool MarkSlotUnbootable(BootControlInterface::Slot slot) override { | 
|  | 62 | if (slot >= num_slots_) | 
|  | 63 | return false; | 
|  | 64 | is_bootable_[slot] = false; | 
|  | 65 | return true; | 
|  | 66 | } | 
|  | 67 |  | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 68 | bool SetActiveBootSlot(Slot slot) override { return true; } | 
|  | 69 |  | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 70 | bool MarkBootSuccessfulAsync(base::Callback<void(bool)> callback) override { | 
|  | 71 | // We run the callback directly from here to avoid having to setup a message | 
|  | 72 | // loop in the test environment. | 
|  | 73 | callback.Run(true); | 
|  | 74 | return true; | 
|  | 75 | } | 
|  | 76 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 77 | // Setters | 
|  | 78 | void SetNumSlots(unsigned int num_slots) { | 
|  | 79 | num_slots_ = num_slots; | 
|  | 80 | is_bootable_.resize(num_slots_, false); | 
|  | 81 | devices_.resize(num_slots_); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | void SetCurrentSlot(BootControlInterface::Slot slot) { | 
|  | 85 | current_slot_ = slot; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | void SetPartitionDevice(const std::string partition_name, | 
|  | 89 | BootControlInterface::Slot slot, | 
|  | 90 | const std::string device) { | 
|  | 91 | DCHECK(slot < num_slots_); | 
|  | 92 | devices_[slot][partition_name] = device; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | void SetSlotBootable(BootControlInterface::Slot slot, bool bootable) { | 
|  | 96 | DCHECK(slot < num_slots_); | 
|  | 97 | is_bootable_[slot] = bootable; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | private: | 
|  | 101 | BootControlInterface::Slot num_slots_{2}; | 
|  | 102 | BootControlInterface::Slot current_slot_{0}; | 
|  | 103 |  | 
|  | 104 | std::vector<bool> is_bootable_; | 
|  | 105 | std::vector<std::map<std::string, std::string>> devices_; | 
|  | 106 |  | 
|  | 107 | DISALLOW_COPY_AND_ASSIGN(FakeBootControl); | 
|  | 108 | }; | 
|  | 109 |  | 
|  | 110 | }  // namespace chromeos_update_engine | 
|  | 111 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 112 | #endif  // UPDATE_ENGINE_COMMON_FAKE_BOOT_CONTROL_H_ |