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

Allow errors in TCP.start_protocol/1 pipeline to be returned. #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/romeo/transports/tcp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ defmodule Romeo.Transports.TCP do
end)
end

defp negotiate_features({:error, _} = error), do: error
defp negotiate_features(%Conn{} = conn) do
recv(conn, fn conn, xmlel(name: "stream:features") = packet ->
%{conn | features: Features.parse_stream_features(packet)}
end)
end

defp maybe_start_tls({:error, _} = error), do: error
defp maybe_start_tls(%Conn{features: %Features{tls?: true}} = conn) do
conn
|> send(Stanza.start_tls)
Expand All @@ -93,6 +95,7 @@ defmodule Romeo.Transports.TCP do
end
defp maybe_start_tls(%Conn{} = conn), do: conn

defp upgrade_to_tls({:error, _} = error), do: error
defp upgrade_to_tls(%Conn{parser: parser, socket: {:gen_tcp, socket}} = conn) do
Logger.info fn -> "Negotiating secure connection" end

Expand All @@ -103,6 +106,7 @@ defmodule Romeo.Transports.TCP do
%{conn | socket: {:ssl, socket}, parser: parser}
end

defp authenticate({:error, _} = error), do: error
defp authenticate(%Conn{} = conn) do
conn
|> Romeo.Auth.authenticate!
Expand All @@ -111,10 +115,12 @@ defmodule Romeo.Transports.TCP do
|> negotiate_features
end

defp handshake({:error, _} = error), do: error
defp handshake(%Conn{} = conn) do
Romeo.Auth.handshake!(conn)
end

defp bind({:error, _} = error), do: error
defp bind(%Conn{owner: owner, resource: resource} = conn) do
stanza = Romeo.Stanza.bind(resource)
id = Romeo.XML.attr(stanza, "id")
Expand All @@ -138,6 +144,7 @@ defmodule Romeo.Transports.TCP do
end)
end

defp session({:error, _} = error), do: error
defp session(%Conn{} = conn) do
stanza = Romeo.Stanza.session
id = Romeo.XML.attr(stanza, "id")
Expand All @@ -153,11 +160,13 @@ defmodule Romeo.Transports.TCP do
end)
end

defp ready({:error, _} = error), do: error
defp ready(%Conn{owner: owner} = conn) do
Kernel.send(owner, :connection_ready)
{:ok, conn}
end

defp reset_parser({:error, _} = error), do: error
defp reset_parser(%Conn{parser: parser} = conn) do
parser = :fxml_stream.reset(parser)
%{conn | parser: parser}
Expand Down Expand Up @@ -193,11 +202,14 @@ defmodule Romeo.Transports.TCP do
def send(%Conn{jid: jid, socket: {mod, socket}} = conn, stanza) do
stanza = Romeo.XML.encode!(stanza)
Logger.debug fn -> "[#{jid}][OUTGOING] #{inspect stanza}" end
:ok = mod.send(socket, stanza)
{:ok, conn}
case mod.send(socket, stanza) do
:ok -> {:ok, conn}
{:error, _} = error -> error
end
end

def recv({:ok, conn}, fun), do: recv(conn, fun)
def recv({:error, _} = error, _fun), do: error
def recv(%Conn{socket: {:gen_tcp, socket}, timeout: timeout} = conn, fun) do
receive do
{:xmlstreamelement, stanza} ->
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ defmodule Romeo.Mixfile do

# Test deps
{:ejabberd, "~> 16.8", only: :test},
{:mock, "~> 0.3.1", only: :test},
{:excoveralls, "~> 0.5", only: :test}]
end

