true, 'verboseOutput' => true ]); // 2つのサーバー(A・B)を1つのプロセスで動かす。公開IPのURLなので名前解決は要らない $tmp = sys_get_temp_dir().'/lbfedi_'.bin2hex(random_bytes(6)); $inst = ['A' => 'https://1.1.1.1', 'B' => 'https://8.8.8.8']; $use = function(string $n) use ($tmp, $inst): void { Fedi::$baseUrl = $inst[$n]; Fedi::$dataDir = "{$tmp}/{$n}/ap"; Fedi::$userDir = "{$tmp}/{$n}/user/"; if (!is_dir(Fedi::$userDir)) mkdir(Fedi::$userDir, 0700, true); }; $mkuser = function(string $n, string $name, int $id, int $role = 1) use ($use): void { $use($n); file_put_contents(Fedi::$userDir."{$id}.{$name}.json", json_encode([ 'id' => $id, 'username' => $name, 'displayname' => ucfirst($name), 'bio' => "I am {$name}", 'avatar' => '', 'email' => "{$name}@example.com", 'regDate' => time(), 'gender' => -1, 'role' => $role, 'namecolor' => '', ])); }; // 相手のサーバーのアクターをキャッシュに入れる(取得しなくても良い様に) $seed = function(string $from, string $to, string $name) use ($use): void { $use($to); $doc = Actor::document(Fedi::localActor($name)); $use($from); Remote::cacheActor($doc); }; // 待ち行列を相手のサーバーの受付ボックスに届ける $pump = function() use ($use, $inst): array { $log = []; for ($round = 0; $round < 5; $round++) { $any = false; foreach (array_keys($inst) as $n) { $use($n); foreach (glob(Fedi::path('queue/').'*.json') ?: [] as $f) { $job = json_decode(file_get_contents($f), true); unlink($f); $any = true; $dest = array_search('https://'.parse_url($job['inbox'], PHP_URL_HOST), $inst, true); $use($dest); [$code] = Inbox::handle($job['activity']); $log[] = "{$job['activity']['type']}:{$code}"; $use($n); } } if (!$any) break; } return $log; }; $alice = 'https://1.1.1.1/ap/users/alice'; $carol = 'https://1.1.1.1/ap/users/carol'; $bob = 'https://8.8.8.8/ap/users/bob'; $test->describe('Fediverseの本文', function($test): void { if (!ACTIVITYPUB_ENABLED) { $test->skipAll('ACTIVITYPUB_ENABLEDが無効'); } $test->it('メンションとハッシュタグを取り出すはず', function($test): void { $t = "Hi @bob@b.example and @alice! #タグ #php https://x.example/#notatag @not@url"; $test->assertEquals(['@bob@b.example' => ['bob', 'b.example'], '@alice' => ['alice', null]], Html::mentions($t)); $test->assertEquals(['タグ', 'php'], Html::hashtags($t)); }); $test->it('プレーンテキストをHTMLにするはず', function($test): void { Fedi::$baseUrl = 'https://1.1.1.1'; $html = Html::render("Hi @bob@b.example \nhttps://x.example/a?b=1, #tag\n\nnext", ['@bob@b.example' => 'https://8.8.8.8/ap/users/bob']); $test->assertStringContains('@bob', $html); $test->assertStringContains('<b>
', $html); $test->assertStringContains('x.example/a?b=1,', $html); $test->assertStringContains('', $html); $test->assertStringContains('

next

', $html); // 解決出来なかったメンションはリンクにしない $test->assertEquals('

@nobody@x.example

', Html::render('@nobody@x.example')); }); $test->it('Markdownを描画するはず', function($test): void { Fedi::$baseUrl = 'https://1.1.1.1'; $r = fn(string $t) => Html::render($t); $test->assertEquals('

太字と斜体と線と<b>*x*

', $r('**太字**と*斜体*と~~線~~と`*x*`')); $test->assertEquals('

リンク

