Skip to content

Commit

Permalink
test: update unit tests for websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Apr 5, 2024
1 parent 3393c13 commit 9329c95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions tests/unit/ws/t_ws.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ let read_file file : string =

let apply_masking = Tiny_httpd_ws.Private_.apply_masking

let decode ~key b =
let decode ?(mask_offset = 0) ~key b =
let buf = Bytes.copy b in
apply_masking ~mask_key:key buf 0 (Bytes.length buf);
apply_masking ~mask_key:key ~mask_offset buf 0 (Bytes.length buf);
buf

let () =
Expand Down
17 changes: 10 additions & 7 deletions tests/unit/ws/t_ws_q.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ open Test_util

let apply_masking = Tiny_httpd_ws.Private_.apply_masking

let decode ~key b =
let decode ?(mask_offset = 0) ~key b =
let buf = Bytes.copy b in
apply_masking ~mask_key:key buf 0 (Bytes.length buf);
apply_masking ~mask_key:key ~mask_offset buf 0 (Bytes.length buf);
buf

let ppb b = Printf.sprintf "%S" (Bytes.unsafe_to_string b)
Expand All @@ -13,12 +13,15 @@ let () =
add_qcheck
@@ QCheck.Test.make ~count:10_000
Q.(
pair (bytes_of_size (Gen.return 4)) (bytes_of_size Gen.(0 -- 6000))
triple
(bytes_of_size (Gen.return 4))
(option small_nat)
(bytes_of_size Gen.(0 -- 6000))
(* |> Q.add_stat ("b.size", fun (_k, b) -> Bytes.length b) *)
|> Q.add_shrink_invariant (fun (k, _) -> Bytes.length k = 4))
(fun (key, b) ->
|> Q.add_shrink_invariant (fun (k, _, _) -> Bytes.length k = 4))
(fun (key, mask_offset, b) ->
Q.assume (Bytes.length key = 4);
let b2 = decode ~key b in
let b2 = decode ~key ?mask_offset b in

let is_zero =
Bytes.equal key (Bytes.unsafe_of_string "\x00\x00\x00\x00")
Expand All @@ -32,7 +35,7 @@ let () =
(ppb key) (ppb b) (ppb b2)
);

let b3 = decode ~key b2 in
let b3 = decode ~key ?mask_offset b2 in
assert (Bytes.equal b b3);
true)

Expand Down

0 comments on commit 9329c95

Please sign in to comment.