From fed75a724a284b8bacc2aba31fbcdebf8fcca7b0 Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Thu, 28 Sep 2023 16:44:42 +0300 Subject: [PATCH 1/6] chore: adding NetConfig test suite --- apps/wakunode2/external_config.nim | 15 ++ tests/all_tests_waku.nim | 1 + tests/test_waku_netconfig.nim | 297 +++++++++++++++++++++++++++++ tests/testlib/wakunode.nim | 2 +- tests/wakunode2/test_app.nim | 12 -- waku/node/config.nim | 8 +- 6 files changed, 318 insertions(+), 17 deletions(-) create mode 100644 tests/test_waku_netconfig.nim diff --git a/apps/wakunode2/external_config.nim b/apps/wakunode2/external_config.nim index 3c604dafe1..a63805811d 100644 --- a/apps/wakunode2/external_config.nim +++ b/apps/wakunode2/external_config.nim @@ -552,4 +552,19 @@ proc load*(T: type WakuNodeConf, version=""): ConfResult[T] = except CatchableError: err(getCurrentExceptionMsg()) +proc defaultTestWakuNodeConf*(): WakuNodeConf = + WakuNodeConf( + tcpPort: Port(60000), + websocketPort: Port(8000), + listenAddress: ValidIpAddress.init("0.0.0.0"), + rpcAddress: ValidIpAddress.init("127.0.0.1"), + restAddress: ValidIpAddress.init("127.0.0.1"), + metricsServerAddress: ValidIpAddress.init("127.0.0.1"), + dnsAddrsNameServers: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")], + nat: "any", + maxConnections: 50, + topics: @["/waku/2/default-waku/proto"], + relay: true + ) + {.pop.} diff --git a/tests/all_tests_waku.nim b/tests/all_tests_waku.nim index f5846a0c56..2922817700 100644 --- a/tests/all_tests_waku.nim +++ b/tests/all_tests_waku.nim @@ -72,6 +72,7 @@ import ./test_peer_exchange, ./test_waku_noise, ./test_waku_noise_sessions, + ./test_waku_netconfig, ./test_waku_switch, ./test_waku_rendezvous diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim new file mode 100644 index 0000000000..1f7ca659c4 --- /dev/null +++ b/tests/test_waku_netconfig.nim @@ -0,0 +1,297 @@ +{.used.} + +import + chronos, + libp2p/multiaddress, + testutils/unittests + +import + ../../waku/node/config, + ../../waku/waku_enr/capabilities, + ../../apps/wakunode2/external_config + + +proc defaultTestWakuFlags(): CapabilitiesBitfield = + CapabilitiesBitfield.init( + lightpush = false, + filter = false, + store = false, + relay = true + ) + +suite "Waku NetConfig": + + asyncTest "Create NetConfig with default values": + + let conf = defaultTestWakuNodeConf() + + let wakuFlags = defaultTestWakuFlags() + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extIp = none(ValidIpAddress), + extPort = none(Port), + extMultiAddrs = @[], + wsBindPort = conf.websocketPort, + wsEnabled = conf.websocketSupport, + wssEnabled = conf.websocketSecureSupport, + dns4DomainName = none(string), + discv5UdpPort = none(Port), + wakuFlags = some(wakuFlags) + ) + + check: + netConfigRes.isOk() + +asyncTest "AnnouncedAddresses contains only bind address when no external addresses are provided": + + let conf = defaultTestWakuNodeConf() + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 1 # Only bind address should be present + netConfig.announcedAddresses[0] == formatListenAddress(ip4TcpEndPoint(conf.listenAddress, conf.tcpPort)) + + +asyncTest "AnnouncedAddresses contains external address if extIp/Port are provided": + + let + conf = defaultTestWakuNodeConf() + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extIp = some(extIp), + extPort = some(extPort) + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 1 # Only external address should be present + netConfig.announcedAddresses[0] == ip4TcpEndPoint(extIp, extPort) + +asyncTest "AnnouncedAddresses contains dns4DomainName if provided": + + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort) + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 1 # Only DNS address should be present + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + +asyncTest "AnnouncedAddresses includes extMultiAddrs when provided": + + let + conf = defaultTestWakuNodeConf() + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + extMultiAddrs = @[ip4TcpEndPoint(extIp, extPort)] + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extMultiAddrs = extMultiAddrs + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + extAddress + netConfig.announcedAddresses[1] == extMultiAddrs[0] + + +asyncTest "AnnouncedAddresses includes WebSocket addresses when enabled": + + var + conf = defaultTestWakuNodeConf() + wssEnabled = false + + var netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + wsEnabled = true, + wssEnabled = wssEnabled + ) + + require: + netConfigRes.isOk() + + var netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress + netConfig.announcedAddresses[1] == (ip4TcpEndPoint(conf.listenAddress, + conf.websocketPort) & wsFlag(wssEnabled)) + + ## Now try the same for the case of wssEnabled = true + + wssEnabled = true + + netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + wsEnabled = true, + wssEnabled = wssEnabled + ) + + require: + netConfigRes.isOk() + + netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress + netConfig.announcedAddresses[1] == (ip4TcpEndPoint(conf.listenAddress, + conf.websocketPort) & wsFlag(wssEnabled)) + +asyncTest "Announced WebSocket address contains external IP if provided": + + let + conf = defaultTestWakuNodeConf() + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + wssEnabled = false + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extIp = some(extIp), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # External address + wsHostAddress + netConfig.announcedAddresses[1] == (ip4TcpEndPoint(extIp, + conf.websocketPort) & wsFlag(wssEnabled)) + +asyncTest "Announced WebSocket address contains dns4DomainName if provided": + + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) + wssEnabled = false + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress + netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & + wsFlag(wssEnabled)) + + +asyncTest "ENR is set with bindIp/Port if no extIp/Port are provided": + + let conf = defaultTestWakuNodeConf() + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.enrIp.get() == conf.listenAddress + netConfig.enrPort.get() == conf.tcpPort + +asyncTest "ENR is set with extIp/Port if provided": + + let + conf = defaultTestWakuNodeConf() + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extIp = some(extIp), + extPort = some(extPort) + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.extIp.get() == extIp + netConfig.enrPort.get() == extPort + +asyncTest "ENR is set with dns4DomainName if provided": + + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort) + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort)) + diff --git a/tests/testlib/wakunode.nim b/tests/testlib/wakunode.nim index cf5b157bdb..1f55d6f3c5 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -13,8 +13,8 @@ import ../../../waku/node/peer_manager, ../../../waku/waku_enr, ../../../waku/waku_discv5, + ../../apps/wakunode2/external_config, ../../apps/wakunode2/internal_config, - ../wakunode2/test_app, ./common diff --git a/tests/wakunode2/test_app.nim b/tests/wakunode2/test_app.nim index d0edd27df8..542e67975f 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -15,18 +15,6 @@ import ../testlib/common, ../testlib/wakucore -proc defaultTestWakuNodeConf*(): WakuNodeConf = - WakuNodeConf( - listenAddress: ValidIpAddress.init("127.0.0.1"), - rpcAddress: ValidIpAddress.init("127.0.0.1"), - restAddress: ValidIpAddress.init("127.0.0.1"), - metricsServerAddress: ValidIpAddress.init("127.0.0.1"), - dnsAddrsNameServers: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")], - nat: "any", - maxConnections: 50, - topics: @["/waku/2/default-waku/proto"], - relay: true - ) suite "Wakunode2 - App": test "compilation version should be reported": diff --git a/waku/node/config.nim b/waku/node/config.nim index b1817904dd..4a0ad1edcc 100644 --- a/waku/node/config.nim +++ b/waku/node/config.nim @@ -36,7 +36,7 @@ type NetConfig* = object type NetConfigResult* = Result[NetConfig, string] -template ip4TcpEndPoint(address, port): MultiAddress = +template ip4TcpEndPoint*(address, port): MultiAddress = MultiAddress.init(address, tcpProtocol, port) template dns4Ma(dns4DomainName: string): MultiAddress = @@ -45,15 +45,15 @@ template dns4Ma(dns4DomainName: string): MultiAddress = template tcpPortMa(port: Port): MultiAddress = MultiAddress.init("/tcp/" & $port).tryGet() -template dns4TcpEndPoint(dns4DomainName: string, port: Port): MultiAddress = +template dns4TcpEndPoint*(dns4DomainName: string, port: Port): MultiAddress = dns4Ma(dns4DomainName) & tcpPortMa(port) -template wsFlag(wssEnabled: bool): MultiAddress = +template wsFlag*(wssEnabled: bool): MultiAddress = if wssEnabled: MultiAddress.init("/wss").tryGet() else: MultiAddress.init("/ws").tryGet() -proc formatListenAddress(inputMultiAdd: MultiAddress): MultiAddress = +proc formatListenAddress*(inputMultiAdd: MultiAddress): MultiAddress = let inputStr = $inputMultiAdd # If MultiAddress contains "0.0.0.0", replace it for "127.0.0.1" return MultiAddress.init(inputStr.replace("0.0.0.0", "127.0.0.1")).get() From 0b78fa40723a37ebd7b7eb5e2c07872589c4c52d Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Thu, 28 Sep 2023 17:55:17 +0300 Subject: [PATCH 2/6] Adding test cases for (ws) + dns + extIp --- tests/test_waku_netconfig.nim | 56 ++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 1f7ca659c4..7f9468e377 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -133,6 +133,31 @@ asyncTest "AnnouncedAddresses includes extMultiAddrs when provided": netConfig.announcedAddresses[1] == extMultiAddrs[0] +asyncTest "AnnouncedAddresses uses dns4DomainName over extIp when both are provided": + + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extIp = some(extIp), + extPort = some(extPort) + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 1 # DNS address + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + asyncTest "AnnouncedAddresses includes WebSocket addresses when enabled": var @@ -230,7 +255,36 @@ asyncTest "Announced WebSocket address contains dns4DomainName if provided": netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & wsFlag(wssEnabled)) - + +asyncTest "Announced WebSocket address contains dns4DomainName if provided alongside extIp": + + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + wssEnabled = false + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extIp = some(extIp), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) + + require: + netConfigRes.isOk() + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # DNS address + wsHostAddress + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & + wsFlag(wssEnabled)) asyncTest "ENR is set with bindIp/Port if no extIp/Port are provided": From ccdaed8cd4cb0d77b922f249e1a9410696930ce1 Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Fri, 29 Sep 2023 09:57:05 +0300 Subject: [PATCH 3/6] Fixing indentation --- tests/test_waku_netconfig.nim | 248 +++++++++++++++++----------------- 1 file changed, 124 insertions(+), 124 deletions(-) diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 7f9468e377..5501726324 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -44,7 +44,7 @@ suite "Waku NetConfig": check: netConfigRes.isOk() -asyncTest "AnnouncedAddresses contains only bind address when no external addresses are provided": + asyncTest "AnnouncedAddresses contains only bind address when no external addresses are provided": let conf = defaultTestWakuNodeConf() @@ -63,7 +63,7 @@ asyncTest "AnnouncedAddresses contains only bind address when no external addres netConfig.announcedAddresses[0] == formatListenAddress(ip4TcpEndPoint(conf.listenAddress, conf.tcpPort)) -asyncTest "AnnouncedAddresses contains external address if extIp/Port are provided": + asyncTest "AnnouncedAddresses contains external address if extIp/Port are provided": let conf = defaultTestWakuNodeConf() @@ -86,30 +86,30 @@ asyncTest "AnnouncedAddresses contains external address if extIp/Port are provid netConfig.announcedAddresses.len == 1 # Only external address should be present netConfig.announcedAddresses[0] == ip4TcpEndPoint(extIp, extPort) -asyncTest "AnnouncedAddresses contains dns4DomainName if provided": + asyncTest "AnnouncedAddresses contains dns4DomainName if provided": - let - conf = defaultTestWakuNodeConf() - dns4DomainName = "example.com" - extPort = Port(1234) + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - dns4DomainName = some(dns4DomainName), - extPort = some(extPort) - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort) + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.announcedAddresses.len == 1 # Only DNS address should be present - netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + check: + netConfig.announcedAddresses.len == 1 # Only DNS address should be present + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) -asyncTest "AnnouncedAddresses includes extMultiAddrs when provided": + asyncTest "AnnouncedAddresses includes extMultiAddrs when provided": let conf = defaultTestWakuNodeConf() @@ -133,32 +133,32 @@ asyncTest "AnnouncedAddresses includes extMultiAddrs when provided": netConfig.announcedAddresses[1] == extMultiAddrs[0] -asyncTest "AnnouncedAddresses uses dns4DomainName over extIp when both are provided": + asyncTest "AnnouncedAddresses uses dns4DomainName over extIp when both are provided": - let - conf = defaultTestWakuNodeConf() - dns4DomainName = "example.com" - extIp = ValidIpAddress.init("1.2.3.4") - extPort = Port(1234) + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - dns4DomainName = some(dns4DomainName), - extIp = some(extIp), - extPort = some(extPort) - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extIp = some(extIp), + extPort = some(extPort) + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.announcedAddresses.len == 1 # DNS address - netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + check: + netConfig.announcedAddresses.len == 1 # DNS address + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) -asyncTest "AnnouncedAddresses includes WebSocket addresses when enabled": + asyncTest "AnnouncedAddresses includes WebSocket addresses when enabled": var conf = defaultTestWakuNodeConf() @@ -202,91 +202,91 @@ asyncTest "AnnouncedAddresses includes WebSocket addresses when enabled": netConfig.announcedAddresses[1] == (ip4TcpEndPoint(conf.listenAddress, conf.websocketPort) & wsFlag(wssEnabled)) -asyncTest "Announced WebSocket address contains external IP if provided": + asyncTest "Announced WebSocket address contains external IP if provided": - let - conf = defaultTestWakuNodeConf() - extIp = ValidIpAddress.init("1.2.3.4") - extPort = Port(1234) - wssEnabled = false + let + conf = defaultTestWakuNodeConf() + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + wssEnabled = false - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - extIp = some(extIp), - extPort = some(extPort), - wsEnabled = true, - wssEnabled = wssEnabled - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extIp = some(extIp), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.announcedAddresses.len == 2 # External address + wsHostAddress - netConfig.announcedAddresses[1] == (ip4TcpEndPoint(extIp, - conf.websocketPort) & wsFlag(wssEnabled)) + check: + netConfig.announcedAddresses.len == 2 # External address + wsHostAddress + netConfig.announcedAddresses[1] == (ip4TcpEndPoint(extIp, + conf.websocketPort) & wsFlag(wssEnabled)) -asyncTest "Announced WebSocket address contains dns4DomainName if provided": + asyncTest "Announced WebSocket address contains dns4DomainName if provided": - let - conf = defaultTestWakuNodeConf() - dns4DomainName = "example.com" - extPort = Port(1234) - wssEnabled = false + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) + wssEnabled = false - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - dns4DomainName = some(dns4DomainName), - extPort = some(extPort), - wsEnabled = true, - wssEnabled = wssEnabled - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress - netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & - wsFlag(wssEnabled)) + check: + netConfig.announcedAddresses.len == 2 # Bind address + wsHostAddress + netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & + wsFlag(wssEnabled)) -asyncTest "Announced WebSocket address contains dns4DomainName if provided alongside extIp": + asyncTest "Announced WebSocket address contains dns4DomainName if provided alongside extIp": - let - conf = defaultTestWakuNodeConf() - dns4DomainName = "example.com" - extIp = ValidIpAddress.init("1.2.3.4") - extPort = Port(1234) - wssEnabled = false + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extIp = ValidIpAddress.init("1.2.3.4") + extPort = Port(1234) + wssEnabled = false - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - dns4DomainName = some(dns4DomainName), - extIp = some(extIp), - extPort = some(extPort), - wsEnabled = true, - wssEnabled = wssEnabled - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extIp = some(extIp), + extPort = some(extPort), + wsEnabled = true, + wssEnabled = wssEnabled + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.announcedAddresses.len == 2 # DNS address + wsHostAddress - netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) - netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & - wsFlag(wssEnabled)) + check: + netConfig.announcedAddresses.len == 2 # DNS address + wsHostAddress + netConfig.announcedAddresses[0] == dns4TcpEndPoint(dns4DomainName, extPort) + netConfig.announcedAddresses[1] == (dns4TcpEndPoint(dns4DomainName, conf.websocketPort) & + wsFlag(wssEnabled)) -asyncTest "ENR is set with bindIp/Port if no extIp/Port are provided": + asyncTest "ENR is set with bindIp/Port if no extIp/Port are provided": let conf = defaultTestWakuNodeConf() @@ -304,7 +304,7 @@ asyncTest "ENR is set with bindIp/Port if no extIp/Port are provided": netConfig.enrIp.get() == conf.listenAddress netConfig.enrPort.get() == conf.tcpPort -asyncTest "ENR is set with extIp/Port if provided": + asyncTest "ENR is set with extIp/Port if provided": let conf = defaultTestWakuNodeConf() @@ -327,25 +327,25 @@ asyncTest "ENR is set with extIp/Port if provided": netConfig.extIp.get() == extIp netConfig.enrPort.get() == extPort -asyncTest "ENR is set with dns4DomainName if provided": + asyncTest "ENR is set with dns4DomainName if provided": - let - conf = defaultTestWakuNodeConf() - dns4DomainName = "example.com" - extPort = Port(1234) + let + conf = defaultTestWakuNodeConf() + dns4DomainName = "example.com" + extPort = Port(1234) - let netConfigRes = NetConfig.init( - bindIp = conf.listenAddress, - bindPort = conf.tcpPort, - dns4DomainName = some(dns4DomainName), - extPort = some(extPort) - ) + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + dns4DomainName = some(dns4DomainName), + extPort = some(extPort) + ) - require: - netConfigRes.isOk() + require: + netConfigRes.isOk() - let netConfig = netConfigRes.get() + let netConfig = netConfigRes.get() - check: - netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort)) + check: + netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort)) From a0e6f5bfba31cb2a7c51f400ffa58dc9c2f8a944 Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Fri, 29 Sep 2023 14:29:26 +0300 Subject: [PATCH 4/6] Moving defaultTestWakuNodeConfg to testlib --- apps/wakunode2/external_config.nim | 15 --------------- tests/test_waku_netconfig.nim | 5 +++-- tests/testlib/wakunode.nim | 16 ++++++++++++++++ tests/wakunode2/test_app.nim | 6 +++--- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/apps/wakunode2/external_config.nim b/apps/wakunode2/external_config.nim index a63805811d..3c604dafe1 100644 --- a/apps/wakunode2/external_config.nim +++ b/apps/wakunode2/external_config.nim @@ -552,19 +552,4 @@ proc load*(T: type WakuNodeConf, version=""): ConfResult[T] = except CatchableError: err(getCurrentExceptionMsg()) -proc defaultTestWakuNodeConf*(): WakuNodeConf = - WakuNodeConf( - tcpPort: Port(60000), - websocketPort: Port(8000), - listenAddress: ValidIpAddress.init("0.0.0.0"), - rpcAddress: ValidIpAddress.init("127.0.0.1"), - restAddress: ValidIpAddress.init("127.0.0.1"), - metricsServerAddress: ValidIpAddress.init("127.0.0.1"), - dnsAddrsNameServers: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")], - nat: "any", - maxConnections: 50, - topics: @["/waku/2/default-waku/proto"], - relay: true - ) - {.pop.} diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 5501726324..19cd947e6a 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -2,13 +2,14 @@ import chronos, + confutils/toml/std/net, libp2p/multiaddress, testutils/unittests import + ./testlib/wakunode, ../../waku/node/config, - ../../waku/waku_enr/capabilities, - ../../apps/wakunode2/external_config + ../../waku/waku_enr/capabilities proc defaultTestWakuFlags(): CapabilitiesBitfield = diff --git a/tests/testlib/wakunode.nim b/tests/testlib/wakunode.nim index 1f55d6f3c5..6c9f3d0584 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -20,6 +20,22 @@ import # Waku node +proc defaultTestWakuNodeConf*(): WakuNodeConf = + WakuNodeConf( + tcpPort: Port(60000), + websocketPort: Port(8000), + listenAddress: ValidIpAddress.init("0.0.0.0"), + rpcAddress: ValidIpAddress.init("127.0.0.1"), + restAddress: ValidIpAddress.init("127.0.0.1"), + metricsServerAddress: ValidIpAddress.init("127.0.0.1"), + dnsAddrsNameServers: @[ValidIpAddress.init("1.1.1.1"), ValidIpAddress.init("1.0.0.1")], + nat: "any", + maxConnections: 50, + topics: @["/waku/2/default-waku/proto"], + relay: true + ) + + proc newTestWakuNode*(nodeKey: crypto.PrivateKey, bindIp: ValidIpAddress, bindPort: Port, diff --git a/tests/wakunode2/test_app.nim b/tests/wakunode2/test_app.nim index 542e67975f..62a3913c27 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -10,10 +10,10 @@ import libp2p/multiaddress, libp2p/switch import - ../../apps/wakunode2/external_config, - ../../apps/wakunode2/app, ../testlib/common, - ../testlib/wakucore + ../testlib/wakucore, + ../testlib/wakunode, + ../../apps/wakunode2/app suite "Wakunode2 - App": From e95b9edccd545235346b62561f04388d38a3664c Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Fri, 29 Sep 2023 14:56:04 +0300 Subject: [PATCH 5/6] Improving tests, including instead of importing private functions --- tests/test_waku_netconfig.nim | 46 +++++++++++++---------------------- waku/node/config.nim | 8 +++--- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 19cd947e6a..5ecbd258f8 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -7,10 +7,11 @@ import testutils/unittests import - ./testlib/wakunode, - ../../waku/node/config, - ../../waku/waku_enr/capabilities + ./testlib/wakunode, + ../../waku/waku_enr/capabilities +include + ../../waku/node/config proc defaultTestWakuFlags(): CapabilitiesBitfield = CapabilitiesBitfield.init( @@ -54,8 +55,7 @@ suite "Waku NetConfig": bindPort = conf.tcpPort ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -78,8 +78,7 @@ suite "Waku NetConfig": extPort = some(extPort) ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -101,8 +100,7 @@ suite "Waku NetConfig": extPort = some(extPort) ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -124,8 +122,7 @@ suite "Waku NetConfig": extMultiAddrs = extMultiAddrs ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -150,8 +147,7 @@ suite "Waku NetConfig": extPort = some(extPort) ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -172,8 +168,7 @@ suite "Waku NetConfig": wssEnabled = wssEnabled ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error var netConfig = netConfigRes.get() @@ -193,8 +188,7 @@ suite "Waku NetConfig": wssEnabled = wssEnabled ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error netConfig = netConfigRes.get() @@ -220,8 +214,7 @@ suite "Waku NetConfig": wssEnabled = wssEnabled ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -247,8 +240,7 @@ suite "Waku NetConfig": wssEnabled = wssEnabled ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -276,8 +268,7 @@ suite "Waku NetConfig": wssEnabled = wssEnabled ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -296,8 +287,7 @@ suite "Waku NetConfig": bindPort = conf.tcpPort ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -319,8 +309,7 @@ suite "Waku NetConfig": extPort = some(extPort) ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() @@ -342,8 +331,7 @@ suite "Waku NetConfig": extPort = some(extPort) ) - require: - netConfigRes.isOk() + assert netConfigRes.isOk(), $netConfigRes.error let netConfig = netConfigRes.get() diff --git a/waku/node/config.nim b/waku/node/config.nim index 4a0ad1edcc..b1817904dd 100644 --- a/waku/node/config.nim +++ b/waku/node/config.nim @@ -36,7 +36,7 @@ type NetConfig* = object type NetConfigResult* = Result[NetConfig, string] -template ip4TcpEndPoint*(address, port): MultiAddress = +template ip4TcpEndPoint(address, port): MultiAddress = MultiAddress.init(address, tcpProtocol, port) template dns4Ma(dns4DomainName: string): MultiAddress = @@ -45,15 +45,15 @@ template dns4Ma(dns4DomainName: string): MultiAddress = template tcpPortMa(port: Port): MultiAddress = MultiAddress.init("/tcp/" & $port).tryGet() -template dns4TcpEndPoint*(dns4DomainName: string, port: Port): MultiAddress = +template dns4TcpEndPoint(dns4DomainName: string, port: Port): MultiAddress = dns4Ma(dns4DomainName) & tcpPortMa(port) -template wsFlag*(wssEnabled: bool): MultiAddress = +template wsFlag(wssEnabled: bool): MultiAddress = if wssEnabled: MultiAddress.init("/wss").tryGet() else: MultiAddress.init("/ws").tryGet() -proc formatListenAddress*(inputMultiAdd: MultiAddress): MultiAddress = +proc formatListenAddress(inputMultiAdd: MultiAddress): MultiAddress = let inputStr = $inputMultiAdd # If MultiAddress contains "0.0.0.0", replace it for "127.0.0.1" return MultiAddress.init(inputStr.replace("0.0.0.0", "127.0.0.1")).get() From 5af720ef5ad4b89eb884c3fbcc04b21940d9a386 Mon Sep 17 00:00:00 2001 From: Gabriel Mermelstein Date: Fri, 29 Sep 2023 15:06:23 +0300 Subject: [PATCH 6/6] Deleting extra lines --- tests/testlib/wakunode.nim | 1 - tests/wakunode2/test_app.nim | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/testlib/wakunode.nim b/tests/testlib/wakunode.nim index 6c9f3d0584..5920d3fcad 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -35,7 +35,6 @@ proc defaultTestWakuNodeConf*(): WakuNodeConf = relay: true ) - proc newTestWakuNode*(nodeKey: crypto.PrivateKey, bindIp: ValidIpAddress, bindPort: Port, diff --git a/tests/wakunode2/test_app.nim b/tests/wakunode2/test_app.nim index 62a3913c27..fe2c735c18 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -15,7 +15,6 @@ import ../testlib/wakunode, ../../apps/wakunode2/app - suite "Wakunode2 - App": test "compilation version should be reported": ## Given