diff options
-rw-r--r-- | sid.ml | 37 | ||||
-rw-r--r-- | sid.mli | 4 | ||||
-rw-r--r-- | sid_test.ml | 81 | ||||
-rw-r--r-- | util/sidparse.ml | 5 |
4 files changed, 59 insertions, 68 deletions
@@ -85,23 +85,26 @@ module StringFmt = struct Error (Printf.sprintf "Invalid SID: ‘%s’ too short to be a SID in string format" s) - else - expect_char s 'S' 0; - expect_char s '-' 1; - expect_char s '1' 2; - expect_char s '-' 3; - let p = 4 in - let p, ia = read_decimal_u64 s p in - let sa = ref [] and p' = ref p in - while !p' < n && List.length !sa < max_subauth_count do - expect_char s '-' !p'; - let np, d = read_decimal_u32 s (!p' + 1) in - sa := d :: !sa; - p' := np - done; - Ok { sid_ident_auth = ia - ; sid_sub_auths = Array.of_list (List.rev !sa) - } + else begin + try + expect_char s 'S' 0; + expect_char s '-' 1; + expect_char s '1' 2; + expect_char s '-' 3; + let p = 4 in + let p, ia = read_decimal_u64 s p in + let sa = ref [] and p' = ref p in + while !p' < n && List.length !sa < max_subauth_count do + expect_char s '-' !p'; + let np, d = read_decimal_u32 s (!p' + 1) in + sa := d :: !sa; + p' := np + done; + Ok { sid_ident_auth = ia + ; sid_sub_auths = Array.of_list (List.rev !sa) + } + with Invalid_argument e -> Error e; + end let fmt_ident_auth b ia = Buffer.add_string b (U64.to_string ia) @@ -11,7 +11,7 @@ val get_sub_auths : t -> sub_auths module StringFmt : sig - val decode : string -> (t, string result) + val decode : string -> (t, string) result val encode : t -> string end @@ -48,6 +48,6 @@ module WellKnown : end end -val of_string : string -> t +val of_string : string -> (t, string) result val to_string : t -> string 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 diff --git a/util/sidparse.ml b/util/sidparse.ml index 5ab6bf4..ba0efc2 100644 --- a/util/sidparse.ml +++ b/util/sidparse.ml @@ -31,8 +31,9 @@ let handle_input q v s = match String.trim s with | "" -> `Nothing | s -> - try `Done (Sid.of_string s) - with Invalid_argument e -> err q v "ERROR: %s\n%!" e; `Junk + (match Sid.of_string s with + | Ok s -> `Done s + | Error e -> err q v "ERROR: %s\n%!" e; `Junk) let from_argv sids = let rest = ref sids in |