2023-07-21 21:47:41 +00:00
|
|
|
<?php
|
|
|
|
// BencodeTest.php
|
|
|
|
// Created: 2023-07-21
|
2024-12-02 01:30:36 +00:00
|
|
|
// Updated: 2024-12-02
|
2023-07-21 21:47:41 +00:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2024-07-31 18:12:46 +00:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
2024-07-31 19:10:26 +00:00
|
|
|
use Index\Bencode\Bencode;
|
2023-07-21 21:47:41 +00:00
|
|
|
|
2024-07-31 18:12:46 +00:00
|
|
|
#[CoversClass(Bencode::class)]
|
2023-07-21 21:47:41 +00:00
|
|
|
final class BencodeTest extends TestCase {
|
|
|
|
public const TORRENT = 'ZDg6YW5ub3VuY2U0NTpodHRwczovL3RyYWNrZXIuZmxhc2hpaS5uZXQvYW5ub3VuY2UucGhwL21lb3c3OmNvbW1lbnQyNjp0aGlzIGlzIHRoZSBjb21tZW50cyBmaWVsZDEwOmNyZWF0ZWQgYnkxODpxQml0dG9ycmVudCB2NC41LjQxMzpjcmVhdGlvbiBkYXRlaTE2ODk5NzM2NjRlNDppbmZvZDY6bGVuZ3RoaTM5NDg4ZTQ6bmFtZTEwOmF1dGlzbS5qcGcxMjpwaWVjZSBsZW5ndGhpMTYzODRlNjpwaWVjZXM2MDpJCQyONUjTseqtXs9g1xIC+yWtTGjuWTho+hSXmOUkSAiKnD4vs5E9QewQ1NrXvVeg2xCRHYD/HL9RNRc3OnByaXZhdGVpMWVlZQ';
|
|
|
|
|
|
|
|
public function testDecode(): void {
|
|
|
|
$decoded = Bencode::decode(base64_decode(self::TORRENT));
|
|
|
|
|
2024-12-02 01:30:36 +00:00
|
|
|
$this->assertIsArray($decoded);
|
2023-07-21 21:47:41 +00:00
|
|
|
$this->assertEquals(5, count($decoded));
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('announce', $decoded);
|
|
|
|
$this->assertEquals('https://tracker.flashii.net/announce.php/meow', $decoded['announce']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('creation date', $decoded);
|
|
|
|
$this->assertEquals(1689973664, $decoded['creation date']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('info', $decoded);
|
|
|
|
|
2024-12-02 01:30:36 +00:00
|
|
|
$this->assertIsArray($decoded['info']);
|
2023-07-21 21:47:41 +00:00
|
|
|
$this->assertArrayHasKey('private', $decoded['info']);
|
|
|
|
$this->assertEquals(1, $decoded['info']['private']);
|
|
|
|
|
2024-08-01 22:16:38 +00:00
|
|
|
$decoded = Bencode::decode(base64_decode(self::TORRENT), associative: false);
|
2023-07-21 21:47:41 +00:00
|
|
|
|
2024-12-02 01:30:36 +00:00
|
|
|
$this->assertIsObject($decoded);
|
|
|
|
$this->assertObjectHasProperty('announce', $decoded);
|
|
|
|
$this->assertEquals('https://tracker.flashii.net/announce.php/meow', $decoded->announce); // @phpstan-ignore-line: checked above
|
|
|
|
$this->assertObjectHasProperty('creation date', $decoded);
|
|
|
|
$this->assertEquals(1689973664, $decoded->{'creation date'}); // @phpstan-ignore-line: checked above
|
|
|
|
$this->assertObjectHasProperty('comment', $decoded);
|
|
|
|
$this->assertEquals('this is the comments field', $decoded->comment); // @phpstan-ignore-line: checked above
|
|
|
|
$this->assertObjectHasProperty('info', $decoded);
|
|
|
|
$this->assertIsObject($decoded->info); // @phpstan-ignore-line: checked above
|
|
|
|
$this->assertObjectHasProperty('private', $decoded->info);
|
|
|
|
$this->assertEquals(1, $decoded->info->private); // @phpstan-ignore-line: checked above
|
2023-07-21 21:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEncode(): void {
|
|
|
|
$original = base64_decode(self::TORRENT);
|
|
|
|
$decoded = Bencode::decode($original);
|
|
|
|
$encoded = Bencode::encode($decoded);
|
|
|
|
|
|
|
|
$this->assertEquals($original, $encoded);
|
|
|
|
}
|
|
|
|
}
|