hare

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

+test.ha (3612B)


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