|
| 1 | +let major_nbd = 43 |
| 2 | + |
| 3 | +(** This module type helps us to implement alternative modules for [Stat]. |
| 4 | + In particular one that uses the previous ad-hoc functions that where |
| 5 | + incorrect, and one that we can use as reference in case the behaviour |
| 6 | + changes and possibly change the users of [Stat]. |
| 7 | + *) |
| 8 | +module type S = sig |
| 9 | + type device |
| 10 | + |
| 11 | + val device : major:int -> minor:int -> device option |
| 12 | + |
| 13 | + val encode_st_dev : device -> int |
| 14 | + |
| 15 | + val decode_st_dev : int -> device |
| 16 | + |
| 17 | + val major : device -> int |
| 18 | + |
| 19 | + val minor : device -> int |
| 20 | + |
| 21 | + val pp : Format.formatter -> device -> unit |
| 22 | +end |
| 23 | + |
| 24 | +module Stat : S = struct |
| 25 | + include Xapi_stdext_unix.Unixext.Stat |
| 26 | + |
| 27 | + let major {major; _} = major |
| 28 | + |
| 29 | + let minor {minor; _} = minor |
| 30 | + |
| 31 | + let pp = |
| 32 | + Fmt.( |
| 33 | + record ~sep:(any ", ") |
| 34 | + [ |
| 35 | + field "major" (fun d -> d.major) int |
| 36 | + ; field "minor" (fun d -> d.minor) int |
| 37 | + ] |
| 38 | + ) |
| 39 | +end |
| 40 | + |
| 41 | +module Stat_reference : S = struct |
| 42 | + type device = {major: int; minor: int} |
| 43 | + |
| 44 | + let ( << ) = Stdlib.( lsl ) |
| 45 | + |
| 46 | + let ( >> ) = Stdlib.( lsr ) |
| 47 | + |
| 48 | + let ( &^ ) = Stdlib.( land ) |
| 49 | + |
| 50 | + let ( |^ ) = Stdlib.( lor ) |
| 51 | + |
| 52 | + let device ~major ~minor = |
| 53 | + (* Linux's devids are 32-bit wide and the major and minor ones are 16-bit |
| 54 | + wide, but we can support well up to 32-bit-wide minors *) |
| 55 | + let minor_max = (1 << 32) - 1 in |
| 56 | + let major_max = (1 << 16) - 1 in |
| 57 | + if major < 0 || major_max < major || minor < 0 || minor_max < minor then |
| 58 | + None |
| 59 | + else |
| 60 | + Some {major; minor} |
| 61 | + |
| 62 | + let encode_st_dev {major; minor} = |
| 63 | + 0 |
| 64 | + |^ (major &^ 0x00000fff << 8) |
| 65 | + |^ (major &^ 0x7ffff000 << 32) |
| 66 | + |^ (minor &^ 0x000000ff << 0) |
| 67 | + |^ (minor &^ 0xffffff00 << 12) |
| 68 | + |
| 69 | + let decode_st_dev dev = |
| 70 | + (* follow glibc's implementation, with an exception: the most significant |
| 71 | + bit is ignored because ints are 63 bits in ocaml. In any case, |
| 72 | + [Unix.stat] returns a 63-bit int, so we can't do much in this code to |
| 73 | + avoid this. *) |
| 74 | + let major = |
| 75 | + 0 |^ (dev &^ 0x7ffff00000000000 >> 32) |^ (dev &^ 0x00000000000fff00 >> 8) |
| 76 | + in |
| 77 | + let minor = |
| 78 | + 0 |^ (dev &^ 0x00000ffffff00000 >> 12) |^ (dev &^ 0x00000000000000ff >> 0) |
| 79 | + in |
| 80 | + {major; minor} |
| 81 | + |
| 82 | + let major {major; _} = major |
| 83 | + |
| 84 | + let minor {minor; _} = minor |
| 85 | + |
| 86 | + let pp = |
| 87 | + Fmt.( |
| 88 | + record ~sep:(any ", ") |
| 89 | + [ |
| 90 | + field "major" (fun d -> d.major) int |
| 91 | + ; field "minor" (fun d -> d.minor) int |
| 92 | + ] |
| 93 | + ) |
| 94 | +end |
| 95 | + |
| 96 | +let hex = Alcotest.testable (Fmt.of_to_string (Format.sprintf "0x%x")) ( = ) |
| 97 | + |
| 98 | +let current_t = Alcotest.testable Stat.pp ( = ) |
| 99 | + |
| 100 | +let test_combinations f ~major:lst_a ~minor:lst_b = |
| 101 | + let test a b = (Printf.sprintf "major %i, minor %i" a b, `Quick, f a b) in |
| 102 | + List.concat_map (fun a -> List.map (test a) lst_b) lst_a |
| 103 | + |
| 104 | +let spec_minor = [0; 31; 65; 256; 1025; 4098; (1 lsl 32) - 1] |
| 105 | + |
| 106 | +let spec_major = [0; major_nbd; (1 lsl 16) - 1] |
| 107 | + |
| 108 | +let test_reference = |
| 109 | + let test major minor () = |
| 110 | + let current = Stat.device ~major ~minor |> Option.get in |
| 111 | + let reference = Stat_reference.device ~major ~minor |> Option.get in |
| 112 | + let encoded_cur = Stat.encode_st_dev current in |
| 113 | + let encoded_ref = Stat_reference.encode_st_dev reference in |
| 114 | + |
| 115 | + Alcotest.check hex "Encode must match reference implementation" encoded_ref |
| 116 | + encoded_cur ; |
| 117 | + |
| 118 | + let decoded_cur = Stat.decode_st_dev encoded_ref in |
| 119 | + let decoded_ref = Stat_reference.decode_st_dev encoded_ref in |
| 120 | + |
| 121 | + Alcotest.(check @@ pair int int) |
| 122 | + "Decode must match reference implementation" |
| 123 | + Stat_reference.(major decoded_ref, minor decoded_ref) |
| 124 | + Stat.(major decoded_cur, minor decoded_cur) |
| 125 | + in |
| 126 | + let tests = test_combinations test ~major:spec_major ~minor:spec_minor in |
| 127 | + ("Compare with reference", tests) |
| 128 | + |
| 129 | +let test_roundtrip = |
| 130 | + let test major minor () = |
| 131 | + let current = Stat.device ~major ~minor |> Option.get in |
| 132 | + let encoded_cur = Stat.encode_st_dev current in |
| 133 | + |
| 134 | + let decoded_cur = Stat.decode_st_dev encoded_cur in |
| 135 | + Alcotest.check current_t "Roundtripped current" current decoded_cur |
| 136 | + in |
| 137 | + let tests = test_combinations test ~major:spec_major ~minor:spec_minor in |
| 138 | + ("Roundtrip", tests) |
| 139 | + |
| 140 | +let tests = [test_reference; test_roundtrip] |
| 141 | + |
| 142 | +let () = Alcotest.run "Uniext.Stat suite" tests |
0 commit comments