eZ components PHP Cross Reference Developer Tools

Source: /Archive/tests/tar/ustar_tar_test.php - 283 lines - 8369 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @filesource
   6   * @package Archive
   7   * @version 1.4.1
   8   * @subpackage Tests
   9   */
  10  
  11  require_once ( "v7_tar_test.php" );
  12  
  13  /**
  14   * @package Archive
  15   * @version 1.4.1
  16   * @subpackage Tests
  17   */
  18  class ezcArchiveUstarTarTest extends ezcArchiveV7TarTest // Extend the V7 tests (should work with Ustar also)
  19  {
  20      protected function setUp()
  21      {
  22          date_default_timezone_set( "UTC" ); 
  23          $this->tarFormat = "ustar";
  24          $this->tarMimeFormat = ezcArchive::TAR_USTAR;
  25  
  26          $this->createTempDir( "ezcArchive_" );
  27  
  28          $this->file = $this->createTempFile( "tar_ustar_2_textfiles.tar" );
  29          $blockFile = new ezcArchiveBlockFile( $this->file );
  30          $this->archive = new ezcArchiveUstarTar( $blockFile );
  31  
  32          $this->complexFile = $this->createTempFile( "tar_ustar_file_dir_symlink_link.tar" );
  33          $blockFile = new ezcArchiveBlockFile( $this->complexFile );
  34          $this->complexArchive = new ezcArchiveUstarTar( $blockFile );
  35          unset( $blockFile );
  36  
  37          $this->setUsersGid();
  38      }
  39  
  40      protected function tearDown()
  41      {
  42          unset( $this->archive );
  43          unset( $this->file );
  44          unset( $this->complexArchive ) ;
  45          unset( $this->complexFile );
  46          $this->removeTempDir();
  47      }
  48  
  49      /*
  50      public function testTarType()
  51      {
  52          $ustarFile = new ezcArchiveBlockFile( dirname( __FILE__) . "/../data/tar_ustar_2_textfiles.tar" );
  53          $v7File = new ezcArchiveBlockFile( dirname( __FILE__) . "/../data/tar_v7_2_textfiles.tar" );
  54  
  55          $this->assertEquals( ezcArchive::CAN_READ_AND_WRITE, ezcArchiveUstarTar::canHandle( $ustarFile ) );
  56          $this->assertEquals( ezcArchive::NONE, ezcArchiveUstarTar::canHandle( $v7File ) );
  57      }
  58      */
  59  
  60      public function testLongFileName()
  61      {
  62          if ( !$this->canWrite )
  63          {
  64              return;
  65          }
  66  
  67          $dir = $this->getTempDir();
  68  
  69          $filename = "";
  70  
  71          if ( $this->isWindows() )
  72          {
  73              $pathItemLen = 50;  // long but not looong path. Windows max path = 255.
  74          }
  75          else
  76          {
  77              $pathItemLen = 70;
  78          }
  79  
  80          for ( $i = 0; $i < $pathItemLen; $i++ )
  81          {
  82              $filename .= ( $i % 10 );
  83          }
  84  
  85          mkdir( "$dir/$filename" );
  86          mkdir( "$dir/$filename/$filename" );
  87  
  88          touch( "$dir/$filename/$filename/$filename" );
  89  
  90          $bf = new ezcArchiveBlockFile( "$dir/myarchive.tar", true );
  91          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat ); 
  92  
  93          $archive->appendToCurrent( "$dir/$filename/$filename/$filename", $dir );
  94          $archive->close();
  95  
  96          exec( "tar -cf $dir/gnutar.tar --format=" . $this->tarFormat . " -C $dir $filename/$filename/$filename" );
  97          $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( "$dir/myarchive.tar" ) );
  98  
  99          unset( $archive );
 100          unset( $bf );
 101      }
 102  
 103      public function testLongFilenameException()
 104      {
 105          if ( !$this->canWrite )
 106          {
 107              return;
 108          }
 109  
 110          $dir = $this->getTempDir();
 111  
 112          $filename = "";
 113  
 114          // $filename too long.
 115          for ( $i = 0; $i < 101; $i++ )
 116          {
 117              $filename .= ( $i % 10 );
 118          }
 119  
 120          touch( "$dir/$filename" );
 121  
 122          $bf = new ezcArchiveBlockFile( "$dir/myarchive.tar", true );
 123          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat ); 
 124  
 125          try
 126          {
 127              $archive->appendToCurrent( "$dir/$filename", $dir );
 128              $this->fail( "Expected a 'filename too long' exception." );
 129          }
 130          catch ( ezcArchiveException $e )
 131          {
 132              // Okay.
 133          }
 134      }
 135  
 136      public function testExtractCharacterDevice()
 137      {
 138          $dir = $this->getTempDir();
 139  
 140          // Can we create a character device?
 141          if (!ezcBaseFeatures::hasFunction( 'posix_mknod' ) ||
 142              !posix_mknod( "$dir/can_create_character_device", POSIX_S_IFCHR | 0777, 5, 1 ) )
 143          {
 144              // Failed, skip the test.
 145              return;
 146          }
 147  
 148          unlink( "$dir/can_create_character_device" );
 149  
 150          $org = dirname(__FILE__) . "/../data/tar_ustar_character_device.tar";
 151          copy( $org, $dir . "/tar_ustar_character_device.tar" );
 152  
 153          $bf = new ezcArchiveBlockFile( $dir . "/tar_ustar_character_device.tar" );
 154          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat );
 155  
 156          $archive->extractCurrent( $dir );
 157  
 158          // Zero device should be here..
 159          $this->assertEquals( "\0\0\0\0\0\0\0\0\0\0", file_get_contents( "$dir/myzero", false, null, 0, 10 ) );
 160  
 161          unlink( "$dir/myzero" );
 162      }
 163  
 164      public function testExtractFIFO()
 165      {
 166          $dir = $this->getTempDir();
 167  
 168          $org = dirname(__FILE__) . "/../data/tar_ustar_fifo.tar";
 169          copy( $org, $dir . "/tar_ustar_fifo.tar" );
 170  
 171          $bf = new ezcArchiveBlockFile( $dir . "/tar_ustar_fifo.tar" );
 172          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat );
 173  
 174          $archive->extractCurrent( $dir );
 175  
 176          // Zero device should be here..
 177          clearstatcache();
 178          $this->assertTrue( file_exists( "$dir/myfifo" ) );
 179  
 180          unlink( "$dir/myfifo" );
 181      }
 182  
 183      public function testAppendToCurrentAtEndOfArchive()
 184      {
 185          if ( !$this->canWrite )
 186          {
 187              return;
 188          }
 189  
 190          $dir = $this->getTempDir();
 191  
 192          $bf = new ezcArchiveBlockFile( $this->file );
 193          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat );
 194          $archive->extractCurrent( $dir );
 195  
 196          copy( $this->file, $dir . "/gnutar.tar" );
 197  
 198          $archive->seek( 0, SEEK_END ); // File number two.
 199          $archive->appendToCurrent( "$dir/file1.txt", $dir );
 200          $archive->close();
 201  
 202          // Do the same with gnu tar.
 203          exec( "tar -rf $dir/gnutar.tar --format=" . $this->tarFormat . " -C $dir file1.txt" );
 204  
 205          $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( $this->file ) );
 206      }
 207  
 208      public function testAppendToCurrentCharacterDevice()
 209      {
 210          if ( !$this->canWrite )
 211          {
 212              return;
 213          }
 214  
 215          $dir = $this->getTempDir();
 216  
 217          $chartar = "$dir/my_character_device.tar";
 218          $bf = new ezcArchiveBlockFile( $chartar, true );
 219  
 220          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat ); 
 221  
 222          // Appending an character device.
 223          try
 224          {
 225              $archive->appendToCurrent( "/dev/zero", "/dev/" );
 226              $archive->close();
 227  
 228              // Do the same with gnu tar.
 229              exec( "tar -cf $dir/gnutar.tar --format=" . $this->tarFormat . " -C /dev/ zero" );
 230              $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( $chartar ) );
 231          }
 232          catch ( ezcArchiveException $e )
 233          {
 234              if ( $e->getMessage() != "Cannot add a device to the TAR because the device type cannot be determined. Your system / PHP version does not support 'st_blksize'." )
 235              {
 236                  $this->fail( "Unexpected exception: " . $e->getMessage() );
 237              }
 238  
 239              $s = stat( "/dev/zero" );
 240              if ( $s["rdev"] != -1 )
 241              {
 242                  $this->fail( "This exception is only valid when the stat()['rdev'] is not available." );
 243              }
 244          }
 245      }
 246  
 247      public function testAppendToCurrentFifo()
 248      {
 249          if ( !$this->canWrite )
 250          {
 251              return;
 252          }
 253  
 254          if ( !ezcBaseFeatures::hasFunction( 'posix_mknod' ) )
 255          {
 256              return;
 257          }
 258  
 259          $dir = $this->getTempDir();
 260  
 261          posix_mknod( "$dir/myfifo", POSIX_S_IFIFO );
 262  
 263          $fifo = "$dir/my_fifo.tar";
 264          $bf = new ezcArchiveBlockFile( $fifo, true );
 265  
 266          $archive = ezcArchive::getTarInstance( $bf, $this->tarMimeFormat );
 267  
 268          $archive->appendToCurrent( "$dir/myfifo", "$dir" );
 269          $archive->close();
 270  
 271          // Do the same with gnu tar.
 272          exec( "tar -cf $dir/gnutar.tar --format=" . $this->tarFormat . " -C $dir/ myfifo" );
 273          $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( $fifo ) );
 274  
 275          unlink( "$dir/myfifo" );
 276      }
 277  
 278      public static function suite()
 279      {
 280          return new PHPUnit_Framework_TestSuite( __CLASS__ );
 281      }
 282  }
 283  ?>

title

Description

title

Description

title

Description

title

title

Body