summaryrefslogtreecommitdiff
path: root/sid_test.ml
diff options
context:
space:
mode:
authorPhilipp Gesang <phg@phi-gamma.net>2018-10-29 00:48:03 +0100
committerPhilipp Gesang <phg@phi-gamma.net>2018-10-30 01:15:03 +0100
commit269cef804819e9aef1c2d6642b26c87f306cb176 (patch)
treeb7a3b118ecafb06d7e3350690c3c99f52681f67a /sid_test.ml
parent7aa81d2a490e161082f3c38c9d0e806d841caca2 (diff)
downloadocaml-sid-269cef804819e9aef1c2d6642b26c87f306cb176.tar.gz
sid: sid_test: move conversion functions to result
Get rid of all “StringFmt” APIs involving exceptions. There is now only the “decode” function which returns a result type.
Diffstat (limited to 'sid_test.ml')
-rw-r--r--sid_test.ml81
1 files changed, 34 insertions, 47 deletions
diff --git a/sid_test.ml b/sid_test.ml
index 0a571c9..f34e4b0 100644
--- a/sid_test.ml
+++ b/sid_test.ml
@@ -33,9 +33,16 @@ let create_fail () =
| None -> ()
| Some s -> assert_failure ("Sid.create succeeded on invalid sa array")
+let unwrap_of_string s =
+ match Sid.of_string s with
+ | Error e ->
+ assert_failure
+ (Printf.sprintf "error parsing assumed well-formed sid [%s]: %s" s e)
+ | Ok r -> r
+
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
+ let s = unwrap_of_string "S-1-1-0"
+ and 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);
@@ -46,43 +53,27 @@ let sf_parse_ok () =
(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 "")
+ match Sid.of_string "" with
+ | Ok _ -> assert_failure "unexpectedly parsed the empty string"
+ | Error e ->
+ assert_equal e "Invalid SID: ‘’ too short to be a SID in string format"
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")
+ match Sid.of_string "not a sid" with
+ | Ok _ -> assert_failure "unexpectedly parsed junk inputs as SID"
+ | Error e ->
+ assert_equal
+ e "Invalid SID [not a sid]: expected ‘S’ at position 0, found ‘n’"
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)
+ match Sid.of_string "S-I-3-3-7" with
+ | Ok _ -> assert_failure "unexpectedly parsed junk inputs as SID"
+ | Error e ->
+ assert_equal
+ e "Invalid SID [S-I-3-3-7]: expected ‘1’ at position 2, found ‘I’"
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"
+ let s = unwrap_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))
@@ -90,8 +81,8 @@ let sf_parse_long_ok () =
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"
+ let s1 = unwrap_of_string "S-1-1-0-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15"
+ and s2 = unwrap_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))
@@ -114,12 +105,12 @@ let pr_encode_null_ok () =
let pr_encode_be_ok () =
let sid = "S-1-0-42" in
- let sle = Sid.of_string sid
- |> Sid.PacketRep.encode
- |> Xxd.xxd_of_bytes ~blocklen:2
- and sbe = Sid.of_string sid
- |> Sid.PacketRep.encode ~endian:Big
- |> Xxd.xxd_of_bytes ~blocklen:2
+ let sle = unwrap_of_string sid
+ |> Sid.PacketRep.encode
+ |> Xxd.xxd_of_bytes ~blocklen:2
+ and sbe = unwrap_of_string sid
+ |> Sid.PacketRep.encode ~endian:Big
+ |> Xxd.xxd_of_bytes ~blocklen:2
in
let expect_le = "0101 0000 0000 0000 2a00 0000"
and expect_be = "0101 0000 0000 0000 0000 002a" in
@@ -162,7 +153,7 @@ let pr_decode_all_ok () =
(Sid.equal s w)
let pr_decode_be_ok () =
- let sid = Sid.of_string "S-1-1-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15"
+ let sid = unwrap_of_string "S-1-1-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15"
and sle =
match Xxd.bytes_of_xxd
"010f 0000 0000 0001 \
@@ -282,15 +273,11 @@ let string_format_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-ia-junk-fail" >:: sf_parse_ia_junk_fail
; "parse-long-ok" >:: sf_parse_long_ok
; "parse-too-long-ok" >:: sf_parse_too_long_ok
]
-
let packet_rep_test = "packet-rep" >:::
[ "encode-null-ok" >:: pr_encode_null_ok
; "encode-all-ok" >:: pr_encode_all_ok