summaryrefslogtreecommitdiffstats
path: root/ocaml/t/hivex_100_errors.ml
blob: 05776326a0156a4f3b2c74f2b5c0c17617bb82f9 (plain)
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
(* hivex OCaml bindings
 * Copyright (C) 2009-2010 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *)

(* Test different types of error handling used by the API. *)

open Unix
open Printf
let (//) = Filename.concat
let srcdir = try Sys.getenv "srcdir" with Not_found -> "."

let () =
  printf "01 non-existent file\n%!";
  (try
     ignore (Hivex.open_file "no_such_file" []);
     failwith "no exception thrown when opening a non-existent file"
   with
   | Hivex.Error ("open", ENOENT, _) -> () (* ok *)
   (* let any other exception escape and stop the test *)
  );

  printf "02 closed handle\n%!";
  let h = Hivex.open_file (srcdir // "../images/minimal") [] in
  Hivex.close h;
  (try
     ignore (Hivex.root h)
   with
   | Hivex.Handle_closed "root" -> () (* ok *)
   (* let any other exception escape and stop the test *)
  );

  printf "03 write to read-only file\n%!";
  let h = Hivex.open_file (srcdir // "../images/minimal") [] in
  (try
     ignore (Hivex.node_add_child h (Hivex.root h) "Foo")
   with
   | Hivex.Error ("node_add_child", EROFS, _) -> () (* ok *)
   (* let any other exception escape and stop the test *)
  );
  Hivex.close h;

  printf "04 node_get_child node not found\n%!";
  let h = Hivex.open_file (srcdir // "../images/minimal") [] in
  (try
     ignore (Hivex.node_get_child h (Hivex.root h) "NoSuchNode")
   with
   | Not_found -> () (* ok *)
   (* let any other exception escape and stop the test *)
  );
  Hivex.close h;

  (* Gc.compact is a good way to ensure we don't have
   * heap corruption or double-freeing.
   *)
  Gc.compact ()