diff options
| author | Rob Mensching <rob@firegiant.com> | 2021-05-11 07:36:37 -0700 |
|---|---|---|
| committer | Rob Mensching <rob@firegiant.com> | 2021-05-11 07:36:37 -0700 |
| commit | 3f583916719eeef598d10a5d4e14ef14f008243b (patch) | |
| tree | 3d528e0ddb5c0550954217c97059d2f19cd6152a /src/dtf/WixToolsetTests.Dtf.Compression | |
| parent | 2e5ab696b8b4666d551b2a0532b95fb7fe6dbe03 (diff) | |
| download | wix-3f583916719eeef598d10a5d4e14ef14f008243b.tar.gz wix-3f583916719eeef598d10a5d4e14ef14f008243b.tar.bz2 wix-3f583916719eeef598d10a5d4e14ef14f008243b.zip | |
Merge Dtf
Diffstat (limited to 'src/dtf/WixToolsetTests.Dtf.Compression')
4 files changed, 924 insertions, 0 deletions
diff --git a/src/dtf/WixToolsetTests.Dtf.Compression/CompressionTestUtil.cs b/src/dtf/WixToolsetTests.Dtf.Compression/CompressionTestUtil.cs new file mode 100644 index 00000000..e7a5373d --- /dev/null +++ b/src/dtf/WixToolsetTests.Dtf.Compression/CompressionTestUtil.cs | |||
| @@ -0,0 +1,649 @@ | |||
| 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 | using System; | ||
| 4 | using System.IO; | ||
| 5 | using System.Text; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Security.Cryptography; | ||
| 8 | using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| 9 | using WixToolset.Dtf.Compression; | ||
| 10 | |||
| 11 | namespace WixToolset.Dtf.Test | ||
| 12 | { | ||
| 13 | public class CompressionTestUtil | ||
| 14 | { | ||
| 15 | private static MD5 md5 = new MD5CryptoServiceProvider(); | ||
| 16 | |||
| 17 | private string progressTextFile; | ||
| 18 | |||
| 19 | public CompressionTestUtil(string progressTextFile) | ||
| 20 | { | ||
| 21 | this.progressTextFile = progressTextFile; | ||
| 22 | } | ||
| 23 | |||
| 24 | public static IList<int[]> ExpectedProgress | ||
| 25 | { | ||
| 26 | get { return CompressionTestUtil.expectedProgress; } | ||
| 27 | set { CompressionTestUtil.expectedProgress = value; } | ||
| 28 | } | ||
| 29 | private static IList<int[]> expectedProgress; | ||
| 30 | |||
| 31 | public void PrintArchiveProgress(object source, ArchiveProgressEventArgs e) | ||
| 32 | { | ||
| 33 | switch (e.ProgressType) | ||
| 34 | { | ||
| 35 | case ArchiveProgressType.StartFile: | ||
| 36 | { | ||
| 37 | Console.WriteLine("StartFile: {0}", e.CurrentFileName); | ||
| 38 | } break; | ||
| 39 | case ArchiveProgressType.FinishFile: | ||
| 40 | { | ||
| 41 | Console.WriteLine("FinishFile: {0}", e.CurrentFileName); | ||
| 42 | } break; | ||
| 43 | case ArchiveProgressType.StartArchive: | ||
| 44 | { | ||
| 45 | Console.WriteLine("StartArchive: {0} : {1}", e.CurrentArchiveNumber, e.CurrentArchiveName); | ||
| 46 | } break; | ||
| 47 | case ArchiveProgressType.FinishArchive: | ||
| 48 | { | ||
| 49 | Console.WriteLine("FinishArchive: {0} : {1}", e.CurrentArchiveNumber, e.CurrentArchiveName); | ||
| 50 | } break; | ||
| 51 | } | ||
| 52 | |||
| 53 | File.AppendAllText(this.progressTextFile, e.ToString().Replace("\n", Environment.NewLine)); | ||
| 54 | |||
| 55 | if (CompressionTestUtil.expectedProgress != null && | ||
| 56 | e.ProgressType != ArchiveProgressType.PartialFile && | ||
| 57 | e.ProgressType != ArchiveProgressType.PartialArchive) | ||
| 58 | { | ||
| 59 | Assert.AreNotEqual<int>(0, CompressionTestUtil.expectedProgress.Count); | ||
| 60 | int[] expected = CompressionTestUtil.expectedProgress[0]; | ||
| 61 | CompressionTestUtil.expectedProgress.RemoveAt(0); | ||
| 62 | Assert.AreEqual<ArchiveProgressType>((ArchiveProgressType) expected[0], e.ProgressType, "Checking ProgressType."); | ||
| 63 | Assert.AreEqual<int>(expected[1], e.CurrentFileNumber, "Checking CurrentFileNumber."); | ||
| 64 | Assert.AreEqual<int>(expected[2], e.TotalFiles, "Checking TotalFiles."); | ||
| 65 | Assert.AreEqual<int>(expected[4], e.CurrentArchiveNumber, "Checking CurrentArchiveNumber."); | ||
| 66 | Assert.AreEqual<int>(expected[5], e.TotalArchives, "Checking TotalArchives."); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | public static bool CompareDirectories(string dirA, string dirB) | ||
| 71 | { | ||
| 72 | bool difference = false; | ||
| 73 | Console.WriteLine("Comparing directories {0}, {1}", dirA, dirB); | ||
| 74 | |||
| 75 | string[] filesA = Directory.GetFiles(dirA); | ||
| 76 | string[] filesB = Directory.GetFiles(dirB); | ||
| 77 | for (int iA = 0; iA < filesA.Length; iA++) | ||
| 78 | { | ||
| 79 | filesA[iA] = Path.GetFileName(filesA[iA]); | ||
| 80 | } | ||
| 81 | for (int iB = 0; iB < filesB.Length; iB++) | ||
| 82 | { | ||
| 83 | filesB[iB] = Path.GetFileName(filesB[iB]); | ||
| 84 | } | ||
| 85 | Array.Sort(filesA); | ||
| 86 | Array.Sort(filesB); | ||
| 87 | |||
| 88 | for (int iA = 0, iB = 0; iA < filesA.Length || iB < filesB.Length; ) | ||
| 89 | { | ||
| 90 | int comp; | ||
| 91 | if (iA == filesA.Length) | ||
| 92 | { | ||
| 93 | comp = 1; | ||
| 94 | } | ||
| 95 | else if (iB == filesB.Length) | ||
| 96 | { | ||
| 97 | comp = -1; | ||
| 98 | } | ||
| 99 | else | ||
| 100 | { | ||
| 101 | comp = String.Compare(filesA[iA], filesB[iB]); | ||
| 102 | } | ||
| 103 | if (comp < 0) | ||
| 104 | { | ||
| 105 | Console.WriteLine("< " + filesA[iA]); | ||
| 106 | difference = true; | ||
| 107 | iA++; | ||
| 108 | } | ||
| 109 | else if (comp > 0) | ||
| 110 | { | ||
| 111 | Console.WriteLine("> " + filesB[iB]); | ||
| 112 | difference = true; | ||
| 113 | iB++; | ||
| 114 | } | ||
| 115 | else | ||
| 116 | { | ||
| 117 | string fileA = Path.Combine(dirA, filesA[iA]); | ||
| 118 | string fileB = Path.Combine(dirB, filesB[iB]); | ||
| 119 | |||
| 120 | byte[] hashA; | ||
| 121 | byte[] hashB; | ||
| 122 | |||
| 123 | lock (CompressionTestUtil.md5) | ||
| 124 | { | ||
| 125 | using (Stream fileAStream = File.OpenRead(fileA)) | ||
| 126 | { | ||
| 127 | hashA = CompressionTestUtil.md5.ComputeHash(fileAStream); | ||
| 128 | } | ||
| 129 | using (Stream fileBStream = File.OpenRead(fileB)) | ||
| 130 | { | ||
| 131 | hashB = CompressionTestUtil.md5.ComputeHash(fileBStream); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | for (int i = 0; i < hashA.Length; i++) | ||
| 136 | { | ||
| 137 | if (hashA[i] != hashB[i]) | ||
| 138 | { | ||
| 139 | Console.WriteLine("~ " + filesA[iA]); | ||
| 140 | difference = true; | ||
| 141 | break; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | iA++; | ||
| 146 | iB++; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | string[] dirsA = Directory.GetDirectories(dirA); | ||
| 151 | string[] dirsB = Directory.GetDirectories(dirB); | ||
| 152 | for (int iA = 0; iA < dirsA.Length; iA++) | ||
| 153 | { | ||
| 154 | dirsA[iA] = Path.GetFileName(dirsA[iA]); | ||
| 155 | } | ||
| 156 | for (int iB = 0; iB < dirsB.Length; iB++) | ||
| 157 | { | ||
| 158 | dirsB[iB] = Path.GetFileName(dirsB[iB]); | ||
| 159 | } | ||
| 160 | Array.Sort(dirsA); | ||
| 161 | Array.Sort(dirsB); | ||
| 162 | |||
| 163 | for (int iA = 0, iB = 0; iA < dirsA.Length || iB < dirsB.Length; ) | ||
| 164 | { | ||
| 165 | int comp; | ||
| 166 | if (iA == dirsA.Length) | ||
| 167 | { | ||
| 168 | comp = 1; | ||
| 169 | } | ||
| 170 | else if (iB == dirsB.Length) | ||
| 171 | { | ||
| 172 | comp = -1; | ||
| 173 | } | ||
| 174 | else | ||
| 175 | { | ||
| 176 | comp = String.Compare(dirsA[iA], dirsB[iB]); | ||
| 177 | } | ||
| 178 | if (comp < 0) | ||
| 179 | { | ||
| 180 | Console.WriteLine("< {0}\\", dirsA[iA]); | ||
| 181 | difference = true; | ||
| 182 | iA++; | ||
| 183 | } | ||
| 184 | else if (comp > 0) | ||
| 185 | { | ||
| 186 | Console.WriteLine("> {1}\\", dirsB[iB]); | ||
| 187 | difference = true; | ||
| 188 | iB++; | ||
| 189 | } | ||
| 190 | else | ||
| 191 | { | ||
| 192 | string subDirA = Path.Combine(dirA, dirsA[iA]); | ||
| 193 | string subDirB = Path.Combine(dirB, dirsB[iB]); | ||
| 194 | if (!CompressionTestUtil.CompareDirectories(subDirA, subDirB)) | ||
| 195 | { | ||
| 196 | difference = true; | ||
| 197 | } | ||
| 198 | iA++; | ||
| 199 | iB++; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | return !difference; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | public static void GenerateRandomFile(string path, int seed, long size) | ||
| 208 | { | ||
| 209 | Console.WriteLine("Generating random file {0} (seed={1}, size={2})", | ||
| 210 | path, seed, size); | ||
| 211 | Random random = new Random(seed); | ||
| 212 | bool easy = random.Next(2) == 1; | ||
| 213 | int chunk = 1024 * random.Next(1, 100); | ||
| 214 | using (TextWriter tw = new StreamWriter( | ||
| 215 | File.Create(path, 4096), Encoding.ASCII)) | ||
| 216 | { | ||
| 217 | for (long count = 0; count < size; count++) | ||
| 218 | { | ||
| 219 | char c = (char) (easy ? random.Next('a', 'b' + 1) | ||
| 220 | : random.Next(32, 127)); | ||
| 221 | tw.Write(c); | ||
| 222 | if (--chunk == 0) | ||
| 223 | { | ||
| 224 | chunk = 1024 * random.Next(1, 101); | ||
| 225 | easy = random.Next(2) == 1; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | public static void TestArchiveInfoNullParams( | ||
| 232 | ArchiveInfo archiveInfo, | ||
| 233 | string dirA, | ||
| 234 | string dirB, | ||
| 235 | string[] files) | ||
| 236 | { | ||
| 237 | Exception caughtEx = null; | ||
| 238 | try | ||
| 239 | { | ||
| 240 | archiveInfo.PackFiles(null, null, files); | ||
| 241 | } | ||
| 242 | catch (Exception ex) { caughtEx = ex; } | ||
| 243 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 244 | caughtEx = null; | ||
| 245 | try | ||
| 246 | { | ||
| 247 | archiveInfo.PackFiles(null, files, new string[] { }); | ||
| 248 | } | ||
| 249 | catch (Exception ex) { caughtEx = ex; } | ||
| 250 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentOutOfRangeException), "Caught exception: " + caughtEx); | ||
| 251 | caughtEx = null; | ||
| 252 | try | ||
| 253 | { | ||
| 254 | archiveInfo.PackFileSet(dirA, null); | ||
| 255 | } | ||
| 256 | catch (Exception ex) { caughtEx = ex; } | ||
| 257 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 258 | caughtEx = null; | ||
| 259 | try | ||
| 260 | { | ||
| 261 | archiveInfo.PackFiles(null, files, files); | ||
| 262 | } | ||
| 263 | catch (Exception ex) { caughtEx = ex; } | ||
| 264 | Assert.IsInstanceOfType(caughtEx, typeof(FileNotFoundException), "Caught exception: " + caughtEx); | ||
| 265 | caughtEx = null; | ||
| 266 | try | ||
| 267 | { | ||
| 268 | archiveInfo.PackFiles(dirA, null, files); | ||
| 269 | } | ||
| 270 | catch (Exception ex) { caughtEx = ex; } | ||
| 271 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 272 | caughtEx = null; | ||
| 273 | try | ||
| 274 | { | ||
| 275 | archiveInfo.PackFiles(dirA, files, null); | ||
| 276 | } | ||
| 277 | catch (Exception ex) { caughtEx = ex; } | ||
| 278 | Assert.IsNull(caughtEx, "Caught exception: " + caughtEx); | ||
| 279 | |||
| 280 | caughtEx = null; | ||
| 281 | try | ||
| 282 | { | ||
| 283 | archiveInfo.CopyTo(null); | ||
| 284 | } | ||
| 285 | catch (Exception ex) { caughtEx = ex; } | ||
| 286 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 287 | caughtEx = null; | ||
| 288 | try | ||
| 289 | { | ||
| 290 | archiveInfo.CopyTo(null, true); | ||
| 291 | } | ||
| 292 | catch (Exception ex) { caughtEx = ex; } | ||
| 293 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 294 | caughtEx = null; | ||
| 295 | try | ||
| 296 | { | ||
| 297 | archiveInfo.MoveTo(null); | ||
| 298 | } | ||
| 299 | catch (Exception ex) { caughtEx = ex; } | ||
| 300 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 301 | caughtEx = null; | ||
| 302 | try | ||
| 303 | { | ||
| 304 | archiveInfo.GetFiles(null); | ||
| 305 | } | ||
| 306 | catch (Exception ex) { caughtEx = ex; } | ||
| 307 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 308 | caughtEx = null; | ||
| 309 | try | ||
| 310 | { | ||
| 311 | archiveInfo.UnpackFile(null, "test.txt"); | ||
| 312 | } | ||
| 313 | catch (Exception ex) { caughtEx = ex; } | ||
| 314 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 315 | caughtEx = null; | ||
| 316 | try | ||
| 317 | { | ||
| 318 | archiveInfo.UnpackFile("test.txt", null); | ||
| 319 | } | ||
| 320 | catch (Exception ex) { caughtEx = ex; } | ||
| 321 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 322 | caughtEx = null; | ||
| 323 | try | ||
| 324 | { | ||
| 325 | archiveInfo.UnpackFiles(null, dirB, files); | ||
| 326 | } | ||
| 327 | catch (Exception ex) { caughtEx = ex; } | ||
| 328 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 329 | caughtEx = null; | ||
| 330 | try | ||
| 331 | { | ||
| 332 | archiveInfo.UnpackFiles(files, null, null); | ||
| 333 | } | ||
| 334 | catch (Exception ex) { caughtEx = ex; } | ||
| 335 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 336 | caughtEx = null; | ||
| 337 | try | ||
| 338 | { | ||
| 339 | archiveInfo.UnpackFiles(files, null, files); | ||
| 340 | } | ||
| 341 | catch (Exception ex) { caughtEx = ex; } | ||
| 342 | Assert.IsNull(caughtEx, "Caught exception: " + caughtEx); | ||
| 343 | caughtEx = null; | ||
| 344 | try | ||
| 345 | { | ||
| 346 | archiveInfo.UnpackFiles(files, dirB, null); | ||
| 347 | } | ||
| 348 | catch (Exception ex) { caughtEx = ex; } | ||
| 349 | Assert.IsNull(caughtEx, "Caught exception: " + caughtEx); | ||
| 350 | caughtEx = null; | ||
| 351 | try | ||
| 352 | { | ||
| 353 | archiveInfo.UnpackFiles(files, dirB, new string[] { }); | ||
| 354 | } | ||
| 355 | catch (Exception ex) { caughtEx = ex; } | ||
| 356 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentOutOfRangeException), "Caught exception: " + caughtEx); | ||
| 357 | caughtEx = null; | ||
| 358 | try | ||
| 359 | { | ||
| 360 | archiveInfo.UnpackFileSet(null, dirB); | ||
| 361 | } | ||
| 362 | catch (Exception ex) { caughtEx = ex; } | ||
| 363 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 364 | } | ||
| 365 | |||
| 366 | public static void TestCompressionEngineNullParams( | ||
| 367 | CompressionEngine engine, | ||
| 368 | ArchiveFileStreamContext streamContext, | ||
| 369 | string[] testFiles) | ||
| 370 | { | ||
| 371 | Exception caughtEx; | ||
| 372 | |||
| 373 | Console.WriteLine("Testing null streamContext."); | ||
| 374 | caughtEx = null; | ||
| 375 | try | ||
| 376 | { | ||
| 377 | engine.Pack(null, testFiles); | ||
| 378 | } | ||
| 379 | catch (Exception ex) { caughtEx = ex; } | ||
| 380 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 381 | caughtEx = null; | ||
| 382 | try | ||
| 383 | { | ||
| 384 | engine.Pack(null, testFiles, 0); | ||
| 385 | } | ||
| 386 | catch (Exception ex) { caughtEx = ex; } | ||
| 387 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 388 | |||
| 389 | Console.WriteLine("Testing null files."); | ||
| 390 | caughtEx = null; | ||
| 391 | try | ||
| 392 | { | ||
| 393 | engine.Pack(streamContext, null); | ||
| 394 | } | ||
| 395 | catch (Exception ex) { caughtEx = ex; } | ||
| 396 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 397 | |||
| 398 | Console.WriteLine("Testing null files."); | ||
| 399 | caughtEx = null; | ||
| 400 | try | ||
| 401 | { | ||
| 402 | engine.Pack(streamContext, null, 0); | ||
| 403 | } | ||
| 404 | catch (Exception ex) { caughtEx = ex; } | ||
| 405 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 406 | |||
| 407 | |||
| 408 | Console.WriteLine("Testing null stream."); | ||
| 409 | caughtEx = null; | ||
| 410 | try | ||
| 411 | { | ||
| 412 | engine.IsArchive(null); | ||
| 413 | } | ||
| 414 | catch (Exception ex) { caughtEx = ex; } | ||
| 415 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 416 | caughtEx = null; | ||
| 417 | try | ||
| 418 | { | ||
| 419 | engine.FindArchiveOffset(null); | ||
| 420 | } | ||
| 421 | catch (Exception ex) { caughtEx = ex; } | ||
| 422 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 423 | caughtEx = null; | ||
| 424 | try | ||
| 425 | { | ||
| 426 | engine.GetFiles(null); | ||
| 427 | } | ||
| 428 | catch (Exception ex) { caughtEx = ex; } | ||
| 429 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 430 | caughtEx = null; | ||
| 431 | try | ||
| 432 | { | ||
| 433 | engine.GetFileInfo(null); | ||
| 434 | } | ||
| 435 | catch (Exception ex) { caughtEx = ex; } | ||
| 436 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 437 | caughtEx = null; | ||
| 438 | try | ||
| 439 | { | ||
| 440 | engine.Unpack(null, "testUnpack.txt"); | ||
| 441 | } | ||
| 442 | catch (Exception ex) { caughtEx = ex; } | ||
| 443 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 444 | Console.WriteLine("Testing null streamContext."); | ||
| 445 | caughtEx = null; | ||
| 446 | try | ||
| 447 | { | ||
| 448 | engine.GetFiles(null, null); | ||
| 449 | } | ||
| 450 | catch (Exception ex) { caughtEx = ex; } | ||
| 451 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 452 | caughtEx = null; | ||
| 453 | try | ||
| 454 | { | ||
| 455 | engine.GetFileInfo(null, null); | ||
| 456 | } | ||
| 457 | catch (Exception ex) { caughtEx = ex; } | ||
| 458 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 459 | caughtEx = null; | ||
| 460 | try | ||
| 461 | { | ||
| 462 | engine.Unpack((IUnpackStreamContext) null, null); | ||
| 463 | } | ||
| 464 | catch (Exception ex) { caughtEx = ex; } | ||
| 465 | Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); | ||
| 466 | } | ||
| 467 | |||
| 468 | public static void TestBadPackStreamContexts( | ||
| 469 | CompressionEngine engine, string archiveName, string[] testFiles) | ||
| 470 | { | ||
| 471 | Exception caughtEx; | ||
| 472 | |||
| 473 | Console.WriteLine("Testing streamContext that returns null from GetName."); | ||
| 474 | caughtEx = null; | ||
| 475 | try | ||
| 476 | { | ||
| 477 | engine.Pack( | ||
| 478 | new MisbehavingStreamContext(archiveName, null, null, false, false, true, true, true, true), | ||
| 479 | testFiles); | ||
| 480 | } | ||
| 481 | catch (Exception ex) { caughtEx = ex; } | ||
| 482 | Assert.IsTrue(caughtEx is FileNotFoundException, "Caught exception: " + caughtEx); | ||
| 483 | Console.WriteLine("Testing streamContext that returns null from OpenArchive."); | ||
| 484 | caughtEx = null; | ||
| 485 | try | ||
| 486 | { | ||
| 487 | engine.Pack( | ||
| 488 | new MisbehavingStreamContext(archiveName, null, null, false, true, false, true, true, true), | ||
| 489 | testFiles); | ||
| 490 | } | ||
| 491 | catch (Exception ex) { caughtEx = ex; } | ||
| 492 | Assert.IsTrue(caughtEx is FileNotFoundException, "Caught exception: " + caughtEx); | ||
| 493 | Console.WriteLine("Testing streamContext that returns null from OpenFile."); | ||
| 494 | caughtEx = null; | ||
| 495 | try | ||
| 496 | { | ||
| 497 | engine.Pack( | ||
| 498 | new MisbehavingStreamContext(archiveName, null, null, false, true, true, true, false, true), | ||
| 499 | testFiles); | ||
| 500 | } | ||
| 501 | catch (Exception ex) { caughtEx = ex; } | ||
| 502 | Assert.IsNull(caughtEx, "Caught exception: " + caughtEx); | ||
| 503 | Console.WriteLine("Testing streamContext that throws on GetName."); | ||
| 504 | caughtEx = null; | ||
| 505 | try | ||
| 506 | { | ||
| 507 | engine.Pack( | ||
| 508 | new MisbehavingStreamContext(archiveName, null, null, true, false, true, true, true, true), | ||
| 509 | testFiles); | ||
| 510 | } | ||
| 511 | catch (Exception ex) { caughtEx = ex; } | ||
| 512 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 513 | Console.WriteLine("Testing streamContext that throws on OpenArchive."); | ||
| 514 | caughtEx = null; | ||
| 515 | try | ||
| 516 | { | ||
| 517 | engine.Pack( | ||
| 518 | new MisbehavingStreamContext(archiveName, null, null, true, true, false, true, true, true), | ||
| 519 | testFiles); | ||
| 520 | } | ||
| 521 | catch (Exception ex) { caughtEx = ex; } | ||
| 522 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 523 | Console.WriteLine("Testing streamContext that throws on CloseArchive."); | ||
| 524 | caughtEx = null; | ||
| 525 | try | ||
| 526 | { | ||
| 527 | engine.Pack( | ||
| 528 | new MisbehavingStreamContext(archiveName, null, null, true, true, true, false, true, true), | ||
| 529 | testFiles); | ||
| 530 | } | ||
| 531 | catch (Exception ex) { caughtEx = ex; } | ||
| 532 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 533 | Console.WriteLine("Testing streamContext that throws on OpenFile."); | ||
| 534 | caughtEx = null; | ||
| 535 | try | ||
| 536 | { | ||
| 537 | engine.Pack( | ||
| 538 | new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, false, true), | ||
| 539 | testFiles); | ||
| 540 | } | ||
| 541 | catch (Exception ex) { caughtEx = ex; } | ||
| 542 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 543 | Console.WriteLine("Testing streamContext that throws on CloseFile."); | ||
| 544 | caughtEx = null; | ||
| 545 | try | ||
| 546 | { | ||
| 547 | engine.Pack( | ||
| 548 | new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, true, false), | ||
| 549 | testFiles); | ||
| 550 | } | ||
| 551 | catch (Exception ex) { caughtEx = ex; } | ||
| 552 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 553 | } | ||
| 554 | |||
| 555 | public static void TestBadUnpackStreamContexts( | ||
| 556 | CompressionEngine engine, string archiveName) | ||
| 557 | { | ||
| 558 | Exception caughtEx; | ||
| 559 | |||
| 560 | Console.WriteLine("Testing streamContext that returns null from OpenArchive."); | ||
| 561 | caughtEx = null; | ||
| 562 | try | ||
| 563 | { | ||
| 564 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, false, true, false, true, true, true), null); | ||
| 565 | } | ||
| 566 | catch (Exception ex) { caughtEx = ex; } | ||
| 567 | Assert.IsInstanceOfType(caughtEx, typeof(FileNotFoundException), "Caught exception: " + caughtEx); | ||
| 568 | Console.WriteLine("Testing streamContext that returns null from OpenFile."); | ||
| 569 | caughtEx = null; | ||
| 570 | try | ||
| 571 | { | ||
| 572 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, false, true, true, true, false, true), null); | ||
| 573 | } | ||
| 574 | catch (Exception ex) { caughtEx = ex; } | ||
| 575 | Assert.IsNull(caughtEx, "Caught exception: " + caughtEx); | ||
| 576 | Console.WriteLine("Testing streamContext that throws on OpenArchive."); | ||
| 577 | caughtEx = null; | ||
| 578 | try | ||
| 579 | { | ||
| 580 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, false, true, true, true), null); | ||
| 581 | } | ||
| 582 | catch (Exception ex) { caughtEx = ex; } | ||
| 583 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 584 | Console.WriteLine("Testing streamContext that throws on CloseArchive."); | ||
| 585 | caughtEx = null; | ||
| 586 | try | ||
| 587 | { | ||
| 588 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, false, true, true), null); | ||
| 589 | } | ||
| 590 | catch (Exception ex) { caughtEx = ex; } | ||
| 591 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 592 | Console.WriteLine("Testing streamContext that throws on OpenFile."); | ||
| 593 | caughtEx = null; | ||
| 594 | try | ||
| 595 | { | ||
| 596 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, false, true), null); | ||
| 597 | } | ||
| 598 | catch (Exception ex) { caughtEx = ex; } | ||
| 599 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 600 | Console.WriteLine("Testing streamContext that throws on CloseFile."); | ||
| 601 | caughtEx = null; | ||
| 602 | try | ||
| 603 | { | ||
| 604 | engine.Unpack(new MisbehavingStreamContext(archiveName, null, null, true, true, true, true, true, false), null); | ||
| 605 | } | ||
| 606 | catch (Exception ex) { caughtEx = ex; } | ||
| 607 | Assert.IsTrue(caughtEx != null && caughtEx.Message == MisbehavingStreamContext.EXCEPTION, "Caught exception: " + caughtEx); | ||
| 608 | } | ||
| 609 | |||
| 610 | public static void TestTruncatedArchive( | ||
| 611 | ArchiveInfo archiveInfo, Type expectedExceptionType) | ||
| 612 | { | ||
| 613 | for (long len = archiveInfo.Length - 1; len >= 0; len--) | ||
| 614 | { | ||
| 615 | string testArchive = String.Format("{0}.{1:d06}", | ||
| 616 | archiveInfo.FullName, len); | ||
| 617 | if (File.Exists(testArchive)) | ||
| 618 | { | ||
| 619 | File.Delete(testArchive); | ||
| 620 | } | ||
| 621 | |||
| 622 | archiveInfo.CopyTo(testArchive); | ||
| 623 | using (FileStream truncateStream = | ||
| 624 | File.Open(testArchive, FileMode.Open, FileAccess.ReadWrite)) | ||
| 625 | { | ||
| 626 | truncateStream.SetLength(len); | ||
| 627 | } | ||
| 628 | |||
| 629 | ArchiveInfo testArchiveInfo = (ArchiveInfo) archiveInfo.GetType() | ||
| 630 | .GetConstructor(new Type[] { typeof(string) }).Invoke(new object[] { testArchive }); | ||
| 631 | |||
| 632 | Exception caughtEx = null; | ||
| 633 | try | ||
| 634 | { | ||
| 635 | testArchiveInfo.GetFiles(); | ||
| 636 | } | ||
| 637 | catch (Exception ex) { caughtEx = ex; } | ||
| 638 | File.Delete(testArchive); | ||
| 639 | |||
| 640 | if (caughtEx != null) | ||
| 641 | { | ||
| 642 | Assert.IsInstanceOfType(caughtEx, expectedExceptionType, | ||
| 643 | String.Format("Caught exception listing archive truncated to {0}/{1} bytes", | ||
| 644 | len, archiveInfo.Length)); | ||
| 645 | } | ||
| 646 | } | ||
| 647 | } | ||
| 648 | } | ||
| 649 | } | ||
diff --git a/src/dtf/WixToolsetTests.Dtf.Compression/MisbehavingStreamContext.cs b/src/dtf/WixToolsetTests.Dtf.Compression/MisbehavingStreamContext.cs new file mode 100644 index 00000000..2531f3bc --- /dev/null +++ b/src/dtf/WixToolsetTests.Dtf.Compression/MisbehavingStreamContext.cs | |||
| @@ -0,0 +1,202 @@ | |||
| 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 | using System; | ||
| 4 | using System.IO; | ||
| 5 | using System.Collections.Generic; | ||
| 6 | using WixToolset.Dtf.Compression; | ||
| 7 | |||
| 8 | namespace WixToolset.Dtf.Test | ||
| 9 | { | ||
| 10 | public class MisbehavingStreamContext : ArchiveFileStreamContext | ||
| 11 | { | ||
| 12 | public const string EXCEPTION = "Test exception."; | ||
| 13 | |||
| 14 | private bool throwEx; | ||
| 15 | private bool getName; | ||
| 16 | private bool openArchive; | ||
| 17 | private bool closeArchive; | ||
| 18 | private bool openFile; | ||
| 19 | private bool closeFile; | ||
| 20 | private int closeFileCount; | ||
| 21 | |||
| 22 | public MisbehavingStreamContext( | ||
| 23 | string cabinetFile, | ||
| 24 | string directory, | ||
| 25 | IDictionary<string, string> files, | ||
| 26 | bool throwEx, | ||
| 27 | bool getName, | ||
| 28 | bool openArchive, | ||
| 29 | bool closeArchive, | ||
| 30 | bool openFile, | ||
| 31 | bool closeFile) | ||
| 32 | : base(cabinetFile, directory, files) | ||
| 33 | { | ||
| 34 | this.throwEx = throwEx; | ||
| 35 | this.getName = getName; | ||
| 36 | this.openArchive = openArchive; | ||
| 37 | this.closeArchive = closeArchive; | ||
| 38 | this.openFile = openFile; | ||
| 39 | this.closeFile = closeFile; | ||
| 40 | } | ||
| 41 | |||
| 42 | public override string GetArchiveName(int archiveNumber) | ||
| 43 | { | ||
| 44 | if (!this.getName) | ||
| 45 | { | ||
| 46 | if (throwEx) | ||
| 47 | { | ||
| 48 | throw new Exception(EXCEPTION); | ||
| 49 | } | ||
| 50 | else | ||
| 51 | { | ||
| 52 | return null; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | return base.GetArchiveName(archiveNumber); | ||
| 56 | } | ||
| 57 | |||
| 58 | public override Stream OpenArchiveWriteStream( | ||
| 59 | int archiveNumber, | ||
| 60 | string archiveName, | ||
| 61 | bool truncate, | ||
| 62 | CompressionEngine compressionEngine) | ||
| 63 | { | ||
| 64 | if (!this.openArchive) | ||
| 65 | { | ||
| 66 | if (throwEx) | ||
| 67 | { | ||
| 68 | throw new Exception(EXCEPTION); | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | return null; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | return base.OpenArchiveWriteStream( | ||
| 76 | archiveNumber, archiveName, truncate, compressionEngine); | ||
| 77 | } | ||
| 78 | |||
| 79 | public override void CloseArchiveWriteStream( | ||
| 80 | int archiveNumber, | ||
| 81 | string archiveName, | ||
| 82 | Stream stream) | ||
| 83 | { | ||
| 84 | if (!this.closeArchive) | ||
| 85 | { | ||
| 86 | if (throwEx) | ||
| 87 | { | ||
| 88 | this.closeArchive = true; | ||
| 89 | throw new Exception(EXCEPTION); | ||
| 90 | } | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | base.CloseArchiveWriteStream(archiveNumber, archiveName, stream); | ||
| 94 | } | ||
| 95 | |||
| 96 | public override Stream OpenFileReadStream( | ||
| 97 | string path, | ||
| 98 | out FileAttributes attributes, | ||
| 99 | out DateTime lastWriteTime) | ||
| 100 | { | ||
| 101 | if (!this.openFile) | ||
| 102 | { | ||
| 103 | if (throwEx) | ||
| 104 | { | ||
| 105 | throw new Exception(EXCEPTION); | ||
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | attributes = FileAttributes.Normal; | ||
| 110 | lastWriteTime = DateTime.MinValue; | ||
| 111 | return null; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | return base.OpenFileReadStream(path, out attributes, out lastWriteTime); | ||
| 115 | } | ||
| 116 | |||
| 117 | public override void CloseFileReadStream(string path, Stream stream) | ||
| 118 | { | ||
| 119 | if (!this.closeFile && ++closeFileCount == 2) | ||
| 120 | { | ||
| 121 | if (throwEx) | ||
| 122 | { | ||
| 123 | throw new Exception(EXCEPTION); | ||
| 124 | } | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | base.CloseFileReadStream(path, stream); | ||
| 128 | } | ||
| 129 | |||
| 130 | public override Stream OpenArchiveReadStream( | ||
| 131 | int archiveNumber, | ||
| 132 | string archiveName, | ||
| 133 | CompressionEngine compressionEngine) | ||
| 134 | { | ||
| 135 | if (!this.openArchive) | ||
| 136 | { | ||
| 137 | if (throwEx) | ||
| 138 | { | ||
| 139 | throw new Exception(EXCEPTION); | ||
| 140 | } | ||
| 141 | else | ||
| 142 | { | ||
| 143 | return null; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | return base.OpenArchiveReadStream(archiveNumber, archiveName, compressionEngine); | ||
| 147 | } | ||
| 148 | |||
| 149 | public override void CloseArchiveReadStream( | ||
| 150 | int archiveNumber, | ||
| 151 | string archiveName, | ||
| 152 | Stream stream) | ||
| 153 | { | ||
| 154 | if (!this.closeArchive) | ||
| 155 | { | ||
| 156 | if (throwEx) | ||
| 157 | { | ||
| 158 | this.closeArchive = true; | ||
| 159 | throw new Exception(EXCEPTION); | ||
| 160 | } | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | base.CloseArchiveReadStream(archiveNumber, archiveName, stream); | ||
| 164 | } | ||
| 165 | |||
| 166 | public override Stream OpenFileWriteStream( | ||
| 167 | string path, | ||
| 168 | long fileSize, | ||
| 169 | DateTime lastWriteTime) | ||
| 170 | { | ||
| 171 | if (!this.openFile) | ||
| 172 | { | ||
| 173 | if (throwEx) | ||
| 174 | { | ||
| 175 | throw new Exception(EXCEPTION); | ||
| 176 | } | ||
| 177 | else | ||
| 178 | { | ||
| 179 | return null; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return base.OpenFileWriteStream(path, fileSize, lastWriteTime); | ||
| 183 | } | ||
| 184 | |||
| 185 | public override void CloseFileWriteStream( | ||
| 186 | string path, | ||
| 187 | Stream stream, | ||
| 188 | FileAttributes attributes, | ||
| 189 | DateTime lastWriteTime) | ||
| 190 | { | ||
| 191 | if (!this.closeFile && ++closeFileCount == 2) | ||
| 192 | { | ||
| 193 | if (throwEx) | ||
| 194 | { | ||
| 195 | throw new Exception(EXCEPTION); | ||
| 196 | } | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | base.CloseFileWriteStream(path, stream, attributes, lastWriteTime); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
diff --git a/src/dtf/WixToolsetTests.Dtf.Compression/OptionStreamContext.cs b/src/dtf/WixToolsetTests.Dtf.Compression/OptionStreamContext.cs new file mode 100644 index 00000000..98354d97 --- /dev/null +++ b/src/dtf/WixToolsetTests.Dtf.Compression/OptionStreamContext.cs | |||
| @@ -0,0 +1,42 @@ | |||
| 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 | using System; | ||
| 4 | using System.Collections.Generic; | ||
| 5 | using WixToolset.Dtf.Compression; | ||
| 6 | |||
| 7 | namespace WixToolset.Dtf.Test | ||
| 8 | { | ||
| 9 | public class OptionStreamContext : ArchiveFileStreamContext | ||
| 10 | { | ||
| 11 | private PackOptionHandler packOptionHandler; | ||
| 12 | |||
| 13 | public OptionStreamContext(IList<string> archiveFiles, string directory, IDictionary<string, string> files) | ||
| 14 | : base(archiveFiles, directory, files) | ||
| 15 | { | ||
| 16 | } | ||
| 17 | |||
| 18 | public delegate object PackOptionHandler(string optionName, object[] parameters); | ||
| 19 | |||
| 20 | public PackOptionHandler OptionHandler | ||
| 21 | { | ||
| 22 | get | ||
| 23 | { | ||
| 24 | return this.packOptionHandler; | ||
| 25 | } | ||
| 26 | set | ||
| 27 | { | ||
| 28 | this.packOptionHandler = value; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | public override object GetOption(string optionName, object[] parameters) | ||
| 33 | { | ||
| 34 | if (this.OptionHandler == null) | ||
| 35 | { | ||
| 36 | return null; | ||
| 37 | } | ||
| 38 | |||
| 39 | return this.OptionHandler(optionName, parameters); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | } | ||
diff --git a/src/dtf/WixToolsetTests.Dtf.Compression/WixToolsetTests.Dtf.Compression.csproj b/src/dtf/WixToolsetTests.Dtf.Compression/WixToolsetTests.Dtf.Compression.csproj new file mode 100644 index 00000000..120779ee --- /dev/null +++ b/src/dtf/WixToolsetTests.Dtf.Compression/WixToolsetTests.Dtf.Compression.csproj | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | <?xml version="1.0" encoding="utf-8" ?> | ||
| 2 | <!-- 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. --> | ||
| 3 | |||
| 4 | <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| 5 | <PropertyGroup> | ||
| 6 | <ProjectGuid>{F045FFC1-05F9-4EA2-9F03-E1CBDB7BC4F9}</ProjectGuid> | ||
| 7 | <OutputType>Library</OutputType> | ||
| 8 | <RootNamespace>WixToolsetTests.Dtf</RootNamespace> | ||
| 9 | <AssemblyName>WixToolsetTests.Dtf.Compression</AssemblyName> | ||
| 10 | <CreateDocumentation>false</CreateDocumentation> | ||
| 11 | </PropertyGroup> | ||
| 12 | |||
| 13 | <ItemGroup> | ||
| 14 | <Compile Include="CompressionTestUtil.cs" /> | ||
| 15 | <Compile Include="MisbehavingStreamContext.cs" /> | ||
| 16 | <Compile Include="OptionStreamContext.cs" /> | ||
| 17 | </ItemGroup> | ||
| 18 | |||
| 19 | <ItemGroup> | ||
| 20 | <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> | ||
| 21 | <Reference Include="System" /> | ||
| 22 | <Reference Include="System.Data" /> | ||
| 23 | <Reference Include="System.Xml" /> | ||
| 24 | </ItemGroup> | ||
| 25 | |||
| 26 | <ItemGroup> | ||
| 27 | <ProjectReference Include="..\WixToolset.Dtf.Compression\WixToolset.Dtf.Compression.csproj" /> | ||
| 28 | </ItemGroup> | ||
| 29 | |||
| 30 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | ||
| 31 | </Project> | ||
