File: test_macaddr.ml

package info (click to toggle)
ocaml-ipaddr 5.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: ml: 3,136; makefile: 12
file content (114 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(*
 * 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 test_bad_sep_mix () =
  let s = "00:11-22:33-44:55" in
  assert_result_failure ~msg:s (of_string s)

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;
         "bad_sep_mix" >:: test_bad_sep_mix;
       ]
;;

run_test_tt_main suite