~ubuntu-branches/ubuntu/utopic/libcommons-compress-java/utopic

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-11-02 13:55:47 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131102135547-nhcubj3ae7rv6t13
Tags: 1.6-1
* New upstream release
* Updated Standards-Version to 3.9.5 (no changes)
* Build depend on debhelper >= 9
* Removed the unused debian/orig-tar.sh script

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
import org.apache.commons.compress.archivers.ArchiveOutputStream;
33
33
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
34
34
import org.apache.commons.compress.utils.CharsetNames;
 
35
import org.apache.commons.compress.utils.IOUtils;
35
36
 
36
37
public class TarArchiveOutputStreamTest extends AbstractTestCase {
37
38
 
372
373
        tin.close();
373
374
    }
374
375
 
 
376
    public void testWriteLongDirectoryNameErrorMode() throws Exception {
 
377
        String n = "01234567890123456789012345678901234567890123456789"
 
378
                + "01234567890123456789012345678901234567890123456789"
 
379
                + "01234567890123456789012345678901234567890123456789/";
 
380
 
 
381
        try {
 
382
            TarArchiveEntry t = new TarArchiveEntry(n);
 
383
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
384
            TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
 
385
            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
 
386
            tos.putArchiveEntry(t);
 
387
            tos.closeArchiveEntry();
 
388
            tos.close();
 
389
            
 
390
            fail("Truncated name didn't throw an exception");
 
391
        } catch (RuntimeException e) {
 
392
            // expected
 
393
        }
 
394
    }
 
395
 
 
396
    public void testWriteLongDirectoryNameTruncateMode() throws Exception {
 
397
        String n = "01234567890123456789012345678901234567890123456789"
 
398
            + "01234567890123456789012345678901234567890123456789"
 
399
            + "01234567890123456789012345678901234567890123456789/";
 
400
        TarArchiveEntry t = new TarArchiveEntry(n);
 
401
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
402
        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
 
403
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
 
404
        tos.putArchiveEntry(t);
 
405
        tos.closeArchiveEntry();
 
406
        tos.close();
 
407
        byte[] data = bos.toByteArray();
 
408
        TarArchiveInputStream tin =
 
409
            new TarArchiveInputStream(new ByteArrayInputStream(data));
 
410
        TarArchiveEntry e = tin.getNextTarEntry();
 
411
        assertEquals("Entry name", n.substring(0, TarConstants.NAMELEN) + "/", e.getName());
 
412
        assertTrue("The entry is not a directory", e.isDirectory());
 
413
        tin.close();
 
414
    }
 
415
 
375
416
    /**
376
417
     * @see "https://issues.apache.org/jira/browse/COMPRESS-203"
377
418
     */
427
468
        tin.close();
428
469
    }
429
470
 
 
471
    /**
 
472
     * @see "https://issues.apache.org/jira/browse/COMPRESS-237"
 
473
     */
 
474
    public void testWriteLongLinkNameErrorMode() throws Exception {
 
475
        String linkname = "01234567890123456789012345678901234567890123456789"
 
476
                + "01234567890123456789012345678901234567890123456789"
 
477
                + "01234567890123456789012345678901234567890123456789/test";
 
478
        TarArchiveEntry entry = new TarArchiveEntry("test", TarArchiveEntry.LF_SYMLINK);
 
479
        entry.setLinkName(linkname);
 
480
        
 
481
        try {
 
482
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
483
            TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
 
484
            tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_ERROR);
 
485
            tos.putArchiveEntry(entry);
 
486
            tos.closeArchiveEntry();
 
487
            tos.close();
 
488
            
 
489
            fail("Truncated link name didn't throw an exception");
 
490
        } catch (RuntimeException e) {
 
491
            // expected
 
492
        }
 
493
    }
 
494
 
 
495
    public void testWriteLongLinkNameTruncateMode() throws Exception {
 
496
        String linkname = "01234567890123456789012345678901234567890123456789"
 
497
            + "01234567890123456789012345678901234567890123456789"
 
498
            + "01234567890123456789012345678901234567890123456789/";
 
499
        TarArchiveEntry entry = new TarArchiveEntry("test" , TarArchiveEntry.LF_SYMLINK);
 
500
        entry.setLinkName(linkname);
 
501
        
 
502
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
503
        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
 
504
        tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
 
505
        tos.putArchiveEntry(entry);
 
506
        tos.closeArchiveEntry();
 
507
        tos.close();
 
508
        
 
509
        byte[] data = bos.toByteArray();
 
510
        TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data));
 
511
        TarArchiveEntry e = tin.getNextTarEntry();
 
512
        assertEquals("Link name", linkname.substring(0, TarConstants.NAMELEN), e.getLinkName());
 
513
        tin.close();
 
514
    }
 
515
 
 
516
    /**
 
517
     * @see "https://issues.apache.org/jira/browse/COMPRESS-237"
 
518
     */
 
519
    public void testWriteLongLinkNameGnuMode() throws Exception {
 
520
        testWriteLongLinkName(TarArchiveOutputStream.LONGFILE_GNU);
 
521
    }
 
522
 
 
523
    /**
 
524
     * @see "https://issues.apache.org/jira/browse/COMPRESS-237"
 
525
     */
 
526
    public void testWriteLongLinkNamePosixMode() throws Exception {
 
527
        testWriteLongLinkName(TarArchiveOutputStream.LONGFILE_POSIX);
 
528
    }
 
529
 
 
530
    /**
 
531
     * @see "https://issues.apache.org/jira/browse/COMPRESS-237"
 
532
     */
 
533
    public void testWriteLongLinkName(int mode) throws Exception {
 
534
        String linkname = "01234567890123456789012345678901234567890123456789"
 
535
            + "01234567890123456789012345678901234567890123456789"
 
536
            + "01234567890123456789012345678901234567890123456789/test";
 
537
        TarArchiveEntry entry = new TarArchiveEntry("test", TarArchiveEntry.LF_SYMLINK);
 
538
        entry.setLinkName(linkname);
 
539
        
 
540
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 
541
        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
 
542
        tos.setLongFileMode(mode);
 
543
        tos.putArchiveEntry(entry);
 
544
        tos.closeArchiveEntry();
 
545
        tos.close();
 
546
        
 
547
        byte[] data = bos.toByteArray();
 
548
        TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data));
 
549
        TarArchiveEntry e = tin.getNextTarEntry();
 
550
        assertEquals("Entry name", "test", e.getName());
 
551
        assertEquals("Link name", linkname, e.getLinkName());
 
552
        assertTrue("The entry is not a symbolic link", e.isSymbolicLink());
 
553
        tin.close();
 
554
    }
 
555
 
 
556
    public void testPadsOutputToFullBlockLength() throws Exception {
 
557
        File f = File.createTempFile("commons-compress-padding", ".tar");
 
558
        f.deleteOnExit();
 
559
        FileOutputStream fos = new FileOutputStream(f);
 
560
        TarArchiveOutputStream tos = new TarArchiveOutputStream(fos);
 
561
        File file1 = getFile("test1.xml");
 
562
        TarArchiveEntry sEntry = new TarArchiveEntry(file1);
 
563
        tos.putArchiveEntry(sEntry);
 
564
        FileInputStream in = new FileInputStream(file1);
 
565
        IOUtils.copy(in, tos);
 
566
        in.close();
 
567
        tos.closeArchiveEntry();
 
568
        tos.close();
 
569
        // test1.xml is small enough to fit into the default blockv size
 
570
        assertEquals(TarConstants.DEFAULT_BLKSIZE, f.length());
 
571
    }
 
572
 
430
573
}