| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolsetTest.CoreNative |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Linq; |
| 8 | using WixInternal.TestSupport; |
| 9 | using WixToolset.Core.Native; |
| 10 | using WixToolset.Data; |
| 11 | using Xunit; |
| 12 | |
| 13 | public class CabinetFixture |
| 14 | { |
| 15 | [Fact] |
| 16 | public void CanCreateSingleFileCabinet() |
| 17 | { |
| 18 | using (var fs = new DisposableFileSystem()) |
| 19 | { |
| 20 | var intermediateFolder = fs.GetFolder(true); |
| 21 | var cabPath = Path.Combine(intermediateFolder, "testout.cab"); |
| 22 | |
| 23 | var files = new[] { new CabinetCompressFile(TestData.Get(@"TestData", "test.txt"), "test.txt") }; |
| 24 | |
| 25 | var cabinet = new Cabinet(cabPath); |
| 26 | var created = cabinet.Compress(files, CompressionLevel.Low); |
| 27 | |
| 28 | Assert.True(File.Exists(cabPath)); |
| 29 | Assert.Equal(new[] |
| 30 | { |
| 31 | "testout.cab, test.txt" |
| 32 | }, created.Select(c => String.Join(", ", c.CabinetName, c.FirstFileToken)).ToArray()); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | [Fact] |
| 37 | public void CanCreateCabinetUsingLocalizedPath() |
| 38 | { |
| 39 | using (var fs = new DisposableFileSystem()) |
| 40 | { |
| 41 | var intermediateFolder = fs.GetFolder(true); |
| 42 | var cabPath = Path.Combine(intermediateFolder, "testout.cab"); |
| 43 | |
| 44 | var files = new[] { new CabinetCompressFile(TestData.Get(@"TestData", "Testüber", "t道場t.txt"), "test.txt") }; |
| 45 | |
| 46 | var cabinet = new Cabinet(cabPath); |
| 47 | var created = cabinet.Compress(files, CompressionLevel.Low); |
| 48 | |
| 49 | Assert.True(File.Exists(cabPath)); |
| 50 | Assert.Equal(new[] |
| 51 | { |
| 52 | "testout.cab, test.txt" |
| 53 | }, created.Select(c => String.Join(", ", c.CabinetName, c.FirstFileToken)).ToArray()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | [Fact] |
| 58 | public void CanCreateSpannedFileCabinet() |
| 59 | { |
| 60 | using (var fs = new DisposableFileSystem()) |
| 61 | { |
| 62 | var intermediateFolder = fs.GetFolder(true); |
| 63 | |
| 64 | // Put more than non-zero bytes in a file sized just under 3MB since there is |
| 65 | // some overhead in cabinets that prevent perfect packing on the megabyte boundary. |
| 66 | var threeMBPath = Path.Combine(intermediateFolder, "_3mb.dat"); |
| 67 | TestData.CreateFile(threeMBPath, (long)(2.9 * 1024 * 1024), fill: true); |
| 68 | |
| 69 | var cabPath = Path.Combine(intermediateFolder, "test.cab"); |
| 70 | |
| 71 | var files = new[] { new CabinetCompressFile(threeMBPath, Path.GetFileNameWithoutExtension(threeMBPath)) }; |
| 72 | |
| 73 | var cabinet = new Cabinet(cabPath); |
| 74 | var created = cabinet.Compress(files, CompressionLevel.None, maxSize: 1); |
| 75 | |
| 76 | Assert.True(File.Exists(cabPath)); |
| 77 | Assert.Equal(new[] |
| 78 | { |
| 79 | "test.cab, _3mb", |
| 80 | "testa.cab, _3mb", |
| 81 | "testb.cab, _3mb" |
| 82 | }, created.Select(c => String.Join(", ", c.CabinetName, c.FirstFileToken)).ToArray()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | [Fact] |
| 87 | public void CanEnumerateSingleFileCabinet() |
| 88 | { |
| 89 | var cabinetPath = TestData.Get(@"TestData\test.cab"); |
| 90 | |
| 91 | var cabinet = new Cabinet(cabinetPath); |
| 92 | var files = cabinet.Enumerate(); |
| 93 | |
| 94 | var file = files.Single(); |
| 95 | Assert.Equal("test.txt", file.FileId); |
| 96 | Assert.Equal(17, file.Size); |
| 97 | |
| 98 | Assert.Equal(19259, file.Date); |
| 99 | Assert.Equal(47731, file.Time); |
| 100 | // TODO: This doesn't seem to always pass, not clear why but it'd be good to understand one day. |
| 101 | // Assert.True(file.SameAsDateTime(new DateTime(2017, 9, 28, 0, 19, 38))); |
| 102 | } |
| 103 | |
| 104 | [Fact] |
| 105 | public void IntegrationTest() |
| 106 | { |
| 107 | using (var fs = new DisposableFileSystem()) |
| 108 | { |
| 109 | var intermediateFolder = fs.GetFolder(true); |
| 110 | var cabinetPath = Path.Combine(intermediateFolder, "testout.cab"); |
| 111 | var extractFolder = fs.GetFolder(true); |
| 112 | |
| 113 | // Compress. |
| 114 | { |
| 115 | var files = new[] { |
| 116 | new CabinetCompressFile(TestData.Get(@"TestData", "test.txt"), "test1.txt"), |
| 117 | new CabinetCompressFile(TestData.Get(@"TestData", "test.txt"), "test2.txt"), |
| 118 | }; |
| 119 | |
| 120 | var cabinet = new Cabinet(cabinetPath); |
| 121 | cabinet.Compress(files, CompressionLevel.Low); |
| 122 | } |
| 123 | |
| 124 | // Extract. |
| 125 | { |
| 126 | var cabinet = new Cabinet(cabinetPath); |
| 127 | var reportedFiles = cabinet.Extract(extractFolder); |
| 128 | Assert.Equal(2, reportedFiles.Count()); |
| 129 | } |
| 130 | |
| 131 | // Enumerate to compare cabinet to extracted files. |
| 132 | { |
| 133 | var cabinet = new Cabinet(cabinetPath); |
| 134 | var enumerated = cabinet.Enumerate().OrderBy(f => f.FileId).ToArray(); |
| 135 | |
| 136 | var files = Directory.EnumerateFiles(extractFolder).OrderBy(f => f).ToArray(); |
| 137 | |
| 138 | for (var i = 0; i < enumerated.Length; ++i) |
| 139 | { |
| 140 | var cabFileInfo = enumerated[i]; |
| 141 | var fileInfo = new FileInfo(files[i]); |
| 142 | |
| 143 | WixInternal.TestSupport.WixAssert.StringEqual(cabFileInfo.FileId, fileInfo.Name); |
| 144 | Assert.Equal(cabFileInfo.Size, fileInfo.Length); |
| 145 | Assert.True(cabFileInfo.SameAsDateTime(fileInfo.CreationTime)); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |