aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/Lib')
-rw-r--r--tests/unit/Lib/ActivityTest.php39
-rw-r--r--tests/unit/Lib/ConfigTest.php12
-rw-r--r--tests/unit/Lib/JcsEddsa2022Test.php174
-rw-r--r--tests/unit/Lib/PermissionDescriptionTest.php47
4 files changed, 269 insertions, 3 deletions
diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php
new file mode 100644
index 000000000..c9ce79d8c
--- /dev/null
+++ b/tests/unit/Lib/ActivityTest.php
@@ -0,0 +1,39 @@
+<?php
+namespace Zotlabs\Tests\Unit\Lib;
+
+error_reporting(E_ALL);
+
+use Zotlabs\Tests\Unit\UnitTestCase;
+use Zotlabs\Lib\Activity;
+
+class ActivityTest extends UnitTestCase {
+ /**
+ * Test get a textfield from an activitystreams object
+ *
+ * @dataProvider get_textfield_provider
+ */
+ public function test_get_textfield(array $src, null|string|array $expected): void {
+ $this->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
+ ]
+ ];
+ }
+
+}
diff --git a/tests/unit/Lib/ConfigTest.php b/tests/unit/Lib/ConfigTest.php
index a8ae3631b..c853cdd87 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,
),
);
@@ -51,6 +52,17 @@ class ConfigTest extends Zotlabs\Tests\Unit\UnitTestCase {
}
/*
+ * Test that we can retreive unserialized arrays which are usually
+ * returned from the existing config cache.
+ */
+ 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.
*/
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 @@
+<?php
+
+namespace Zotlabs\Tests\Unit\Lib;
+
+use Zotlabs\Lib\JcsEddsa2022;
+use Zotlabs\Tests\Unit\UnitTestCase;
+
+class JcsEddsa2022Test extends UnitTestCase {
+
+ public function testVerifyFromSpec() {
+ $publicKey = 'z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2';
+ $privateKey = 'z3u2en7t5LR2WtQH5PfFqMqwVHBeXouLzo6haApm8XHqvjxq';
+
+ $document = '{
+ "@context": [
+ "https://www.w3.org/ns/credentials/v2",
+ "https://www.w3.org/ns/credentials/examples/v2"
+ ],
+ "id": "urn:uuid:58172aac-d8ba-11ed-83dd-0b3aef56cc33",
+ "type": [
+ "VerifiableCredential",
+ "AlumniCredential"
+ ],
+ "name": "Alumni Credential",
+ "description": "A minimum viable example of an Alumni Credential.",
+ "issuer": "https://vc.example/issuers/5678",
+ "validFrom": "2023-01-01T00:00:00Z",
+ "credentialSubject": {
+ "id": "did:example:abcdefgh",
+ "alumniOf": "The School of Examples"
+ },
+ "proof": {
+ "type": "DataIntegrityProof",
+ "cryptosuite": "eddsa-jcs-2022",
+ "created": "2023-02-24T23:36:38Z",
+ "verificationMethod": "https://vc.example/issuers/5678#z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2",
+ "proofPurpose": "assertionMethod",
+ "proofValue": "z3P6rHMUaWG6e3Ac6xYFht8aEvoVXndgKTtEY8kzWYXzk8dKmAo2GJeZiJw4qoZ2PGp4ugdaHx3oQiLpeFBLDqP2M"
+ }
+ }';
+
+ $verified = (new JcsEddsa2022())->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');
+
+ }
+}
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()
);
}