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 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | #include "update_engine/gpio_handler.h" |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 8 | #include "update_engine/gpio_mock_file_descriptor.h" |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 9 | #include "update_engine/gpio_mock_udev_interface.h" |
| 10 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 11 | namespace chromeos_update_engine { |
| 12 | |
| 13 | class StandardGpioHandlerTest : public ::testing::Test {}; |
| 14 | |
| 15 | TEST(StandardGpioHandlerTest, NormalInitTest) { |
| 16 | // Ensure that initialization of the GPIO module works as expected, and that |
| 17 | // all udev resources are deallocated afterwards. The mock file descriptor is |
| 18 | // not to be used. |
| 19 | StandardGpioMockUdevInterface mock_udev; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 20 | TestModeGpioMockFileDescriptor |
| 21 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 22 | StandardGpioHandler gpio_hander(&mock_udev, &mock_file_descriptor, |
| 23 | false, false); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 24 | mock_udev.ExpectAllResourcesDeallocated(); |
| 25 | mock_udev.ExpectDiscoverySuccess(); |
Gilad Arnold | 95931b8 | 2013-01-09 10:37:17 -0800 | [diff] [blame^] | 26 | mock_file_descriptor.ExpectNumOpenAttempted(0); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | TEST(StandardGpioHandlerTest, MultiGpioChipInitTest) { |
| 30 | // Attempt GPIO discovery with a udev mock that returns two GPIO chip devices. |
| 31 | // It should fail, of course. The mock file descriptor is not to be used. |
| 32 | MultiChipGpioMockUdevInterface mock_udev; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 33 | TestModeGpioMockFileDescriptor |
| 34 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 35 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 36 | false, false); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 37 | mock_udev.ExpectAllResourcesDeallocated(); |
| 38 | mock_udev.ExpectDiscoveryFail(); |
Gilad Arnold | 95931b8 | 2013-01-09 10:37:17 -0800 | [diff] [blame^] | 39 | mock_file_descriptor.ExpectNumOpenAttempted(0); |
| 40 | } |
| 41 | |
| 42 | TEST(StandardGpioHandlerTest, FailedFirstGpioInitTest) { |
| 43 | // Attempt GPIO discovery with a udev mock that fails the initialization on |
| 44 | // the first attempt, then check for test mode. Ensure that (a) discovery is |
| 45 | // not attempted a second time, and (b) test mode check returns false (the |
| 46 | // default) without attempting to use GPIO signals. |
| 47 | FailInitGpioMockUdevInterface mock_udev; |
| 48 | TestModeGpioMockFileDescriptor |
| 49 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 50 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 51 | false, false); |
| 52 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 53 | mock_udev.ExpectAllResourcesDeallocated(); |
| 54 | mock_udev.ExpectDiscoveryFail(); |
| 55 | mock_udev.ExpectNumInitAttempts(1); |
| 56 | mock_file_descriptor.ExpectNumOpenAttempted(0); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 59 | TEST(StandardGpioHandlerTest, TestModeGpioSignalingTest) { |
| 60 | // Initialize the GPIO module and test for successful completion of the test |
| 61 | // signaling protocol. |
| 62 | StandardGpioMockUdevInterface mock_udev; |
| 63 | TestModeGpioMockFileDescriptor |
| 64 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 65 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 66 | false, false); |
| 67 | EXPECT_TRUE(gpio_handler.IsTestModeSignaled()); |
| 68 | mock_udev.ExpectAllResourcesDeallocated(); |
| 69 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 70 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 71 | } |
| 72 | |
| 73 | TEST(StandardGpioHandlerTest, DeferredInitTestModeGpioSignalingTest) { |
| 74 | // Initialize the GPIO module with deferred initialization, test for |
| 75 | // successful completion of the test signaling protocol. |
| 76 | StandardGpioMockUdevInterface mock_udev; |
| 77 | TestModeGpioMockFileDescriptor |
| 78 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 79 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 80 | true, false); |
| 81 | EXPECT_TRUE(gpio_handler.IsTestModeSignaled()); |
| 82 | mock_udev.ExpectAllResourcesDeallocated(); |
| 83 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 84 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 85 | } |
| 86 | |
| 87 | TEST(StandardGpioHandlerTest, TestModeGpioSignalingTwiceTest) { |
| 88 | // Initialize the GPIO module and query for test signal twice (uncached); the |
| 89 | // first query should succeed whereas the second should fail. |
| 90 | StandardGpioMockUdevInterface mock_udev; |
| 91 | TestModeGpioMockFileDescriptor |
| 92 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 93 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 94 | false, false); |
| 95 | EXPECT_TRUE(gpio_handler.IsTestModeSignaled()); |
| 96 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 97 | mock_udev.ExpectAllResourcesDeallocated(); |
| 98 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 99 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 100 | } |
| 101 | |
| 102 | TEST(StandardGpioHandlerTest, TestModeGpioSignalingTwiceCachedTest) { |
| 103 | // Initialize the GPIO module and query for test signal twice (cached); both |
| 104 | // queries should succeed. |
| 105 | StandardGpioMockUdevInterface mock_udev; |
| 106 | TestModeGpioMockFileDescriptor |
| 107 | mock_file_descriptor(base::TimeDelta::FromSeconds(1)); |
| 108 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 109 | false, true); |
| 110 | EXPECT_TRUE(gpio_handler.IsTestModeSignaled()); |
| 111 | EXPECT_TRUE(gpio_handler.IsTestModeSignaled()); |
| 112 | mock_udev.ExpectAllResourcesDeallocated(); |
| 113 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 114 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 115 | } |
| 116 | |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 117 | TEST(StandardGpioHandlerTest, NormalModeGpioSignalingTest) { |
| 118 | // Initialize the GPIO module, run the signaling procedure, ensure that it |
| 119 | // concluded that this is a normal mode run. |
| 120 | StandardGpioMockUdevInterface mock_udev; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 121 | NormalModeGpioMockFileDescriptor mock_file_descriptor; |
| 122 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 123 | false, false); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 124 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 125 | mock_udev.ExpectAllResourcesDeallocated(); |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 126 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 127 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 128 | } |
| 129 | |
| 130 | TEST(StandardGpioHandlerTest, NonPulledUpNormalModeGpioSignalingTest) { |
| 131 | // Initialize the GPIO module with a non-pulled up mock (which means the it |
| 132 | // returns a different default signal), run the signaling procedure, ensure |
| 133 | // that it concluded that this is a normal mode run. |
| 134 | StandardGpioMockUdevInterface mock_udev; |
| 135 | NonPulledUpNormalModeGpioMockFileDescriptor mock_file_descriptor; |
| 136 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 137 | false, false); |
| 138 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 139 | mock_udev.ExpectAllResourcesDeallocated(); |
| 140 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 141 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | TEST(StandardGpioHandlerTest, DeferredInitNormalModeGpioSignalingTest) { |
| 145 | // Initialize the GPIO module with deferred discovery, run the signaling |
| 146 | // procedure, ensure that it concluded that this is a normal mode run. |
| 147 | StandardGpioMockUdevInterface mock_udev; |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 148 | NormalModeGpioMockFileDescriptor mock_file_descriptor; |
| 149 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 150 | true, false); |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 151 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 152 | mock_udev.ExpectAllResourcesDeallocated(); |
Gilad Arnold | 6eccc53 | 2012-05-17 15:44:22 -0700 | [diff] [blame] | 153 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 154 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 155 | } |
| 156 | |
| 157 | TEST(StandardGpioHandlerTest, FlipInputDirErrorNormalModeGpioSignalingTest) { |
| 158 | // Test the GPIO module with a mock that simulates a GPIO sysfs race/hack, |
| 159 | // which causes the input GPIO to flip direction. Ensure that it concludes |
| 160 | // that this is a normal mode run. |
| 161 | StandardGpioMockUdevInterface mock_udev; |
| 162 | ErrorNormalModeGpioMockFileDescriptor |
| 163 | mock_file_descriptor( |
| 164 | base::TimeDelta::FromSeconds(1), |
| 165 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorFlipInputDir); |
| 166 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 167 | false, false); |
| 168 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 169 | mock_udev.ExpectAllResourcesDeallocated(); |
| 170 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 171 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 172 | } |
| 173 | |
| 174 | TEST(StandardGpioHandlerTest, ReadInvalidValErrorNormalModeGpioSignalingTest) { |
| 175 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 176 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 177 | // run. |
| 178 | StandardGpioMockUdevInterface mock_udev; |
| 179 | ErrorNormalModeGpioMockFileDescriptor |
| 180 | mock_file_descriptor( |
| 181 | base::TimeDelta::FromSeconds(1), |
| 182 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorReadInvalidVal); |
| 183 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 184 | false, false); |
| 185 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 186 | mock_udev.ExpectAllResourcesDeallocated(); |
| 187 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 188 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 189 | } |
| 190 | |
| 191 | TEST(StandardGpioHandlerTest, ReadInvalidDirErrorNormalModeGpioSignalingTest) { |
| 192 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 193 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 194 | // run. |
| 195 | StandardGpioMockUdevInterface mock_udev; |
| 196 | ErrorNormalModeGpioMockFileDescriptor |
| 197 | mock_file_descriptor( |
| 198 | base::TimeDelta::FromSeconds(1), |
| 199 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorReadInvalidDir); |
| 200 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 201 | false, false); |
| 202 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 203 | mock_udev.ExpectAllResourcesDeallocated(); |
| 204 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 205 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 206 | } |
| 207 | |
| 208 | TEST(StandardGpioHandlerTest, FailFileOpenErrorNormalModeGpioSignalingTest) { |
| 209 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 210 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 211 | // run. |
| 212 | StandardGpioMockUdevInterface mock_udev; |
| 213 | ErrorNormalModeGpioMockFileDescriptor |
| 214 | mock_file_descriptor( |
| 215 | base::TimeDelta::FromSeconds(1), |
| 216 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorFailFileOpen); |
| 217 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 218 | false, false); |
| 219 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 220 | mock_udev.ExpectAllResourcesDeallocated(); |
| 221 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 222 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 223 | } |
| 224 | |
| 225 | TEST(StandardGpioHandlerTest, FailFileReadErrorNormalModeGpioSignalingTest) { |
| 226 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 227 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 228 | // run. |
| 229 | StandardGpioMockUdevInterface mock_udev; |
| 230 | ErrorNormalModeGpioMockFileDescriptor |
| 231 | mock_file_descriptor( |
| 232 | base::TimeDelta::FromSeconds(1), |
| 233 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorFailFileRead); |
| 234 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 235 | false, false); |
| 236 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 237 | mock_udev.ExpectAllResourcesDeallocated(); |
| 238 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 239 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 240 | } |
| 241 | |
| 242 | TEST(StandardGpioHandlerTest, FailFileWriteErrorNormalModeGpioSignalingTest) { |
| 243 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 244 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 245 | // run. |
| 246 | StandardGpioMockUdevInterface mock_udev; |
| 247 | ErrorNormalModeGpioMockFileDescriptor |
| 248 | mock_file_descriptor( |
| 249 | base::TimeDelta::FromSeconds(1), |
| 250 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorFailFileWrite); |
| 251 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 252 | false, false); |
| 253 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 254 | mock_udev.ExpectAllResourcesDeallocated(); |
| 255 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 256 | mock_file_descriptor.ExpectAllGpiosRestoredToDefault(); |
| 257 | } |
| 258 | |
| 259 | TEST(StandardGpioHandlerTest, FailFileCloseErrorNormalModeGpioSignalingTest) { |
| 260 | // Test the GPIO module with a mock that simulates an invalid value reading |
| 261 | // from a GPIO device. Ensure that it concludes that this is a normal mode |
| 262 | // run. |
| 263 | StandardGpioMockUdevInterface mock_udev; |
| 264 | ErrorNormalModeGpioMockFileDescriptor |
| 265 | mock_file_descriptor( |
| 266 | base::TimeDelta::FromSeconds(1), |
| 267 | ErrorNormalModeGpioMockFileDescriptor::kGpioErrorFailFileClose); |
| 268 | StandardGpioHandler gpio_handler(&mock_udev, &mock_file_descriptor, |
| 269 | false, false); |
| 270 | EXPECT_FALSE(gpio_handler.IsTestModeSignaled()); |
| 271 | mock_udev.ExpectAllResourcesDeallocated(); |
| 272 | mock_file_descriptor.ExpectAllResourcesDeallocated(); |
| 273 | // Don't test GPIO status restored; since closing of sysfs files fails, all |
| 274 | // bets are off. |
Gilad Arnold | 4d740eb | 2012-05-15 08:48:13 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | } // namespace chromeos_update_engine |