Extracts an archive.
Inheritance: NanoByte.Common.Tasks.TaskBase, IDisposable
示例#1
0
        public void TestExtractInvalidData()
        {
            Skip.IfNot(WindowsUtils.IsWindows, "CAB extraction relies on a Win32 API and therefore will not work on non-Windows platforms");

            using var sandbox = new TemporaryDirectory("0install-unit-tests");
            Assert.Throws <IOException>(() => ArchiveExtractor.Create(new MemoryStream(_garbageData), sandbox, Archive.MimeTypeCab));
        }
示例#2
0
        public void TestExtract()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("MSI extraction relies on a Win32 API and therefore will not work on non-Windows platforms");
            }

            using (var tempFile = Deploy(typeof(MsiExtractorTest).GetEmbeddedStream("testArchive.msi")))
                using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
                    using (var extractor = ArchiveExtractor.Create(tempFile, sandbox, Archive.MimeTypeMsi))
                    {
                        extractor.SubDir = "SourceDir";
                        extractor.Run();

                        string filePath = Path.Combine(sandbox, "file");
                        File.Exists(filePath).Should().BeTrue(because: "Should extract file 'file'");
                        File.GetLastWriteTimeUtc(filePath).Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time should be set");
                        File.ReadAllText(filePath).Should().Be("abc");

                        filePath = Path.Combine(sandbox, Path.Combine("folder1", "file"));
                        File.Exists(filePath).Should().BeTrue(because: "Should extract file 'dir/file'");
                        File.GetLastWriteTimeUtc(filePath).Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time should be set");
                        File.ReadAllText(filePath).Should().Be("def");
                    }
        }
示例#3
0
        public void TestExtractOverwritingExistingItems()
        {
            new TestRoot
            {
                new TestFile("file0")
                {
                    Contents = "This file should not be touched"
                },
                new TestFile("file1")
                {
                    Contents = "Wrong content"
                }
            }.Build(_sandbox);

            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
                extractor.Run();

            new TestRoot
            {
                new TestFile("file0")
                {
                    Contents = "This file should not be touched"
                }
            }.Verify(_sandbox);
            SamplePackageHierarchy.Verify(_sandbox);
        }
示例#4
0
        public void TestExtractInvalidData()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("7z extraction relies on a Win32 DLL and therefore will not work on non-Windows platforms");
            }

            using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
                Assert.Throws <IOException>(() => ArchiveExtractor.Create(new MemoryStream(_garbageData), sandbox, Archive.MimeType7Z).Run());
        }
示例#5
0
        public void ExtractionIntoFolder()
        {
            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
                extractor.Run();

            Directory.Exists(_sandbox).Should().BeTrue();
            var comparer = new CompareHierarchyToExtractedFolder(_sandbox);

            _package.AcceptVisitor(comparer);
        }
示例#6
0
        public void Suffix()
        {
            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
            {
                extractor.TargetSuffix = "suffix";
                extractor.Run();
            }

            SamplePackageHierarchy.Verify(Path.Combine(_sandbox, "suffix"));
        }
示例#7
0
        public void EnsureSubDirDoesNotTouchFileNames()
        {
            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
            {
                extractor.Extract = "/sub/folder/nested";
                extractor.Run();
            }

            Directory.Exists(Path.Combine(_sandbox, "Folder")).Should().BeFalse(because: "Should not apply subdir matching to part of filename");
            File.Exists(Path.Combine(_sandbox, "File")).Should().BeFalse(because: "Should not apply subdir matching to part of filename");
        }
