From 23b49ca53df788e7610d7adeb67172e75cb81282 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Fri, 29 Sep 2023 16:30:07 +0300 Subject: [PATCH] chore: adding NetConfig test suite (#2091) --- tests/all_tests_waku.nim | 1 + tests/test_waku_netconfig.nim | 340 ++++++++++++++++++++++++++++++++++ tests/testlib/wakunode.nim | 17 +- tests/wakunode2/test_app.nim | 19 +- 4 files changed, 360 insertions(+), 17 deletions(-) create mode 100644 tests/test_waku_netconfig.nim diff --git a/tests/all_tests_waku.nim b/tests/all_tests_waku.nim index f5846a0c5..292281770 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 000000000..5ecbd258f --- /dev/null +++ b/tests/test_waku_netconfig.nim @@ -0,0 +1,340 @@ +{.used.} + +import + chronos, + confutils/toml/std/net, + libp2p/multiaddress, + testutils/unittests + +import + ./testlib/wakunode, + ../../waku/waku_enr/capabilities + +include + ../../waku/node/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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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) + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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) + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + let netConfig = netConfigRes.get() + + check: + netConfig.announcedAddresses.len == 2 # Bind address + extAddress + 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) + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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 + conf = defaultTestWakuNodeConf() + wssEnabled = false + + var netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + wsEnabled = true, + wssEnabled = wssEnabled + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + let netConfig = netConfigRes.get() + + 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": + + 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 + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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": + + let conf = defaultTestWakuNodeConf() + + let netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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) + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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) + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + let netConfig = netConfigRes.get() + + check: + netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort)) + diff --git a/tests/testlib/wakunode.nim b/tests/testlib/wakunode.nim index cf5b157bd..5920d3fca 100644 --- a/tests/testlib/wakunode.nim +++ b/tests/testlib/wakunode.nim @@ -13,13 +13,28 @@ import ../../../waku/node/peer_manager, ../../../waku/waku_enr, ../../../waku/waku_discv5, + ../../apps/wakunode2/external_config, ../../apps/wakunode2/internal_config, - ../wakunode2/test_app, ./common # 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 d0edd27df..fe2c735c1 100644 --- a/tests/wakunode2/test_app.nim +++ b/tests/wakunode2/test_app.nim @@ -10,23 +10,10 @@ import libp2p/multiaddress, libp2p/switch import - ../../apps/wakunode2/external_config, - ../../apps/wakunode2/app, ../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 - ) + ../testlib/wakucore, + ../testlib/wakunode, + ../../apps/wakunode2/app suite "Wakunode2 - App": test "compilation version should be reported":