forked from tpitale/coap_ex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the SocketServer attempts to decode messages there is the possibility of error caused by bad data. This commit adapts the code so that any exceptions caused by decoding are captured, and a warning is logged, and the process is stopped without causing an error. The types of errors seen were: - bad return value: {:error, "" is not repeatable""} - (CondClauseError) no cond clause evaluated to a truthy value (coap_ex 0.1.0) lib/coap/message_options.ex - (CaseClauseError) no case clause matching <<...>> (coap_ex 0.1.0) lib/coap/message_options.ex:54 - (FunctionClauseError) no function clause matching in CoAP.Block.decode/1 - (MatchError) no match of right hand side value: """" One could argue that these "errors" should be fixed by updating the MessageOptions module to gracefully handle such conditions, and that is definitely worth considering. By doing that, we would then be able to complete the decoding, which would allow extracting the message token which is required to identify the associated connection process.
- Loading branch information
Showing
5 changed files
with
123 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
use Mix.Config | ||
import Mix.Config | ||
|
||
import_config "#{Mix.env()}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
use Mix.Config | ||
import Mix.Config | ||
|
||
config :logger, level: :info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule CoAP.SocketServerTest do | ||
use ExUnit.Case | ||
|
||
import ExUnit.CaptureLog | ||
|
||
alias CoAP.SocketServer | ||
alias CoAP.Adapters.GenericServer | ||
|
||
defmodule FakeEndpoint do | ||
end | ||
|
||
describe "CoAP.SocketServer decoding errors" do | ||
test "non repeatable options" do | ||
peer_ip = "127.0.0.1" | ||
peer_port = 5830 | ||
|
||
{:ok, server} = SocketServer.start_link([{GenericServer, FakeEndpoint}, peer_port]) | ||
|
||
data = | ||
<<100, 69, 0, 1, 252, 1, 46, 56, 68, 80, 231, 168, 249, 100, 69, 0, 1, 252, 1, 46, 56, 68, | ||
80, 231, 168, 249>> | ||
|
||
log = | ||
capture_log(fn -> | ||
ref = Process.monitor(server) | ||
send(server, {:udp, :socket, peer_ip, peer_port, data}) | ||
|
||
response = | ||
receive do | ||
{:DOWN, ^ref, :process, ^server, _reason} -> | ||
:ok | ||
after | ||
100 -> {:error, {:timeout, :await_response}} | ||
end | ||
|
||
assert response == :ok | ||
end) | ||
|
||
assert log =~ "CoAP socket failed to decode udp packets" | ||
assert log =~ "is not repeatable" | ||
assert log =~ "from ip: \"#{peer_ip}\"" | ||
assert log =~ "port: #{peer_port}" | ||
assert log =~ "data: #{inspect(data, binaries: :as_binary)}" | ||
end | ||
|
||
test "CaseClauseError" do | ||
peer_ip = "127.0.0.1" | ||
peer_port = 5830 | ||
|
||
{:ok, server} = SocketServer.start_link([{GenericServer, FakeEndpoint}, peer_port]) | ||
|
||
data = | ||
<<100, 69, 0, 1, 83, 61, 239, 152, 68, 244, 81, 253, 46, 100, 69, 0, 1, 83, 61, 239, 152, | ||
68, 244, 81, 253, 46>> | ||
|
||
log = | ||
capture_log(fn -> | ||
ref = Process.monitor(server) | ||
send(server, {:udp, :socket, peer_ip, peer_port, data}) | ||
|
||
response = | ||
receive do | ||
{:DOWN, ^ref, :process, ^server, _reason} -> | ||
:ok | ||
after | ||
100 -> {:error, {:timeout, :await_response}} | ||
end | ||
|
||
assert response == :ok | ||
end) | ||
|
||
assert log =~ "CoAP socket failed to decode udp packets" | ||
assert log =~ "CaseClauseError" | ||
assert log =~ "from ip: \"#{peer_ip}\"" | ||
assert log =~ "port: #{peer_port}" | ||
assert log =~ "data: #{inspect(data, binaries: :as_binary)}" | ||
end | ||
end | ||
end |