示例#8
0
        public void TestFileExtract()
        {
            using (var extractor = ArchiveExtractor.Create(typeof(ZipExtractorTest).GetEmbeddedStream("testArchive.zip"), _sandbox, Archive.MimeTypeZip))
                extractor.Run();

            File.Exists(Path.Combine(_sandbox, "subdir1/regular")).Should().BeTrue(because: "Should extract file 'regular'");
            File.GetLastWriteTimeUtc(Path.Combine(_sandbox, "subdir1/regular")).Should().Be(new DateTime(2000, 1, 1, 13, 0, 0), because: "Correct last write time for file 'regular' should be set");

            File.Exists(Path.Combine(_sandbox, "subdir2/executable")).Should().BeTrue(because: "Should extract file 'executable'");
            File.GetLastWriteTimeUtc(Path.Combine(_sandbox, "subdir2/executable")).Should().Be(new DateTime(2000, 1, 1, 13, 0, 0), because: "Correct last write time for file 'executable' should be set");
        }
示例#9
0
        private void TestExtract(string mimeType, Stream archive)
        {
            using (var extractor = ArchiveExtractor.Create(archive, _sandbox, mimeType))
                extractor.Run();

            File.Exists("subdir1/regular").Should().BeTrue(because: "Should extract file 'regular'");
            File.GetLastWriteTimeUtc("subdir1/regular").Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time for file 'regular' should be set");

            File.Exists("subdir2/executable").Should().BeTrue(because: "Should extract file 'executable'");
            File.GetLastWriteTimeUtc("subdir2/executable").Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time for file 'executable' should be set");
        }
示例#10
0
        public void ExtractionOfSubDir()
        {
            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
            {
                extractor.SubDir = "/sub/folder/";
                extractor.Run();
            }

            Directory.Exists(Path.Combine(_sandbox, "nestedFolder")).Should().BeTrue();
            Directory.GetLastWriteTimeUtc(Path.Combine(_sandbox, "nestedFolder")).Should().Be(PackageBuilder.DefaultDate);
            File.Exists(Path.Combine(_sandbox, "nestedFile")).Should().BeTrue();
            File.Exists(Path.Combine(_sandbox, "file1")).Should().BeFalse();
            File.Exists(Path.Combine(_sandbox, "file2")).Should().BeFalse();
        }
        public void TestExtractSubDir()
        {
            using var sandbox   = new TemporaryDirectory("0install-unit-tests");
            using var extractor = ArchiveExtractor.Create(typeof(ArchiveExtractorTestBase).GetEmbeddedStream(FileName), sandbox, MimeType);
            extractor.Extract   = "folder1";
            extractor.Run();

            string filePath = Path.Combine(sandbox, "file");

            File.Exists(filePath).Should().BeTrue(because: "Should extract file 'dir/file'");
            File.GetLastWriteTimeUtc(filePath).Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time should be set");
            File.ReadAllText(filePath).Should().Be("def");

            Directory.GetDirectories(sandbox).Should().BeEmpty();
        }
示例#12
0
        public void TestExtractSubDir()
        {
            Skip.IfNot(WindowsUtils.IsWindows, "CAB extraction relies on a Win32 API and therefore will not work on non-Windows platforms");

            using var sandbox   = new TemporaryDirectory("0install-unit-tests");
            using var extractor = ArchiveExtractor.Create(typeof(CabExtractorTest).GetEmbeddedStream("testArchive.cab"), sandbox, Archive.MimeTypeCab);
            extractor.Extract   = "folder1";
            extractor.Run();

            string filePath = Path.Combine(sandbox, "file");

            File.Exists(filePath).Should().BeTrue(because: "Should extract file 'dir/file'");
            File.GetLastWriteTimeUtc(filePath).Should().Be(new DateTime(2000, 1, 1, 13, 0, 0), because: "Correct last write time should be set");
            File.ReadAllText(filePath).Should().Be("def");
        }
