From baa8d7a819742f89f4e289e868a85dad8e28d49b Mon Sep 17 00:00:00 2001 From: Arnaud Date: Mon, 1 Jun 2026 17:22:10 +0400 Subject: [PATCH] Add more tests --- libplum/plum.nim | 6 ++++++ tests/test_plum.nim | 38 +++++++++++++++++++++++++++++++++++--- vendor/libplum | 2 +- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/libplum/plum.nim b/libplum/plum.nim index 5defa9c..71e154f 100644 --- a/libplum/plum.nim +++ b/libplum/plum.nim @@ -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) diff --git a/tests/test_plum.nim b/tests/test_plum.nim index e8dfc12..7e7d2ae 100644 --- a/tests/test_plum.nim +++ b/tests/test_plum.nim @@ -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 diff --git a/vendor/libplum b/vendor/libplum index b74757f..b5d5f7d 160000 --- a/vendor/libplum +++ b/vendor/libplum @@ -1 +1 @@ -Subproject commit b74757f88c2a98ea905f5b81ad040232bc019c35 +Subproject commit b5d5f7d31b319b21136ec861d59d95f02eaf207a