floof/test/floof_test.exs
Alain Zscheile 19409b738b make session handling more robust
previously, a crashing client process would take the whole backlog
with it. now, the SessionManager manages the backlog if a session is setup
2022-11-25 17:16:44 +01:00

69 lines
2.4 KiB
Elixir

defmodule FloofTest do
use ExUnit.Case
doctest Floof
test "SessionManager basic" do
cur = self()
spawn_link(fn ->
Floof.SessionManager.set(:a, 0, 8)
Floof.SessionManager.set(:a, 2, 16)
Floof.SessionManager.set(:a, 3, 32)
assert Floof.SessionManager.peek(:a) == MapSet.new([0, 2, 3])
assert Floof.SessionManager.get(:a, 0) == 8
assert Floof.SessionManager.get(:a, 1) == nil
assert Floof.SessionManager.get(:a, 2) == 16
assert Floof.SessionManager.get(:a, 3) == 32
Floof.SessionManager.drop(:a, [0, 2, 3])
assert Floof.SessionManager.peek(:a) == MapSet.new()
send(cur, :done)
end)
spawn_link(fn ->
Floof.SessionManager.set(:b, 50, 128)
Floof.SessionManager.set(:b, 12, 256)
Floof.SessionManager.set(:b, 1, 512)
assert Floof.SessionManager.peek(:b) == MapSet.new([1, 12, 50])
assert Floof.SessionManager.get(:b, 1) == 512
assert Floof.SessionManager.get(:b, 12) == 256
assert Floof.SessionManager.get(:b, 50) == 128
Floof.SessionManager.drop(:b, [1, 12, 50])
assert Floof.SessionManager.peek(:b) == MapSet.new()
send(cur, :done)
end)
for _ <- [1, 2] do
receive do
:done -> nil
end
end
end
test "Simple usage" do
# kp = :enacl.sign_keypair()
# :ok = File.write("mock_keydb/test01.secret", kp.secret)
# {:ok, pkname} = :FloofProtocol.encode(:RDNSequence, [{:RelativeDistinguishedName, {0, 9, 2342, 19200300, 100, 1, 25}, "test"}])
# :ok = File.write("mock_keydb/" <> Base.url_encode64(pkname), kp.public)
{:ok, secret_key} = File.read("mock_keydb/test01.secret")
source = [{:RelativeDistinguishedName, {0, 9, 2342, 19_200_300, 100, 1, 25}, "test"}]
msg_topic = [{:RelativeDistinguishedName, {2, 5, 4, 41}, "test topic"}]
msg_inner = Floof.Message.pack_inner(msg_topic, [], :info, <<0, 0>>)
ttl = :erlang.convert_time_unit(:erlang.system_time(), :native, :second) + 60
msg_outer = Floof.Message.build_outer(source, ttl, [], secret_key, msg_inner)
{:ok, msg_encd} = :FloofProtocol.encode(:ProtoMessage, {:xfer, msg_outer})
Floof.Distributor.register(self())
{:ok, socket} = :gen_tcp.connect({127, 0, 0, 1}, 2540, packet: 4, active: false)
:gen_tcp.send(socket, msg_encd)
:gen_tcp.close(socket)
receive do
{:fwdxfer, {id, _}} -> assert id == :crypto.hash(:blake2b, msg_inner)
end
end
end