diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/Lib/ActivityTest.php | 46 | ||||
-rw-r--r-- | tests/unit/Lib/JcsEddsa2022Test.php | 8 | ||||
-rw-r--r-- | tests/unit/Lib/KeyutilsTest.php | 49 | ||||
-rw-r--r-- | tests/unit/includes/AccountTest.php | 21 | ||||
-rw-r--r-- | tests/unit/includes/BBCodeTest.php | 24 | ||||
-rw-r--r-- | tests/unit/includes/MarkdownTest.php | 4 |
6 files changed, 122 insertions, 30 deletions
diff --git a/tests/unit/Lib/ActivityTest.php b/tests/unit/Lib/ActivityTest.php index 1857487c8..456a7535e 100644 --- a/tests/unit/Lib/ActivityTest.php +++ b/tests/unit/Lib/ActivityTest.php @@ -274,4 +274,50 @@ class ActivityTest extends UnitTestCase { ]; } + public function testBuildPacketWithEmptyChannel(): void { + $data = [ 'aKey' => 'aValue' ]; + $packet = json_decode(Activity::build_packet($data, []), true); + + $this->assertArrayHasKey('aKey', $packet); + $this->assertEquals('aValue', $packet['aKey']); + } + + /** + * Test get protocols from an activitystreams actor object + * + * @dataProvider get_actor_protocols_provider + */ + public function test_get_actor_protocols(array $actor, array $expected): void { + $this->assertEquals($expected, Activity::get_actor_protocols($actor)); + } + + /** + * Dataprovider for test_get_actor_protocols. + */ + public static function get_actor_protocols_provider(): array { + return [ + 'none' => [ + ['tag' => [['type' => 'Note', 'name' => 'Website', 'content' => 'https://example.com']]], + [] + ], + 'legacy' => [ + ['tag' => [ + ['type' => 'PropertyValue', 'name' => 'Protocol', 'value' => 'zot6'], + ['type' => 'PropertyValue', 'name' => 'Protocol', 'value' => 'activitypub'], + ['type' => 'PropertyValue', 'name' => 'Protocol', 'value' => 'diaspora'] + ]], + ['zot6', 'activitypub', 'diaspora'] + ], + 'fep-fb2a' => [ + ['tag' => [['type' => 'Note', 'name' => 'Protocols', 'content' => 'zot6,activitypub,diaspora']]], + ['zot6', 'activitypub', 'diaspora'] + ], + 'fep-fb2a with spaces' => [ + ['tag' => [['type' => 'Note', 'name' => 'Protocols', 'content' => 'zot6, activitypub, diaspora']]], + ['zot6', 'activitypub', 'diaspora'] + ], + ]; + + + } } diff --git a/tests/unit/Lib/JcsEddsa2022Test.php b/tests/unit/Lib/JcsEddsa2022Test.php index d18ad01ce..7cdc655f8 100644 --- a/tests/unit/Lib/JcsEddsa2022Test.php +++ b/tests/unit/Lib/JcsEddsa2022Test.php @@ -3,6 +3,7 @@ namespace Zotlabs\Tests\Unit\Lib; use Zotlabs\Lib\JcsEddsa2022; +use Zotlabs\Lib\JcsEddsa2022SignException; use Zotlabs\Tests\Unit\UnitTestCase; class JcsEddsa2022Test extends UnitTestCase { @@ -171,4 +172,11 @@ class JcsEddsa2022Test extends UnitTestCase { $this->assertTrue($verified, 'Verify encode and decode eddsa-jcs-2022'); } + + public function testSignWithInvalidChannelShouldBeRejected(): void { + $this->expectException(JcsEddsa2022SignException::class); + + $alg = new JcsEddsa2022(); + $res = $alg->sign([], []); + } } diff --git a/tests/unit/Lib/KeyutilsTest.php b/tests/unit/Lib/KeyutilsTest.php index d1b0b5ab8..1f3fa2295 100644 --- a/tests/unit/Lib/KeyutilsTest.php +++ b/tests/unit/Lib/KeyutilsTest.php @@ -23,8 +23,9 @@ namespace Zotlabs\Tests\Unit\Lib; -use phpseclib\Crypt\RSA; -use phpseclib\Math\BigInteger; +use phpseclib3\Crypt\PublicKeyLoader; +use phpseclib3\Math\BigInteger; + use Zotlabs\Tests\Unit\UnitTestCase; use Zotlabs\Lib\Keyutils; @@ -38,56 +39,54 @@ class KeyutilsTest extends UnitTestCase { protected function getPubPKCS1() { $key = '-----BEGIN RSA PUBLIC KEY----- -MIIBCgKCAQEArXcEXQSkk25bwDxq5Ym85/OwernfOz0hgve46Jm1KXCF0+yeje8J -BDbQTsMgkF+G8eP1er3oz3E0qlIFpYrza5o6kaaLETSroTyZR5QW5S21r/QJHE+4 -F08bw1zp9hrlvoOCE/g/W0mr3asO/x7LrQRKOETlZ/U6HGexTdYLyKlXJtB+VKjI -XKAHxfVLRW2AvnFj+deowS1OhTN8ECpz88xG9wnh5agoq7Uol0WZNNm0p4oR6+cd -zTPx/mBwcOoSqHLlO7ZACbx/VyD5G7mQKWfGP4b96D8FcUO74531my+aKIpLF4Io -1JN4R4a4P8tZ8BkCnMvpuq9TF1s6vEthYQIDAQAB +MIIBCgKCAQEAsSBBV5khOWvMAOIV2AhxQukBgWc1pfYqUM/9OIFfL+KaRDOFAT0y +jMbtTCbHEjxZbasS3CNnHtAgJ+4BWjtFnZl7zY4pXb2RDe4IWNnK8BjqsWDVuPks +sc+yRBTepR/50FG/xB4HfI4GRc/8EDmynyAdLDD/r6oPxmz1gMICqSGDX8yXYG4o +DliNT3bWDH+uf1+6pWsN6IFQYmUoRLly1xsfc8AV4H1GmSFrbAvQpHA95GOlxnPY +IwVth1m8O+D9SH2+0e8ourdXH3/9ccJnh6FVvFvkyeC1e2vY+J+uC0uwu+6IvHFV +pSnS8lTmwG/BBh2dA5eqV3s+REdLdS/tsQIDAQAB -----END RSA PUBLIC KEY-----'; return str_replace(["\r", "\n"], "\r\n", $key); } protected function getPubPKCS8() { $key = '-----BEGIN PUBLIC KEY----- -MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUKfOIkFX/Zcv6bmaTIYO6OO2g -XQOne+iPfXo6YDdrtvvQNZwW5P/fptrgBzmUBkpuc/sEEKpMV2bGhBLsWSlPBYHe -2ewwLwyzbnuHvGhc1PzwMNQ7R60ubVDQT6sBVigYGZIDBgUPjAXeqmg5qgWWh04H -8Zf/YxyoGEovWDMxGQIDAQAB +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsSBBV5khOWvMAOIV2Ahx +QukBgWc1pfYqUM/9OIFfL+KaRDOFAT0yjMbtTCbHEjxZbasS3CNnHtAgJ+4BWjtF +nZl7zY4pXb2RDe4IWNnK8BjqsWDVuPkssc+yRBTepR/50FG/xB4HfI4GRc/8EDmy +nyAdLDD/r6oPxmz1gMICqSGDX8yXYG4oDliNT3bWDH+uf1+6pWsN6IFQYmUoRLly +1xsfc8AV4H1GmSFrbAvQpHA95GOlxnPYIwVth1m8O+D9SH2+0e8ourdXH3/9ccJn +h6FVvFvkyeC1e2vY+J+uC0uwu+6IvHFVpSnS8lTmwG/BBh2dA5eqV3s+REdLdS/t +sQIDAQAB -----END PUBLIC KEY-----'; return str_replace(["\r", "\n"], "\r\n", $key); } public function testMeToPem() { - Keyutils::pemToMe($this->getPubPKCS8(), $m, $e); + [$m, $e] = Keyutils::pemToMe($this->getPubPKCS8()); $gen_key = Keyutils::meToPem($m, $e); self::assertEquals($this->getPubPKCS8(), $gen_key); } public function testRsaToPem() { - $rsa = new RSA(); - $rsa->setPublicKey($this->getPubPKCS8()); - $key = $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1); - $gen_key = Keyutils::rsaToPem($key); + $gen_key = Keyutils::rsaToPem($this->getPubPKCS1()); self::assertEquals($gen_key, $this->getPubPKCS8()); } public function testPemToRsa() { - $rsa = new RSA(); - $rsa->setPublicKey($this->getPubPKCS1()); - $key = $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8); - $gen_key = Keyutils::pemToRsa($key); + $gen_key = Keyutils::pemToRsa($this->getPubPKCS8()); self::assertEquals($gen_key, $this->getPubPKCS1()); } public function testPemToMe() { - Keyutils::pemToMe($this->getPubPKCS8(), $m, $e); - $gen_key = new RSA(); - $gen_key->loadKey([ + [$m, $e] = Keyutils::pemToMe($this->getPubPKCS8()); + + $parsedKey = PublicKeyLoader::load([ 'e' => new BigInteger($e, 256), 'n' => new BigInteger($m, 256) ]); - self::assertEquals($gen_key->getPublicKey(), $this->getPubPKCS8()); + + self::assertEquals($parsedKey->toString('PKCS8'), $this->getPubPKCS8()); } } diff --git a/tests/unit/includes/AccountTest.php b/tests/unit/includes/AccountTest.php index 3978f9d04..66c761ef5 100644 --- a/tests/unit/includes/AccountTest.php +++ b/tests/unit/includes/AccountTest.php @@ -1,9 +1,28 @@ <?php + +use Zotlabs\Tests\Unit\UnitTestCase; + /** * Tests for account handling helper functions. */ +class AccountTest extends UnitTestCase { + + /** + * Test the `get_account_id()` function. + */ + public function test_get_account_id() { + App::set_account(null); + unset($_SESSION['account_id']); + + $this->assertEquals(false, get_account_id(), 'get_account_id() should return false if not authenticated'); + + App::set_account(['account_id' => 36]); + $this->assertEquals(36, get_account_id(), 'get_account_id() should return account from global App object'); + + $_SESSION['account_id'] = 42; + $this->assertEquals(42, get_account_id(), 'get_account_id() should return the account from the session'); + } -class AccountTest extends Zotlabs\Tests\Unit\UnitTestCase { public function test_get_account_by_id_returns_existing_account() { $account = get_account_by_id(42); $this->assertNotFalse($account); diff --git a/tests/unit/includes/BBCodeTest.php b/tests/unit/includes/BBCodeTest.php index 50475efea..982ef4eb9 100644 --- a/tests/unit/includes/BBCodeTest.php +++ b/tests/unit/includes/BBCodeTest.php @@ -142,20 +142,36 @@ class BBCodeTest extends UnitTestCase { ], 'naked url is converted to link' => [ 'example url: https://example.com', - 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>' + 'example url: <a href="https://example.com" target="_blank" rel="nofollow noopener">https://example.com</a>' ], 'naked url followed by newline' => [ "https://www.example.com\nhave a great day.", - '<a href="https://www.example.com" target="_blank" rel="nofollow noopener">https://www.example.com</a><br />have a great day.', + '<a href="https://www.example.com" target="_blank" rel="nofollow noopener">https://www.example.com</a><br />have a great day.', ], 'inline naked url' => [ "This is a link https://example.com/some/path more info.", - 'This is a link <a href="https://example.com/some/path" target="_blank" rel="nofollow noopener">https://example.com/some/path</a> more info.', + 'This is a link <a href="https://example.com/some/path" target="_blank" rel="nofollow noopener">https://example.com/some/path</a> more info.', ], 'naked url within code block is not converted to link' => [ "[code]\nhttp://example.com\n[/code]", "<pre><code>http://example.com</code></pre>" ], + 'geo uri is converted to link' => [ + 'example url: [url]geo:37.786971,-122.399677;u=35[/url]', + 'example url: <a href="geo:37.786971,-122.399677;u=35" target="_blank" rel="nofollow noopener">geo:37.786971,-122.399677;u=35</a>' + ], + 'geo uri with label is converted to link' => [ + 'example url: [url=geo:37.786971,-122.399677;u=35(Wikimedia+Foundation)]Wikimedia Foundation[/url]', + 'example url: <a href="geo:37.786971,-122.399677;u=35(Wikimedia+Foundation)" target="_blank" rel="nofollow noopener">Wikimedia Foundation</a>' + ], + 'naked geo uri is converted to link' => [ + 'example url: geo:37.786971,-122.399677;u=35', + 'example url: <a href="geo:37.786971,-122.399677;u=35" target="_blank" rel="nofollow noopener">geo:37.786971,-122.399677;u=35</a>' + ], + 'naked geo uri with label is converted to link' => [ + 'example url: geo:37.78918,-122.40335(Wikimedia+Foundation)', + 'example url: <a href="geo:37.78918,-122.40335(Wikimedia+Foundation)" target="_blank" rel="nofollow noopener">📍Wikimedia Foundation</a>' + ], ]; } @@ -206,7 +222,7 @@ class BBCodeTest extends UnitTestCase { '[rpost=a title]This is the body[/rpost]', true, 'en', - '<a href="https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body" target="_blank" rel="nofollow noopener">https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body</a>', + '<a href="https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body" target="_blank" rel="nofollow noopener">https://example.com:666/rpost?f=&title=a+title&body=This+is+the+body</a>', ], 'unauthenticated observer rpost' => [ '[rpost=a title]This is the body[/rpost]', diff --git a/tests/unit/includes/MarkdownTest.php b/tests/unit/includes/MarkdownTest.php index 55dbb4445..eec809174 100644 --- a/tests/unit/includes/MarkdownTest.php +++ b/tests/unit/includes/MarkdownTest.php @@ -114,6 +114,10 @@ class MarkdownTest extends UnitTestCase { 'This is a link https://example.com/some/path more info.', 'This is a link https://example.com/some/path more info.', ], + 'mention with underscores is untouched' => [ + '@{_test_@somesite.example} @{test_2_@othersite.example}', + '@{_test_@somesite.example} @{test_2_@othersite.example}', + ], ]; } |