diff options
author | Alex Auvolat <alex@adnab.me> | 2018-11-02 16:26:59 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-11-02 16:26:59 +0100 |
commit | a26dd9284352000cca6338b68c03594dcd900494 (patch) | |
tree | b51c1a9ba734d0fba9a7d4a97df4ddca85dafbca /shard/test | |
parent | 353769402b6fd2ca4ea1807c2733e161a768f85e (diff) | |
download | shard-a26dd9284352000cca6338b68c03594dcd900494.tar.gz shard-a26dd9284352000cca6338b68c03594dcd900494.zip |
WIP for file upload (Merkle tree for signatures)
Diffstat (limited to 'shard/test')
-rw-r--r-- | shard/test/conn_test.exs | 58 | ||||
-rw-r--r-- | shard/test/mkltree_test.exs | 27 |
2 files changed, 39 insertions, 46 deletions
diff --git a/shard/test/conn_test.exs b/shard/test/conn_test.exs index ae43d9d..42f4bc6 100644 --- a/shard/test/conn_test.exs +++ b/shard/test/conn_test.exs @@ -2,62 +2,28 @@ defmodule ShardTest.Conn do use ExUnit.Case doctest Shard.Application - require Salty.Box.Curve25519xchacha20poly1305, as: Box - require Salty.Sign.Ed25519, as: Sign - - test "crypto connection" do - {srv_pkey, srv_skey} = Shard.Identity.get_keypair - {:ok, sess_pkey, sess_skey} = Box.keypair - {:ok, challenge} = Salty.Random.buf 32 - {:ok, socket} = :gen_tcp.connect {127,0,0,1}, 4045, [:binary, packet: 2, active: false] - - hello = {srv_pkey, sess_pkey, challenge, 0} - :gen_tcp.send(socket, :erlang.term_to_binary hello) - {:ok, pkt} = :gen_tcp.recv(socket, 0) - {cli_pkey, cli_sess_pkey, cli_challenge, _his_port} = :erlang.binary_to_term(pkt, [:safe]) - - {:ok, cli_challenge_sign} = Sign.sign_detached(cli_challenge, srv_skey) - sendmsg(socket, cli_challenge_sign, cli_sess_pkey, sess_skey) - - challenge_sign = recvmsg(socket, cli_sess_pkey, sess_skey) - :ok = Sign.verify_detached(challenge_sign, challenge, cli_pkey) - - pkt = :erlang.binary_to_term(recvmsg(socket, cli_sess_pkey, sess_skey), [:safe]) - IO.puts (inspect pkt) - end - - defp sendmsg(sock, msg, pk, sk) do - {:ok, n} = Salty.Random.buf Box.noncebytes - {:ok, msg} = Box.easy(msg, n, pk, sk) - :gen_tcp.send(sock, n <> msg) - end - - defp recvmsg(sock, pk, sk) do - {:ok, pkt} = :gen_tcp.recv(sock, 0) - n = binary_part(pkt, 0, Box.noncebytes) - enc = binary_part(pkt, Box.noncebytes, (byte_size pkt) - Box.noncebytes) - {:ok, msg} = Box.open_easy(enc, n, pk, sk) - msg - end - test "set nickname" do - Shard.Identity.set_nickname "test bot" + pk = Shard.Keys.get_any_identity + pid = SApp.Identity.find_proc(pk) + info = SApp.Identity.get_info(pid) + new_info = %{info | nick: "test bot"} + SApp.Identity.set_info(pid, new_info) end test "connect to other instance" do - Shard.Manager.add_peer({127, 0, 0, 1}, 4045) + SNet.Manager.add_peer({:inet, {127, 0, 0, 1}, 4045}) receive do after 100 -> nil end end - @tag :skip test "connect to chat rooms" do - {:ok, pid1} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, "test"}) - {:ok, pid2} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, "other_test"}) - GenServer.cast(pid1, {:chat_send, "test msg 1"}) - GenServer.cast(pid2, {:chat_send, "test msg 2"}) + pk = Shard.Keys.get_any_identity + + pid1 = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: "test"} + pid2 = Shard.Manager.find_or_start %SApp.Chat.Manifest{channel: "other_test"} - {:error, :redundant} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, "test"}) + SApp.Chat.chat_send(pid1, pk, "test msg 1") + SApp.Chat.chat_send(pid2, pk, "test msg 2") end end diff --git a/shard/test/mkltree_test.exs b/shard/test/mkltree_test.exs new file mode 100644 index 0000000..248a37f --- /dev/null +++ b/shard/test/mkltree_test.exs @@ -0,0 +1,27 @@ +defmodule ShardTest.MklTree do + use ExUnit.Case + doctest Shard.Application + + test "merkle tree" do + alias SData.MerkleTree, as: MT + + nblk = 14119 + + {:ok, path} = Briefly.create + fh = File.open!(path, [:write]) + hashes = for i <- 0..nblk do + block = :enacl.randombytes 4096 + :file.write(fh, block) + :crypto.hash(:sha256, block) + end + lastblock = :enacl.randombytes 128 + :file.write(fh, lastblock) + hashes = hashes ++ [:crypto.hash(:sha256, lastblock)] + :file.close fh + + mt = MT.create(path) + hashes2 = 0..(nblk+1) |> Enum.map(&(MT.get(mt, &1))) + + assert hashes == hashes2 + end +end |