summaryrefslogtreecommitdiff
path: root/sid_test.ml
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-10-19 23:37:39 +0200
committerPhilipp Gesang <phg@phi-gamma.net>2018-10-28 23:07:05 +0100
commite5fc2699660b440fa02260091b5bd019866f081c (patch)
treeb0f57e71440bc871db01a1c3875a006b54c4edf4 /sid_test.ml
parent3ba808f9dc1a9c01ee4ff369611c87315a98bc20 (diff)
downloadocaml-sid-e5fc2699660b440fa02260091b5bd019866f081c.tar.gz
sid: sid_test: add current state to repo
Diffstat (limited to 'sid_test.ml')
-rw-r--r--sid_test.ml95
1 files changed, 95 insertions, 0 deletions
diff --git a/sid_test.ml b/sid_test.ml
new file mode 100644
index 0000000..9812152
--- /dev/null
+++ b/sid_test.ml
@@ -0,0 +1,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)
+