Skip to content

Commit

Permalink
test an inconsistent zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
rcstr committed May 9, 2024
1 parent 4deb9dc commit 060fa17
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/tests/ZipBuilderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
class ZipBuilderHelper {
private $filename;
private $files = array();
/**
* @var string
*/
private $filepath;

/**
* @param string $filename The name of the zip file to be created.
*/
public function __construct( $filename ) {
$this->filename = $filename;
$this->filepath = __DIR__ . "/$this->filename";
}

/**
Expand Down Expand Up @@ -43,7 +48,7 @@ public function with_file( $source, $target ): self {
public function build(): string {
$zip = new ZipArchive();

if ( $zip->open( __DIR__ . "/$this->filename", ZipArchive::CREATE ) !== true ) {
if ( $zip->open( $this->get_file_path(), ZipArchive::CREATE ) !== true ) {
throw new RuntimeException( 'Could not create zip file.' );
}

Expand All @@ -57,10 +62,25 @@ public function build(): string {

clearstatcache();

if ( ! file_exists( __DIR__ . "/$this->filename" ) ) {
if ( ! file_exists( $this->get_file_path() ) ) {
throw new RuntimeException( 'Zip file was not created.' );
}

return __DIR__ . "/$this->filename";
return $this->get_file_path();
}

function corrupt() {
$data = file_get_contents( $this->get_file_path() );
if ( ! $data ) {
throw new \RuntimeException( 'Could not read zip file.' );
}

$data = substr_replace( $data, "", 20, 4 ); // Removing 4 bytes from offset 20

file_put_contents( $this->get_file_path(), $data );
}

public function get_file_path(): string {
return $this->filepath;
}
}
19 changes: 19 additions & 0 deletions src/tests/ZipValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,23 @@ function test_zip_with_invalid_file_inside_vendor_folder() {

$this->assertTrue( true );
}

function test_zip_created_by_Archive_Utility_OSX_is_invalid() {
$this->inject_request_builder_mock();

$file_name = 'zip-created-by-archive-utility-osx.zip';
$slug = 'zip-created-by-archive-utility-osx';

$zip_validator = App::make( ZipValidator::class );

$zip_file = new ZipBuilder( $file_name );
$zip_file->with_file( __DIR__ . '/plugin-entrypoint.php', $slug . '/plugin-entrypoint.php' )
->build();
$zip_file->corrupt();

$this->expectException( \UnexpectedValueException::class );
$this->expectExceptionMessage( 'Zip file is inconsistent. Zip files generated by the Archive Utility from macOS may be the cause.' );

$zip_validator->validate_zip( $zip_file->get_file_path() );
}
}

0 comments on commit 060fa17

Please sign in to comment.