Skip to content

Commit 74d7442

Browse files
committed
testing: run created archives through native tools
This should ensure the files we create are actually understood by common tools.
1 parent 2e7533b commit 74d7442

2 files changed

Lines changed: 128 additions & 48 deletions

File tree

tests/tar.test.php

Lines changed: 82 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function test_createdynamic()
6161
{
6262
$tar = new Tar();
6363

64-
$dir = dirname(__FILE__).'/tar';
64+
$dir = dirname(__FILE__) . '/tar';
6565
$tdir = ltrim($dir, '/');
6666

6767
$tar->create();
@@ -99,9 +99,9 @@ public function test_createfile()
9999
{
100100
$tar = new Tar();
101101

102-
$dir = dirname(__FILE__).'/tar';
102+
$dir = dirname(__FILE__) . '/tar';
103103
$tdir = ltrim($dir, '/');
104-
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest');
104+
$tmp = tempnam(sys_get_temp_dir(), 'dwtartest');
105105

106106
$tar->create($tmp);
107107
$tar->AddFile("$dir/testdata1.txt");
@@ -137,10 +137,10 @@ public function test_createfile()
137137
*/
138138
public function test_tarcontent()
139139
{
140-
$dir = dirname(__FILE__).'/tar';
140+
$dir = dirname(__FILE__) . '/tar';
141141

142142
foreach ($this->extensions as $ext) {
143-
$tar = new Tar();
143+
$tar = new Tar();
144144
$file = "$dir/test.$ext";
145145

146146
$tar->open($file);
@@ -167,7 +167,7 @@ public function test_dogfood()
167167

168168
$tar = new Tar();
169169
$tar->create($archive);
170-
foreach($input as $path) {
170+
foreach ($input as $path) {
171171
$file = basename($path);
172172
$tar->addFile($path, $file);
173173
}
@@ -182,33 +182,67 @@ public function test_dogfood()
182182
$this->assertFileExists("$extract/Zip.php");
183183
$this->assertFileNotExists("$extract/FileInfo.php");
184184

185+
$this->nativeCheck($archive, $ext);
186+
185187
self::rdelete($extract);
186188
unlink($archive);
187189
}
188190
}
189191

192+
/**
193+
* Test the given archive with a native tar installation (if available)
194+
*
195+
* @param $archive
196+
* @param $ext
197+
*/
198+
protected function nativeCheck($archive, $ext)
199+
{
200+
if (!is_executable('/usr/bin/tar')) {
201+
return;
202+
}
203+
204+
$switch = array(
205+
'tar' => '-tf',
206+
'tgz' => '-tzf',
207+
'tar.gz' => '-tzf',
208+
'tbz' => '-tjf',
209+
'tar.bz2' => '-tjf',
210+
);
211+
$arg = $switch[$ext];
212+
$archive = escapeshellarg($archive);
213+
214+
$return = 0;
215+
$output = array();
216+
$ok = exec("/usr/bin/tar $arg $archive 2>&1 >/dev/null", $output, $return);
217+
$output = join("\n", $output);
218+
219+
$this->assertNotFalse($ok, "native tar execution for $archive failed:\n$output");
220+
$this->assertSame(0, $return, "native tar execution for $archive had non-zero exit code $return:\n$output");
221+
$this->assertSame('', $output, "native tar execution for $archive had non-empty output:\n$output");
222+
}
223+
190224
/**
191225
* Extract the prebuilt tar files
192226
*/
193227
public function test_tarextract()
194228
{
195-
$dir = dirname(__FILE__).'/tar';
196-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
229+
$dir = dirname(__FILE__) . '/tar';
230+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
197231

198232
foreach ($this->extensions as $ext) {
199-
$tar = new Tar();
233+
$tar = new Tar();
200234
$file = "$dir/test.$ext";
201235

202236
$tar->open($file);
203237
$tar->extract($out);
204238

205239
clearstatcache();
206240

207-
$this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
208-
$this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");
241+
$this->assertFileExists($out . '/tar/testdata1.txt', "Extracted $file");
242+
$this->assertEquals(13, filesize($out . '/tar/testdata1.txt'), "Extracted $file");
209243

210-
$this->assertFileExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");
211-
$this->assertEquals(13, filesize($out.'/tar/foobar/testdata2.txt'), "Extracted $file");
244+
$this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
245+
$this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file");
212246

213247
self::rdelete($out);
214248
}
@@ -219,23 +253,23 @@ public function test_tarextract()
219253
*/
220254
public function test_compstripextract()
221255
{
222-
$dir = dirname(__FILE__).'/tar';
223-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
256+
$dir = dirname(__FILE__) . '/tar';
257+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
224258

225259
foreach ($this->extensions as $ext) {
226-
$tar = new Tar();
260+
$tar = new Tar();
227261
$file = "$dir/test.$ext";
228262

229263
$tar->open($file);
230264
$tar->extract($out, 1);
231265

232266
clearstatcache();
233267

234-
$this->assertFileExists($out.'/testdata1.txt', "Extracted $file");
235-
$this->assertEquals(13, filesize($out.'/testdata1.txt'), "Extracted $file");
268+
$this->assertFileExists($out . '/testdata1.txt', "Extracted $file");
269+
$this->assertEquals(13, filesize($out . '/testdata1.txt'), "Extracted $file");
236270

237-
$this->assertFileExists($out.'/foobar/testdata2.txt', "Extracted $file");
238-
$this->assertEquals(13, filesize($out.'/foobar/testdata2.txt'), "Extracted $file");
271+
$this->assertFileExists($out . '/foobar/testdata2.txt', "Extracted $file");
272+
$this->assertEquals(13, filesize($out . '/foobar/testdata2.txt'), "Extracted $file");
239273

240274
self::rdelete($out);
241275
}
@@ -246,23 +280,23 @@ public function test_compstripextract()
246280
*/
247281
public function test_prefixstripextract()
248282
{
249-
$dir = dirname(__FILE__).'/tar';
250-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
283+
$dir = dirname(__FILE__) . '/tar';
284+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
251285

252286
foreach ($this->extensions as $ext) {
253-
$tar = new Tar();
287+
$tar = new Tar();
254288
$file = "$dir/test.$ext";
255289

256290
$tar->open($file);
257291
$tar->extract($out, 'tar/foobar/');
258292

259293
clearstatcache();
260294

261-
$this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
262-
$this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");
295+
$this->assertFileExists($out . '/tar/testdata1.txt', "Extracted $file");
296+
$this->assertEquals(13, filesize($out . '/tar/testdata1.txt'), "Extracted $file");
263297

264-
$this->assertFileExists($out.'/testdata2.txt', "Extracted $file");
265-
$this->assertEquals(13, filesize($out.'/testdata2.txt'), "Extracted $file");
298+
$this->assertFileExists($out . '/testdata2.txt', "Extracted $file");
299+
$this->assertEquals(13, filesize($out . '/testdata2.txt'), "Extracted $file");
266300

267301
self::rdelete($out);
268302
}
@@ -273,22 +307,22 @@ public function test_prefixstripextract()
273307
*/
274308
public function test_includeextract()
275309
{
276-
$dir = dirname(__FILE__).'/tar';
277-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
310+
$dir = dirname(__FILE__) . '/tar';
311+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
278312

279313
foreach ($this->extensions as $ext) {
280-
$tar = new Tar();
314+
$tar = new Tar();
281315
$file = "$dir/test.$ext";
282316

283317
$tar->open($file);
284318
$tar->extract($out, '', '', '/\/foobar\//');
285319

286320
clearstatcache();
287321

288-
$this->assertFileNotExists($out.'/tar/testdata1.txt', "Extracted $file");
322+
$this->assertFileNotExists($out . '/tar/testdata1.txt', "Extracted $file");
289323

290-
$this->assertFileExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");
291-
$this->assertEquals(13, filesize($out.'/tar/foobar/testdata2.txt'), "Extracted $file");
324+
$this->assertFileExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
325+
$this->assertEquals(13, filesize($out . '/tar/foobar/testdata2.txt'), "Extracted $file");
292326

293327
self::rdelete($out);
294328
}
@@ -299,22 +333,22 @@ public function test_includeextract()
299333
*/
300334
public function test_excludeextract()
301335
{
302-
$dir = dirname(__FILE__).'/tar';
303-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
336+
$dir = dirname(__FILE__) . '/tar';
337+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
304338

305339
foreach ($this->extensions as $ext) {
306-
$tar = new Tar();
340+
$tar = new Tar();
307341
$file = "$dir/test.$ext";
308342

309343
$tar->open($file);
310344
$tar->extract($out, '', '/\/foobar\//');
311345

312346
clearstatcache();
313347

314-
$this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
315-
$this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");
348+
$this->assertFileExists($out . '/tar/testdata1.txt', "Extracted $file");
349+
$this->assertEquals(13, filesize($out . '/tar/testdata1.txt'), "Extracted $file");
316350

317-
$this->assertFileNotExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");
351+
$this->assertFileNotExists($out . '/tar/foobar/testdata2.txt', "Extracted $file");
318352

319353
self::rdelete($out);
320354
}
@@ -336,7 +370,7 @@ public function test_filetype()
336370
$this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tar.BZ2'));
337371
$this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tar.bz2'));
338372

339-
$dir = dirname(__FILE__).'/tar';
373+
$dir = dirname(__FILE__) . '/tar';
340374
$this->assertEquals(Tar::COMPRESS_NONE, $tar->filetype("$dir/test.tar"));
341375
$this->assertEquals(Tar::COMPRESS_GZIP, $tar->filetype("$dir/test.tgz"));
342376
$this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype("$dir/test.tbz"));
@@ -350,16 +384,16 @@ public function test_filetype()
350384
*/
351385
public function test_longpathextract()
352386
{
353-
$dir = dirname(__FILE__).'/tar';
354-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
387+
$dir = dirname(__FILE__) . '/tar';
388+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
355389

356390
foreach (array('ustar', 'gnu') as $format) {
357391
$tar = new Tar();
358392
$tar->open("$dir/longpath-$format.tgz");
359393
$tar->extract($out);
360394

361395
$this->assertFileExists(
362-
$out.'/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt'
396+
$out . '/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt'
363397
);
364398

365399
self::rdelete($out);
@@ -454,8 +488,8 @@ public function test_createlongpathgnu()
454488
*/
455489
public function test_tarbomb()
456490
{
457-
$dir = dirname(__FILE__).'/tar';
458-
$out = sys_get_temp_dir().'/dwtartest'.md5(time());
491+
$dir = dirname(__FILE__) . '/tar';
492+
$out = sys_get_temp_dir() . '/dwtartest' . md5(time());
459493

460494
$tar = new Tar();
461495

@@ -465,7 +499,7 @@ public function test_tarbomb()
465499
clearstatcache();
466500

467501
$this->assertFileExists(
468-
$out.'/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt'
502+
$out . '/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt'
469503
);
470504

471505
self::rdelete($out);
@@ -476,7 +510,7 @@ public function test_tarbomb()
476510
*/
477511
public function test_zerofile()
478512
{
479-
$dir = dirname(__FILE__).'/tar';
513+
$dir = dirname(__FILE__) . '/tar';
480514
$tar = new Tar();
481515
$tar->setCompression(0);
482516
$tar->create();
@@ -502,7 +536,7 @@ public function test_zerodata()
502536
*/
503537
public function test_blockfile()
504538
{
505-
$dir = dirname(__FILE__).'/tar';
539+
$dir = dirname(__FILE__) . '/tar';
506540
$tar = new Tar();
507541
$tar->setCompression(0);
508542
$tar->create();

tests/zip.test.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,55 @@ public function test_dogfood()
145145
$this->assertFileExists("$extract/Zip.php");
146146
$this->assertFileNotExists("$extract/FileInfo.php");
147147

148+
$this->nativeCheck($archive);
149+
$this->native7ZipCheck($archive);
150+
148151
self::rdelete($extract);
149152
unlink($archive);
153+
}
154+
155+
/**
156+
* Test the given archive with a native zip installation (if available)
157+
*
158+
* @param $archive
159+
*/
160+
protected function nativeCheck($archive)
161+
{
162+
if (!is_executable('/usr/bin/zipinfo')) {
163+
return;
164+
}
165+
$archive = escapeshellarg($archive);
166+
167+
$return = 0;
168+
$output = array();
169+
$ok = exec("/usr/bin/zipinfo $archive 2>&1 >/dev/null", $output, $return);
170+
$output = join("\n", $output);
171+
172+
$this->assertNotFalse($ok, "native zip execution for $archive failed:\n$output");
173+
$this->assertSame(0, $return, "native zip execution for $archive had non-zero exit code $return:\n$output");
174+
$this->assertSame('', $output, "native zip execution for $archive had non-empty output:\n$output");
175+
}
176+
177+
/**
178+
* Test the given archive with a native 7zip installation (if available)
179+
*
180+
* @param $archive
181+
*/
182+
protected function native7ZipCheck($archive)
183+
{
184+
if (!is_executable('/usr/bin/7z')) {
185+
return;
186+
}
187+
$archive = escapeshellarg($archive);
188+
189+
$return = 0;
190+
$output = array();
191+
$ok = exec("/usr/bin/7z t $archive 2>&1 >/dev/null", $output, $return);
192+
$output = join("\n", $output);
150193

194+
$this->assertNotFalse($ok, "native 7zip execution for $archive failed:\n$output");
195+
$this->assertSame(0, $return, "native 7zip execution for $archive had non-zero exit code $return:\n$output");
196+
$this->assertSame('', $output, "native 7zip execution for $archive had non-empty output:\n$output");
151197
}
152198

153199
/**

0 commit comments

Comments
 (0)