hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

test+test.ha (3299B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use fmt;
      5 use strings;
      6 
      7 fn ip_test(s: str, expected: (addr|invalid)) void = {
      8 	let pr = parse(s);
      9 	let ip = if (pr is invalid) {
     10 		assert(expected is invalid);
     11 		return;
     12 	} else {
     13 		assert(expected is addr);
     14 		assert(equal(pr as addr, expected as addr));
     15 		yield pr as addr;
     16 	};
     17 	let fmted = string(ip);
     18 	let iprp = parse(fmted);
     19 	assert(iprp is addr);
     20 	let ipr = iprp as addr;
     21 	assert(equal(ip, ipr));
     22 	if (ip is addr4) {
     23 		assert(fmted == s);
     24 	} else {
     25 		assert(strings::dup(fmted) == string(ipr));
     26 	};
     27 };
     28 
     29 @test fn parse_ip() void = {
     30 	let tests: [](str, (addr|invalid)) = [
     31 		("127.0.0.1", [127, 0, 0, 1]: addr4),
     32 		("192.168.18.1", [192, 168, 18, 1]: addr4),
     33 		("-127.0.0.1", invalid),
     34 		("127.-0.0.1", invalid),
     35 		("0.011.001.000", invalid),
     36 		("::", [0...]: addr6),
     37 		("::1", [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]: addr6),
     38 		("::FFFF:FFFF", [0,0,0,0,0,0,0,0,0,0,0,0,0xFF,0xFF,0xFF,0xFF]: addr6),
     39 		("::FFFF", [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xFF,0xFF]: addr6),
     40 		(":FFFF", invalid),
     41 		("::1:1", [0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1]: addr6),
     42 		("1::1", [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1]: addr6),
     43 		(":::1:1", invalid),
     44 		(":::", invalid),
     45 		("::1::1", invalid),
     46 		("1::::1", invalid),
     47 		("FFFF::FFFF::1", invalid),
     48 		("::127.0.0.1", [0,0,0,0,0,0,0,0,0,0,0,0,127,0,0,1]: addr6),
     49 		("FFFF:FFFF", invalid),
     50 		("DEAD::BEef", [0xDE, 0xAD, 0,0,0,0,0,0,0,0,0,0,0,0, 0xBE, 0xEF]: addr6),
     51 		("DEAD::BEef:A12D", [0xDE, 0xAD, 0,0,0,0,0,0,0,0,0,0, 0xBE, 0xEF, 0xA1, 0x2D]: addr6),
     52 		("DEAD:BEef::0102:A12D", [0xDE, 0xAD,0xBE,0xEF,0,0,0,0,0,0,0,0,0x01,0x02,0xA1,0x2D]: addr6),
     53 		("DEAD:BEef:::A12D", invalid),
     54 		("1980:cafe:a:babe::1", [0x19, 0x80, 0xca, 0xfe, 0x0, 0xa, 0xba, 0xbe, 0, 0, 0, 0, 0, 0, 0, 1]: addr6),
     55 		("a1:a2:a3:a4::b1:b2:b3:b4", invalid),
     56 		("", invalid),
     57 	];
     58 	for (let (string, expected) .. tests) {
     59 		ip_test(string, expected);
     60 	};
     61 };
     62 
     63 fn subnet_test_simple(s: str) void = {
     64 	let net = match (parsecidr(s)) {
     65 	case let a: subnet =>
     66 		yield a;
     67 	case =>
     68 		return;
     69 	};
     70 	let fmted = string(net);
     71 	assert(fmted == s);
     72 	let netrp = parsecidr(fmted);
     73 	assert(netrp is subnet);
     74 	let netr = netrp as subnet;
     75 	assert(equal(net.addr, netr.addr));
     76 	assert(equal(net.mask, netr.mask));
     77 };
     78 
     79 @test fn parse_subnet() void = {
     80 	let subnet_tests: []str = [
     81 		"192.168.1.0/0",
     82 		"192.168.1.0/23",
     83 		"192.168.1.0/24",
     84 		"192.168.1.0/32",
     85 	];
     86 	for (let test .. subnet_tests) {
     87 		subnet_test_simple(test);
     88 	};
     89 };
     90 
     91 @test fn test_subnet_contains() void = {
     92 	let addr_tests: [](str, str, bool) = [
     93 		// a, b, want
     94 		("10.10.10.0/24", "10.10.10.0",   true),
     95 		("10.10.10.0/24", "10.10.10.255", true),
     96 		("10.10.10.0/24", "10.10.11.0",   false),
     97 		("127.0.0.1/24",  "::1",          false),
     98 		("::1/8",         "::1",          true),
     99 	];
    100 	let cidr_tests: [](str, str, bool) = [
    101 		// a, b, want
    102 		("10.10.10.0/24", "10.10.10.0/24", true),
    103 		("10.10.10.0/24", "10.10.10.0/25", true),
    104 		("10.10.10.0/24", "10.10.10.0/23", false),
    105 		("10.10.10.0/24", "10.10.11.0/24", false),
    106 	];
    107 	for (let (a, b, want) .. addr_tests) {
    108 		let a = parsecidr(a)!;
    109 		let b = parse(b)!;
    110 		assert(subnet_contains(a, b) == want);
    111 	};
    112 	for (let (a, b, want) .. cidr_tests) {
    113 		let a = parsecidr(a)!;
    114 		let b = parsecidr(b)!;
    115 		assert(subnet_contains(a, b) == want);
    116 	};
    117 };