forked from Incubaid/rsync-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ml
75 lines (67 loc) · 2.15 KB
/
test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
open Rsync
open Signature
open Hash
module S = SDigest
module AA = Rsync(Adler)(S)
module MySig = Signature(Adler)(S)
let old_dir = "tests/old/"
let new_dir = "tests/new/"
let sync_dir = "tests/synced/"
let test_io () =
let values = [128;127;4;1;0;] in
let do_one v =
let fn = "/tmp/test_io.bin" in
let oc = open_out fn in
let () = Io.write_int oc v in
let () = close_out oc in
let ic = open_in fn in
let v2 = Io.read_int ic in
let () = close_in ic in
Printf.printf "%i <> %i\n" v v2;
assert (v = v2)
in
List.iter do_one values
let test_signature fn bs =
Printf.printf "SIGNATURE:%s\n" fn;
let old_fn = old_dir ^ fn in
let signature = MySig.create old_fn bs in
let signature_fn = sync_dir ^ fn ^ ".signature" in
let () = MySig.to_file signature signature_fn in
let signature2 = MySig.from_file signature_fn in
Printf.printf "%s: %b \n%!" fn (MySig.equals signature2 signature)
let test_one fn bs =
let old_fn = old_dir ^ fn in
let signature = MySig.create old_fn bs in
let signature_fn = sync_dir ^ fn ^ ".signature" in
let delta_fn = sync_dir ^ fn ^ ".delta" in
let () = MySig.to_file signature signature_fn in
let oc = open_out delta_fn in
let handle_action action = output_action oc action in
let new_fn = new_dir ^ fn in
let signature2 = MySig.from_file signature_fn in
(* let signature2 = signature in *)
assert (signature2 = signature);
let emitter = new AA.delta_emitter signature2 new_fn handle_action in
let () = emitter # run () in
let () = close_out oc in
let ic = open_in delta_fn in
let sync_fn = sync_dir ^ fn in
let sync_oc = open_out sync_fn in
let applier = new AA.delta_applier ic old_fn sync_oc in
applier # run () ;
close_out sync_oc;
let d_new = S.to_hex(S.file new_fn) in
let d_synced = S.to_hex(S.file sync_fn) in
Printf.printf "%s: %s =?= %s\n%!" fn d_new d_synced;
()
let test_correctness () =
let tests = ["small.txt", 16;
"fischer.txt",16;
"big.bmp", 256;
]
in
List.iter (fun (fn,bs) -> test_signature fn bs) tests;
List.iter (fun (fn,bs) -> test_one fn bs) tests ;;
let suite () =
test_io ();
test_correctness();;