From 0cd682d85e15309292aab26328c8502f1ead7df2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 2 May 2026 11:41:54 +0200 Subject: [PATCH] tests: Add basic test for LDSignature::verify --- tests/unit/Lib/LDSignaturesTest.php | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/unit/Lib/LDSignaturesTest.php diff --git a/tests/unit/Lib/LDSignaturesTest.php b/tests/unit/Lib/LDSignaturesTest.php new file mode 100644 index 000000000..afc029abb --- /dev/null +++ b/tests/unit/Lib/LDSignaturesTest.php @@ -0,0 +1,86 @@ + + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Lib; + +use Zotlabs\Lib\Crypto; +use Zotlabs\Lib\LDSignatures; +use Zotlabs\Tests\Unit\UnitTestCase; + +class LDSignaturesTest extends UnitTestCase { + + public function testVerifyLDSignature(): void { + $channel = $this->fixtures['channel'][1]; + + $activity = [ + '@context' => 'https://www.w3.org/ns/activitystreams', + 'type' => 'Like', + 'actor' => 'https://hubzilla.test/channel/test', + 'to' => [ + 'https://hatchat.example/sarah/', + 'https://example.com/peeps/john/', + ], + 'object' => [ + '@context' => ['@language' => 'en'], + 'id' => 'https://example.org/~alice/note/23', + 'type' => 'Note', + 'attributedTo' => 'https://example.org/~alice', + 'content' => "I'm a goat", + ], + 'signature' => [ + '@context' => [ + 'https://www.w3.org/ns/activitystreams', + 'https://w3id.org/security/v1' + ], + 'type' => 'RsaSignature2017', + 'nonce' => 'e3158dce627da99138d238805d2f129db05bd65efce0f712de494565c852f7fc', + 'created' => '2026-05-01T09:22:05Z', + 'creator' => 'https://hubzilla.test/channel/test', + 'signatureValue' => + 'iFo/j+sG4h7zDiVis8FMOaJvw7ZanbsPdUrS5WTUkcMzL/dg3xPPpt9pKREhVx73ne6y6tIjzq6TWzWEpYvUFg==' + ], + ]; + + $this->assertTrue(LDSignatures::verify($activity, $channel['channel_pubkey'])); + + + // Verify that the signature is independent of the order + // of the fields + $activity_2 = [ + 'type' => 'Like', + '@context' => 'https://www.w3.org/ns/activitystreams', + 'actor' => 'https://hubzilla.test/channel/test', + 'to' => [ + 'https://example.com/peeps/john/', + 'https://hatchat.example/sarah/', + ], + 'signature' => [ + 'nonce' => 'e3158dce627da99138d238805d2f129db05bd65efce0f712de494565c852f7fc', + '@context' => [ + 'https://www.w3.org/ns/activitystreams', + 'https://w3id.org/security/v1' + ], + 'created' => '2026-05-01T09:22:05Z', + 'type' => 'RsaSignature2017', + 'creator' => 'https://hubzilla.test/channel/test', + 'signatureValue' => + 'iFo/j+sG4h7zDiVis8FMOaJvw7ZanbsPdUrS5WTUkcMzL/dg3xPPpt9pKREhVx73ne6y6tIjzq6TWzWEpYvUFg==' + ], + 'object' => [ + '@context' => ['@language' => 'en'], + 'type' => 'Note', + 'attributedTo' => 'https://example.org/~alice', + 'id' => 'https://example.org/~alice/note/23', + 'content' => "I'm a goat", + ], + ]; + + $this->assertFalse($activity == $activity_2); + $this->assertTrue(LDSignatures::verify($activity_2, $channel['channel_pubkey'])); + } +}