fastboot: socket testing improvements.

(This code was originally part of a huge fastboot CL but has been split
out to try to make the CLs a little more manageable).

More prep for fastboot TCP and UDP implementations. This CL adds a
SocketMock class that makes it easy to mock out network behavior so we
can unit test the TCP and UDP protocols.

Also uses the new libcutils socket_get_local_port() to avoid hardcoding
a server port in unit tests.

Bug: http://b/26157893.
Change-Id: I1ba10f31e98d7349313fc15f240383d63378a8db
diff --git a/fastboot/socket.cpp b/fastboot/socket.cpp
index d41f1fe..0a3ddfa 100644
--- a/fastboot/socket.cpp
+++ b/fastboot/socket.cpp
@@ -28,6 +28,7 @@
 
 #include "socket.h"
 
+#include <android-base/errors.h>
 #include <android-base/stringprintf.h>
 
 Socket::Socket(cutils_socket_t sock) : sock_(sock) {}
@@ -77,6 +78,10 @@
     return total;
 }
 
+int Socket::GetLocalPort() {
+    return socket_get_local_port(sock_);
+}
+
 // Implements the Socket interface for UDP.
 class UdpSocket : public Socket {
   public:
@@ -210,3 +215,12 @@
 
     return nullptr;
 }
+
+std::string Socket::GetErrorMessage() {
+#if defined(_WIN32)
+    DWORD error_code = WSAGetLastError();
+#else
+    int error_code = errno;
+#endif
+    return android::base::SystemErrorCodeToString(error_code);
+}