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