nim-libplum/tests/test_plum.nim
2026-05-14 16:40:10 +04:00

82 lines
2.0 KiB
Nim

# Copyright (c) 2026 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import std/envvars
import unittest2
import chronos
import libplum/plum
suite "plum":
test "init and cleanup":
let r = init()
check r.isOk()
let c = cleanup()
check c.isOk()
test "double cleanup returns error":
discard init()
discard cleanup()
let c = cleanup()
check c.isErr()
test "getLocalAddress after init":
discard init()
let r = getLocalAddress()
check r.isOk()
check r.value.len > 0
discard cleanup()
test "hasMapping returns false for unknown id":
check not hasMapping(999)
test "createMapping fails without router":
# In CI with no NAT device, expect Failure or timeout — both return err.
if getEnv("NAT_TEST_PLUM") == "1":
skip()
return
discard init()
let r = waitFor createMapping(UDP, 12345, timeout = seconds(5))
check r.isErr()
discard cleanup()
suite "plum - NAT port mapping (requires NAT_TEST_PLUM=1)":
test "createMapping TCP and destroyMapping":
if getEnv("NAT_TEST_PLUM") != "1":
skip()
return
check init(discoverTimeout = 15000).isOk()
let r = waitFor createMapping(TCP, 8101, timeout = seconds(40))
check r.isOk()
if r.isOk():
let res = r.value
check res.mapping.externalPort > 0
check res.mapping.externalHost.len > 0
check hasMapping(res.id)
destroyMapping(res.id)
discard cleanup()
test "createMapping UDP":
if getEnv("NAT_TEST_PLUM") != "1":
skip()
return
check init(discoverTimeout = 2000).isOk()
let r = waitFor createMapping(UDP, 8090, timeout = seconds(40))
check r.isOk()
if r.isOk():
let res = r.value
check res.mapping.externalPort > 0
destroyMapping(res.id)
discard cleanup()