From 3d00dcf19fc80afa163f0babb56d8b02faefc4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 10 Apr 2017 00:44:10 +0200 Subject: [PATCH] Shell/P2p: add unit test with "garbled" messages. --- test/p2p/test_p2p_connection.ml | 28 ++++++++++++++++++++++++++ test/p2p/test_p2p_connection_pool.ml | 30 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/p2p/test_p2p_connection.ml b/test/p2p/test_p2p_connection.ml index b2e199aa8..6b5e7709d 100644 --- a/test/p2p/test_p2p_connection.ml +++ b/test/p2p/test_p2p_connection.ml @@ -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 () = diff --git a/test/p2p/test_p2p_connection_pool.ml b/test/p2p/test_p2p_connection_pool.ml index e81a31399..f677f3796 100644 --- a/test/p2p/test_p2p_connection_pool.ml +++ b/test/p2p/test_p2p_connection_pool.ml @@ -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 () =