From babe17674797f175341a2f3b5b7e581a4161ee4e Mon Sep 17 00:00:00 2001
From: John Smith <jsmith@example.com>
Date: Thu, 17 Mar 2022 20:54:50 -0400
Subject: [PATCH] solve unit test issue for docker

---
 .../intf/native/network/protocol/sockets.rs   | 36 +++++++-------
 .../intf/native/network/start_protocols.rs    | 48 +++++++++----------
 .../src/tests/common/test_veilid_config.rs    | 24 +++++-----
 3 files changed, 54 insertions(+), 54 deletions(-)

diff --git a/veilid-core/src/intf/native/network/protocol/sockets.rs b/veilid-core/src/intf/native/network/protocol/sockets.rs
index 7f97bee7..ac1e18db 100644
--- a/veilid-core/src/intf/native/network/protocol/sockets.rs
+++ b/veilid-core/src/intf/native/network/protocol/sockets.rs
@@ -83,14 +83,14 @@ pub fn new_bound_first_udp_socket(local_address: SocketAddr) -> Result<Socket, S
 
     // Set 'reuse address' so future binds to this port will succeed
     // This does not work on Windows, where reuse options can not be set after the bind
-    // cfg_if! {
-    //     if #[cfg(unix)] {
-    //         socket
-    //             .set_reuse_address(true)
-    //             .map_err(|e| format!("Couldn't set reuse address: {}", e))?;
-    //         socket.set_reuse_port(true).map_err(|e| format!("Couldn't set reuse port: {}", e))?;
-    //     }
-    // }
+    cfg_if! {
+        if #[cfg(unix)] {
+            socket
+                .set_reuse_address(true)
+                .map_err(|e| format!("Couldn't set reuse address: {}", e))?;
+            socket.set_reuse_port(true).map_err(|e| format!("Couldn't set reuse port: {}", e))?;
+        }
+    }
     log_net!("created bound first udp socket on {:?}", &local_address);
 
     Ok(socket)
@@ -170,16 +170,16 @@ pub fn new_bound_first_tcp_socket(local_address: SocketAddr) -> Result<Socket, S
         .bind(&socket2_addr)
         .map_err(|e| format!("failed to bind TCP socket: {}", e))?;
 
-    // // Set 'reuse address' so future binds to this port will succeed
-    // // This does not work on Windows, where reuse options can not be set after the bind
-    // cfg_if! {
-    //     if #[cfg(unix)] {
-    //     socket
-    //         .set_reuse_address(true)
-    //         .map_err(|e| format!("Couldn't set reuse address: {}", e))?;
-    //     socket.set_reuse_port(true).map_err(|e| format!("Couldn't set reuse port: {}", e))?;
-    //     }
-    // }
+    // Set 'reuse address' so future binds to this port will succeed
+    // This does not work on Windows, where reuse options can not be set after the bind
+    cfg_if! {
+        if #[cfg(unix)] {
+        socket
+            .set_reuse_address(true)
+            .map_err(|e| format!("Couldn't set reuse address: {}", e))?;
+        socket.set_reuse_port(true).map_err(|e| format!("Couldn't set reuse port: {}", e))?;
+        }
+    }
     log_net!("created bound first tcp socket on {:?}", &local_address);
 
     Ok(socket)
