Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: test error conditions #151

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions fuzz/fuzz.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ let hash =
const (Pack Digestif.sha512);
]

let with_get_into_bytes off (type ctx)
let with_get_into_bytes off len (type ctx)
(module Hash : Digestif.S with type ctx = ctx) (ctx : ctx) =
let buf = Bytes.create (off + Hash.digest_size) in
Hash.get_into_bytes ctx ~off buf ;
let buf = Bytes.create len in
let () =
try Hash.get_into_bytes ctx ~off buf
with Invalid_argument e -> (
(* Skip if the invalid argument is valid; otherwise fail *)
match Bytes.sub buf off Hash.digest_size with
| _ -> failf "Hash.get_into_bytes: Invalid_argument %S" e
| exception Invalid_argument _ -> bad_test ()) in
Bytes.sub_string buf off Hash.digest_size

let () =
add_test ~name:"get_into_bytes" [ hash; int8; bytes ]
@@ fun (Pack hash) off bytes ->
add_test ~name:"get_into_bytes" [ hash; int8; range 1024; bytes ]
@@ fun (Pack hash) off len bytes ->
let (module Hash) = Digestif.module_of hash in
let ctx = Hash.empty in
let ctx = Hash.feed_string ctx bytes in
let a = with_get_into_bytes (abs off) (module Hash) ctx in
let a = with_get_into_bytes off len (module Hash) ctx in
let b = Hash.(to_raw_string (get ctx)) in
check_eq ~eq:String.equal a b
Loading