This commit is contained in:
swrup 2025-11-11 02:07:51 +01:00
parent aa2ff7b2f0
commit 2f3113f55d
11742 changed files with 1223940 additions and 0 deletions

View file

@ -0,0 +1,48 @@
(rule
(copy# ../lib/ipaddr_sexp.ml ipaddr_sexp.ml))
(rule
(copy# ../lib/macaddr_sexp.ml macaddr_sexp.ml))
(rule
(copy# ../lib/ipaddr.ml ipaddr_internal.ml))
(library
(name test_macaddr_sexp)
(wrapped false)
(modules macaddr_sexp)
(preprocess
(pps ppx_sexp_conv))
(libraries macaddr sexplib0))
(library
(name test_ipaddr_sexp)
(wrapped false)
(modules ipaddr_sexp)
(preprocess
(pps ppx_sexp_conv))
(libraries ipaddr sexplib0))
(test
(name test_ipaddr)
(package ipaddr-sexp)
(modules test_ipaddr)
(libraries ipaddr ipaddr-cstruct test_ipaddr_sexp ounit2))
(test
(name test_ipaddr_b128)
(package ipaddr-sexp)
(modules test_ipaddr_b128 ipaddr_internal)
(libraries ipaddr ipaddr-cstruct test_ipaddr_sexp ounit2))
(test
(name test_macaddr)
(package macaddr-sexp)
(modules test_macaddr)
(libraries macaddr macaddr-cstruct test_macaddr_sexp ounit2))
(test
(name test_ppx)
(modules test_ppx)
(package ipaddr-sexp)
(libraries ipaddr macaddr test_ipaddr_sexp test_macaddr_sexp ounit2))

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,199 @@
(*
* Copyright (c) 2013-2014 David Sheets <sheets@alum.mit.edu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)
open OUnit
module B128 = Ipaddr_internal.S128
(* copied from test_ipaddr.ml *)
let assert_raises ~msg exn test_fn =
assert_raises ~msg exn (fun () ->
try test_fn ()
with rtexn ->
if exn <> rtexn then (
Printf.eprintf "Stacktrace for '%s':\n%!" msg;
Printexc.print_backtrace stderr);
raise rtexn)
let int_of_hex_char c =
match c with
| '0' .. '9' -> Char.code c - 48
| 'a' .. 'f' -> Char.code c - 87
| 'A' .. 'F' -> Char.code c - 55
| _ -> invalid_arg "char is not a valid hex digit"
let to_string (s : Ipaddr_internal.S128.t) =
let s : string = Obj.magic s in
List.init 16 (fun i -> Printf.sprintf "%.2x" (Char.code (String.get s i)))
|> String.concat ""
let of_string_exn s : B128.t =
if String.length s <> 32 then invalid_arg "not 32 chars long";
Bytes.init 16 (fun bi ->
let i = bi * 2 in
let x = int_of_hex_char s.[i + 1] and y = int_of_hex_char s.[i] in
char_of_int ((y lsl 4) + x))
|> Bytes.unsafe_to_string
|> Obj.magic
let assert_equal = assert_equal ~printer:to_string
let test_addition () =
(* simple addition *)
let d1 = B128.zero in
let d2 = of_string_exn "00000000000000000000000000000001" in
assert_equal ~msg:"adding one to zero is one" d2 (B128.add_exn d1 d2);
(* addition carry *)
let d1 = of_string_exn "000000000000000000ff000000000000" in
let d2 = of_string_exn "00000000000000000001000000000000" in
let d3 = of_string_exn "00000000000000000100000000000000" in
assert_equal ~msg:"test addition carry over" d3 (B128.add_exn d1 d2);
(* adding one to max_int overflows *)
let d1 = B128.max_int in
let d2 = of_string_exn "00000000000000000000000000000001" in
assert_raises ~msg:"adding one to max_int overflows" B128.Overflow (fun () ->
B128.add_exn d1 d2)
let test_pred () =
(* simple subtraction *)
let d1 = of_string_exn "00000000000000000000000000000001" in
let d2 = B128.zero in
assert_equal ~msg:"subtracting one from one is zero" d2 (B128.pred_exn d1);
(* subtract carry *)
let d1 = of_string_exn "00000000000000000000000000000300" in
let d2 = of_string_exn "000000000000000000000000000002ff" in
assert_equal ~msg:"test subtraction carry over" d2 (B128.pred_exn d1);
(* subtracting one from zero overflows *)
assert_raises ~msg:"subtracting one from min_int overflows" B128.Overflow
(fun () -> B128.pred_exn B128.zero)
let test_of_to_string () =
let s = "ff000000000000004200000000000001" in
OUnit.assert_equal ~msg:"input of of_string is equal to output of to_string" s
(of_string_exn s |> to_string)
let test_lognot () =
let d1 = of_string_exn "00000000000000000000000000000001" in
let d2 = of_string_exn "fffffffffffffffffffffffffffffffe" in
assert_equal ~msg:"lognot inverts bits" d2 (B128.lognot d1)
let test_shift_left () =
(* bit shift count, input, expected output *)
let test_shifts =
[
(1, "f0000000000000000000000000000000", "e0000000000000000000000000000000");
(1, "0000000000000000000000000000000f", "0000000000000000000000000000001e");
(1, "00000000000000000000000000000001", "00000000000000000000000000000002");
(2, "f0000000000000000000000000000000", "c0000000000000000000000000000000");
(2, "0000000000000000000000000000ffff", "0000000000000000000000000003fffc");
(8, "00000000000000000000000000000100", "00000000000000000000000000010000");
(9, "f0000000000000000000000000000000", "00000000000000000000000000000000");
( 64,
"00000000000000000000000000000001",
"00000000000000010000000000000000" );
( 127,
"00000000000000000000000000000001",
"80000000000000000000000000000000" );
( 128,
"00000000000000000000000000000001",
"00000000000000000000000000000000" );
]
in
List.iter
(fun (bits, input_value, expected_output) ->
assert_equal
~msg:(Printf.sprintf "shift left by %i" bits)
(of_string_exn expected_output)
(B128.shift_left (of_string_exn input_value) bits))
test_shifts
let test_shift_right () =
(* (bit shift count, input, expected output) *)
let test_shifts =
[
(1, "f0000000000000000000000000000000", "78000000000000000000000000000000");
(2, "f0000000000000000000000000000000", "3c000000000000000000000000000000");
(2, "0000000000000000000000000000ffff", "00000000000000000000000000003fff");
(2, "000000000000000000000000000ffff0", "0000000000000000000000000003fffc");
(8, "00000000000000000000000000000100", "00000000000000000000000000000001");
(9, "f0000000000000000000000000000000", "00780000000000000000000000000000");
( 32,
"000000000000000000000000ffffffff",
"00000000000000000000000000000000" );
( 32,
"0000000000000000aaaabbbbffffffff",
"000000000000000000000000aaaabbbb" );
( 40,
"0000000000000000aaaabbbbffffffff",
"00000000000000000000000000aaaabb" );
( 64,
"01000000000000000000000000000000",
"00000000000000000100000000000000" );
( 120,
"aaaabbbbccccdddd0000000000000000",
"000000000000000000000000000000aa" );
( 127,
"80000000000000000000000000000000",
"00000000000000000000000000000001" );
( 128,
"ffff0000000000000000000000000000",
"00000000000000000000000000000000" );
]
in
List.iter
(fun (bits, input_value, expected_output) ->
assert_equal
~msg:(Printf.sprintf "shift right by %i" bits)
(of_string_exn expected_output)
(B128.shift_right (of_string_exn input_value) bits))
test_shifts
let test_byte_module () =
let assert_equal = OUnit2.assert_equal ~printer:(Printf.sprintf "0x%x") in
assert_equal ~msg:"get 3 lsb" 0x00 (B128.Byte.get_lsbits 3 0x00);
assert_equal ~msg:"get 4 lsb" 0x0f (B128.Byte.get_lsbits 4 0xff);
assert_equal ~msg:"get 5 lsb" 0x10 (B128.Byte.get_lsbits 5 0x10);
assert_equal ~msg:"get 8 lsb" 0xff (B128.Byte.get_lsbits 8 0xff);
assert_equal ~msg:"get 3 msb" 0x0 (B128.Byte.get_msbits 3 0x00);
assert_equal ~msg:"get 4 msb" 0xf (B128.Byte.get_msbits 4 0xff);
assert_equal ~msg:"get 5 msb" 0x2 (B128.Byte.get_msbits 5 0x10);
assert_equal ~msg:"get 8 msb" 0xff (B128.Byte.get_msbits 8 0xff);
assert_equal ~msg:"set 3 msb" 0x20 (B128.Byte.set_msbits 3 0x1 0x00);
assert_equal ~msg:"set 4 msb" 0xa0 (B128.Byte.set_msbits 4 0xa 0x00);
assert_equal ~msg:"set 5 msb" 0x98 (B128.Byte.set_msbits 5 0x13 0x00);
assert_equal ~msg:"set 8 msb" 0xff (B128.Byte.set_msbits 8 0xff 0x00)
let suite =
"Test B128 module"
>::: [
"addition" >:: test_addition;
"pred" >:: test_pred;
"of_to_string" >:: test_of_to_string;
"lognot" >:: test_lognot;
"shift_left" >:: test_shift_left;
"shift_right" >:: test_shift_right;
"byte_module" >:: test_byte_module;
]
;;
let _results = run_test_tt_main suite in
()

View file

@ -0,0 +1,109 @@
(*
* Copyright (c) 2013-2014 David Sheets <sheets@alum.mit.edu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)
open OUnit
open Macaddr
let test_string_rt () =
let addrs = [ ("ca:fe:ba:be:ee:ee", ':'); ("ca-fe-ba-be-ee-ee", '-') ] in
List.iter
(fun (addr, sep) ->
let os = of_string_exn addr in
let ts = to_string ~sep os in
assert_equal ~msg:(addr ^ " <> " ^ ts) ts addr;
let os = Macaddr_sexp.(t_of_sexp (sexp_of_t os)) in
let ts = to_string ~sep os in
assert_equal ~msg:(addr ^ " <> " ^ ts) ts addr)
addrs
let assert_result_failure ~msg a =
match a with Ok _ -> assert_failure msg | Error (`Msg _) -> ()
let test_string_rt_bad () =
let addrs =
[
"ca:fe:ba:be:ee:e";
"ca:fe:ba:be:ee:eee";
"ca:fe:ba:be:eeee";
"ca:fe:ba:be:ee::ee";
"ca:fe:ba:be:e:eee";
"ca:fe:ba:be:ee-ee";
]
in
List.iter (fun addr -> assert_result_failure ~msg:addr (of_string addr)) addrs
let test_bytes_rt () =
let addr = "\254\099\003\128\000\000" in
assert_equal ~msg:(String.escaped addr) (to_octets (of_octets_exn addr)) addr
let test_bytes_rt_bad () =
let addrs = [ "\254\099\003\128\000"; "\254\099\003\128\000\000\233" ] in
List.iter
(fun addr ->
assert_result_failure ~msg:(String.escaped addr) (of_octets addr))
addrs
let test_cstruct_rt () =
let open Macaddr_cstruct in
let addr = "\254\099\003\128\000\000" in
assert_equal ~msg:(String.escaped addr)
(Cstruct.to_string (to_cstruct (of_cstruct_exn (Cstruct.of_string addr))))
addr
let error s = (s, Parse_error ("MAC is exactly 6 bytes", s))
let test_cstruct_rt_bad () =
let open Macaddr_cstruct in
let addrs =
[ error "\254\099\003\128\000"; error "\254\099\003\128\000\000\233" ]
in
List.iter
(fun (addr, exn) ->
assert_raises ~msg:(String.escaped addr) exn (fun () ->
of_cstruct_exn (Cstruct.of_string addr)))
addrs
let test_make_local () =
let () = Random.self_init () in
let bytegen i = if i = 0 then 253 else 255 - i in
let local_addr = make_local bytegen in
assert_equal ~msg:"is_local" (is_local local_addr) true;
assert_equal ~msg:"is_unicast" (is_unicast local_addr) true;
assert_equal ~msg:"localize" (to_octets local_addr).[0] (Char.chr 254);
for i = 1 to 5 do
assert_equal
~msg:("addr.[" ^ string_of_int i ^ "]")
(to_octets local_addr).[i]
(Char.chr (bytegen i))
done;
assert_equal ~msg:"get_oui" (get_oui local_addr)
((254 lsl 16) + (254 lsl 8) + 253)
let suite =
"Test"
>::: [
"string_rt" >:: test_string_rt;
"string_rt_bad" >:: test_string_rt_bad;
"bytes_rt" >:: test_bytes_rt;
"bytes_rt_bad" >:: test_bytes_rt_bad;
"cstruct_rt" >:: test_cstruct_rt;
"cstruct_rt_bad" >:: test_cstruct_rt_bad;
"make_local" >:: test_make_local;
]
;;
run_test_tt_main suite

View file

@ -0,0 +1,28 @@
(*
* Copyright (c) 2018 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)
type t = {
ip : Ipaddr_sexp.t;
ipv6 : Ipaddr_sexp.V6.t;
ipv6p : Ipaddr_sexp.V6.Prefix.t;
ipv4 : Ipaddr_sexp.V4.t;
ipv4p : Ipaddr_sexp.V4.Prefix.t;
scope : Ipaddr_sexp.scope;
mac : Macaddr_sexp.t;
ipp : Ipaddr_sexp.Prefix.t;
}
[@@deriving sexp]