diff options
Diffstat (limited to '')
-rw-r--r-- | src/dtf/WixToolsetTests.Dtf.Compression/CompressionTestUtil.cs | 649 |
1 files changed, 649 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 | } | ||