AU: fix bug in GPIO handler that caused failed discovery to be ignored

This solves two issues:

* The GPIO handler will attempt to use GPIO devices even when discovery
  via udev has failed. Not any more.

* GPIO discovery would return success even when it failed on a previous
  attempt. Now it'll reflect the actual result of the discovery attempt.

* Reporting of file descriptor open errors is obscured due to
  intermittent operations that reset errno, now fixed.

* Added test case to ensure that repeat GPIO discovery is not attempted,
  and that test mode check will automatically fail if a previous
  initialization/discovery had failed.

BUG=None
TEST=Passes unit tests
TEST=All symptoms gone on real update attempt w/ x86-alex

Change-Id: I01a7b1e316dbb5b94487a5aad1560d994feae9ff
Reviewed-on: https://gerrit.chromium.org/gerrit/40946
Commit-Queue: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
diff --git a/gpio_mock_file_descriptor.cc b/gpio_mock_file_descriptor.cc
index 16f9473..cadec4c 100644
--- a/gpio_mock_file_descriptor.cc
+++ b/gpio_mock_file_descriptor.cc
@@ -49,7 +49,8 @@
 
 GpioMockFileDescriptor::GpioMockFileDescriptor()
     : gpio_id_(kMockGpioIdMax),
-      gpio_subdev_(kMockGpioSubdevMax) {
+      gpio_subdev_(kMockGpioSubdevMax),
+      num_open_attempted_(0) {
   // All GPIOs are initially in the input direction, their read value is "up",
   // and they assume an initial write value of "up" with current (init) time.
   Time init_time = Time::Now();
@@ -67,6 +68,8 @@
 }
 
 bool GpioMockFileDescriptor::Open(const char* path, int flags, mode_t mode) {
+  num_open_attempted_++;
+
   EXPECT_EQ(gpio_id_, kMockGpioIdMax);
   if (gpio_id_ != kMockGpioIdMax)
     return false;
@@ -265,6 +268,11 @@
   return is_all_gpios_restored_to_default;
 }
 
+bool GpioMockFileDescriptor::ExpectNumOpenAttempted(unsigned count) {
+  EXPECT_EQ(num_open_attempted_, count);
+  return (num_open_attempted_ == count);
+}
+
 size_t GpioMockFileDescriptor::DecodeGpioString(const char* buf,
                                                       size_t count,
                                                       const char** strs,