Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_MOCK_UDEV_INTERFACE_H_ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_MOCK_UDEV_INTERFACE_H_ |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 7 | |
| 8 | #include "update_engine/gpio_handler.h" |
| 9 | #include "update_engine/udev_interface.h" |
| 10 | |
| 11 | // A set of mock udev interfaces for unit testing of GPIO handler. |
| 12 | |
| 13 | // Constant defining number of allowable mock udev objects. |
| 14 | #define MAX_UDEV_OBJECTS 1 |
| 15 | #define MAX_UDEV_ENUM_OBJECTS 3 |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | // An abstract class providing some diagnostic methods for testing. |
| 20 | class GpioMockUdevInterface : public UdevInterface { |
| 21 | public: |
| 22 | // Asserts that all resources have been properly deallocated. |
| 23 | virtual void ExpectAllResourcesDeallocated() const = 0; |
| 24 | // Asserts the the udev client has successfully discovered the syspath of the |
| 25 | // GPIO signals. |
| 26 | virtual void ExpectDiscoverySuccess() const = 0; |
| 27 | // Asserts the opposite. |
| 28 | virtual void ExpectDiscoveryFail() const = 0; |
| 29 | }; |
| 30 | |
| 31 | class StandardGpioMockUdevInterface : public GpioMockUdevInterface { |
| 32 | public: |
| 33 | // Default constructor. |
| 34 | StandardGpioMockUdevInterface(); |
| 35 | |
| 36 | // Inherited interface methods. |
| 37 | virtual const char* ListEntryGetName(struct udev_list_entry* list_entry); |
| 38 | virtual udev_list_entry* ListEntryGetNext(struct udev_list_entry* list_entry); |
| 39 | |
| 40 | virtual struct udev_device* DeviceNewFromSyspath(struct udev* udev, |
| 41 | const char* syspath); |
| 42 | virtual const char* DeviceGetPropertyValue(struct udev_device* udev_device, |
| 43 | const char* key); |
| 44 | virtual const char* DeviceGetSyspath(struct udev_device* udev_device); |
| 45 | virtual void DeviceUnref(struct udev_device* udev_device); |
| 46 | |
| 47 | virtual struct udev_enumerate* EnumerateNew(struct udev* udev); |
| 48 | virtual int EnumerateAddMatchSubsystem(struct udev_enumerate* udev_enum, |
| 49 | const char* subsystem); |
| 50 | virtual int EnumerateAddMatchSysname(struct udev_enumerate* udev_enum, |
| 51 | const char* sysname); |
| 52 | virtual int EnumerateScanDevices(struct udev_enumerate* udev_enum); |
| 53 | virtual struct udev_list_entry* EnumerateGetListEntry( |
| 54 | struct udev_enumerate* udev_enum); |
| 55 | virtual void EnumerateUnref(struct udev_enumerate* udev_enum); |
| 56 | |
| 57 | virtual struct udev* New(); |
| 58 | virtual void Unref(struct udev* udev); |
| 59 | |
| 60 | virtual void ExpectAllResourcesDeallocated() const; |
| 61 | virtual void ExpectDiscoverySuccess() const; |
| 62 | virtual void ExpectDiscoveryFail() const; |
| 63 | |
| 64 | protected: |
| 65 | // Some constants. |
| 66 | static const char* kUdevGpioSubsystem; |
| 67 | static const char* kUdevGpioChipSysname; |
| 68 | static const char* kMockGpioSysfsRoot; |
| 69 | |
| 70 | // GPIO descriptor lookup. |
| 71 | struct GpioDescriptor { |
| 72 | const char* property; |
| 73 | const char* value; |
| 74 | }; |
| 75 | static const GpioDescriptor gpio_descriptors_[]; |
| 76 | |
| 77 | // Numeric identifiers for new udev and enumerate objects. |
| 78 | size_t udev_id_; |
| 79 | size_t udev_enum_id_; |
| 80 | |
| 81 | // Null-terminated lists of devices returned by various enumerations. |
| 82 | static const char* enum_gpio_chip_dev_list_[]; |
| 83 | static const char* enum_dutflaga_gpio_dev_list_[]; |
| 84 | static const char* enum_dutflagb_gpio_dev_list_[]; |
| 85 | |
| 86 | // Mock devices to be used during GPIO discovery. These contain the syspath |
| 87 | // and a set of properties that the device may contain. |
| 88 | struct MockDevice { |
| 89 | const char* syspath; |
| 90 | const GpioDescriptor* properties; |
| 91 | size_t num_properties; |
| 92 | bool is_gpio; |
| 93 | bool is_used; |
| 94 | }; |
| 95 | MockDevice gpio_chip_dev_; |
| 96 | MockDevice dutflaga_gpio_dev_; |
| 97 | MockDevice dutflagb_gpio_dev_; |
| 98 | |
| 99 | // Tracking active mock object handles. |
| 100 | bool used_udev_ids_[MAX_UDEV_OBJECTS]; |
| 101 | bool used_udev_enum_ids_[MAX_UDEV_ENUM_OBJECTS]; |
| 102 | |
| 103 | // Track discovery progress of GPIO signals. |
| 104 | unsigned num_discovered_; |
| 105 | |
| 106 | // Convert mock udev handles into internal handles, with sanity checks. |
| 107 | MockDevice* UdevDeviceToMock(struct udev_device* udev_dev) const; |
| 108 | size_t UdevEnumToId(struct udev_enumerate* udev_enum) const; |
| 109 | size_t UdevToId(struct udev* udev) const; |
| 110 | }; |
| 111 | |
| 112 | class MultiChipGpioMockUdevInterface : public StandardGpioMockUdevInterface { |
| 113 | public: |
| 114 | // Default constructor. |
| 115 | MultiChipGpioMockUdevInterface(); |
| 116 | |
| 117 | protected: |
| 118 | virtual struct udev_device* DeviceNewFromSyspath(struct udev* udev, |
| 119 | const char* syspath); |
| 120 | virtual struct udev_list_entry* EnumerateGetListEntry( |
| 121 | struct udev_enumerate* udev_enum); |
| 122 | |
| 123 | // Null-terminated lists of devices returned by various enumerations. |
| 124 | static const char* enum_gpio_chip_dev_list_[]; |
| 125 | |
| 126 | // Mock devices to be used during GPIO discovery. These contain the syspath |
| 127 | // and a set of properties that the device may contain. |
| 128 | MockDevice gpio_chip1_dev_; |
| 129 | MockDevice gpio_chip2_dev_; |
| 130 | }; |
| 131 | |
Gilad Arnold | 95931b8 | 2013-01-09 10:37:17 -0800 | [diff] [blame] | 132 | // A udev mock that fails to provide constructs for proper GPIO initialization. |
| 133 | class FailInitGpioMockUdevInterface : public StandardGpioMockUdevInterface { |
| 134 | public: |
| 135 | // Default constructor. |
| 136 | FailInitGpioMockUdevInterface() : num_init_attempts_(0) {} |
| 137 | |
| 138 | virtual void ExpectNumInitAttempts(unsigned count) const; |
| 139 | |
| 140 | protected: |
| 141 | virtual struct udev* New(); |
| 142 | |
| 143 | private: |
| 144 | unsigned num_init_attempts_; |
| 145 | }; |
| 146 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 147 | } // namespace chromeos_update_engine |
| 148 | |
Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [diff] [blame] | 149 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_GPIO_MOCK_UDEV_INTERFACE_H_ |