示例#13
0
        public void TestExtractOverwritingExistingItems()
        {
            File.WriteAllText(Path.Combine(_sandbox, "file1"), @"Wrong content");
            File.WriteAllText(Path.Combine(_sandbox, "file0"), @"This file should not be touched");

            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
                extractor.Run();

            File.Exists(Path.Combine(_sandbox, "file0")).Should().BeTrue(because: "Extractor cleaned directory.");
            string file0Content = File.ReadAllText(Path.Combine(_sandbox, "file0"));

            file0Content.Should().Be("This file should not be touched");
            var comparer = new CompareHierarchyToExtractedFolder(_sandbox);

            _package.AcceptVisitor(comparer);
        }
        [Fact] // Ensures ArchiveExtractor.FromStream() correctly creates a ZipExtractor.
        public void TestCreateExtractor()
        {
            using var tempDir = new TemporaryDirectory("0install-unit-tests");
            string path = Path.Combine(tempDir, "a.zip");

            using (var file = File.Create(path))
            {
                using var zipStream = new ZipOutputStream(file)
                      {
                          IsStreamOwner = false
                      };
                var entry = new ZipEntry("file");
                zipStream.PutNextEntry(entry);
                zipStream.CloseEntry();
            }

            using var extractor = ArchiveExtractor.Create(File.OpenRead(path), tempDir, Archive.MimeTypeZip);
            extractor.Should().BeOfType <ZipExtractor>();
        }
示例#15
0
        public void TestExtractSubDir()
        {
            if (!WindowsUtils.IsWindows)
            {
                Assert.Ignore("7z extraction relies on a Win32 DLL and therefore will not work on non-Windows platforms");
            }

            using (var sandbox = new TemporaryDirectory("0install-unit-tests"))
                using (var extractor = ArchiveExtractor.Create(typeof(SevenZipExtractorTest).GetEmbeddedStream("testArchive.7z"), sandbox, Archive.MimeType7Z))
                {
                    extractor.SubDir = "folder1";
                    extractor.Run();

                    string filePath = Path.Combine(sandbox, "file");
                    File.Exists(filePath).Should().BeTrue(because: "Should extract file 'dir/file'");
                    File.GetLastWriteTimeUtc(filePath).Should().Be(new DateTime(2000, 1, 1, 12, 0, 0), because: "Correct last write time should be set");
                    File.ReadAllText(filePath).Should().Be("def");

                    Directory.GetDirectories(sandbox).Should().BeEmpty();
                }
        }
示例#16
0
        public void ExtractionOfSubDir()
        {
            using (var extractor = ArchiveExtractor.Create(new MemoryStream(_archiveData), _sandbox, Archive.MimeTypeZip))
            {
                extractor.Extract = "/sub/folder/";
                extractor.Run();
            }

            new TestRoot
            {
                new TestFile("nestedFile")
                {
                    Contents = "File 3\n"
                },
                new TestDirectory("nestedFolder")
                {
                    new TestFile("doublyNestedFile")
                    {
                        Contents = "File 4"
                    }
                }
            }.Verify(_sandbox);
        }
示例#17
0
        public void ComplexHierarchy()
        {
            using (var extractor = ArchiveExtractor.Create(typeof(ZipExtractorTest).GetEmbeddedStream("testArchive.zip"), _sandbox, Archive.MimeTypeZip))
                extractor.Run();

            new TestRoot
            {
                new TestSymlink("symlink", "subdir1/regular"),
                new TestDirectory("subdir1")
                {
                    new TestFile("regular")
                    {
                        LastWrite = new DateTime(2000, 1, 1, 12, 0, 0, DateTimeKind.Utc)
                    }
                },
                new TestDirectory("subdir2")
                {
                    new TestFile("executable")
                    {
                        IsExecutable = true, LastWrite = new DateTime(2000, 1, 1, 12, 0, 0, DateTimeKind.Utc)
                    }
                }
            }.Verify(_sandbox);
        }
 public void TestExtractInvalidData()
 {
     using var sandbox = new TemporaryDirectory("0install-unit-tests");
     ArchiveExtractor.Create(new MemoryStream(_garbageData), sandbox, MimeType).Invoking(x => x.Run()).Should().Throw <IOException>();
 }
示例#19
0
 public void TestVerifySupport()
 {
     Assert.DoesNotThrow(() => ArchiveExtractor.VerifySupport(Model.Archive.MimeTypeZip));
     Assert.Throws <NotSupportedException>(() => ArchiveExtractor.VerifySupport("test/format"));
 }