Skip to content

Commit

Permalink
Update tests to new Awa.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
reynir committed Sep 18, 2024
1 parent f98de92 commit 5fdd2d6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
46 changes: 31 additions & 15 deletions test/awa_test_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ module Driver = struct
| None -> poll t
| Some event -> Ok (t, event)

let user_auth t userauth success =
let* server, reply =
if success then
Awa.Server.accept_userauth t.server userauth
else
Awa.Server.reject_userauth t.server userauth
in
send_msg { t with server } reply

let send_channel_data t id data =
let* server, msgs = Server.output_channel_data t.server id data in
send_msgs { t with server } msgs
Expand Down Expand Up @@ -133,10 +142,13 @@ let bc t id data =
in
Driver.send_channel_data t id (Cstruct.of_string reply)

let rec serve t cmd =
let rec serve t user_auth cmd =
let open Server in
let* t, poll_result = Driver.poll t in
match poll_result with
| Userauth (username, userauth) ->
let* t = Driver.user_auth t userauth (user_auth username userauth) in
serve t user_auth cmd
| Disconnected s ->
Logs.info (fun m -> m "Disconnected: %s" s);
Ok ()
Expand All @@ -146,17 +158,17 @@ let rec serve t cmd =
| Channel_data (id, data) ->
Logs.info (fun m -> m "channel data %d" (Cstruct.length data));
(match cmd with
| None -> serve t cmd
| None -> serve t user_auth cmd
| Some "echo" ->
if (Cstruct.to_string data) = "rekey\n" then
let* t = Driver.rekey t in
serve t cmd
serve t user_auth cmd
else
let* t = echo t id data in
serve t cmd
serve t user_auth cmd
| Some "bc" ->
let* t = bc t id data in
serve t cmd
serve t user_auth cmd
| _ -> Error "Unexpected cmd")
| Channel_subsystem (id, exec) (* same as exec *)
| Channel_exec (id, exec) ->
Expand All @@ -170,16 +182,16 @@ let rec serve t cmd =
let* _ = Driver.disconnect t in
Logs.info (fun m -> m "sent pong");
Ok ()
| "echo" | "bc" as c -> serve t (Some c)
| "echo" | "bc" as c -> serve t user_auth (Some c)
| _ ->
let msg = Printf.sprintf "Unknown command %s" exec in
let* t = Driver.send_channel_data t id (Cstruct.of_string msg) in
Logs.info (fun m -> m "%s" msg);
let* t = Driver.disconnect t in
serve t cmd end
serve t user_auth cmd end
| Set_env (k, v) ->
Logs.info (fun m -> m "Ignoring Set_env (%S, %S)" k v);
serve t cmd
serve t user_auth cmd
| Pty _ | Pty_set _ ->
let msg =
Ssh.disconnect_msg Ssh.DISCONNECT_SERVICE_NOT_AVAILABLE
Expand All @@ -195,29 +207,33 @@ let rec serve t cmd =
let* _ = Driver.send_msg t msg in
Ok ()

let user_db =
(* User foo auths by passoword *)
let foo = Auth.make_user "foo" ~password:"bar" [] in
let user_auth =
(* User awa auths by pubkey *)
let fd = Unix.(openfile "test/data/awa_test_rsa.pub" [O_RDONLY] 0) in
let file_buf = Unix_cstruct.of_fd fd in
let key = Result.get_ok (Wire.pubkey_of_openssh file_buf) in
Unix.close fd;
let awa = Auth.make_user "awa" [ key ] in
[ foo; awa ]
fun user userauth ->
match user, userauth with
| "foo", Awa.Server.Password "bar" ->
true
| "awa", Awa.Server.Pubkey pubkeyauth ->
Awa.Server.verify_pubkeyauth ~user:"awa" pubkeyauth &&
Awa.Server.pubkey_of_pubkeyauth pubkeyauth = key
| _ -> false

let rec wait_connection priv_key listen_fd server_port =
Logs.info (fun m -> m "Awa server waiting connections on port %d" server_port);
let client_fd, _ = Unix.(accept listen_fd) in
Logs.info (fun m -> m "Client connected!");
let server, msgs = Server.make priv_key user_db in
let server, msgs = Server.make priv_key in
let* t =
Driver.of_server server msgs
(write_cstruct client_fd)
(read_cstruct client_fd)
Mtime_clock.now
in
let () = match serve t None with
let () = match serve t user_auth None with
| Ok () -> Logs.info (fun m -> m "Client finished")
| Error e -> Logs.warn (fun m -> m "error: %s" e)
in
Expand Down
4 changes: 2 additions & 2 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ let t_mpint () =
test_ok

let t_version () =
let t, _ = Server.make (Hostkey.Rsa_priv (Mirage_crypto_pk.Rsa.generate ~bits:2048 ())) [] in
let t, _ = Server.make (Hostkey.Rsa_priv (Mirage_crypto_pk.Rsa.generate ~bits:2048 ())) in
let client_version = "SSH-2.0-OpenSSH_6.9\r\n" in
let* t, msg, input_buffer =
Server.pop_msg2 t (Cstruct.of_string client_version)
Expand Down Expand Up @@ -410,7 +410,7 @@ let t_signature () =
test_ok

let t_ignore_next_packet () =
let t, _ = Server.make (Hostkey.Rsa_priv (Mirage_crypto_pk.Rsa.generate ~bits:2048 ())) [] in
let t, _ = Server.make (Hostkey.Rsa_priv (Mirage_crypto_pk.Rsa.generate ~bits:2048 ())) in
let t = Server.{ t with client_version = Some "SSH-2.0-client";
expect = Some(Ssh.MSG_KEXINIT) }
in
Expand Down

0 comments on commit 5fdd2d6

Please sign in to comment.