Last active
December 20, 2015 03:39
-
-
Save felds/6065203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @param string $dir The dir to be removed | |
| */ | |
| function deleteTempDir($dir) | |
| { | |
| return `rm -fr $dir`; | |
| } | |
| /** | |
| * @author Luiz “Felds” Liscia (felds@seepix.comm.br) | |
| * @version 1.0.0 (2013-07-23) | |
| * | |
| * @param string $tar The tar file | |
| * @param array $expected How does the Tar should be? | |
| * @return boolean | |
| */ | |
| function checkTarContentsMd5($tar, array $expected) | |
| { | |
| // check args | |
| if (!is_file($tar)) { | |
| throw new RuntimeException("Tar $tar not found."); | |
| } | |
| if (!is_readable($tar)) { | |
| throw new RuntimeException("Tar $tar is unreadable."); | |
| } | |
| $tarBin = trim(`which tar`); | |
| if (!$tarBin) { | |
| throw new RuntimeException("tar binary not found."); | |
| } | |
| // extract tar contents | |
| $baseDir = realpath(dirname($tar)); // dir in which the tar is located | |
| $tarName = basename($tar); // the name of the tar file without the path | |
| $tempDir = $baseDir . '/tmp/'; // the directory that will hold the files | |
| $tempTar = $tempDir . $tarName; // the temporary tar to be extracted | |
| @mkdir($tempDir, 0777, true); | |
| @copy($tar, $tempTar); | |
| register_shutdown_function('deleteTempDir', $tempDir); // mark the temp dir for death | |
| $cmd = sprintf( | |
| 'cd %s; tar xvf %s 2>&1 | grep -v /\\$ | cut -d" " -f2', | |
| escapeshellarg($tempDir), | |
| escapeshellarg($tarName) | |
| ); | |
| exec($cmd, $output, $returnVar); | |
| if ($returnVar !== 0) { | |
| throw new RuntimeException("Command failed."); | |
| } | |
| // prepare the array | |
| $md5Sums = array(); | |
| foreach ($output as $file) { | |
| $md5Sums[$file] = md5_file($tempDir . $file); | |
| } | |
| // compare! | |
| if (count($md5Sums) !== count($expected)) { | |
| $expectedCount = count($expected); | |
| $actualCount = count($md5Sums); | |
| throw new LengthException("The number of expected files ($expectedCount) doesn't match the number of files in the Tar archive ($actualCount)."); | |
| } | |
| foreach ($md5Sums as $file => $hash) { | |
| if (!array_key_exists($file, $expected)) { | |
| throw new RuntimeException("The file $file wasn't expected."); | |
| } | |
| if ($hash !== $expected[$file]) { | |
| throw new RuntimeException("The file $file has the wrong hash. (it is: $hash; it should be: {$expected[$file]})"); | |
| } | |
| } | |
| return true; | |
| } | |
| if (class_exists('PHPUnit_Framework_TestCase')) { | |
| class CheckTarContentsMd5FunctionTest extends PHPUnit_Framework_TestCase | |
| { | |
| public function setUp() | |
| { | |
| $this->tarFile = 'test.tgz'; | |
| $this->expected = array( | |
| 'file1.txt' => 'f737a087bca81f69a6048ec744c73e41', | |
| 'file2.txt' => '02063b9bf9d6e15ad61226fa4584aae0', | |
| 'file3.txt' => '5f20730ddc7a1fedbf265358f0ce4f26', | |
| 'folder/file-inside-the-folder.txt' => 'bc8ff7c3fe2a8d0fb07061c6d7ae8e8a', | |
| ); | |
| } | |
| public function testSuccess() | |
| { | |
| $success = checkTarContentsMd5($this->tarFile, $this->expected); | |
| $this->assertTrue($success); | |
| } | |
| /** | |
| * @expectedException LengthException | |
| **/ | |
| public function testFailByLength() | |
| { | |
| $failArray = $this->expected; | |
| array_pop($failArray); | |
| checkTarContentsMd5($this->tarFile, $failArray); | |
| } | |
| /** | |
| * @expectedException RuntimeException | |
| **/ | |
| public function testFailByHash() | |
| { | |
| $failArray = $this->expected; | |
| $failArray['file3.txt'] = '23456787654345678998765456'; | |
| checkTarContentsMd5($this->tarFile, $failArray); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment