From 9fb5cd12be57f4108de743da9004b6545a61d1ad Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 27 Jan 2026 09:43:44 +0100 Subject: [PATCH] tests: A few more xchan tests --- tests/unit/includes/XchanTest.php | 62 +++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/tests/unit/includes/XchanTest.php b/tests/unit/includes/XchanTest.php index 2d7261c5b..c6a9bdff4 100644 --- a/tests/unit/includes/XchanTest.php +++ b/tests/unit/includes/XchanTest.php @@ -8,27 +8,67 @@ namespace Zotlabs\Tests\Unit; +use PHPUnit\Framework\Attributes\Before; use Zotlabs\Lib\Libzot; class XChanTest extends UnitTestCase { - public function testXChanFetchShouldRejectInvalidArgs(): void { - $addr = 'example'; - $guid = Libzot::new_uid($addr); - $hash = Libzot::make_xchan_hash($guid, 'dummy-public-key'); + private string $addr; + private string $guid; + private string $hash; + + #[Before] + public function populateXChans(): void { + $this->addr = 'example'; + $this->guid = Libzot::new_uid($this->addr); + $this->hash = Libzot::make_xchan_hash($this->guid, 'dummy-public-key'); xchan_store_lowlevel([ - 'xchan_hash' => $hash, - 'xchan_guid' => $guid, - 'xchan_addr' => $addr, + 'xchan_hash' => $this->hash, + 'xchan_guid' => $this->guid, + 'xchan_addr' => $this->addr, ]); + } + public function testXChanFetchReturnsStoredXchan(): void { + $byHash = xchan_fetch(['hash' => $this->hash]); + $this->assertIsArray($byHash); + $this->assertEquals($this->addr, $byHash['address']); + $this->assertEquals($this->guid, $byHash['guid']); + $this->assertEquals($this->hash, $byHash['hash']); + + $byGuid = xchan_fetch(['guid' => $this->guid]); + $this->assertIsArray($byGuid); + $this->assertEquals($this->addr, $byGuid['address']); + $this->assertEquals($this->guid, $byGuid['guid']); + $this->assertEquals($this->hash, $byGuid['hash']); + + $byAddr = xchan_fetch(['address' => $this->addr]); + $this->assertIsArray($byAddr); + $this->assertEquals($this->addr, $byAddr['address']); + $this->assertEquals($this->guid, $byAddr['guid']); + $this->assertEquals($this->hash, $byAddr['hash']); + } + + public function testXChanFetchShouldRejectInvalidArgs(): void { // Trivial SQL Injection - $this->assertFalse(xchan_fetch(['hash' => "{$hash}' or 1=1; -- "])); - $this->assertFalse(xchan_fetch(['guid' => "{$guid}' or 1=1; -- "])); - $this->assertFalse(xchan_fetch(['address' => "{$addr}' or 1=1; -- "])); + $this->assertFalse(xchan_fetch(['hash' => "{$this->hash}' or 1=1; -- "])); + $this->assertFalse(xchan_fetch(['guid' => "{$this->guid}' or 1=1; -- "])); + $this->assertFalse(xchan_fetch(['address' => "{$this->addr}' or 1=1; -- "])); // Not a valid key - $this->assertFalse(xchan_fetch(['wrongkey' => $hash])); + $this->assertFalse(xchan_fetch(['wrongkey' => $this->hash])); + + // Empty values + $this->assertFalse(xchan_fetch(['hash' => ''])); + $this->assertFalse(xchan_fetch(['guid' => ''])); + $this->assertFalse(xchan_fetch(['address' => ''])); + + // Non existing xchan hash/guid/address + $this->assertFalse(xchan_fetch([ + 'hash' => Libzot::make_xchan_hash(Libzot::new_uid('nobody'), 'another-dummy-key') + ])); + $this->assertFalse(xchan_fetch(['guid' => Libzot::new_uid('nobody')])); + $this->assertFalse(xchan_fetch(['address' => 'nobody'])); } }