Expand Down
20 changes: 11 additions & 9 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
%{"cache_tab": {:hex, :cache_tab, "1.0.4", "3fd2b1ab40c36e7830a4e09e836c6b0fa89191cd4e5fd471873e4eb42f5cd37c", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [:rebar3], []},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"certifi": {:hex, :certifi, "0.7.0", "861a57f3808f7eb0c2d1802afeaae0fa5de813b0df0979153cbafcd853ababaf", [], []},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [], []},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [], []},
"ejabberd": {:hex, :ejabberd, "16.8.0", "9146b895864e01757319d68bb5e13ce9baedceb66e64ea71e1b780c36c836fc7", [], [{:cache_tab, "~> 1.0", [hex: :cache_tab, optional: false]}, {:eredis, "~> 1.0", [hex: :eredis, optional: false]}, {:esip, "~> 1.0", [hex: :esip, optional: false]}, {:ezlib, "~> 1.0", [hex: :ezlib, optional: false]}, {:fast_tls, "~> 1.0", [hex: :fast_tls, optional: false]}, {:fast_xml, "~> 1.1", [hex: :fast_xml, optional: false]}, {:fast_yaml, "~> 1.0", [hex: :fast_yaml, optional: false]}, {:iconv, "~> 1.0", [hex: :iconv, optional: false]}, {:jiffy, "~> 0.14.7", [hex: :jiffy, optional: false]}, {:lager, "~> 3.2", [hex: :lager, optional: false]}, {:p1_mysql, "~> 1.0", [hex: :p1_mysql, optional: false]}, {:p1_oauth2, "~> 0.6.1", [hex: :p1_oauth2, optional: false]}, {:p1_pgsql, "~> 1.1", [hex: :p1_pgsql, optional: false]}, {:p1_utils, "~> 1.0", [hex: :p1_utils, optional: false]}, {:p1_xmlrpc, "~> 1.15", [hex: :p1_xmlrpc, optional: false]}, {:sqlite3, "~> 1.1", [hex: :sqlite3, optional: false]}, {:stringprep, "~> 1.0", [hex: :stringprep, optional: false]}, {:stun, "~> 1.0", [hex: :stun, optional: false]}]},
"eredis": {:hex, :eredis, "1.0.8", "ab4fda1c4ba7fbe6c19c26c249dc13da916d762502c4b4fa2df401a8d51c5364", [], []},
"esip": {:hex, :esip, "1.0.8", "69885a6c07964aabc6c077fe1372aa810a848bd3d9a415b160dabdce9c7a79b5", [], [{:fast_tls, "1.0.7", [hex: :fast_tls, optional: false]}, {:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}, {:stun, "1.0.7", [hex: :stun, optional: false]}]},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.7", "5d26e4a7cdf08294217594a1b0643636accc2ad30e984d62f1d166f70629ff50", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"ex_doc": {:hex, :ex_doc, "0.14.3", "e61cec6cf9731d7d23d254266ab06ac1decbb7651c3d1568402ec535d387b6f7", [], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"excoveralls": {:hex, :excoveralls, "0.5.7", "5d26e4a7cdf08294217594a1b0643636accc2ad30e984d62f1d166f70629ff50", [], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"ezlib": {:hex, :ezlib, "1.0.1", "add8b2770a1a70c174aaea082b4a8668c0c7fdb03ee6cc81c6c68d3a6c3d767d", [], []},
"fast_tls": {:hex, :fast_tls, "1.0.7", "9b72ecfcdcad195ab072c196fab8334f49d8fea76bf1a51f536d69e7527d902a", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"fast_xml": {:hex, :fast_xml, "1.1.15", "6d23eb7f874e1357cf80a48d75a7bd0c8f6318029dc4b70122e9f54911f57f83", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"fast_yaml": {:hex, :fast_yaml, "1.0.6", "3fe6feb7935ae8028b337e53e1db29e73ad3bca8041108f6a8f73b7175ece75c", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"goldrush": {:hex, :goldrush, "0.1.8", "2024ba375ceea47e27ea70e14d2c483b2d8610101b4e852ef7f89163cdb6e649", [], []},
"hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"iconv": {:hex, :iconv, "1.0.2", "a0792f06ab4b5ea1b5bb49789405739f1281a91c44cf3879cb70e4d777666217", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [], []},
"jiffy": {:hex, :jiffy, "0.14.7", "9f33b893edd6041ceae03bc1e50b412e858cc80b46f3d7535a7a9940a79a1c37", [], []},
"jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [:mix, :rebar], []},
"jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [], []},
"lager": {:hex, :lager, "3.2.1", "eef4e18b39e4195d37606d9088ea05bf1b745986cf8ec84f01d332456fe88d17", [], [{:goldrush, "0.1.8", [hex: :goldrush, optional: false]}]},
"meck": {:hex, :meck, "0.8.8", "eeb3efe811d4346e1a7f65b2738abc2ad73cbe1a2c91b5dd909bac2ea0414fa6", [], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], []},
"mock": {:hex, :mock, "0.3.1", "994f00150f79a0ea50dc9d86134cd9ebd0d177ad60bd04d1e46336cdfdb98ff9", [], [{:meck, "~> 0.8.8", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm"},
"p1_mysql": {:hex, :p1_mysql, "1.0.1", "d2be1cfc71bb4f1391090b62b74c3f5cb8e7a45b0076b8cb290cd6b2856c581b", [], []},
"p1_oauth2": {:hex, :p1_oauth2, "0.6.1", "4e021250cc198c538b097393671a41e7cebf463c248980320e038fe0316eb56b", [], []},
"p1_pgsql": {:hex, :p1_pgsql, "1.1.0", "ca525c42878eac095e5feb19563acc9915c845648f48fdec7ba6266c625d4ac7", [], []},
"p1_utils": {:hex, :p1_utils, "1.0.5", "3e698354fdc1fea5491d991457b0cb986c0a00a47d224feb841dc3ec82b9f721", [], []},
"p1_xmlrpc": {:hex, :p1_xmlrpc, "1.15.1", "a382b62dc21bb372281c2488f99294d84f2b4020ed0908a1c4ad710ace3cf35a", [], []},
"sqlite3": {:hex, :sqlite3, "1.1.5", "794738b6d07b6d36ec6d42492cb9d629bad9cf3761617b8b8d728e765db19840", [], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], []},
"stringprep": {:hex, :stringprep, "1.0.6", "1cf1c439eb038aa590da5456e019f86afbfbfeb5a2d37b6e5f873041624c6701", [], [{:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]},
"stun": {:hex, :stun, "1.0.7", "904dc6f26a3c30c54881c4c3003699f2a4968067ee6b3aecdf9895aad02df75e", [], [{:fast_tls, "1.0.7", [hex: :fast_tls, optional: false]}, {:p1_utils, "1.0.5", [hex: :p1_utils, optional: false]}]}}
Empty file.
24 changes: 24 additions & 0 deletions test/romeo/transports/tcp_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Romeo.Transports.TCPTest do
use ExUnit.Case
import Mock

use UserHelper

setup do
{:ok, romeo: build_user("romeo")}
end

test_with_mock "error on connect", %{romeo: romeo},
:gen_tcp, [:unstick], [connect: fn _, _, _, _ -> {:error, :kaboom} end] do
{:ok, pid} = Romeo.Connection.start_link(romeo)

assert {:error, _} = Connection.call(pid, "anything")
end

test_with_mock "error on send", %{romeo: romeo},
:gen_tcp, [:passthrough, :unstick], [send: fn _, _ -> {:error, :kaboom} end] do
{:ok, pid} = Romeo.Connection.start_link(romeo)

assert {:error, _} = Romeo.Connection.send(pid, "anything")
end
end