Skip to content

Commit

Permalink
More tests clean-ups
Browse files Browse the repository at this point in the history
  • Loading branch information
samoht committed Jun 9, 2015
1 parent 34ede38 commit 2de53c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
10 changes: 4 additions & 6 deletions lib_test/common.ml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
open Lwt
let (>>=) = Lwt.(>>=)

let cmp a b = String.compare a b = 0

let fail fmt = Printf.ksprintf OUnit.assert_failure fmt

let expect msg expected actual =
(if not (cmp expected actual) then
fail "Expected '%s', got '%s': %s" expected actual msg) ;
Lwt.return_unit
if cmp expected actual then Lwt.return_unit
else fail "Expected '%s', got '%s': %s" expected actual msg

let or_error name fn t =
fn t >>= function
| `Error e -> fail "or_error starting %s" name
| `Ok t -> return t

| `Ok t -> Lwt.return t
64 changes: 45 additions & 19 deletions lib_test/test_connect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Lwt
open Common
open Vnetif_common
let (>>=) = Lwt.(>>=)

module Test_connect (B : Vnetif_backends.Backend) = struct
module C = Console
Expand All @@ -29,14 +29,31 @@ module Test_connect (B : Vnetif_backends.Backend) = struct
let test_string = "Hello world from Mirage 123456789...."
let backend = V.create_backend ()

let log_s c fmt = Printf.ksprintf (C.log_s c) (fmt ^^ "%!")

let err_read_eof () = fail "accept got EOF while reading"
let err_write_eof () = fail "client tried to write, got EOF"

let err_read e =
let err = V.Stackv4.TCPV4.error_message e in
fail "Error while reading: %s" err

let err_write e =
let err = V.Stackv4.TCPV4.error_message e in
fail "client tried to write, got %s" err

let accept c flow expected =
let ip, port = V.Stackv4.TCPV4.get_dest flow in
C.log_s c (Printf.sprintf "Accepted connection from %s:%d%!" (Ipaddr.V4.to_string ip) port) >>= fun () ->
V.Stackv4.TCPV4.read flow >>= (function
| `Ok b -> OS.Time.sleep 0.1 >>= fun () -> expect "accept" expected (Cstruct.to_string b) (* sleep first to capture data in pcap *)
| `Eof | `Error _ -> fail "Error while reading%!")
log_s c "Accepted connection from %s:%d" (Ipaddr.V4.to_string ip) port
>>= fun () ->
C.log_s c "Connection closed%!"
V.Stackv4.TCPV4.read flow >>= function
| `Eof -> err_read_eof ()
| `Error e -> err_read e
| `Ok b ->
OS.Time.sleep 0.1 >>= fun () ->
(* sleep first to capture data in pcap *)
expect "accept" expected (Cstruct.to_string b) >>= fun () ->
log_s c "Connection closed"

let test_tcp_connect_two_stacks () =
or_error "console" Console.connect "console" >>= fun c ->
Expand All @@ -51,15 +68,17 @@ module Test_connect (B : Vnetif_backends.Backend) = struct

(Lwt_unix.sleep 0.1 >>= fun () ->
V.create_stack c backend client_ip netmask [gw] >>= fun s2 ->
or_error "connect" (V.Stackv4.TCPV4.create_connection (V.Stackv4.tcpv4 s2)) (server_ip, 80) >>= fun flow ->
C.log_s c "Connected to other end...%!" >>= fun () ->
V.Stackv4.TCPV4.write flow (Cstruct.of_string test_string) >>= (function
| `Ok () -> C.log_s c "wrote hello world%!"
| `Error _ -> fail "client tried to write, got error%!"
| `Eof -> fail "client tried to write, got eof%!") >>= fun () ->
V.Stackv4.TCPV4.close flow >>= fun () ->
Lwt_unix.sleep 1.0 >>= fun () -> (* record some traffic after close *)
Lwt.return_unit) ] >>= fun () ->
let conn = V.Stackv4.TCPV4.create_connection (V.Stackv4.tcpv4 s2) in
or_error "connect" conn (server_ip, 80) >>= fun flow ->
log_s c "Connected to other end..." >>= fun () ->
V.Stackv4.TCPV4.write flow (Cstruct.of_string test_string) >>= function
| `Error e -> err_write e
| `Eof -> err_write_eof ()
| `Ok () ->
log_s c "wrote hello world" >>= fun () ->
V.Stackv4.TCPV4.close flow >>= fun () ->
Lwt_unix.sleep 1.0 >>= fun () -> (* record some traffic after close *)
Lwt.return_unit) ] >>= fun () ->

Lwt.return_unit

Expand All @@ -70,13 +89,20 @@ end

let test_tcp_connect_two_stacks_basic () =
let module Test = Test_connect(Vnetif_backends.Basic) in
Test.record_pcap "tests/pcap/tcp_connect_two_stacks_basic.pcap" Test.test_tcp_connect_two_stacks
Test.record_pcap
"tests/pcap/tcp_connect_two_stacks_basic.pcap"
Test.test_tcp_connect_two_stacks

let test_tcp_connect_two_stacks_trailing_bytes () =
let module Test = Test_connect(Vnetif_backends.Trailing_bytes) in
Test.record_pcap "tests/pcap/tcp_connect_two_stacks_trailing_bytes.pcap" Test.test_tcp_connect_two_stacks
Test.record_pcap
"tests/pcap/tcp_connect_two_stacks_trailing_bytes.pcap"
Test.test_tcp_connect_two_stacks

let suite = [
"test_tcp_connect_two_stacks_basic" , test_tcp_connect_two_stacks_basic;
"test_tcp_connect_two_stacks_trailing_bytes" , test_tcp_connect_two_stacks_trailing_bytes;
"connect two stacks, basic test",
test_tcp_connect_two_stacks_basic;

"connect two stacks, with trailing bytes",
test_tcp_connect_two_stacks_trailing_bytes;
]
7 changes: 3 additions & 4 deletions lib_test/test_iperf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ module Test_iperf (B : Vnetif_backends.Backend) = struct

let mlen = String.length msg

let fail fmt = Printf.kprintf (fun str -> Lwt.fail (Failure str)) fmt
let err_eof () = fail "EOF while writing to TCP flow"

let err_connect e ip port () =
Expand Down Expand Up @@ -216,12 +215,12 @@ let test_tcp_iperf_two_stacks_uniform_packet_loss () =
"tests/pcap/tcp_iperf_two_stacks_uniform_packet_loss.pcap" Test.tcp_iperf

let suite = [
"test_tcp_iperf_two_stacks_basic",
"iperf with two stacks, basic tests",
test_tcp_iperf_two_stacks_basic;

"test_tcp_iperf_two_stacks_trailing_bytes",
"iperf with two stacks, testing trailing_bytes",
test_tcp_iperf_two_stacks_trailing_bytes;

"test_tcp_iperf_two_stacks_uniform_packet_loss",
"iperf with two stacks and uniform packet loss",
test_tcp_iperf_two_stacks_uniform_packet_loss;
]

0 comments on commit 2de53c7

Please sign in to comment.