Skip to content

Commit

Permalink
chore: adding NetConfig test suite (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer authored and ABresting committed Sep 30, 2023
1 parent db55ead commit 806c7b9
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 17 deletions.
1 change: 1 addition & 0 deletions tests/all_tests_waku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import
./test_peer_exchange,
./test_waku_noise,
./test_waku_noise_sessions,
./test_waku_netconfig,
./test_waku_switch,
./test_waku_rendezvous

Expand Down
340 changes: 340 additions & 0 deletions tests/test_waku_netconfig.nim
Original file line number Diff line number Diff line change
@@ -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))

17 changes: 16 additions & 1 deletion tests/testlib/wakunode.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 806c7b9

Please sign in to comment.