Something went wrong. Try again.
BSDIFF40 binary diff and patch in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394open Alcobar
(* The defining property: apply inverts diff for any pair of inputs. *)let test_diff_apply source target = let patch = Bsdiff.diff ~source ~target in match Bsdiff.apply ~source patch with | Ok t -> if not (String.equal t target) then fail "apply (diff s t) <> t" | Error e -> failf "apply failed: %a" Loc.Error.pp e
(* The suffix array invariant on arbitrary bytes. *)let test_suffix_sorted s = let t = Bsdiff.Suffix_array.v s in let suffix i = let p = Bsdiff.Suffix_array.suffix t i in String.sub s p (String.length s - p) in for i = 0 to Bsdiff.Suffix_array.length t - 2 do if not (String.compare (suffix i) (suffix (i + 1)) < 0) then fail "suffixes out of order" done
(* Applying arbitrary bytes must terminate with a value or the codec's own error. *)let test_apply_total source patch = match Bsdiff.apply ~source patch with Ok _ | Error _ -> ()
(* Same, past the magic check. *)let test_apply_total_after_magic source patch = match Bsdiff.apply ~source ("BSDIFF40" ^ patch) with Ok _ | Error _ -> ()
(* Run the streaming apply, collecting the target into a string. *)let apply_streaming ~source patch = let buf = Buffer.create 256 in let w = Bytesrw.Bytes.Writer.of_buffer buf in match Bsdiff.apply_writer ~source patch w with | Ok () -> Ok (Buffer.contents buf) | Error _ as e -> e
(* The streaming apply must agree with the in-RAM apply on every input: the same reconstructed target, or rejection on both paths. *)let test_streaming_agrees source patch = match (Bsdiff.apply ~source patch, apply_streaming ~source patch) with | Ok a, Ok b -> if not (String.equal a b) then fail "streaming target <> in-RAM target" | Error _, Error _ -> () | Ok _, Error _ -> fail "streaming rejected a patch in-RAM accepted" | Error _, Ok _ -> fail "streaming accepted a patch in-RAM rejected"
(* The streaming apply inverts diff for any pair of inputs. *)let test_streaming_identity source target = let patch = Bsdiff.diff ~source ~target in match apply_streaming ~source patch with | Ok t -> if not (String.equal t target) then fail "streaming (diff s t) <> t" | Error e -> failf "streaming apply failed: %a" Loc.Error.pp e
(* A valid patch with one byte flipped drives apply's control-triple decode -- the seek/mix/copy fields and the three bzip2 sub-streams -- on corrupted-but-plausible values, reaching the seek sign-magnitude out-of-bounds path that fully-arbitrary bytes rarely get past the header to hit. Both apply and the streaming apply must stay total (a seek OOB would surface as an uncaught exception here) and agree on whether they accept it. *)let test_corrupted_patch source target pos = let patch = Bsdiff.diff ~source ~target in if String.length patch = 0 then bad_test () else begin let b = Bytes.of_string patch in let i = pos mod Bytes.length b in Bytes.set b i (Char.chr (Char.code (Bytes.get b i) lxor 0xff)); let corrupt = Bytes.to_string b in match (Bsdiff.apply ~source corrupt, apply_streaming ~source corrupt) with | Ok a, Ok b -> if not (String.equal a b) then fail "streaming and in-RAM disagree on a corrupted patch" | Error _, Error _ -> () | Ok _, Error _ | Error _, Ok _ -> fail "streaming and in-RAM disagree on accepting a corrupted patch" end
let suite = ( "bsdiff", [ test_case "diff/apply identity" [ bytes; bytes ] test_diff_apply; test_case "suffix array sorted" [ bytes ] test_suffix_sorted; test_case "apply is total" [ bytes; bytes ] test_apply_total; test_case "apply is total after magic" [ bytes; bytes ] test_apply_total_after_magic; test_case "streaming agrees with apply" [ bytes; bytes ] test_streaming_agrees; test_case "diff/streaming identity" [ bytes; bytes ] test_streaming_identity; test_case "corrupted patch stays total" [ bytes; bytes; uint16 ] test_corrupted_patch; ] )