Files
nix-ip-utils/tests/ip-tests.nix
2026-02-20 20:07:52 +00:00

206 lines
3.3 KiB
Nix

{ lib, ip }:
let
inherit (lib) runTests;
in
runTests {
testParseBasic = {
expr = ip.parse "192.168.1.1";
expected = 3232235777;
};
testParseZeros = {
expr = ip.parse "0.0.0.0";
expected = 0;
};
testParseMax = {
expr = ip.parse "255.255.255.255";
expected = 4294967295;
};
testParseLoopback = {
expr = ip.parse "127.0.0.1";
expected = 2130706433;
};
testParseTen = {
expr = ip.parse "10.0.0.1";
expected = 167772161;
};
testParsePassthroughInt = {
expr = ip.parse 3232235777;
expected = 3232235777;
};
testFormatBasic = {
expr = ip.format 3232235777;
expected = "192.168.1.1";
};
testFormatZeros = {
expr = ip.format 0;
expected = "0.0.0.0";
};
testFormatMax = {
expr = ip.format 4294967295;
expected = "255.255.255.255";
};
testFormatPassthroughString = {
expr = ip.format "192.168.1.1";
expected = "192.168.1.1";
};
testToOctetsBasic = {
expr = ip.toOctets "192.168.1.1";
expected = [
192
168
1
1
];
};
testToOctetsFromInt = {
expr = ip.toOctets 3232235777;
expected = [
192
168
1
1
];
};
testToOctetsZeros = {
expr = ip.toOctets "0.0.0.0";
expected = [
0
0
0
0
];
};
testFromOctetsBasic = {
expr = ip.fromOctets [
192
168
1
1
];
expected = 3232235777;
};
testFromOctetsZeros = {
expr = ip.fromOctets [
0
0
0
0
];
expected = 0;
};
testFromOctetsTen = {
expr = ip.fromOctets [
10
0
0
1
];
expected = 167772161;
};
testAddBasic = {
expr = ip.add "192.168.1.1" 10;
expected = 3232235787;
};
testAddStrBasic = {
expr = ip.addStr "192.168.1.1" 10;
expected = "192.168.1.11";
};
testAddCrossOctet = {
expr = ip.addStr "192.168.1.250" 10;
expected = "192.168.2.4";
};
testSubtractBasic = {
expr = ip.subtract "192.168.1.10" 5;
expected = 3232235781;
};
testSubtractStrBasic = {
expr = ip.subtractStr "192.168.1.10" 5;
expected = "192.168.1.5";
};
testDiffBasic = {
expr = ip.diff "192.168.1.10" "192.168.1.1";
expected = 9;
};
testDiffNegative = {
expr = ip.diff "192.168.1.1" "192.168.1.10";
expected = -9;
};
testCompareLess = {
expr = ip.compare "10.0.0.1" "10.0.0.2";
expected = -1;
};
testCompareGreater = {
expr = ip.compare "10.0.0.2" "10.0.0.1";
expected = 1;
};
testCompareEqual = {
expr = ip.compare "10.0.0.1" "10.0.0.1";
expected = 0;
};
testLtTrue = {
expr = ip.lt "10.0.0.1" "10.0.0.2";
expected = true;
};
testLtFalse = {
expr = ip.lt "10.0.0.2" "10.0.0.1";
expected = false;
};
testGtTrue = {
expr = ip.gt "10.0.0.2" "10.0.0.1";
expected = true;
};
testEqTrue = {
expr = ip.eq "10.0.0.1" "10.0.0.1";
expected = true;
};
testEqFalse = {
expr = ip.eq "10.0.0.1" "10.0.0.2";
expected = false;
};
testMinStr = {
expr = ip.minStr "10.0.0.5" "10.0.0.2";
expected = "10.0.0.2";
};
testMaxStr = {
expr = ip.maxStr "10.0.0.5" "10.0.0.2";
expected = "10.0.0.5";
};
testRoundtrip = {
expr = ip.format (ip.parse "172.16.32.64");
expected = "172.16.32.64";
};
}