diff --git a/veilid-core/src/intf/native/network/start_protocols.rs b/veilid-core/src/intf/native/network/start_protocols.rs
index 7ac260d3..9ab710c8 100644
--- a/veilid-core/src/intf/native/network/start_protocols.rs
+++ b/veilid-core/src/intf/native/network/start_protocols.rs
@@ -100,18 +100,18 @@ impl Network {
             }
         }
         if let (Some(bfs4), Some(bfs6)) = (bound_first_socket_v4, bound_first_socket_v6) {
-            //cfg_if! {
-            //if #[cfg(windows)] {
-            // On windows, drop the socket. This is a race condition, but there's
-            // no way around it. This isn't for security anyway, it's to prevent multiple copies of the
-            // app from binding on the same port.
-            drop(bfs4);
-            drop(bfs6);
-            inner.bound_first_udp.insert(udp_port, None);
-            //} else {
-            //    inner.bound_first_udp.insert(udp_port, Some((bfs4, bfs6)));
-            //}
-            //}
+            cfg_if! {
+                if #[cfg(windows)] {
+                    // On windows, drop the socket. This is a race condition, but there's
+                    // no way around it. This isn't for security anyway, it's to prevent multiple copies of the
+                    // app from binding on the same port.
+                    drop(bfs4);
+                    drop(bfs6);
+                    inner.bound_first_udp.insert(udp_port, None);
+                } else {
+                    inner.bound_first_udp.insert(udp_port, Some((bfs4, bfs6)));
+                }
+            }
             true
         } else {
             false
@@ -138,18 +138,18 @@ impl Network {
             }
         }
         if let (Some(bfs4), Some(bfs6)) = (bound_first_socket_v4, bound_first_socket_v6) {
-            //cfg_if! {
-            //if #[cfg(windows)] {
-            // On windows, drop the socket. This is a race condition, but there's
-            // no way around it. This isn't for security anyway, it's to prevent multiple copies of the
-            // app from binding on the same port.
-            drop(bfs4);
-            drop(bfs6);
-            inner.bound_first_tcp.insert(tcp_port, None);
-            // } else {
-            //     inner.bound_first_tcp.insert(tcp_port, Some((bfs4, bfs6)));
-            // }
-            //}
+            cfg_if! {
+                if #[cfg(windows)] {
+                    // On windows, drop the socket. This is a race condition, but there's
+                    // no way around it. This isn't for security anyway, it's to prevent multiple copies of the
+                    // app from binding on the same port.
+                    drop(bfs4);
+                    drop(bfs6);
+                    inner.bound_first_tcp.insert(tcp_port, None);
+                } else {
+                    inner.bound_first_tcp.insert(tcp_port, Some((bfs4, bfs6)));
+                }
+            }
             true
         } else {
             false
diff --git a/veilid-core/src/tests/common/test_veilid_config.rs b/veilid-core/src/tests/common/test_veilid_config.rs
index dcad6a10..cc3c1fb1 100644
--- a/veilid-core/src/tests/common/test_veilid_config.rs
+++ b/veilid-core/src/tests/common/test_veilid_config.rs
@@ -217,32 +217,32 @@ fn config_callback(key: String) -> ConfigCallbackReturn {
         "network.tls.private_key_path" => Ok(Box::new(get_keyfile_path())),
         "network.tls.connection_initial_timeout_ms" => Ok(Box::new(2_000u32)),
         "network.application.https.enabled" => Ok(Box::new(false)),
-        "network.application.https.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.application.https.listen_address" => Ok(Box::new("".to_owned())),
         "network.application.https.path" => Ok(Box::new(String::from("app"))),
         "network.application.https.url" => Ok(Box::new(Option::<String>::None)),
         "network.application.http.enabled" => Ok(Box::new(false)),
-        "network.application.http.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.application.http.listen_address" => Ok(Box::new("".to_owned())),
         "network.application.http.path" => Ok(Box::new(String::from("app"))),
         "network.application.http.url" => Ok(Box::new(Option::<String>::None)),
         "network.protocol.udp.enabled" => Ok(Box::new(true)),
         "network.protocol.udp.socket_pool_size" => Ok(Box::new(16u32)),
-        "network.protocol.udp.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.protocol.udp.listen_address" => Ok(Box::new("".to_owned())),
         "network.protocol.udp.public_address" => Ok(Box::new(Option::<String>::None)),
         "network.protocol.tcp.connect" => Ok(Box::new(true)),
         "network.protocol.tcp.listen" => Ok(Box::new(true)),
         "network.protocol.tcp.max_connections" => Ok(Box::new(32u32)),
-        "network.protocol.tcp.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.protocol.tcp.listen_address" => Ok(Box::new("".to_owned())),
         "network.protocol.tcp.public_address" => Ok(Box::new(Option::<String>::None)),
         "network.protocol.ws.connect" => Ok(Box::new(false)),
         "network.protocol.ws.listen" => Ok(Box::new(false)),
         "network.protocol.ws.max_connections" => Ok(Box::new(16u32)),
-        "network.protocol.ws.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.protocol.ws.listen_address" => Ok(Box::new("".to_owned())),
         "network.protocol.ws.path" => Ok(Box::new(String::from("ws"))),
         "network.protocol.ws.url" => Ok(Box::new(Option::<String>::None)),
         "network.protocol.wss.connect" => Ok(Box::new(false)),
         "network.protocol.wss.listen" => Ok(Box::new(false)),
         "network.protocol.wss.max_connections" => Ok(Box::new(16u32)),
-        "network.protocol.wss.listen_address" => Ok(Box::new(String::from("[::1]:5150"))),
+        "network.protocol.wss.listen_address" => Ok(Box::new("".to_owned())),
         "network.protocol.wss.path" => Ok(Box::new(String::from("ws"))),
         "network.protocol.wss.url" => Ok(Box::new(Option::<String>::None)),
         "network.leases.max_server_signal_leases" => Ok(Box::new(256u32)),
@@ -326,32 +326,32 @@ pub async fn test_config() {
     assert_eq!(inner.network.tls.connection_initial_timeout_ms, 2_000u32);
 
     assert_eq!(inner.network.application.https.enabled, false);
-    assert_eq!(inner.network.application.https.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.application.https.listen_address, "");
     assert_eq!(inner.network.application.https.path, "app");
     assert_eq!(inner.network.application.https.url, None);
     assert_eq!(inner.network.application.http.enabled, false);
-    assert_eq!(inner.network.application.http.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.application.http.listen_address, "");
     assert_eq!(inner.network.application.http.path, "app");
     assert_eq!(inner.network.application.http.url, None);
     assert_eq!(inner.network.protocol.udp.enabled, true);
     assert_eq!(inner.network.protocol.udp.socket_pool_size, 16u32);
-    assert_eq!(inner.network.protocol.udp.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.protocol.udp.listen_address, "");
     assert_eq!(inner.network.protocol.udp.public_address, None);
     assert_eq!(inner.network.protocol.tcp.connect, true);
     assert_eq!(inner.network.protocol.tcp.listen, true);
     assert_eq!(inner.network.protocol.tcp.max_connections, 32u32);
-    assert_eq!(inner.network.protocol.tcp.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.protocol.tcp.listen_address, "");
     assert_eq!(inner.network.protocol.tcp.public_address, None);
     assert_eq!(inner.network.protocol.ws.connect, false);
     assert_eq!(inner.network.protocol.ws.listen, false);
     assert_eq!(inner.network.protocol.ws.max_connections, 16u32);
-    assert_eq!(inner.network.protocol.ws.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.protocol.ws.listen_address, "");
     assert_eq!(inner.network.protocol.ws.path, "ws");
     assert_eq!(inner.network.protocol.ws.url, None);
     assert_eq!(inner.network.protocol.wss.connect, false);
     assert_eq!(inner.network.protocol.wss.listen, false);
     assert_eq!(inner.network.protocol.wss.max_connections, 16u32);
-    assert_eq!(inner.network.protocol.wss.listen_address, "[::1]:5150");
+    assert_eq!(inner.network.protocol.wss.listen_address, "");
     assert_eq!(inner.network.protocol.wss.path, "ws");
     assert_eq!(inner.network.protocol.wss.url, None);
 }