Add more tests

This commit is contained in:
Arnaud 2026-06-01 17:22:10 +04:00
parent 7f84c28d91
commit baa8d7a819
No known key found for this signature in database
GPG Key ID: A6C7C781817146FA
3 changed files with 42 additions and 4 deletions

View File

@ -287,6 +287,12 @@ proc hasMapping*(id: cint): bool {.raises: [].} =
else:
false
proc activeMappingCount*(): int {.raises: [].} =
## Number of mappings the wrapper still tracks. Drops to 0 once every
## mapping has fired DESTROYED. Mainly useful to detect handle leaks.
withSafeLock:
result = activeMappings.len
proc getLocalAddress*(): Result[string, string] {.raises: [].} =
var buf = newString(PLUM_MAX_ADDRESS_LEN)
let res = plum_get_local_address(buf.cstring, buf.len.csize_t)

View File

@ -16,6 +16,8 @@ import chronos
import libplum/plum
import libplum/libplum
const miniupnp_protocol {.strdefine.} = ""
suite "plum":
test "init and cleanup":
let r = init()
@ -39,7 +41,24 @@ suite "plum":
test "hasMapping returns false for unknown id":
check not hasMapping(999)
const miniupnp_protocol {.strdefine.} = ""
# Only valid where no NAT device answers; the integration container runs
# miniupnpd, which would make the mapping succeed before the timeout.
when miniupnp_protocol == "":
test "createMapping times out without a NAT device":
require init().isOk()
defer:
discard cleanup()
let r = waitFor createMapping(TCP, 8101, timeout = milliseconds(50))
check r.isErr()
test "destroyMapping is a no-op on an unknown id":
require init().isOk()
defer:
discard cleanup()
destroyMapping(999.cint)
check not hasMapping(999)
# The flag is passed by the Docker / Podman container.
when miniupnp_protocol != "":
@ -96,8 +115,6 @@ when miniupnp_protocol != "":
let r = waitFor createMapping(TCP, 8101, timeout = mappingTimeout)
require r.isOk()
let res = r.value
defer:
destroyMapping(res.id)
checkpoint miniupnp_protocol & " TCP: " & res.mapping.externalHost & ":" &
$res.mapping.externalPort
@ -112,6 +129,11 @@ when miniupnp_protocol != "":
else:
check res.mapping.mappingProtocol == PCP
destroyMapping(res.id)
check not hasMapping(res.id)
# second destroy on a real id must be a safe no-op
destroyMapping(res.id)
test "createMapping UDP and destroying":
require init(discoverTimeout = discoverMs, logLevel = logLevel).isOk()
defer:
@ -165,3 +187,13 @@ when miniupnp_protocol != "":
startMiniupnpd(miniupnp_protocol)
check waitRenewal()
test "cleanup releases active mappings":
require init(discoverTimeout = discoverMs, logLevel = logLevel).isOk()
let r = waitFor createMapping(TCP, 8401, timeout = mappingTimeout)
require r.isOk()
# no destroyMapping on purpose: cleanup must release everything
check cleanup().isOk()
check activeMappingCount() == 0

2
vendor/libplum vendored

@ -1 +1 @@
Subproject commit b74757f88c2a98ea905f5b81ad040232bc019c35
Subproject commit b5d5f7d31b319b21136ec861d59d95f02eaf207a