Shell/P2p: add unit test with "garbled" messages.

This commit is contained in:
Grégoire Henry 2017-04-10 00:44:10 +02:00
parent e11e9c9ac5
commit 3d00dcf19f
2 changed files with 58 additions and 0 deletions

View File

@ -285,6 +285,33 @@ module Close_on_write = struct
end
module Garbled_data = struct
let encoding = Data_encoding.bytes
let garbled_msg = MBytes.create (1 lsl 4)
let server ch sched socket =
accept sched socket >>=? fun (info, auth_fd) ->
P2p_connection.accept auth_fd encoding >>=? fun conn ->
P2p_connection.raw_write_sync conn garbled_msg >>=? fun () ->
P2p_connection.read conn >>= fun err ->
_assert (is_connection_closed err) __LOC__ "" >>=? fun () ->
P2p_connection.close conn >>= fun _stat ->
return ()
let client ch sched addr port =
connect sched addr port id2 >>=? fun auth_fd ->
P2p_connection.accept auth_fd encoding >>=? fun conn ->
P2p_connection.read conn >>= fun err ->
_assert (is_decoding_error err) __LOC__ "" >>=? fun () ->
P2p_connection.close conn >>= fun _stat ->
return ()
let run _dir = run_nodes client server
end
let spec = Arg.[
"-v", Unit (fun () ->
@ -311,6 +338,7 @@ let main () =
"simple-message", Simple_message.run ;
"close-on-read", Close_on_read.run ;
"close-on-write", Close_on_write.run ;
"garbled-data", Garbled_data.run ;
]
let () =

View File

@ -206,6 +206,35 @@ module Random_connections = struct
end
module Garbled = struct
let is_connection_closed = function
| Error ((Write | Read) :: P2p_io_scheduler.Connection_closed :: _) -> true
| Ok _ -> false
| Error err ->
log_info "Unexpected error: %a" pp_print_error err ;
false
let write_bad_all conns =
let bad_msg = MBytes.of_string (String.make 16 '\000') in
iter_p
(fun conn ->
trace Write @@ P2p_connection_pool.raw_write_sync conn bad_msg)
conns
let node ch pool points =
Simple.connect_all ~timeout:2. pool points >>=? fun conns ->
sync ch >>=? fun () ->
begin
write_bad_all conns >>=? fun () ->
Simple.read_all conns
end >>= fun err ->
_assert (is_connection_closed err) __LOC__ ""
let run points = detach_nodes node points
end
let addr = ref Ipaddr.V6.localhost
let port = ref (1024 + Random.int 8192)
let clients = ref 10
@ -246,6 +275,7 @@ let main () =
Test.run "p2p-connection-pool." [
"simple", (fun _ -> Simple.run points) ;
"random", (fun _ -> Random_connections.run points !repeat_connections) ;
"garbled", (fun _ -> Garbled.run points) ;
]
let () =