Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect does not error #27

Merged
merged 1 commit into from Oct 2, 2016
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -50,16 +50,13 @@ let err e = Lwt.return (`Error e)
let fail fmt = Printf.ksprintf (fun str -> Lwt.fail (Failure str)) fmt
let ok x = Lwt.return (`Ok x)

let err_unknown u = err (`Unknown u)
let err_disconnected () = err `Disconnected

let err_permission_denied devname =
let s = Printf.sprintf
"Permission denied while opening the %s tun device. \n\
Please re-run using sudo, and install the TuntapOSX \n\
package if you are on MacOS X." devname
in
err_unknown s
Printf.sprintf
"Permission denied while opening the %s tun device. \n\
Please re-run using sudo, and install the TuntapOSX \n\
package if you are on MacOS X." devname

let err_partial_write len' page =
fail "tap: partial write (%d, expected %d)" len' page.Cstruct.len
@@ -79,10 +76,10 @@ let connect devname =
in
Hashtbl.add devices devname t;
log "connect %s" devname;
ok t
Lwt.return t
with
| Failure "tun[open]: Permission denied" -> err_permission_denied devname
| exn -> err_unknown (Printexc.to_string exn)
| Failure "tun[open]: Permission denied" -> Lwt.fail_with (err_permission_denied devname)
| exn -> Lwt.fail exn

let disconnect t =
log "disconnect %s" t.id;
@@ -23,7 +23,7 @@ include V1.NETWORK
and type buffer = Cstruct.t
and type macaddr = Macaddr.t

val connect : string -> [`Ok of t | `Error of error] io
val connect : string -> t io
(** [connect tap] connects to the given tap interface. *)

val pp_error: Format.formatter -> error -> unit
@@ -20,36 +20,24 @@ open Printf
let run test =
Lwt_main.run (test ())

let err_connect e =
let buf = Buffer.create 10 in
let fmt = Format.formatter_of_buffer buf in
Format.fprintf fmt "didnt connect: %a" Netif.pp_error e;
failwith (Buffer.contents buf)

let test_open () =
Netif.connect "tap0" >>= function
| `Error e -> err_connect e
| `Ok _t ->
printf "connected\n%!";
Lwt.return_unit
Netif.connect "tap0" >>= fun _t ->
printf "connected\n%!";
Lwt.return_unit

let test_close () =
Netif.connect "tap1" >>= function
| `Error e -> err_connect e
| `Ok t ->
printf "connected\n%!";
Netif.disconnect t >>= function () ->
printf "disconnected\n%!";
Lwt.return_unit
Netif.connect "tap1" >>= fun t ->
printf "connected\n%!";
Netif.disconnect t >>= fun () ->
printf "disconnected\n%!";
Lwt.return_unit

let test_write () =
Netif.connect "tap2" >>= function
| `Error e -> err_connect e
| `Ok t ->
let data = Cstruct.create 4096 in
Netif.writev t [ data ] >>= fun () ->
Netif.writev t [ data ; (Cstruct.create 14) ] >>= fun () ->
Lwt.return_unit
Netif.connect "tap2" >>= fun t ->
let data = Cstruct.create 4096 in
Netif.writev t [ data ] >>= fun () ->
Netif.writev t [ data ; (Cstruct.create 14) ] >>= fun () ->
Lwt.return_unit

let suite : Alcotest.test_case list = [
"connect", `Quick, (fun () -> run test_open) ;
ProTip! Use n and p to navigate between commits in a pull request.