', $r('[**リンク**](https://x.example/a)')); // javascript:等はリンクにしない $test->assertEquals('

[x](javascript:alert(1))

', $r('[x](javascript:alert(1))')); $test->assertEquals('
  1. one
  2. two
', $r("- a\n* *b*\n\n1. one\n2. two")); $test->assertEquals('

引用
強

本文

', $r("> 引用\n> **強**\n\n本文")); $test->assertEquals("
**そのまま** #tag @bob\n<script>
", $r("```php\n**そのまま** #tag @bob\n日本語

'); $test->assertFalse(str_contains($out, 'assertFalse(str_contains($out, 'onerror')); $test->assertFalse(str_contains($out, 'onclick')); $test->assertFalse(str_contains($out, 'javascript:')); $test->assertFalse(str_contains($out, 'assertFalse(str_contains($out, 'assertFalse(str_contains($out, 'target=')); $test->assertStringContains('@u', $out); $test->assertStringContains('日本語', $out); $test->assertEquals("a\nb", Html::text('

a
b

')); }); $test->it('FEDIINFOのdomainをURLにするはず', function($test): void { $test->assertEquals('https://technicalsuwako.moe', Fedi::normalizeDomain('technicalsuwako.moe')); $test->assertEquals('https://technicalsuwako.moe', Fedi::normalizeDomain('https://technicalsuwako.moe/')); $test->assertEquals('http://127.0.0.1:8000', Fedi::normalizeDomain('http://127.0.0.1:8000')); $test->assertEquals('', Fedi::normalizeDomain('')); }); $test->it('公開範囲を判断するはず', function($test): void { $f = 'https://x.example/users/a/followers'; $test->assertEquals('public', Fedi::visibility([Fedi::PUBLIC], [$f])); $test->assertEquals('unlisted', Fedi::visibility([$f], [Fedi::PUBLIC])); $test->assertEquals('followers', Fedi::visibility([$f], [])); $test->assertEquals('direct', Fedi::visibility(['https://x.example/users/b'], [])); }); }); $test->describe('アップロードした画像', function($test): void { $test->it('中身で種類を判断するはず', function($test): void { $test->assertEquals('image/png', Media::detect("\x89PNG\r\n\x1a\nxxxx")); $test->assertEquals('image/jpeg', Media::detect("\xff\xd8\xff\xe0xxxx")); $test->assertEquals('image/gif', Media::detect('GIF89axxxx')); $test->assertEquals('image/webp', Media::detect('RIFF'.pack('V', 100).'WEBPVP8 ')); $test->assertNull(Media::detect('assertEquals('video/mp4', Media::detect("\0\0\0\x20ftypisom\0\0\x02\0")); $test->assertEquals('video/mp4', Media::detect("\0\0\0\x18ftypmp42\0\0\0\0")); $test->assertNull(Media::detect("\0\0\0\x1cftypavif\0\0\0\0")); $test->assertNull(Media::detect("\0\0\0\x14ftypqt \0\0\0\0")); $test->assertEquals('video/webm', Media::detect("\x1a\x45\xdf\xa3\x9f\x42\x86\x81\x01\x42\x82\x84webm")); $test->assertEquals('video/matroska', Media::detect("\x1a\x45\xdf\xa3\x9f\x42\x86\x81\x01\x42\x82\x88matroska")); $test->assertNull(Media::detect("\x1a\x45\xdf\xa3\x9f\x42\x86\x81\x01")); }); $test->it('アイコンには動画を使えないはず', function($test): void { $tmp = tempnam(sys_get_temp_dir(), 'lbmedia'); file_put_contents($tmp, "\0\0\0\x18ftypmp42\0\0\0\0".str_repeat("\0", 16)); $file = ['name' => 'a.mp4', 'tmp_name' => $tmp, 'size' => filesize($tmp), 'error' => UPLOAD_ERR_OK]; $res = Media::save($file, 'test', '', false); @unlink($tmp); $test->assertFalse($res->isSuccess); $test->assertStringContains('画像だけ', $res->message); }); $test->it('MP4のメタデータを同じ大きさの空きにするはず', function($test): void { $box = fn(string $t, string $d) => pack('N', strlen($d) + 8).$t.$d; $mdat = $box('mdat', 'FRAMES'); $mp4 = $box('ftyp', 'isom'.pack('N', 512).'isomiso2') .$box('moov', $box('mvhd', "\0\0\0\0".'DATEDATE'.str_repeat("\1", 12)) .$box('trak', $box('tkhd', 'TKHD').$box('udta', $box("\xa9xyz", '+35.6+139.7/'))) .$box('udta', $box("\xa9day", '2026-11-11')).$box('meta', 'Apple iPhone')) .$box('uuid', 'XMPxmp:CreatorTool') .$mdat; $out = Media::strip($mp4, 'video/mp4'); $test->assertEquals(strlen($mp4), strlen($out)); foreach (['+35.6+139.7', '2026-11-11', 'iPhone', 'CreatorTool', 'udta', 'meta', 'uuid', 'DATEDATE'] as $s) $test->assertFalse(str_contains($out, $s)); foreach (['mvhd', 'tkhd', 'TKHD', 'mdat', 'FRAMES'] as $s) $test->assertStringContains($s, $out); // 動画のデータの位置は変わらない $test->assertEquals(strpos($mp4, $mdat), strpos($out, $mdat)); }); $test->it('JPEG・PNG・WebPのメタデータを取り除くはず', function($test): void { $exif = "\xff\xe1".pack('n', 12).'Exif'."\0\0GPS!"; $jfif = "\xff\xe0".pack('n', 6).'JFIF'; $com = "\xff\xfe".pack('n', 6).'note'; $jpeg = "\xff\xd8".$jfif.$exif.$com."\xff\xda".pack('n', 2)."DATA\xff\xd9"; $out = Media::strip($jpeg, 'image/jpeg'); $test->assertFalse(str_contains($out, 'GPS!')); $test->assertFalse(str_contains($out, 'note')); $test->assertStringContains('JFIF', $out); $test->assertTrue(str_ends_with($out, "DATA\xff\xd9")); $chunk = fn($t, $d) => pack('N', strlen($d)).$t.$d.pack('N', crc32($t.$d)); $png = "\x89PNG\r\n\x1a\n".$chunk('IHDR', pack('NNCCCCC', 1, 1, 8, 2, 0, 0, 0)).$chunk('tEXt', "Comment\0secret") .$chunk('eXIf', 'MM').$chunk('IDAT', 'x').$chunk('IEND', ''); $out = Media::strip($png, 'image/png'); $test->assertFalse(str_contains($out, 'secret')); $test->assertFalse(str_contains($out, 'eXIf')); $test->assertStringContains('IDAT', $out); $test->assertStringContains('IEND', $out); $riff = fn($t, $d) => $t.pack('V', strlen($d)).$d.(strlen($d) & 1 ? "\0" : ''); $body = $riff('VP8X', "\x0c\0\0\0\0\0\0\0\0\0").$riff('VP8L', 'IMG').$riff('EXIF', 'GPS!').$riff('XMP ', ''); $out = Media::strip('RIFF'.pack('V', 4 + strlen($body)).'WEBP'.$body, 'image/webp'); $test->assertFalse(str_contains($out, 'GPS!')); $test->assertFalse(str_contains($out, 'secret')); $test->assertStringContains('IMG', $out); $test->assertEquals(0, ord($out[20]) & 0x0c); $test->assertEquals(strlen($out) - 8, unpack('V', substr($out, 4, 4))[1]); }); }); $test->describe('マルチユーザーのFediverse(2つのサーバー)', function($test) use ($use, $mkuser, $seed, $pump, $tmp, $alice, $bob, $carol): void { if (!ACTIVITYPUB_MULTIUSER) { $test->skipAll('マルチユーザーモードが無効(FEDIINFOのisSingleUserがtrue、又はAUTH_ENABLEDが無効)'); } if (ACTIVITYPUB_MULTIUSER) { $mkuser('A', 'alice', 1); $mkuser('A', 'carol', 2); $mkuser('A', 'dave', 3, 0); // BANされた $mkuser('B', 'bob', 1); $seed('A', 'B', 'bob'); $seed('B', 'A', 'alice'); $seed('B', 'A', 'carol'); $use('A'); Fedi::write(Fedi::path('remote/webfinger/'.sha1('bob@b.example').'.json'), ['url' => $bob, 'at' => time()]); } $test->it('アクターとWebFingerを返すはず', function($test) use ($use, $alice): void { $use('A'); $doc = Actor::document(Fedi::localActor('alice')); $test->assertEquals($alice, $doc['id']); $test->assertEquals("{$alice}/inbox", $doc['inbox']); $test->assertEquals('https://1.1.1.1/ap/inbox', $doc['endpoints']['sharedInbox']); $test->assertStringContains('I am alice', $doc['summary']); $test->assertEquals(ass_url('user/noicon.png', 'https://1.1.1.1'), $doc['icon']['url']); $test->assertEquals($alice, Actor::webfinger('acct:alice@1.1.1.1')['links'][0]['href']); $test->assertNull(Actor::webfinger('acct:alice@other.example')); $test->assertNull(Actor::webfinger('acct:nobody@1.1.1.1')); // BANされたユーザーはアクターにならない $test->assertNull(Fedi::localActor('dave')); }); $test->it('このサイトのアカウントを検索出来るはず(ブログのアカウントも)', function($test) use ($use, $alice): void { $use('A'); // テストのサーバーはIPアドレスなので、「@user@host」はWebFingerで確認する $test->assertEquals($alice, Remote::webfinger('alice', '1.1.1.1')); $test->assertEquals($alice, Remote::resolve('@alice')); $blog = FEDIINFO['actor']; $test->assertEquals(Fedi::blogActorUrl(), Remote::webfinger($blog, '1.1.1.1')); $test->assertEquals(Fedi::blogActorUrl(), Remote::webfinger(strtoupper($blog), '1.1.1.1')); $test->assertEquals(Fedi::blogActorUrl(), Remote::resolve("@{$blog}")); $test->assertNull(Remote::webfinger('nobody', '1.1.1.1')); $test->assertNull(Remote::resolve('@dave'), 'BANされたユーザー'); }); $test->it('リモートのユーザーをフォローして承認されるはず', function($test) use ($use, $pump, $alice, $bob): void { $use('A'); $test->assertTrue(Social::follow('alice', $bob)->isSuccess); $test->assertEquals('pending', Actor::following('alice')[$bob]['state']); $test->assertEquals(['Follow:202', 'Accept:202'], $pump()); $use('A'); $test->assertTrue(Actor::isFollowing('alice', $bob)); // 承認されたら、相手の最近の投稿を次の配信の時に取得する $test->assertArrayHasKey($bob, Fedi::read(Fedi::path('backfill.json'))); $use('B'); $test->assertTrue(Actor::isFollowedBy('bob', $alice)); $test->assertEquals('follow', Actor::notifications('bob')[0]['type']); }); $test->it('送信ボックスからはそのアクターの公開・未収載の投稿だけを取り出すはず', function($test) use ($bob): void { $doc = ['id' => $bob, 'followers' => "{$bob}/followers"]; $note = fn(string $n, array $to, array $cc = [], ?string $by = null) => ['type' => 'Create', 'object' => [ 'id' => "https://8.8.8.8/n/{$n}", 'type' => 'Note', 'attributedTo' => $by ?? $bob, 'to' => $to, 'cc' => $cc, 'content' => $n]]; $items = [ $note('public', [Fedi::PUBLIC]), $note('unlisted', ["{$bob}/followers"], [Fedi::PUBLIC]), $note('followers', ["{$bob}/followers"]), $note('direct', ['https://1.1.1.1/ap/users/alice']), $note('other', [Fedi::PUBLIC], [], 'https://8.8.8.8/ap/users/eve'), ['type' => 'Announce', 'object' => 'https://9.9.9.9/n/x'], ['type' => 'Create', 'object' => 'https://8.8.8.8/n/string'], ['type' => 'Create', 'object' => ['id' => 'https://9.9.9.9/n/spoof', 'type' => 'Note', 'attributedTo' => $bob, 'to' => [Fedi::PUBLIC]]], ]; $got = array_map(fn($o) => $o['content'], Remote::outboxNotes($doc, $items)); $test->assertEquals(['public', 'unlisted'], $got); }); $test->it('リモートの画像は署名付きでこのサイト経由にするはず', function($test) use ($use): void { $use('A'); $test->assertEquals('', Remote::imageUrl('')); $test->assertEquals(Fedi::base().'/static/a.png', Remote::imageUrl(Fedi::base().'/static/a.png')); $url = 'https://8.8.8.8/media/a.webp'; $test->assertEquals('/microblog/media?u='.rawurlencode($url).'&s='.Remote::imageSig($url), Remote::imageUrl($url)); $test->assertTrue(Remote::imageSig($url) !== Remote::imageSig($url.'?x')); // 形式は中身で判断し、SVG等は受け付けない $test->assertEquals('image/png', Remote::imageType("\x89PNG\r\n\x1a\n....")); $test->assertEquals('image/jpeg', Remote::imageType("\xff\xd8\xff\xe0....")); $test->assertEquals('image/gif', Remote::imageType('GIF89a....')); $test->assertEquals('image/webp', Remote::imageType('RIFF....WEBPVP8 ')); $test->assertEquals('image/avif', Remote::imageType("\x00\x00\x00\x1cftypavif")); $test->assertNull(Remote::imageType('')); $test->assertNull(Remote::imageType('')); }); $test->it('フォローしている人の投稿がホームに届くはず', function($test) use ($use, $pump): void { $use('B'); $test->assertTrue(Social::post('bob', 'Hello #fedi')->isSuccess); $test->assertEquals(['Create:202'], $pump()); $use('A'); [$home] = Timeline::query('home', 'alice'); $test->assertCount(1, $home); $test->assertStringContains('Hello', $home[0]['object']['content']); $test->assertCount(1, Timeline::query('federated', null)[0]); $test->assertCount(0, Timeline::query('local', null)[0]); $test->assertCount(1, Timeline::query('tag', null, ['tag' => 'fedi'])[0]); }); $test->it('いいね・ブースト・返信が相手に届くはず', function($test) use ($use, $pump): void { $use('A'); $id = Timeline::query('home', 'alice')[0][0]['object']['id']; $test->assertTrue(Social::like('alice', $id)->isSuccess); $test->assertTrue(Social::boost('alice', $id)->isSuccess); $test->assertTrue(Social::post('alice', '@bob@b.example いいね!', ['inReplyTo' => $id])->isSuccess); $pump(); $use('B'); $note = Notes::get(Fedi::localNoteId($id)[1]); $test->assertCount(1, $note['likes']); $test->assertCount(1, $note['boosts']); $test->assertCount(1, $note['replies']); $types = array_column(Actor::notifications('bob'), 'type'); foreach (['like', 'boost', 'reply'] as $t) $test->assertTrue(in_array($t, $types, true), "通知「{$t}」が無い"); // 返信は返信先のスレッドに出る $test->assertCount(1, Timeline::replies($id)); }); $test->it('取り消しも届くはず', function($test) use ($use, $pump): void { $use('A'); $id = Timeline::query('home', 'alice', ['limit' => 50])[0][count(Timeline::query('home', 'alice', ['limit' => 50])[0]) - 1]['object']['id']; $test->assertTrue(Social::unlike('alice', $id)->isSuccess); $test->assertTrue(Social::unboost('alice', $id)->isSuccess); $pump(); $use('B'); $note = Notes::get(Fedi::localNoteId($id)[1]); $test->assertCount(0, $note['likes']); $test->assertCount(0, $note['boosts']); }); $test->it('承認制のアカウントはリクエストを承認するまでフォローされないはず', function($test) use ($use, $pump, $alice, $carol): void { $use('A'); Actor::saveSettings('alice', ['locked' => true]); $test->assertTrue(Social::follow('carol', $alice)->isSuccess); $test->assertEquals('pending', Actor::following('carol')[$alice]['state']); $test->assertTrue(isset(Actor::requests('alice')[$carol])); $test->assertTrue(Social::answerRequest('alice', $carol, true)->isSuccess); $test->assertTrue(Actor::isFollowing('carol', $alice)); $test->assertTrue(Actor::isFollowedBy('alice', $carol)); Actor::saveSettings('alice', ['locked' => false]); }); $test->it('フォロワー限定とダイレクトは関係ある人だけ見えるはず', function($test) use ($use, $pump): void { $use('A'); $test->assertTrue(Social::post('alice', 'followers only', ['visibility' => 'followers'])->isSuccess); $test->assertTrue(Social::post('alice', '@bob@b.example secret', ['visibility' => 'direct'])->isSuccess); $pump(); $use('A'); $texts = fn($items) => array_map(fn($i) => Html::text($i['object']['content']), $items); $carolHome = $texts(Timeline::query('home', 'carol')[0]); $test->assertTrue(in_array('followers only', $carolHome, true), 'フォロワーに見えない'); $test->assertFalse(in_array('@bob secret', $carolHome, true), '関係無い人にダイレクトが見える'); $test->assertFalse(in_array('followers only', $texts(Timeline::query('local', null)[0]), true)); $test->assertFalse(in_array('followers only', $texts(Timeline::query('user', null, ['user' => 'alice'])[0]), true)); $use('B'); $bobHome = $texts(Timeline::query('home', 'bob')[0]); $test->assertTrue(in_array('@bob secret', $bobHome, true), 'ダイレクトが届かない'); // bobはaliceのフォロワーではない $test->assertFalse(in_array('followers only', $bobHome, true)); $test->assertTrue(in_array('mention', array_column(Actor::notifications('bob'), 'type'), true)); }); $test->it('編集と削除が届くはず', function($test) use ($use, $pump): void { $use('B'); $own = Timeline::query('user', 'bob', ['user' => 'bob'])[0][0]['object']['id']; $id = Fedi::localNoteId($own)[1]; $test->assertTrue(Social::edit('bob', $id, 'Edited #changed')->isSuccess); $pump(); $use('A'); $test->assertEquals('Edited #changed', Html::text(Remote::stored($own)['content'])); $test->assertCount(1, Timeline::query('tag', null, ['tag' => 'changed'])[0]); $test->assertCount(0, Timeline::query('tag', null, ['tag' => 'fedi'])[0]); $use('B'); $test->assertTrue(Social::delete('bob', $id)->isSuccess); $pump(); $use('A'); $test->assertNull(Remote::stored($own)); $test->assertFalse(in_array($own, array_map(fn($i) => $i['object']['id'], Timeline::query('federated', null)[0]), true)); }); $test->it('他人の投稿は編集・削除出来ないはず', function($test) use ($use): void { $use('A'); $mine = Timeline::query('user', 'alice', ['user' => 'alice'])[0][0]['object']['id']; $id = Fedi::localNoteId($mine)[1]; $test->assertFalse(Social::edit('carol', $id, 'x')->isSuccess); $test->assertFalse(Social::delete('carol', $id)->isSuccess); }); $test->it('偽装したアクティビティは受け付けないはず', function($test) use ($use, $alice, $bob): void { $use('A'); // ローカルのアクターを名乗るリモートのアクティビティ $test->assertEquals(403, Inbox::handle(['type' => 'Like', 'actor' => $alice, 'object' => 'x'])[0]); // 他人の投稿として作られたCreate $test->assertEquals(403, Inbox::handle(['type' => 'Create', 'actor' => $bob, 'object' => ['type' => 'Note', 'id' => 'https://8.8.8.8/n/1', 'attributedTo' => 'https://8.8.8.8/ap/users/eve', 'to' => [Fedi::PUBLIC]]])[0]); // 違うサーバーのIDのNote $test->assertEquals(403, Inbox::handle(['type' => 'Create', 'actor' => $bob, 'object' => ['type' => 'Note', 'id' => 'https://9.9.9.9/n/1', 'attributedTo' => $bob, 'to' => [Fedi::PUBLIC]]])[0]); // 他人のフォローの取り消し $test->assertEquals(202, Inbox::handle(['type' => 'Undo', 'actor' => $bob, 'object' => ['type' => 'Follow', 'actor' => 'https://8.8.8.8/ap/users/eve', 'object' => $alice]])[0]); // 誰にも関係の無い投稿は保存しない Inbox::handle(['type' => 'Create', 'actor' => 'https://8.8.8.8/ap/users/stranger', 'object' => ['type' => 'Note', 'id' => 'https://8.8.8.8/n/spam', 'attributedTo' => 'https://8.8.8.8/ap/users/stranger', 'to' => [Fedi::PUBLIC], 'content' => 'spam']]); $test->assertNull(Remote::stored('https://8.8.8.8/n/spam')); }); $test->it('ブロックするとフォロー関係が無くなり、投稿が見えなくなるはず', function($test) use ($use, $pump, $alice, $bob): void { $use('B'); $test->assertTrue(Social::block('bob', $alice)->isSuccess); $pump(); $use('A'); $test->assertFalse(Actor::isFollowing('alice', $bob)); $use('B'); $test->assertFalse(Actor::isFollowedBy('bob', $alice)); $test->assertTrue(Actor::isBlocked('bob', $alice)); // ブロックしている人からのフォローは拒否する $use('A'); Social::follow('alice', $bob); $pump(); $use('A'); $test->assertFalse(isset(Actor::following('alice')[$bob])); $use('B'); $test->assertTrue(Social::unblock('bob', $alice)->isSuccess); $pump(); }); $test->it('サイト全体でドメインをブロック出来るはず', function($test) use ($use, $bob): void { $use('A'); $test->assertFalse(Social::blockInstance('1.1.1.1')->isSuccess); $test->assertTrue(Social::blockInstance('evil.example')->isSuccess); $test->assertTrue(Actor::isDomainBlocked('https://sub.evil.example/u/x')); $test->assertFalse(Actor::isDomainBlocked('https://notevil.example/u/x')); $test->assertEquals(403, Inbox::handle(['type' => 'Follow', 'actor' => 'https://evil.example/u/x', 'object' => 'x'])[0]); // 待ち行列にも入れない Delivery::enqueue('alice', ['type' => 'Note', 'id' => 'x'], ['https://evil.example/inbox']); $test->assertEquals(0, Delivery::pending()); $test->assertTrue(Social::blockInstance('evil.example', false)->isSuccess); $test->assertFalse(Actor::isDomainBlocked('https://evil.example/u/x')); }); $test->it('通報を記録し、相手のサーバーにも送るはず', function($test) use ($use, $pump, $bob): void { $use('A'); $test->assertTrue(Social::report('alice', $bob, [], '迷惑です')->isSuccess); $test->assertEquals('迷惑です', Social::reports()[0]['content']); $pump(); $use('B'); $test->assertTrue((bool)Social::reports()[0]['remote']); $test->assertEquals('迷惑です', Social::reports()[0]['content']); }); $test->it('アカウントの削除で全部から消えるはず', function($test) use ($use, $pump, $bob): void { $use('A'); Social::follow('alice', $bob); $pump(); $use('A'); $test->assertTrue(Actor::isFollowing('alice', $bob)); Inbox::handle(['type' => 'Delete', 'actor' => $bob, 'object' => $bob]); $test->assertFalse(isset(Actor::following('alice')[$bob])); $test->assertNull(Remote::cachedActor($bob)); }); $test->it('待ち行列は失敗したら間隔を空けて再試行するはず', function($test) use ($use): void { $use('A'); foreach (glob(Fedi::path('queue/').'*.json') ?: [] as $f) unlink($f); $failing = new class extends Delivery { public static int $calls = 0; public static bool $ok = false; protected static function send(string $from, string $inbox, array $activity): bool { self::$calls++; return self::$ok; } protected static function runBackfill(int $budget): int { return 0; } }; $failing::enqueue('alice', ['type' => 'Like', 'id' => 'x'], ['https://9.9.9.9/inbox']); $res = $failing::run(); $test->assertEquals(['sent' => 0, 'failed' => 1, 'dropped' => 0, 'backfilled' => 0], $res->data); // 次の時刻までは送らない $failing::run(); $test->assertEquals(1, $failing::$calls); $job = glob(Fedi::path('queue/').'*.json')[0]; $data = Fedi::read($job); $data['next'] = time() - 1; Fedi::write($job, $data); $failing::$ok = true; $test->assertEquals(['sent' => 1, 'failed' => 0, 'dropped' => 0, 'backfilled' => 0], $failing::run()->data); $test->assertEquals(0, Delivery::pending()); }); $test->it('古いリモートの投稿を消すはず', function($test) use ($use): void { $use('A'); $old = ['id' => 'https://8.8.8.8/n/old', 'type' => 'Note', 'attributedTo' => 'https://8.8.8.8/ap/users/x', 'to' => [Fedi::PUBLIC], 'content' => 'old', 'published' => Fedi::now(time() - 200 * 86400)]; Remote::store($old); Timeline::add($old); $res = Timeline::cleanup(90); $test->assertTrue($res->data['removed'] >= 1); $test->assertNull(Remote::stored($old['id'])); // ローカルの投稿は消さない $test->assertTrue(count(Timeline::query('user', 'alice', ['user' => 'alice'])[0]) > 0); }); $test->it('GETの署名を検証出来るはず', function($test) use ($use, $alice): void { $use('A'); $key = Actor::privateKey('alice'); $test->assertNotNull($key); $ap = new class extends Activitypub { public array $docs = []; public function fetchRemoteJson(string $url): ?array { return $this->docs[$url] ?? null; } }; $ap->docs[$alice] = Actor::document(Fedi::localActor('alice')); $headers = []; foreach ($ap->signRequest('https://8.8.8.8/ap/users/bob/notes/x', '', $key, 'get', Actor::keyId('alice')) as $line) { [$k, $v] = explode(': ', $line, 2); $headers[$k] = $v; } $server = $_SERVER; try { $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/ap/users/bob/notes/x'; $_SERVER['HTTP_HOST'] = $headers['Host']; $_SERVER['HTTP_DATE'] = $headers['Date']; $_SERVER['HTTP_SIGNATURE'] = $headers['Signature']; $test->assertEquals($alice, $ap->signer()); $_SERVER['REQUEST_URI'] = '/ap/users/bob/notes/y'; $test->assertNull($ap->signer()); } finally { $_SERVER = $server; } }); }); // 後片付け Fedi::$baseUrl = Fedi::$dataDir = Fedi::$userDir = null; $rm = function(string $dir) use (&$rm): void { foreach (scandir($dir) ?: [] as $f) { if ($f === '.' || $f === '..') continue; is_dir("{$dir}/{$f}") ? $rm("{$dir}/{$f}") : @unlink("{$dir}/{$f}"); } @rmdir($dir); }; if (is_dir($tmp)) $rm($tmp); $test->printSummary();