summaryrefslogtreecommitdiff
path: root/sid_test.ml
blob: 981215285f05a4dbf9941162caccdf0f23d1ccd5 (plain)
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
open OUnit

(* S-1-1-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14 *)
let max_sid = Stdint.(
      Sid.create_unsafe
         [| Uint32.zero      ; Uint32.one       ; Uint32.of_int  2
          ; Uint32.of_int  3 ; Uint32.of_int  4 ; Uint32.of_int  5
          ; Uint32.of_int  6 ; Uint32.of_int  7 ; Uint32.of_int  8
          ; Uint32.of_int  9 ; Uint32.of_int 10 ; Uint32.of_int 11
          ; Uint32.of_int 12 ; Uint32.of_int 13 ; Uint32.of_int 14
         |]
         Uint64.one
    )

let sf_parse_ok () =
  let s = Sid.of_string "S-1-1-0" in
  let z = Stdint.((Sid.create_unsafe [| Uint32.zero |] Uint64.one)) in
  assert_bool
    (Printf.sprintf "[%s] ≠ [%s]" (Sid.to_string s) (Sid.to_string z))
    (Sid.equal s z);
  let w = Sid.WellKnown.world in
  assert_bool
    (Printf.sprintf "[%s] ≠ [well known: %s]"
       (Sid.to_string s) (Sid.to_string w))
    (Sid.equal s w)

let sf_parse_empty_fail () =
  assert_raises
    (Invalid_argument
       "Invalid SID: ‘’ too short to be a SID in string format")
    (fun () -> Sid.of_string "")

let sf_parse_junk_fail () =
  assert_raises
    (Invalid_argument
       "Invalid SID [not a sid]: expected ‘S’ at position 0, found ‘n’")
    (fun () -> Sid.of_string "not a sid")

let sf_parse_ia_junk_fail () =
  assert_raises
    (Invalid_argument
       "Invalid SID [not a sid]: expected ‘1’ at position 2, found ‘I’")
    (fun () -> Sid.of_string "S-I-3-3-7")

let sf_parse_opt_ok () =
  let s = Sid.StringFmt.from_string_opt "S-1-1-0" in
  assert_bool "failure parsing [S-1-1-0] with option" (s <> None)

let sf_parse_opt_fail () =
  let s = Sid.StringFmt.from_string_opt "not a sid" in
  assert_bool "unexpected success parsing garbage with option" (s = None)

let is_error = function Error _ -> true | _ -> false

let sf_parse_result_ok () =
  let s = Sid.StringFmt.from_string_res "S-1-1-0" in
  assert_bool "failure parsing [S-1-1-0] with result" (not (is_error s))

let sf_parse_result_fail () =
  let s = Sid.StringFmt.from_string_res "not a sid" in
  assert_bool "unexpected success parsing garbage with result" (is_error s)

let sf_parse_long_ok () =
  let s = Sid.of_string "S-1-1-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14"
  and l = max_sid in
  assert_bool
    (Printf.sprintf "[%s] ≠ [%s]" (Sid.to_string s) (Sid.to_string l))
    (Sid.equal s l)

let sf_parse_too_long_ok () =
  (* must ignore trailing subauths *)
  let s1 = Sid.of_string "S-1-1-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15"
  and s2 = Sid.of_string "S-1-1-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17"
  and l = max_sid in
  assert_bool
    (Printf.sprintf "[%s] ≠ [%s]" (Sid.to_string s1) (Sid.to_string l))
    (Sid.equal s1 l);
  assert_bool
    (Printf.sprintf "[%s] ≠ [%s]" (Sid.to_string s2) (Sid.to_string l))
    (Sid.equal s2 l)

let test = "string-format-syntax" >:::
  [ "parse-ok"              >:: sf_parse_ok
  ; "parse-empty-fail"      >:: sf_parse_empty_fail
  ; "parse-junk-fail"       >:: sf_parse_junk_fail
  ; "parse-opt-ok"          >:: sf_parse_opt_ok
  ; "parse-opt-fail"        >:: sf_parse_opt_fail
  ; "parse-result-ok"       >:: sf_parse_result_ok
  ; "parse-result-fail"     >:: sf_parse_result_fail
  ; "parse-long-ok"         >:: sf_parse_long_ok
  ; "parse-too-long-ok"     >:: sf_parse_too_long_ok
  ]

let () = ignore (run_test_tt_main test)