Merge "fastboot: fix SocketMock send failures."
am: e54ea1c12d

* commit 'e54ea1c12d7d793dceff2f8afdb9bf6461462d92':
  fastboot: fix SocketMock send failures.
diff --git a/fastboot/socket_mock.cpp b/fastboot/socket_mock.cpp
index bcb91ec..c962f30 100644
--- a/fastboot/socket_mock.cpp
+++ b/fastboot/socket_mock.cpp
@@ -55,8 +55,9 @@
         return false;
     }
 
+    bool return_value = events_.front().return_value;
     events_.pop();
-    return true;
+    return return_value;
 }
 
 // Mock out multi-buffer send to be one large send, since that's what it should looks like from
@@ -115,13 +116,12 @@
 }
 
 void SocketMock::ExpectSend(std::string message) {
-    events_.push(Event(EventType::kSend, std::move(message), 0, nullptr));
+    events_.push(Event(EventType::kSend, std::move(message), true, nullptr));
 }
 
-// TODO: make this properly return false to the caller.
-//void SocketMock::ExpectSendFailure(std::string message) {
-//    events_.push(Event(EventType::kSend, std::move(message), 0, nullptr));
-//}
+void SocketMock::ExpectSendFailure(std::string message) {
+    events_.push(Event(EventType::kSend, std::move(message), false, nullptr));
+}
 
 void SocketMock::AddReceive(std::string message) {
     ssize_t return_value = message.length();
diff --git a/fastboot/socket_mock.h b/fastboot/socket_mock.h
index c48aa7b..41fe06d 100644
--- a/fastboot/socket_mock.h
+++ b/fastboot/socket_mock.h
@@ -65,6 +65,9 @@
     // Adds an expectation for Send().
     void ExpectSend(std::string message);
 
+    // Adds an expectation for Send() that returns false.
+    void ExpectSendFailure(std::string message);
+
     // Adds data to provide for Receive().
     void AddReceive(std::string message);
 
diff --git a/fastboot/socket_test.cpp b/fastboot/socket_test.cpp
index 9365792..cc71075 100644
--- a/fastboot/socket_test.cpp
+++ b/fastboot/socket_test.cpp
@@ -250,6 +250,9 @@
 TEST(SocketMockTest, TestSendFailure) {
     SocketMock* mock = new SocketMock;
 
+    mock->ExpectSendFailure("foo");
+    EXPECT_FALSE(SendString(mock, "foo"));
+
     EXPECT_NONFATAL_FAILURE(SendString(mock, "foo"), "no message was expected");
 
     mock->ExpectSend("foo");
@@ -274,11 +277,24 @@
     mock.AddReceive("123");
     EXPECT_TRUE(ReceiveString(&mock, "abc"));
     EXPECT_TRUE(ReceiveString(&mock, "123"));
+
+    // Make sure ReceiveAll() can piece together multiple receives.
+    mock.AddReceive("foo");
+    mock.AddReceive("bar");
+    mock.AddReceive("123");
+    EXPECT_TRUE(ReceiveString(&mock, "foobar123"));
 }
 
 TEST(SocketMockTest, TestReceiveFailure) {
     SocketMock* mock = new SocketMock;
 
+    mock->AddReceiveFailure();
+    EXPECT_FALSE(ReceiveString(mock, "foo"));
+
+    mock->AddReceive("foo");
+    mock->AddReceiveFailure();
+    EXPECT_FALSE(ReceiveString(mock, "foobar"));
+
     EXPECT_NONFATAL_FAILURE(ReceiveString(mock, "foo"), "no message was ready");
 
     mock->ExpectSend("foo");