From bb8e725c2618ee5d02704519c067d262cfe79193 Mon Sep 17 00:00:00 2001 From: Scott Lystig Fritchie Date: Mon, 22 Jun 2015 18:16:15 +0900 Subject: [PATCH] WIP: 'auth' request placeholders --- src/machi_pb_high_client.erl | 38 +++++++++++++++++++++++++++--- test/machi_pb_high_client_test.erl | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/machi_pb_high_client.erl b/src/machi_pb_high_client.erl index 9e413a4..a9db840 100644 --- a/src/machi_pb_high_client.erl +++ b/src/machi_pb_high_client.erl @@ -30,7 +30,8 @@ -export([start_link/1, quit/1, connected_p/1, - echo/2, echo/3]). + echo/2, echo/3, + auth/3, auth/4]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -56,6 +57,15 @@ echo(PidSpec, String) -> echo(PidSpec, String, Timeout) -> send_sync(PidSpec, {echo, String}, Timeout). +%% TODO: auth() is not implemented. Auth requires SSL, and this client +%% doesn't support SSL yet. This is just a placeholder & reminder. + +auth(PidSpec, User, Pass) -> + auth(PidSpec, User, Pass, ?DEFAULT_TIMEOUT). + +auth(PidSpec, User, Pass, Timeout) -> + send_sync(PidSpec, {auth, User, Pass}, Timeout). + send_sync(PidSpec, Cmd, Timeout) -> gen_server:call(PidSpec, {send_sync, Cmd}, Timeout). @@ -132,8 +142,30 @@ do_send_sync({echo, String}, #state{sock=Sock}=S) -> ok = gen_tcp:send(Sock, Bin1a), {ok, Bin1B} = gen_tcp:recv(Sock, 0), case (catch machi_pb:decode_mpb_response(Bin1B)) of - #mpb_response{req_id=ReqID, echo=Echo} = _R1b -> - {Echo#mpb_echoresp.message, S} + #mpb_response{req_id=ReqID, echo=Echo} when Echo /= undefined -> + {Echo#mpb_echoresp.message, S}; + #mpb_response{req_id=ReqID, generic=G} when G /= undefined -> + #mpb_errorresp{code=Code, msg=Msg, extra=Extra} = G, + {{error, {Code, Msg, Extra}}, S} + end + catch X:Y -> + Res = {bummer, {X, Y, erlang:get_stacktrace()}}, + {Res, S} + end; +do_send_sync({auth, User, Pass}, #state{sock=Sock}=S) -> + try + ReqID = <<0>>, + R1a = #mpb_request{req_id=ReqID, + auth=#mpb_authreq{user=User, password=Pass}}, + Bin1a = machi_pb:encode_mpb_request(R1a), + ok = gen_tcp:send(Sock, Bin1a), + {ok, Bin1B} = gen_tcp:recv(Sock, 0), + case (catch machi_pb:decode_mpb_response(Bin1B)) of + #mpb_response{req_id=ReqID, auth=Auth} when Auth /= undefined -> + {Auth#mpb_authresp.code, S}; + #mpb_response{req_id=ReqID, generic=G} when G /= undefined -> + #mpb_errorresp{code=Code, msg=Msg, extra=Extra} = G, + {{error, {Code, Msg, Extra}}, S} end catch X:Y -> Res = {bummer, {X, Y, erlang:get_stacktrace()}}, diff --git a/test/machi_pb_high_client_test.erl b/test/machi_pb_high_client_test.erl index 7b552b9..aa1393e 100644 --- a/test/machi_pb_high_client_test.erl +++ b/test/machi_pb_high_client_test.erl @@ -52,6 +52,10 @@ smoke_test2() -> true = ?C:connected_p(Clnt), String = "yo, dawg", String = ?C:echo(Clnt, String), + %% TODO: auth() is not implemented. Auth requires SSL. + %% Probably ought to put client stuff that relies on SSL into + %% a separate test module? Or separate test func? + {error, _} = ?C:auth(Clnt, "foo", "bar"), ok after