From 945271d7d761943f77c794a6a3306455b229a463 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Mon, 1 Apr 2019 15:50:57 +0300
Subject: [PATCH] Fix more net tests to work in Windows, but not all yet.

---
 cmake/FindURIParser.cmake        |  2 +-
 net/cpp/src/net.cpp              |  4 ++++
 net/cpp/test/CMakeLists.txt      | 10 +++++-----
 net/cpp/test/net_integration.cpp |  7 +++----
 net/cpp/test/socket_unit.cpp     | 20 +++++++++++++++++++-
 5 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/cmake/FindURIParser.cmake b/cmake/FindURIParser.cmake
index 70d3366eb..e1db24168 100644
--- a/cmake/FindURIParser.cmake
+++ b/cmake/FindURIParser.cmake
@@ -31,6 +31,6 @@ mark_as_advanced(URIPARSER_FOUND)
 if(URIPARSER_FOUND)
 	include_directories(${URIPARSER_INCLUDE_DIRS})
     set(URIPARSER_FOUND TRUE CACHE BOOL "" FORCE)
-    set(URIPARSER_LIBRARIES ${URIPARSER_LIBRARY})
+    set(URIPARSER_LIBRARIES "${URIPARSER_LIBRARY}")
     message(STATUS "Found URIParser")
 endif()
diff --git a/net/cpp/src/net.cpp b/net/cpp/src/net.cpp
index 3ea57c1c4..9aae938f8 100644
--- a/net/cpp/src/net.cpp
+++ b/net/cpp/src/net.cpp
@@ -10,6 +10,10 @@
 #include <iostream>
 #include <chrono>
 
+#ifdef WIN32
+#pragma comment(lib, "Ws2_32.lib")
+#endif
+
 using namespace std;
 using namespace std::chrono;
 using ftl::net::Listener;
diff --git a/net/cpp/test/CMakeLists.txt b/net/cpp/test/CMakeLists.txt
index ee774f88d..03a789594 100644
--- a/net/cpp/test/CMakeLists.txt
+++ b/net/cpp/test/CMakeLists.txt
@@ -3,21 +3,21 @@ add_executable(protocol_unit
 	./protocol_unit.cpp
 )
 target_include_directories(protocol_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(protocol_unit glog)
+target_link_libraries(protocol_unit ${GLOG_LIBRARIES})
 
 add_executable(socket_unit
 	./tests.cpp
 	./socket_unit.cpp
 )
 target_include_directories(socket_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(socket_unit uriparser glog)
+target_link_libraries(socket_unit ${URIPARSER_LIBRARIES} ${GLOG_LIBRARIES})
 
 add_executable(uri_unit
 	./tests.cpp
 	./uri_unit.cpp
 )
 target_include_directories(uri_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(uri_unit uriparser)
+target_link_libraries(uri_unit ${URIPARSER_LIBRARIES})
 
 add_executable(p2p_base_unit
 	./tests.cpp
@@ -30,7 +30,7 @@ add_executable(p2p_base_unit
 	../src/listener.cpp
 )
 target_include_directories(p2p_base_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(p2p_base_unit uriparser gflags glog uuid)
+target_link_libraries(p2p_base_unit ${URIPARSER_LIBRARIES} gflags ${GLOG_LIBRARIES} uuid)
 
 add_executable(net_integration
 	./tests.cpp
@@ -42,7 +42,7 @@ add_executable(net_integration
 	../src/net.cpp
 )
 target_include_directories(net_integration PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(net_integration uriparser glog)
+target_link_libraries(net_integration ${URIPARSER_LIBRARIES} ${GLOG_LIBRARIES})
 
 add_test(URIUnitTest uri_unit)
 add_test(ProtocolUnitTest protocol_unit)
diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp
index 86a478d8d..ea915da0b 100644
--- a/net/cpp/test/net_integration.cpp
+++ b/net/cpp/test/net_integration.cpp
@@ -25,9 +25,8 @@ using std::shared_ptr;
 
 #ifdef WIN32
 #include <windows.h>
-#include <winsock.h>
-typedef int socklen_t;
-#define MSG_WAITALL 0
+#include <winsock2.h>
+#include <Ws2tcpip.h>
 #endif
 
 static int ssock = INVALID_SOCKET;
@@ -66,7 +65,7 @@ void init_server() {
 	}
 
 	int enable = 1;
-	if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
+	if (setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, (char*)&enable, sizeof(int)) < 0)
     	std::cerr << "setsockopt(SO_REUSEADDR) failed" << std::endl;
 
 	//Specify listen port and address
diff --git a/net/cpp/test/socket_unit.cpp b/net/cpp/test/socket_unit.cpp
index 881da27a9..fc76725e7 100644
--- a/net/cpp/test/socket_unit.cpp
+++ b/net/cpp/test/socket_unit.cpp
@@ -8,6 +8,10 @@
 
 using ftl::net::Socket;
 
+#ifdef WIN32
+#pragma comment(lib, "Ws2_32.lib")
+#endif
+
 // --- Mock --------------------------------------------------------------------
 
 class MockSocket : public Socket {
@@ -43,7 +47,8 @@ static std::map<int, std::string> fakedata;
 
 void fake_send(int sd, uint32_t  service, const std::string &data) {
 	//std::cout << "HEX SEND: " << hexStr(data) << std::endl;
-	char buf[8+data.size()];
+	char buf[8+1024];
+	assert(data.size() < 1024);
 	ftl::net::Header *h = (ftl::net::Header*)&buf;
 	h->size = data.size()+4;
 	h->service = service;
@@ -53,7 +58,11 @@ void fake_send(int sd, uint32_t  service, const std::string &data) {
 	//std::cout << "HEX SEND2: " << hexStr(fakedata[sd]) << std::endl;
 }
 
+#ifdef WIN32
+extern int recv(SOCKET sd, char *buf, int n, int f) {
+#else
 extern ssize_t recv(int sd, void *buf, size_t n, int f) {
+#endif
 	if (fakedata.count(sd) == 0) {
 		std::cout << "Unrecognised socket" << std::endl;
 		return 0;
@@ -66,6 +75,14 @@ extern ssize_t recv(int sd, void *buf, size_t n, int f) {
 	return l;
 }
 
+#ifdef WIN32
+extern int send(SOCKET sd, const char *v, int cnt, int flags) {
+	int len = cnt;
+	// TODO(nick) merge multiple sends
+	fakedata[sd] = std::string(v, len);
+	return len;
+}
+#else
 extern ssize_t writev(int sd, const struct iovec *v, int cnt) {
 	size_t len = 0; //v[0].iov_len+v[1].iov_len;
 	char buf[1000];
@@ -80,6 +97,7 @@ extern ssize_t writev(int sd, const struct iovec *v, int cnt) {
 	fakedata[sd] = std::string(&buf[0], len);
 	return len;
 }
+#endif
 
 uint32_t get_service(int sd) {
 	auto h = (ftl::net::Header*)fakedata[sd].data();
-- 
GitLab