From 960bcb6b534b758fad06177a2b20e154025d386f Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 6 Jan 2024 16:12:25 +0000 Subject: port test for JcsEddsa2022 from streams --- tests/unit/Lib/JcsEddsa2022Test.php | 174 ++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tests/unit/Lib/JcsEddsa2022Test.php (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/JcsEddsa2022Test.php b/tests/unit/Lib/JcsEddsa2022Test.php new file mode 100644 index 000000000..d18ad01ce --- /dev/null +++ b/tests/unit/Lib/JcsEddsa2022Test.php @@ -0,0 +1,174 @@ +verify(json_decode($document, true), $publicKey); + $this->assertTrue($verified, 'Verify eddsa-jcs-2022 (from specification)'); + + } + + public function testSignAndVerify() { + $publicKey = 'z6MkfpucGTDbMZADwM6vEa8pS3s8Z9xqSEn6HihijZ4fVs9d'; + $channel = [ + 'channel_url' => 'https://example.com/channel/klingon', + 'channel_epubkey' => 'FGdbYgr526Swuyya3e8epCBdHahlWNg9I0sBhMKCzpw', + 'channel_eprvkey' => 'StLRo8xb7VJ5XdR10OUYQM/uooP7D7fMlgvQFa1wrZIUZ1tiCvnbpLC7LJrd7x6kIF0dqGVY2D0jSwGEwoLOnA', + 'channel_address' => 'klingon@example.com', + 'channel_system' => false, + ]; + + $document = '{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + "https://www.w3.org/ns/did/v1", + "https://w3id.org/security/multikey/v1", + { + "nomad": "https://example.com/apschema#", + "toot": "http://joinmastodon.org/ns#", + "litepub": "http://litepub.social/ns#", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "oauthRegistrationEndpoint": "litepub:oauthRegistrationEndpoint", + "sensitive": "as:sensitive", + "movedTo": "as:movedTo", + "discoverable": "toot:discoverable", + "indexable": "toot:indexable", + "capabilities": "litepub:capabilities", + "acceptsJoins": "litepub:acceptsJoins", + "Hashtag": "as:Hashtag", + "canReply": "toot:canReply", + "canSearch": "nomad:canSearch", + "approval": "toot:approval", + "expires": "nomad:expires", + "directMessage": "nomad:directMessage", + "Category": "nomad:Category", + "copiedTo": "nomad:copiedTo", + "searchContent": "nomad:searchContent", + "searchTags": "nomad:searchTags" + } + ], + "type": "Person", + "id": "https://example.com/channel/klingon", + "preferredUsername": "klingon", + "name": "klingon", + "created": "2023-07-13T20:23:32Z", + "updated": "2023-07-13T20:23:32Z", + "icon": { + "type": "Image", + "mediaType": "image/png", + "updated": "2023-07-13T20:23:32Z", + "url": "https://example.com/photo/profile/l/2", + "height": 300, + "width": 300 + }, + "url": "https://example.com/channel/klingon", + "tag": [ + { + "type": "Note", + "name": "Protocol", + "content": "zot6" + }, + { + "type": "Note", + "name": "Protocol", + "content": "nomad" + }, + { + "type": "Note", + "name": "Protocol", + "content": "activitypub" + } + ], + "inbox": "https://example.com/inbox/klingon", + "outbox": "https://example.com/outbox/klingon", + "followers": "https://example.com/followers/klingon", + "following": "https://example.com/following/klingon", + "wall": "https://example.com/outbox/klingon", + "endpoints": { + "sharedInbox": "https://example.com/inbox", + "oauthRegistrationEndpoint": "https://example.com/api/client/register", + "oauthAuthorizationEndpoint": "https://example.com/authorize", + "oauthTokenEndpoint": "https://example.com/token", + "searchContent": "https://example.com/search/klingon?search={}", + "searchTags": "https://example.com/search/klingon?tag={}" + }, + "discoverable": true, + "canSearch": [], + "indexable": false, + "publicKey": { + "id": "https://example.com/channel/klingon?operation=rsakey", + "owner": "https://example.com/channel/klingon", + "signatureAlgorithm": "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", + "publicKeyPem": "-----BEGIN PUBLIC KEY----- + MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA+LXyOD/bzzVgM/nUOJ5m + c4WrQPMlhKqWJvKrumdQw9JJYcyaZp/jmMxDx/w/EwVw+wnV5wZcD0yBVhC7NPRa + nYc5OfNhS4MO74xgZrj+VWSTzNo3YooS/dEIIvsu6bhxfooHj17SA6pMRnZkkVpk + ykpPRYwJw+NvKcRwzpF06rxMqjZ+Bp0ea/X37j4cHaosRoQTJiHmMKKnpByKdImF + TR1juJ69ASh6nh8YVGcz6fz1jBQZPMx05tfNdyN5oZRTr8Nug2CiF3V7yKKS14HD + kE9eeFeTMt58Qi+8kprATYxKrlIuTZmI4YdIRgtM+tPQsosKTFmjzbef4dYooutv + T7XfrE+wYVZlx2pkaeFiKrJVacpmmFJe8zCIFXrofq1aOagU1kpwnXgjneCttA+M + OJ3Y+cPamdfRQDtsBcokJUD40RTwux6OGW9zqkJIpniVB+CZu4nTOHCzMJwbxF0p + JmGZd9kc3PR6Uf/IHAb1xeyTi4FyyYTbRDYuJyqRKbe880QUwgCBcogIbNy4xxsH + UTMy0ucWaDSBRahKUIHl3FRglvnI754NJSXBDIQOwC9oRRH27Vmm1Jy8sltmFLFr + ENJCGgOH8Bhpk+y1jtw1jpTig76wIvw+6zQtgNSfPnrNGIHt5mcoy4pFFXLv2lK2 + /u26hUGQAq71Ra0DwgXIWFECAwEAAQ== + -----END PUBLIC KEY----- + " + }, + "assertionMethod": [ + { + "id": "https://example.com/channel/klingon#z6MkfpucGTDbMZADwM6vEa8pS3s8Z9xqSEn6HihijZ4fVs9d", + "type": "Multikey", + "controller": "https://example.com/channel/klingon", + "publicKeyMultibase": "z6MkfpucGTDbMZADwM6vEa8pS3s8Z9xqSEn6HihijZ4fVs9d" + } + ], + "manuallyApprovesFollowers": true + }'; + + $algorithm = new JcsEddsa2022(); + $documentArray = json_decode($document,true); + $documentArray['proof'] = $algorithm->sign($documentArray, $channel); + + $verified = (new JcsEddsa2022())->verify($documentArray, $publicKey); + $this->assertTrue($verified, 'Verify encode and decode eddsa-jcs-2022'); + + } +} -- cgit v1.2.3 From e3d30763dabdf12f961ed3d1d7cf4eaee44c65a9 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 6 Jan 2024 16:34:38 +0000 Subject: tests: Integrate the DB in "unit" tests. --- tests/unit/Lib/PermissionDescriptionTest.php | 47 ++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/PermissionDescriptionTest.php b/tests/unit/Lib/PermissionDescriptionTest.php index 96c381d0c..fdd676f61 100644 --- a/tests/unit/Lib/PermissionDescriptionTest.php +++ b/tests/unit/Lib/PermissionDescriptionTest.php @@ -63,11 +63,52 @@ class PermissionDescriptionTest extends UnitTestCase { $this->assertNotNull($permDescSelf); } + /** + * Test fetching permission descriptions for the current channel. + */ public function testFromGlobalPermission() { - //$permDesc = PermissionDescription::fromGlobalPermission('view_profile'); + // Initiate the global App with a channel_id + \App::$channel = array( + 'channel_id' => 42, + ); + + // Make sure the requested permission is set for this channel. + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'view_profile', + PERMS_NETWORK + ); + + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'write_storage', + PERMS_SPECIFIC + ); + + // Set an invalid(?) permission + \Zotlabs\Access\PermissionLimits::Set( + \App::$channel['channel_id'], + 'view_wiki', + 1337 + ); + + $permDesc = PermissionDescription::fromGlobalPermission('view_profile'); + $this->assertEquals( + 'Anybody in the Hubzilla network', + $permDesc->get_permission_description() + ); + + $permDesc = PermissionDescription::fromGlobalPermission('write_storage'); + $this->assertEquals( + 'Only connections I specifically allow', + $permDesc->get_permission_description() + ); - $this->markTestIncomplete( - 'The method fromGlobalPermission() is not yet testable ...' + // Permissions we don't know about will get the fallback description. + $permDesc = PermissionDescription::fromGlobalPermission('view_wiki'); + $this->assertEquals( + 'Visible to your default audience', + $permDesc->get_permission_description() ); } -- cgit v1.2.3 From 6262d351b777443bee2a1b5b534082268ebe72f9 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Mar 2024 10:00:02 +0100 Subject: fix deprecation warning and add test --- tests/unit/Lib/ActivityTest.php | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/unit/Lib/ActivityTest.php (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php new file mode 100644 index 000000000..38c3d584c --- /dev/null +++ b/tests/unit/Lib/ActivityTest.php @@ -0,0 +1,42 @@ +assertEquals($expected, Activity::get_textfield($src, 'content')); + } + + /** + * Dataprovider for test_get_textfield. + */ + private function get_textfield_provider(): array { + return [ + 'get content field' => [ + ['content' => 'Some content'], + 'Some content' + ], + 'get content from map' => [ + ['contentMap' => ['en' => 'Some content']], + [ + 'en' => 'Some content' + ] + ], + 'get not available content' => [ + ['some_field' => 'Some content'], + null + ] + ]; + } + +} -- cgit v1.2.3 From c1228d6b6d73afa294d755b4360c8d5fef72b033 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Mar 2024 10:01:46 +0100 Subject: whitespace --- tests/unit/Lib/ActivityTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php index 38c3d584c..53aa16ebc 100644 --- a/tests/unit/Lib/ActivityTest.php +++ b/tests/unit/Lib/ActivityTest.php @@ -28,9 +28,7 @@ class ActivityTest extends UnitTestCase { ], 'get content from map' => [ ['contentMap' => ['en' => 'Some content']], - [ - 'en' => 'Some content' - ] + ['en' => 'Some content'] ], 'get not available content' => [ ['some_field' => 'Some content'], -- cgit v1.2.3 From 5207e1e7749f9e3f2cec4a5cd6e5604bf35fcbb3 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Mar 2024 09:06:20 +0000 Subject: remove unused include --- tests/unit/Lib/ActivityTest.php | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php index 53aa16ebc..c9ce79d8c 100644 --- a/tests/unit/Lib/ActivityTest.php +++ b/tests/unit/Lib/ActivityTest.php @@ -5,7 +5,6 @@ error_reporting(E_ALL); use Zotlabs\Tests\Unit\UnitTestCase; use Zotlabs\Lib\Activity; -use Zotlabs\Lib\ActivityStreams; class ActivityTest extends UnitTestCase { /** -- cgit v1.2.3 From 328ce0a837f596ef53895e272301aff3c2f7c84e Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 13 Mar 2024 14:05:58 +0100 Subject: fix another regression from last Lib/Config refactor which returned the default falue in case the value was an array. also add a testcase for this situation --- tests/unit/Lib/ConfigTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ConfigTest.php b/tests/unit/Lib/ConfigTest.php index a8ae3631b..82b4a28bb 100644 --- a/tests/unit/Lib/ConfigTest.php +++ b/tests/unit/Lib/ConfigTest.php @@ -20,6 +20,7 @@ class ConfigTest extends Zotlabs\Tests\Unit\UnitTestCase { 'php-array' => 'a:3:{i:0;s:3:"one";i:1;s:3:"two";i:2;s:5:"three";}', 'json-array' => 'json:["one","two","three"]', 'object-injection' => 'a:1:{i:0;O:18:"Zotlabs\Lib\Config":0:{}}', + 'unserialized-array' => ['one', 'two', 'three'], 'config_loaded' => true, ), ); @@ -50,6 +51,17 @@ class ConfigTest extends Zotlabs\Tests\Unit\UnitTestCase { ); } + /* + * Test that we can retreive old style serialized arrays that were + * serialized with th PHP `serialize()` function. + */ + public function testGetPHPUnserializedArray(): void { + $this->assertEquals( + Zotlabs\Lib\Config::Get('test', 'unserialized-array'), + array('one', 'two', 'three') + ); + } + /* * Make sure we're not vulnerable to PHP Object injection attacks when * using the PHP `unserialize()` function. -- cgit v1.2.3 From 0e59cfb8390e4c6aee29ef73b53a4dc6b7fb581e Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 13 Mar 2024 14:08:56 +0100 Subject: fix code comment --- tests/unit/Lib/ConfigTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests/unit/Lib') diff --git a/tests/unit/Lib/ConfigTest.php b/tests/unit/Lib/ConfigTest.php index 82b4a28bb..c853cdd87 100644 --- a/tests/unit/Lib/ConfigTest.php +++ b/tests/unit/Lib/ConfigTest.php @@ -52,8 +52,8 @@ class ConfigTest extends Zotlabs\Tests\Unit\UnitTestCase { } /* - * Test that we can retreive old style serialized arrays that were - * serialized with th PHP `serialize()` function. + * Test that we can retreive unserialized arrays which are usually + * returned from the existing config cache. */ public function testGetPHPUnserializedArray(): void { $this->assertEquals( -- cgit v1.2.3