aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_ip_spec.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2023-03-14 18:40:16 +0000
committerMatthew Wild <mwild1@gmail.com>2023-03-14 18:40:16 +0000
commit55106e91cebe1be62436cdf9c6981d16a747f54f (patch)
tree625d66af674c8a6194aa19c32b665c7ec077ae44 /spec/util_ip_spec.lua
parent5afb393d532dca85f66d34d238e37b437fc303c7 (diff)
downloadprosody-55106e91cebe1be62436cdf9c6981d16a747f54f.tar.gz
prosody-55106e91cebe1be62436cdf9c6981d16a747f54f.zip
util.ip: Tests for truncate()
Diffstat (limited to 'spec/util_ip_spec.lua')
-rw-r--r--spec/util_ip_spec.lua22
1 files changed, 22 insertions, 0 deletions
diff --git a/spec/util_ip_spec.lua b/spec/util_ip_spec.lua
index be5e4cff..2725ba3a 100644
--- a/spec/util_ip_spec.lua
+++ b/spec/util_ip_spec.lua
@@ -100,4 +100,26 @@ describe("util.ip", function()
assert_cpl6("abcd::abcd", "abcd::abcd:abcd", 96);
end);
end);
+
+ describe("#truncate()", function ()
+ it("should work for IPv4", function ()
+ local ip1 = ip.new_ip("192.168.0.1");
+ local ip2 = ip.truncate(ip1, 16);
+ assert.truthy(ip.is_ip(ip2));
+ assert.equal("192.168.0.0", ip2.normal);
+ assert.equal("192.168.0.1", ip1.normal); -- original unmodified
+ end);
+
+ it("should work for IPv6", function ()
+ local ip1 = ip.new_ip("2001:db8::ff00:42:8329");
+ local ip2 = ip.truncate(ip1, 24);
+ assert.truthy(ip.is_ip(ip2));
+ assert.equal("2001:d00::", ip2.normal);
+ assert.equal("2001:db8::ff00:42:8329", ip1.normal); -- original unmodified
+ end);
+
+ it("accepts a string", function ()
+ assert.equal("127.0.0.0", ip.truncate("127.0.0.1", 8).normal);
+ end);
+ end);
end);