Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Tests/Bzip2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Joomla\Archive\Bzip2 as ArchiveBzip2;
use Joomla\Test\TestHelper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestDox;

/**
Expand Down Expand Up @@ -78,6 +79,68 @@ public function testExtractWithStreams()
@unlink($this->outputPath . '/logo-bz2.png');
}

/**
* @return array<string, array{string}>
*/
public static function dataFalsyContent(): array
{
return [
'empty file' => [''],
'file with 0' => ['0'],
];
}

#[DataProvider('dataFalsyContent')]
#[TestDox('A file whose content is empty or "0" can be extracted')]
public function testExtractFalsyContent(string $content)
{
if (!ArchiveBzip2::isSupported()) {
$this->markTestSkipped('Bzip2 files can not be extracted.');
}

$archive = $this->outputPath . '/falsy.txt.bz2';
$destination = $this->outputPath . '/falsy.txt';

file_put_contents($archive, bzcompress($content));

try {
$this->assertTrue((new ArchiveBzip2())->extract($archive, $destination));
$this->assertSame($content, file_get_contents($destination));
} finally {
@unlink($archive);
@unlink($destination);
}
}

#[TestDox('Corrupt data is reported as an error and nothing is written')]
public function testExtractCorruptData()
{
if (!ArchiveBzip2::isSupported()) {
$this->markTestSkipped('Bzip2 files can not be extracted.');
}

$archive = $this->outputPath . '/corrupt.txt.bz2';
$destination = $this->outputPath . '/corrupt.txt';

file_put_contents($archive, 'BZh91AY&SYgarbagegarbage');

try {
@(new ArchiveBzip2())->extract($archive, $destination);

$exception = null;
} catch (\RuntimeException $exception) {
}

$written = is_file($destination);

@unlink($archive);
@unlink($destination);

$this->assertInstanceOf(\RuntimeException::class, $exception);
$this->assertSame('Unable to decompress data', $exception->getMessage());
$this->assertFalse($written);
}

#[TestDox('The adapter detects if the environment is supported')]
public function testIsSupported()
{
Expand Down
63 changes: 63 additions & 0 deletions Tests/GzipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Joomla\Archive\Gzip as ArchiveGzip;
use Joomla\Test\TestHelper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\TestDox;

/**
Expand Down Expand Up @@ -80,6 +81,68 @@ public function testExtractWithStreams()
@unlink($this->outputPath . '/logo-gz.png');
}

/**
* @return array<string, array{string}>
*/
public static function dataFalsyContent(): array
{
return [
'empty file' => [''],
'file with 0' => ['0'],
];
}

#[DataProvider('dataFalsyContent')]
#[TestDox('A file whose content is empty or "0" can be extracted')]
public function testExtractFalsyContent(string $content)
{
if (!ArchiveGzip::isSupported()) {
$this->markTestSkipped('Gzip files can not be extracted.');
}

$archive = $this->outputPath . '/falsy.txt.gz';
$destination = $this->outputPath . '/falsy.txt';

file_put_contents($archive, gzencode($content));

try {
$this->assertTrue((new ArchiveGzip())->extract($archive, $destination));
$this->assertSame($content, file_get_contents($destination));
} finally {
@unlink($archive);
@unlink($destination);
}
}

#[TestDox('Corrupt data is reported as an error and nothing is written')]
public function testExtractCorruptData()
{
if (!ArchiveGzip::isSupported()) {
$this->markTestSkipped('Gzip files can not be extracted.');
}

$archive = $this->outputPath . '/corrupt.txt.gz';
$destination = $this->outputPath . '/corrupt.txt';

file_put_contents($archive, substr(gzencode('content'), 0, 10) . 'garbage');

try {
@(new ArchiveGzip())->extract($archive, $destination);

$exception = null;
} catch (\RuntimeException $exception) {
}

$written = is_file($destination);

@unlink($archive);
@unlink($destination);

$this->assertInstanceOf(\RuntimeException::class, $exception);
$this->assertSame('Unable to decompress data', $exception->getMessage());
$this->assertFalse($written);
}

#[TestDox('The adapter detects if the environment is supported')]
public function testIsSupported()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Bzip2.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function extract($archive, $destination)
$buffer = bzdecompress($this->data);
unset($this->data);

if (empty($buffer)) {
// An empty file or one containing "0" is valid; on error bzdecompress() returns false or an error code
if (!\is_string($buffer)) {
throw new \RuntimeException('Unable to decompress data');
}

Expand Down
3 changes: 2 additions & 1 deletion src/Gzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public function extract($archive, $destination)
$position = $this->getFilePosition();
$buffer = gzinflate(substr($this->data, $position, \strlen($this->data) - $position));

if (empty($buffer)) {
// An empty file or one containing "0" is valid, only false signals an error
if ($buffer === false) {
throw new \RuntimeException('Unable to decompress data');
}

Expand Down
Loading