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)