From b17e17733540dbbbbe6650847add621c5e19fbed Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Sat, 5 Jul 2025 21:35:34 +0200 Subject: [PATCH] Add tests --- test/nulla/http_signature_test.exs | 41 ++++++++++++++++++++++++ test/nulla/keys.exs | 23 +++++++++++++ test/support/fixtures/actors_fixtures.ex | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/nulla/http_signature_test.exs create mode 100644 test/nulla/keys.exs diff --git a/test/nulla/http_signature_test.exs b/test/nulla/http_signature_test.exs new file mode 100644 index 0000000..dae4f22 --- /dev/null +++ b/test/nulla/http_signature_test.exs @@ -0,0 +1,41 @@ +defmodule Nulla.HTTPSignatureTest do + use NullaWeb.ConnCase, async: false + import Plug.Conn + import Plug.Test + import Nulla.ActorsFixtures + alias Nulla.HTTPSignature + alias Nulla.Snowflake + + test "make_headers/3 creates valid signature headers and verify/2 validates them" do + actor = actor_fixture() + target_actor = actor_fixture() + + follow_activity = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "id" => Snowflake.next_id(), + "type" => "Follow", + "actor" => actor.ap_id, + "object" => target_actor.ap_id + } + + body = Jason.encode!(follow_activity) + + headers = + HTTPSignature.make_headers( + body, + target_actor.inbox, + actor.publicKey["id"], + actor.privateKeyPem + ) + + conn = + conn(:post, "/users/#{target_actor.preferredUsername}/inbox", body) + |> put_req_header("content-type", Map.new(headers) |> Map.get("Content-Type")) + |> put_req_header("date", Map.new(headers) |> Map.get("Date")) + |> put_req_header("digest", Map.new(headers) |> Map.get("Digest")) + |> put_req_header("signature", Map.new(headers) |> Map.get("Signature")) + |> Map.put(:host, URI.parse(target_actor.inbox).host) + + assert :ok = HTTPSignature.verify(conn, actor.publicKey["publicKeyPem"]) + end +end diff --git a/test/nulla/keys.exs b/test/nulla/keys.exs new file mode 100644 index 0000000..ac5155a --- /dev/null +++ b/test/nulla/keys.exs @@ -0,0 +1,23 @@ +defmodule Nulla.KeysTest do + use NullaWeb.ConnCase, async: false + alias Nulla.KeyGen + + test "test KeyGen module" do + message = "test message" + {publicKeyPem, privateKeyPem} = KeyGen.gen() + + private_key = + :public_key.pem_decode(privateKeyPem) + |> hd() + |> :public_key.pem_entry_decode() + + public_key = + :public_key.pem_decode(publicKeyPem) + |> hd() + |> :public_key.pem_entry_decode() + + signature = :public_key.sign(message, :sha256, private_key) + + assert :public_key.verify(message, :sha256, signature, public_key) + end +end diff --git a/test/support/fixtures/actors_fixtures.ex b/test/support/fixtures/actors_fixtures.ex index 2381cbe..1b018ec 100644 --- a/test/support/fixtures/actors_fixtures.ex +++ b/test/support/fixtures/actors_fixtures.ex @@ -26,7 +26,7 @@ defmodule Nulla.ActorsFixtures do following: "some following", icon: %{}, image: %{}, - inbox: "some inbox", + inbox: "http://localhost/users/#{username}/inbox", indexable: true, manuallyApprovesFollowers: true, memorial: true,