2022-11-22 15:41:59 +00:00
|
|
|
defmodule FloofTest do
|
|
|
|
use ExUnit.Case
|
|
|
|
doctest Floof
|
|
|
|
|
2022-11-23 23:56:38 +00:00
|
|
|
test "SessionManager basic" do
|
|
|
|
cur = self()
|
2022-11-24 19:01:34 +00:00
|
|
|
|
2022-11-23 23:56:38 +00:00
|
|
|
spawn_link(fn ->
|
2022-11-25 16:16:44 +00:00
|
|
|
Floof.SessionManager.set(:a, 0, 8)
|
|
|
|
Floof.SessionManager.set(:a, 2, 16)
|
|
|
|
Floof.SessionManager.set(:a, 3, 32)
|
2022-11-23 23:56:38 +00:00
|
|
|
|
2022-11-25 16:16:44 +00:00
|
|
|
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()
|
2022-11-24 19:01:34 +00:00
|
|
|
send(cur, :done)
|
2022-11-23 23:56:38 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
spawn_link(fn ->
|
2022-11-25 16:16:44 +00:00
|
|
|
Floof.SessionManager.set(:b, 50, 128)
|
|
|
|
Floof.SessionManager.set(:b, 12, 256)
|
|
|
|
Floof.SessionManager.set(:b, 1, 512)
|
2022-11-23 23:56:38 +00:00
|
|
|
|
2022-11-25 16:16:44 +00:00
|
|
|
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()
|
2022-11-24 19:01:34 +00:00
|
|
|
send(cur, :done)
|
2022-11-23 23:56:38 +00:00
|
|
|
end)
|
|
|
|
|
2022-11-24 19:01:34 +00:00
|
|
|
for _ <- [1, 2] do
|
2022-11-23 23:56:38 +00:00
|
|
|
receive do
|
|
|
|
:done -> nil
|
|
|
|
end
|
|
|
|
end
|
2022-11-22 15:41:59 +00:00
|
|
|
end
|
2022-11-24 15:30:14 +00:00
|
|
|
|
|
|
|
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")
|
2022-11-24 19:01:34 +00:00
|
|
|
source = [{:RelativeDistinguishedName, {0, 9, 2342, 19_200_300, 100, 1, 25}, "test"}]
|
2022-11-24 15:30:14 +00:00
|
|
|
msg_topic = [{:RelativeDistinguishedName, {2, 5, 4, 41}, "test topic"}]
|
2022-11-24 22:11:09 +00:00
|
|
|
msg_inner = Floof.Message.pack_inner(msg_topic, [], :info, <<0, 0>>)
|
2022-11-24 15:30:14 +00:00
|
|
|
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})
|
|
|
|
|
2022-11-24 17:32:48 +00:00
|
|
|
Floof.Distributor.register(self())
|
2022-11-24 15:30:14 +00:00
|
|
|
|
2022-11-24 19:01:34 +00:00
|
|
|
{:ok, socket} = :gen_tcp.connect({127, 0, 0, 1}, 2540, packet: 4, active: false)
|
2022-11-24 17:56:15 +00:00
|
|
|
:gen_tcp.send(socket, msg_encd)
|
|
|
|
:gen_tcp.close(socket)
|
2022-11-24 15:30:14 +00:00
|
|
|
|
|
|
|
receive do
|
2022-11-24 22:11:09 +00:00
|
|
|
{:fwdxfer, {id, _}} -> assert id == :crypto.hash(:blake2b, msg_inner)
|
2022-11-24 15:30:14 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-22 15:41:59 +00:00
|
|
|
end
|