From 444e87e58f71d8a207988d98556233d10fa7c818 Mon Sep 17 00:00:00 2001 From: Christopher Date: Sun, 22 Mar 2020 21:28:07 -0500 Subject: [PATCH] expect structs for messages --- lib/client/authentication.ex | 10 +- lib/client/session.ex | 22 +-- mix.exs | 2 +- mix.lock | 2 +- test/support/test_callee.ex | 19 +-- test/wampex_test.exs | 293 ++--------------------------------- 6 files changed, 40 insertions(+), 308 deletions(-) diff --git a/lib/client/authentication.ex b/lib/client/authentication.ex index ebf4a81..511fec1 100644 --- a/lib/client/authentication.ex +++ b/lib/client/authentication.ex @@ -2,11 +2,12 @@ defmodule Wampex.Client.Authentication do @moduledoc false alias Wampex.Crypto + alias Wampex.Roles.Peer.Challenge @enforce_keys [:authid, :authmethods, :secret] defstruct [:authid, :authmethods, :secret] - @callback handle(challenge :: binary(), auth :: __MODULE__.t()) :: {binary(), map()} + @callback handle(challenge :: Challenge.t(), auth :: __MODULE__.t()) :: {binary(), map()} @type t :: %__MODULE__{ authid: binary(), @@ -15,7 +16,10 @@ defmodule Wampex.Client.Authentication do } def handle( - {"wampcra", %{"challenge" => ch, "salt" => salt, "iterations" => it, "keylen" => len}}, + %Challenge{ + auth_method: "wampcra", + extra: %{"challenge" => ch, "salt" => salt, "iterations" => it, "keylen" => len} + }, auth ) do {auth @@ -24,7 +28,7 @@ defmodule Wampex.Client.Authentication do |> Crypto.hash_challenge(ch), %{}} end - def handle({"wampcra", %{"challenge" => ch}}, auth) do + def handle(%Challenge{auth_method: "wampcra", extra: %{"challenge" => ch}}, auth) do {auth |> Map.get(:secret) |> Crypto.hash_challenge(ch), %{}} diff --git a/lib/client/session.ex b/lib/client/session.ex index 9bdfc45..de3d9c0 100644 --- a/lib/client/session.ex +++ b/lib/client/session.ex @@ -9,7 +9,9 @@ defmodule Wampex.Client.Session do alias StatesLanguage, as: SL alias Wampex.Client.Realm alias Wampex.Roles.Peer - alias Wampex.Roles.Peer.{Authenticate, Hello} + alias Wampex.Roles.Peer.{Authenticate, Challenge, Hello} + alias Wampex.Roles.Dealer.Invocation + alias Wampex.Roles.Broker.Event alias Wampex.Serializers.MessagePack alias __MODULE__, as: Sess alias Wampex.Client @@ -48,13 +50,13 @@ defmodule Wampex.Client.Session do url: binary(), transport_pid: pid() | module() | nil, message: Wampex.message() | nil, - event: Wampex.event() | nil, - invocation: Wampex.invocation() | nil, + event: Event.t() | nil, + invocation: Invocation.t() | nil, goodbye: binary() | nil, realm: Realm.t(), name: module() | nil, roles: [module()], - challenge: {binary(), binary()} | nil, + challenge: Challenge.t() | nil, interrupt: integer() | nil, message_queue: [], request_id: integer(), @@ -220,7 +222,7 @@ defmodule Wampex.Client.Session do data: %Sess{ transport: tt, transport_pid: t, - challenge: challenge, + challenge: %Challenge{} = challenge, realm: %Realm{authentication: auth} } } = sl @@ -298,13 +300,13 @@ defmodule Wampex.Client.Session do @handle_event, _, @event, - %SL{data: %Sess{name: name, event: event}} = sl + %SL{data: %Sess{name: name, event: %Event{subscription_id: sub_id} = event}} = sl ) do Logger.debug("Received Event #{inspect(event)}") sub = Client.subscriber_registry_name(name) - Registry.dispatch(sub, elem(event, 1), fn entries -> + Registry.dispatch(sub, sub_id, fn entries -> for {pid, _topic} <- entries, do: send(pid, event) end) @@ -316,11 +318,13 @@ defmodule Wampex.Client.Session do @handle_invocation, _, @invocation, - %SL{data: %Sess{name: name, invocation: invocation}} = sl + %SL{ + data: %Sess{name: name, invocation: %Invocation{registration_id: reg_id} = invocation} + } = sl ) do reg = Client.callee_registry_name(name) - Registry.dispatch(reg, elem(invocation, 2), fn entries -> + Registry.dispatch(reg, reg_id, fn entries -> for {pid, _procedure} <- entries, do: send(pid, invocation) end) diff --git a/mix.exs b/mix.exs index e211406..044cffe 100644 --- a/mix.exs +++ b/mix.exs @@ -47,7 +47,7 @@ defmodule Wampex.Client.MixProject do {:states_language, "~> 0.2"}, {:wampex, git: "https://gitlab.com/entropealabs/wampex.git", - tag: "fef406d7efd7d8d5d49e49951da802280a3d43b6"}, + tag: "5aaf4bf2dd9b69058ba2dc9c8c3f7919748b933a"}, {:websockex, "~> 0.4.2"} ] end diff --git a/mix.lock b/mix.lock index 3f016ed..5284eaa 100644 --- a/mix.lock +++ b/mix.lock @@ -24,7 +24,7 @@ "states_language": {:hex, :states_language, "0.2.8", "f9dfd3c0bd9a9d7bda25ef315f2d90944cd6b2022a7f3c403deb1d4ec451825e", [:mix], [{:elixpath, "~> 0.1.0", [hex: :elixpath, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:json_xema, "~> 0.4.0", [hex: :json_xema, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xema, "~> 0.11.0", [hex: :xema, repo: "hexpm", optional: false]}], "hexpm", "a5231691e7cb37fe32dc7de54c2dc86d1d60e84c4f0379f3246e55be2a85ec78"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"}, - "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "7c5d257937ace7776f61ad63f8590c24ef5e382a", [tag: "7c5d257937ace7776f61ad63f8590c24ef5e382a"]}, + "wampex": {:git, "https://gitlab.com/entropealabs/wampex.git", "5aaf4bf2dd9b69058ba2dc9c8c3f7919748b933a", [tag: "5aaf4bf2dd9b69058ba2dc9c8c3f7919748b933a"]}, "websockex": {:hex, :websockex, "0.4.2", "9a3b7dc25655517ecd3f8ff7109a77fce94956096b942836cdcfbc7c86603ecc", [:mix], [], "hexpm", "803cd76e91544b56f0e655e36790be797fa6436db9224f7c303db9b9df2a3df4"}, "xema": {:hex, :xema, "0.11.0", "7b5118418633cffc27092110d02d4faeea938149dd3f6c64299e41e747067e80", [:mix], [{:conv_case, "~> 0.2.2", [hex: :conv_case, repo: "hexpm", optional: false]}, {:decimal, "~> 1.7", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "51491c9a953d65069d4b30aa2f70bc45ff99fd1bc3345bc72ce4e644d01ea14e"}, } diff --git a/test/support/test_callee.ex b/test/support/test_callee.ex index 71ec326..6a90243 100644 --- a/test/support/test_callee.ex +++ b/test/support/test_callee.ex @@ -3,8 +3,9 @@ defmodule TestCallee do use GenServer require Logger alias Wampex.Client - alias Wampex.Roles.Callee + alias Wampex.Roles.{Callee, Dealer} alias Callee.{Register, Unregister, Yield} + alias Dealer.Invocation def start_link(test, name, device) do GenServer.start_link(__MODULE__, {test, name, device}) @@ -16,8 +17,11 @@ defmodule TestCallee do {:ok, {test, name, reg}} end - def handle_info({:invocation, id, _, _, _, arg_kw} = invocation, {test, name, _reg} = state) do - Logger.info("Got invocation #{inspect(invocation)}") + def handle_info( + %Invocation{request_id: id, details: dets, arg_kw: arg_kw} = invocation, + {test, name, _reg} = state + ) do + Logger.info("Got invocation #{inspect(dets)}") send(test, invocation) @@ -32,13 +36,4 @@ defmodule TestCallee do {:noreply, state} end - - def terminate(_reason, {_test, name, reg}) do - Client.send_request( - name, - Callee.unregister(%Unregister{registration_id: reg}) - ) - - :ok - end end diff --git a/test/wampex_test.exs b/test/wampex_test.exs index 3544821..b58aeb3 100644 --- a/test/wampex_test.exs +++ b/test/wampex_test.exs @@ -19,267 +19,15 @@ defmodule WampexTest do @url "ws://localhost:4000/ws" @authid "admin" - @auth_password "test1234" - @realm_uri "org.entropealabs.admin" + @auth_password "bnASR5qF9y8k/sHF6S+NneCOhvVI0zFkvoKQpc2F+hA=" + @realm_uri "admin" @auth %Authentication{authid: @authid, authmethods: ["wampcra"], secret: @auth_password} @realm %Realm{name: @realm_uri, authentication: @auth} @roles [Callee, Caller, Publisher, Subscriber] @device "as987d9a8sd79a87ds" - setup_all do - topologies = Application.get_env(:cluster_kv, :topologies) - - [ - server: - Router.start_link( - name: TestRouter, - port: 4000, - topologies: topologies, - replicas: 1, - quorum: 1, - admin_realm: @realm_uri, - admin_authid: @authid, - admin_password: @auth_password - ) - ] - end - @session %Session{url: @url, realm: @realm, roles: @roles} - @tag :client - test "session_name" do - assert Test.Session = Client.session_name(Test) - end - - @tag :client - test "subscriber_registry_name" do - assert Test.SubscriberRegistry = Client.subscriber_registry_name(Test) - end - - @tag :client - test "callee_registry_name" do - assert Test.CalleeRegistry = Client.callee_registry_name(Test) - end - - @tag :client - test "transport_name" do - assert Test.Transport = Client.transport_name(Test) - end - - @tag :client - test "Callee.add" do - assert %{callee: %{}} = Callee.add(%{}) - end - - @tag :client - test "Callee.register" do - assert [64, %{}, "test"] = Callee.register(%Register{procedure: "test"}) - end - - @tag :client - test "Callee.unregister" do - assert [66, 123_456] = Callee.unregister(%Unregister{registration_id: 123_456}) - end - - @tag :client - test "Callee.yield" do - assert [70, 1234, %{}, [], %{}] = Callee.yield(%Yield{request_id: 1234}) - assert [70, 1234, %{}, ["1"], %{}] = Callee.yield(%Yield{request_id: 1234, arg_list: ["1"]}) - - assert [70, 1234, %{test: 1}, [2], %{}] = - Callee.yield(%Yield{ - request_id: 1234, - arg_list: [2], - arg_kw: %{}, - options: %{test: 1} - }) - end - - @tag :client - test "Caller.add" do - assert %{caller: %{}} = Caller.add(%{}) - end - - @tag :client - test "Caller.call" do - assert [48, %{}, "test", [], %{}] = Caller.call(%Call{procedure: "test"}) - assert [48, %{}, "test", [1], %{}] = Caller.call(%Call{procedure: "test", arg_list: [1]}) - - assert [48, %{ok: 1}, "test", [1], %{test: 1}] = - Caller.call(%Call{ - procedure: "test", - arg_list: [1], - arg_kw: %{test: 1}, - options: %{ok: 1} - }) - end - - @tag :client - test "Peer.add" do - assert %{} = Caller.add(%{}) - end - - @tag :client - test "Peer.hello" do - assert [1, "test", %{agent: "WAMPex", roles: %{callee: %{}}}] = - Peer.hello(%Hello{realm: "test", roles: [Callee]}) - end - - @tag :client - test "Peer.authenticate" do - assert [5, "a-signa-ture", %{}] == - Peer.authenticate(%Authenticate{signature: "a-signa-ture", extra: %{}}) - end - - @tag :client - test "Peer.gooodbye" do - assert [6, %{}, "test"] = Peer.goodbye(%Goodbye{reason: "test"}) - end - - @tag :client - test "Publisher.add" do - assert %{publisher: %{}} = Publisher.add(%{}) - end - - @tag :client - test "Publisher.publish" do - assert [16, %{}, "test", [], %{}] = Publisher.publish(%Publish{topic: "test"}) - assert [16, %{}, "test", [1], %{}] = Publisher.publish(%Publish{topic: "test", arg_list: [1]}) - - assert [16, %{test: 2}, "test", [1], %{test: 1}] = - Publisher.publish(%Publish{ - topic: "test", - arg_list: [1], - arg_kw: %{test: 1}, - options: %{test: 2} - }) - end - - @tag :client - test "Subscriber.add" do - assert %{subscriber: %{}} = Subscriber.add(%{}) - end - - @tag :client - test "Subscriber.subscribe" do - assert [32, %{test: 1}, "test"] = - Subscriber.subscribe(%Subscribe{topic: "test", options: %{test: 1}}) - end - - @tag :client - test "Subscriber.unsubscribe" do - assert [34, 1234] = Subscriber.unsubscribe(%Unsubscribe{subscription_id: 1234}) - end - - @tag :router - test "Dealer.add" do - assert %{dealer: %{}} = Dealer.add(%{}) - end - - @tag :router - test "Dealer.registered" do - assert [65, 123_456, 765_432] = - Dealer.registered(%Registered{request_id: 123_456, registration_id: 765_432}) - end - - @tag :router - test "Dealer.unregistered" do - assert [67, 123_456] = Dealer.unregistered(%Unregistered{request_id: 123_456}) - end - - @tag :router - test "Dealer.result" do - assert [50, 1234, %{}, [], %{}] = Dealer.result(%Result{request_id: 1234}) - assert [50, 1234, %{}, ["1"], %{}] = Dealer.result(%Result{request_id: 1234, arg_list: ["1"]}) - - assert [50, 1234, %{test: 1}, [2], %{}] = - Dealer.result(%Result{ - request_id: 1234, - arg_list: [2], - arg_kw: %{}, - options: %{test: 1} - }) - end - - @tag :router - test "Dealer.invocation" do - assert [68, 1234, 1234, %{}, [], %{}] = - Dealer.invocation(%Invocation{request_id: 1234, registration_id: 1234}) - - assert [68, 1234, 1234, %{}, ["1"], %{}] = - Dealer.invocation(%Invocation{ - request_id: 1234, - registration_id: 1234, - arg_list: ["1"] - }) - - assert [68, 1234, 1234, %{test: 1}, [2], %{test: 2}] = - Dealer.invocation(%Invocation{ - request_id: 1234, - registration_id: 1234, - arg_list: [2], - arg_kw: %{test: 2}, - options: %{test: 1} - }) - end - - @tag :router - test "Broker.add" do - assert %{broker: %{}} = Broker.add(%{}) - end - - @tag :router - test "Broker.event" do - assert [36, 123_456, 765_432, %{}, [], %{}] = - Broker.event(%Event{subscription_id: 123_456, publication_id: 765_432}) - - assert [36, 123_456, 765_432, %{}, [2], %{}] = - Broker.event(%Event{subscription_id: 123_456, publication_id: 765_432, arg_list: [2]}) - - assert [36, 123_456, 765_432, %{}, [2], %{test: 1}] = - Broker.event(%Event{ - subscription_id: 123_456, - publication_id: 765_432, - arg_list: [2], - arg_kw: %{test: 1} - }) - end - - @tag :router - test "Broker.subscribed" do - assert [33, 123_456, 123_456] = - Broker.subscribed(%Subscribed{request_id: 123_456, subscription_id: 123_456}) - end - - @tag :router - test "Broker.unsubscribed" do - assert [35, 123_456] = Broker.unsubscribed(%Unsubscribed{request_id: 123_456}) - end - - @tag :router - test "Broker.published" do - assert [17, 123_456, 654_321] = - Broker.published(%Published{request_id: 123_456, publication_id: 654_321}) - end - - @tag :client - test "MessagePack Serializer" do - assert :binary = MessagePack.data_type() - assert "\x93\x05\xC4\t123456789\x80" = MessagePack.serialize!([5, "123456789", %{}]) - assert {:ok, "\x93\x05\xC4\t123456789\x80"} = MessagePack.serialize([5, "123456789", %{}]) - assert [5, "123456789", %{}] = MessagePack.deserialize!("\x93\x05\xC4\t123456789\x80") - assert {:ok, [5, "123456789", %{}]} = MessagePack.deserialize("\x93\x05\xC4\t123456789\x80") - end - - @tag :client - test "JSON Serializer" do - assert :text = JSON.data_type() - assert "[5,\"123456789\",{}]" = JSON.serialize!([5, "123456789", %{}]) - assert {:ok, "[5,\"123456789\",{}]"} = JSON.serialize([5, "123456789", %{}]) - assert [5, "123456789", %{}] = JSON.deserialize!("[5,\"123456789\",{}]") - assert {:ok, [5, "123456789", %{}]} = JSON.deserialize("[5,\"123456789\",{}]") - end - @tag :client test "callee registration" do name = TestCalleeRegistration @@ -288,25 +36,6 @@ defmodule WampexTest do assert_receive {:registered, id} end - @tag :router - test "get wildcard key" do - subscriber_name = TestSubscriber - topic = ".test.light..status" - {:ok, _pid} = Client.start_link(name: subscriber_name, session: @session) - - assert {:ok, id} = - Client.send_request( - subscriber_name, - Subscriber.subscribe(%Subscribe{topic: topic, options: %{match: "wildcard"}}) - ) - - assert :ok = - Client.send_request( - subscriber_name, - Subscriber.unsubscribe(%Unsubscribe{subscription_id: id}) - ) - end - @tag :abort test "abort" do Process.flag(:trap_exit, true) @@ -339,11 +68,11 @@ defmodule WampexTest do caller_name = TestAdminCaller Client.start_link(name: caller_name, session: @session) - {:ok, %{}, [id], %{}} = + %Result{arg_list: [id]} = Client.send_request( caller_name, Caller.call(%Call{ - procedure: "admin.create_user", + procedure: "admin.create_peer", arg_kw: %{authid: "chris", password: "woot!", realm: @realm_uri} }) ) @@ -360,7 +89,7 @@ defmodule WampexTest do Client.send_request( caller_name, Caller.call(%Call{ - procedure: "admin.create_user", + procedure: "admin.create_peer", arg_kw: %{authid: "chris", password: "woot!", realm: "not.real"} }) ) @@ -371,11 +100,11 @@ defmodule WampexTest do callee_name = TestCalleeRespond Client.start_link(name: callee_name, session: @session) TestCallee.start_link(self(), callee_name, @device) - assert_receive {:registered, id} + assert_receive {_, _} caller_name = TestCaller Client.start_link(name: caller_name, session: @session) - {:ok, %{}, ["ok"], %{"color" => "#FFFFFF"}} = + %Result{arg_list: ["ok"], arg_kw: %{"color" => "#FFFFFF"}} = Client.send_request( caller_name, Caller.call(%Call{ @@ -385,7 +114,7 @@ defmodule WampexTest do }) ) - assert_receive {:invocation, _, _, _, _, _} + assert_receive %Invocation{} end @tag :client @@ -414,8 +143,8 @@ defmodule WampexTest do }) ) - assert_receive {:event, _, _, _, _, _}, 2000 - assert_receive {:event, _, _, _, _, _}, 2000 - assert_receive {:event, _, _, _, _, _}, 2000 + assert_receive %Event{}, 2000 + assert_receive %Event{}, 2000 + assert_receive %Event{}, 2000 end end -- 2.45.3