Skip to content

Commit 10e898f

Browse files
committed
Fix tests
1 parent ae0df71 commit 10e898f

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

c_src/ex_dtls/native.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,12 @@ UNIFEX_TERM handle_data(UnifexEnv *env, State *state, UnifexPayload *payload) {
331331

332332
UNIFEX_TERM handle_regular_read(State *state, char data[], int ret) {
333333
if (ret > 0) {
334-
UnifexPayload **packets = calloc(1, sizeof(UnifexPayload *));
335-
packets[0] = calloc(1, sizeof(UnifexPayload));
336-
unifex_payload_alloc(state->env, UNIFEX_PAYLOAD_BINARY, ret, packets[0]);
337-
memcpy(packets[0]->data, data, ret);
338-
packets[0]->size = (unsigned int)ret;
339-
UNIFEX_TERM res_term = handle_data_result_ok(state->env, packets, 1);
340-
free_payload_array(packets, 1);
334+
UnifexPayload packets;
335+
unifex_payload_alloc(state->env, UNIFEX_PAYLOAD_BINARY, ret, &packets);
336+
memcpy(packets.data, data, ret);
337+
packets.size = (unsigned int)ret;
338+
UNIFEX_TERM res_term = handle_data_result_ok(state->env, &packets);
339+
unifex_payload_release(&packets);
341340
return res_term;
342341
}
343342

c_src/ex_dtls/native.spec.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec handle_timeout(state) :: (:ok :: label) | {:retransmit :: label, packets ::
2828
spec write_data(state, packets :: payload) :: {:ok :: label, packets :: [payload]} | {:error :: label, :handshake_not_finished :: label}
2929

3030
spec handle_data(state, packets :: payload) ::
31-
{:ok :: label, packets :: [payload]}
31+
{:ok :: label, packets :: payload}
3232
| (:handshake_want_read :: label)
3333
| {:handshake_packets :: label, packets :: [payload], timeout :: int}
3434
| {:handshake_finished :: label, client_keying_material :: payload,

lib/ex_dtls.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ defmodule ExDTLS do
175175
| {:error, :handshake_error | :peer_closed_for_writing}
176176
def handle_data(dtls, packets) do
177177
case Native.handle_data(dtls, packets) do
178-
{:handshake_finished, lkm, rkm, protection_profile, <<>>} ->
178+
{:handshake_finished, lkm, rkm, protection_profile, []} ->
179179
{:handshake_finished, lkm, rkm, protection_profile}
180180

181181
other ->

test/integration_test.exs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule ExDTLS.IntegrationTest do
22
use ExUnit.Case, async: true
33

4-
@tag :debug
54
test "dtls_srtp" do
65
rx_dtls = ExDTLS.init(mode: :server, dtls_srtp: true, verify_peer: true)
76
tx_dtls = ExDTLS.init(mode: :client, dtls_srtp: true, verify_peer: true)
@@ -39,14 +38,12 @@ defmodule ExDTLS.IntegrationTest do
3938
assert :ok == loop({sr_dtls, false}, {cl_dtls, false}, packets)
4039

4140
msg = <<1, 3, 2, 5>>
42-
assert {:ok, data} = ExDTLS.write_data(cl_dtls, msg)
43-
assert data != msg
44-
assert {:ok, ^msg} = ExDTLS.handle_data(sr_dtls, data)
41+
assert {:ok, packets} = ExDTLS.write_data(cl_dtls, msg)
42+
assert {:ok, ^msg} = feed_packets(sr_dtls, packets)
4543

4644
msg = <<1, 3, 8, 9>>
47-
assert {:ok, data} = ExDTLS.write_data(sr_dtls, msg)
48-
assert data != msg
49-
assert {:ok, ^msg} = ExDTLS.handle_data(cl_dtls, data)
45+
assert {:ok, packets} = ExDTLS.write_data(sr_dtls, msg)
46+
assert {:ok, ^msg} = feed_packets(cl_dtls, packets)
5047
end
5148

5249
test "expired cert" do
@@ -59,8 +56,8 @@ defmodule ExDTLS.IntegrationTest do
5956
tx_dtls = ExDTLS.init(mode: :client, dtls_srtp: true, verify_peer: true)
6057

6158
{packets, _timeout} = ExDTLS.do_handshake(tx_dtls)
62-
{:handshake_packets, packets, _timeout} = ExDTLS.handle_data(rx_dtls, packets)
63-
assert {:error, :handshake_error} = ExDTLS.handle_data(tx_dtls, packets)
59+
{:handshake_packets, packets, _timeout} = feed_packets(rx_dtls, packets)
60+
assert {:error, :handshake_error} = feed_packets(tx_dtls, packets)
6461
end
6562

6663
defp loop({_dtls1, true}, {_dtls2, true}, _packets) do
@@ -83,6 +80,9 @@ defmodule ExDTLS.IntegrationTest do
8380
defp feed_packets(dtls, [packet | packets]) do
8481
case ExDTLS.handle_data(dtls, packet) do
8582
:handshake_want_read -> feed_packets(dtls, packets)
83+
# it seems that handshake error (e.g. when a certificate is too old)
84+
# may appear before consuming all packets
85+
{:error, :handshake_error} = msg -> msg
8686
other when packets == [] -> other
8787
end
8888
end

test/retransmission_test.exs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ExDTLS.RetransmissionTest do
1010
{:retransmit, packets, timeout} = wait_for_timeout(tx_dtls, :tx)
1111
Process.send_after(self(), {:handle_timeout, :tx}, timeout)
1212

13-
{:handshake_packets, _packets, timeout} = ExDTLS.handle_data(rx_dtls, packets)
13+
{:handshake_packets, _packets, timeout} = feed_packets(rx_dtls, packets)
1414
Process.send_after(self(), {:handle_timeout, :rx}, timeout)
1515
{:retransmit, packets, _timeout} = wait_for_timeout(rx_dtls, :rx)
1616

@@ -19,7 +19,7 @@ defmodule ExDTLS.RetransmissionTest do
1919
# and waiting for the old one and handling it can trigger retransmission
2020
# instead of noop
2121
Process.sleep(500)
22-
{:handshake_packets, _packets, timeout} = ExDTLS.handle_data(tx_dtls, packets)
22+
{:handshake_packets, _packets, timeout} = feed_packets(tx_dtls, packets)
2323
Process.send_after(self(), {:handle_timeout, :tx}, timeout)
2424
# wait for the old timeout
2525
:ok = wait_for_timeout(tx_dtls, :tx)
@@ -36,8 +36,18 @@ defmodule ExDTLS.RetransmissionTest do
3636
end
3737

3838
defp finish_hsk(rx_dtls, tx_dtls, packets) do
39-
{:handshake_finished, _lkm, _rkm, _p, packets} = ExDTLS.handle_data(rx_dtls, packets)
40-
{:handshake_finished, _lkm, _rkm, _p} = ExDTLS.handle_data(tx_dtls, packets)
39+
{:handshake_finished, _lkm, _rkm, _p, packets} = feed_packets(rx_dtls, packets)
40+
{:handshake_finished, _lkm, _rkm, _p} = feed_packets(tx_dtls, packets)
4141
:ok
4242
end
43+
44+
defp feed_packets(dtls, [packet | packets]) do
45+
case ExDTLS.handle_data(dtls, packet) do
46+
:handshake_want_read -> feed_packets(dtls, packets)
47+
# it seems that handshake error (e.g. when a certificate is too old)
48+
# may appear before consuming all packets
49+
{:error, :handshake_error} = msg -> msg
50+
other when packets == [] -> other
51+
end
52+
end
4353
end

0 commit comments

Comments
 (0)