Skip to content

Commit

Permalink
test negative sub/shift is illegal
Browse files Browse the repository at this point in the history
  • Loading branch information
pqwy committed Jul 21, 2014
1 parent f4de6cf commit 756750e
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions lib_test/bounds.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ let test_positive_shift () =
let y = Cstruct.shift x 1 in
assert_equal ~printer:string_of_int 0 (Cstruct.len y)

(* Check we can shift in the -ve direction *)
(* Check that negative shifts are forbidden. *)
let test_negative_shift () =
let x = Cstruct.create 10 in
let y = Cstruct.sub x 5 5 in
let z = Cstruct.shift y (-5) in
assert_equal ~printer:string_of_int 0 z.Cstruct.off;
assert_equal ~printer:string_of_int 10 z.Cstruct.len
let x = Cstruct.create 2 in
let y = Cstruct.sub x 1 1 in
try
let z = Cstruct.shift x (-1) in
failwith (Printf.sprintf "test_negative_shift/outer: %s" (to_string z))
with Invalid_argument _ ->
try
let z = Cstruct.shift y (-1) in
failwith (Printf.sprintf "test_negative_shift/inner: %s" (to_string z))
with Invalid_argument _ ->
()

(* Check that an attempt to shift beyond the end of the buffer fails *)
let test_bad_positive_shift () =
Expand All @@ -56,14 +62,6 @@ let test_bad_positive_shift () =
failwith (Printf.sprintf "test_bad_positive_shift: %s" (to_string y))
with Invalid_argument _ -> ()

(* Check that an attempt to shift before the start of the buffer fails *)
let test_bad_negative_shift () =
let x = Cstruct.create 10 in
try
let y = Cstruct.shift x (-1) in
failwith (Printf.sprintf "test_bad_negative_shift: %s" (to_string y))
with Invalid_argument _ -> ()

(* Check that 'sub' works *)
let test_sub () =
let x = Cstruct.create 100 in
Expand All @@ -74,6 +72,19 @@ let test_sub () =
assert_equal ~printer:string_of_int 20 z.Cstruct.off;
assert_equal ~printer:string_of_int 60 z.Cstruct.len

let test_negative_sub () =
let x = Cstruct.create 2 in
let y = Cstruct.sub x 1 1 in
try
let z = Cstruct.sub x (-1) 0 in
failwith (Printf.sprintf "test_negative_sub/outer: %s" (to_string z))
with Invalid_argument _ ->
try
let z = Cstruct.sub y (-1) 0 in
failwith (Printf.sprintf "test_negative_sub/inner: %s" (to_string z))
with Invalid_argument _ ->
()

(* Check that 'sub' can't set 'len' too big *)
let test_sub_len_too_big () =
let x = Cstruct.create 0 in
Expand Down Expand Up @@ -105,13 +116,6 @@ let test_sub_offset_too_big () =
with Invalid_argument _ -> ()
end

let test_sub_offset_too_small () =
let x = Cstruct.create 0 in
try
let y = Cstruct.sub x (-1) 0 in
failwith (Printf.sprintf "test_sub_offset_too_small: %s" (to_string y))
with Invalid_argument _ -> ()

let test_set_len_too_big () =
let x = Cstruct.create 0 in
try
Expand Down Expand Up @@ -360,12 +364,11 @@ let _ =
"test positive shift" >:: test_positive_shift;
"test negative shift" >:: test_negative_shift;
"test bad positive shift" >:: test_bad_positive_shift;
"test bad negative shift" >:: test_bad_negative_shift;
"test sub" >:: test_sub;
"test negative sub" >:: test_negative_sub;
"test sub len too big" >:: test_sub_len_too_big;
"test sub len too small" >:: test_sub_len_too_small;
"test sub offset too big" >:: test_sub_offset_too_big;
"test sub offset too small" >:: test_sub_offset_too_small;
"test set len too big" >:: test_set_len_too_big;
"test set len too small" >:: test_set_len_too_small;
"test add len too big" >:: test_add_len_too_big;
Expand Down

0 comments on commit 756750e

Please sign in to comment.