77 lines
3.2 KiB
PHP
77 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Ide;
|
|
use JumPHP\Ide\Workspace\ByteRange;
|
|
use JumPHP\Ide\Workspace\LocalWorkspace;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use JumPHP\Ide\Workspace\WorkspaceException;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class FileBytesTest extends TestCase
|
|
{
|
|
private string $dir;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dir = sys_get_temp_dir() . '/jumphp-bytes-' . bin2hex(random_bytes(4));
|
|
mkdir($this->dir);
|
|
file_put_contents("$this->dir/data.bin", implode('', array_map('chr', range(0, 255))));
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
exec('rm -rf ' . escapeshellarg($this->dir));
|
|
}
|
|
|
|
public function testARangeOfAFileOfTheDiskComesBackInBase64WithTheFileSize(): void
|
|
{
|
|
$ide = new Ide(new LocalWorkspace($this->dir));
|
|
$r = $ide->handle('file.bytes', ['path' => 'data.bin', 'offset' => 16, 'length' => 4]);
|
|
self::assertSame(['path' => 'data.bin', 'size' => 256, 'offset' => 16, 'length' => 4], array_diff_key($r, ['base64' => 1]));
|
|
self::assertSame("\x10\x11\x12\x13", base64_decode($r['base64']));
|
|
}
|
|
|
|
public function testTheRangeStopsAtTheEndOfTheFileAndNeverGoesBeforeItsStart(): void
|
|
{
|
|
$ide = new Ide(new LocalWorkspace($this->dir));
|
|
$end = $ide->handle('file.bytes', ['path' => 'data.bin', 'offset' => 250, 'length' => 100]);
|
|
self::assertSame(6, $end['length']);
|
|
self::assertSame("\xfa\xfb\xfc\xfd\xfe\xff", base64_decode($end['base64']));
|
|
$past = $ide->handle('file.bytes', ['path' => 'data.bin', 'offset' => 9999, 'length' => 10]);
|
|
self::assertSame([256, 0, ''], [$past['offset'], $past['length'], $past['base64']]);
|
|
$before = $ide->handle('file.bytes', ['path' => 'data.bin', 'offset' => -5, 'length' => 1]);
|
|
self::assertSame([0, "\x00"], [$before['offset'], base64_decode($before['base64'])]);
|
|
}
|
|
|
|
public function testOneCallReturnsAtMostOneMegabyte(): void
|
|
{
|
|
file_put_contents("$this->dir/big.bin", str_repeat('x', ByteRange::MAX + 10));
|
|
$r = ByteRange::file("$this->dir/big.bin", 0, ByteRange::MAX * 4);
|
|
self::assertSame([ByteRange::MAX + 10, ByteRange::MAX], [$r['size'], $r['length']]);
|
|
}
|
|
|
|
public function testAWorkspaceWithoutDiskGivesItsContent(): void
|
|
{
|
|
$ide = new Ide(new MemoryWorkspace(['notes.txt' => 'Hello, world']));
|
|
$r = $ide->handle('file.bytes', ['path' => 'notes.txt', 'offset' => 7, 'length' => 5]);
|
|
self::assertSame(['world', 12], [base64_decode($r['base64']), $r['size']]);
|
|
}
|
|
|
|
public function testAMissingFileOrAPathOutsideTheProjectIsRefused(): void
|
|
{
|
|
$ide = new Ide(new LocalWorkspace($this->dir));
|
|
$this->expectException(WorkspaceException::class);
|
|
$ide->handle('file.bytes', ['path' => '../../etc/passwd', 'offset' => 0, 'length' => 10]);
|
|
}
|
|
|
|
public function testABigFileIsNotReadWholeToBeOpened(): void
|
|
{
|
|
$h = fopen("$this->dir/huge.dat", 'wb');
|
|
ftruncate($h, 6_000_000);
|
|
fclose($h);
|
|
$ide = new Ide(new LocalWorkspace($this->dir));
|
|
self::assertSame(['path' => 'huge.dat', 'text' => false, 'size' => 6_000_000], $ide->handle('file.read', ['path' => 'huge.dat']));
|
|
}
|
|
}
|