-
Notifications
You must be signed in to change notification settings - Fork 987
/
Copy pathTarArchiveTests.cs
90 lines (78 loc) · 2.81 KB
/
TarArchiveTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Tar;
using static ICSharpCode.SharpZipLib.Tests.TestSupport.Utils;
using NUnit.Framework;
namespace ICSharpCode.SharpZipLib.Tests.Tar
{
[TestFixture]
public class TarArchiveTests
{
[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
[TestCase("output")]
[TestCase("output/")]
[TestCase(@"output\", IncludePlatform = "Win")]
public void ExtractingContentsWithNonTraversalPathSucceeds(string outputDir)
{
Assert.DoesNotThrow(() => ExtractTarOK(outputDir, "file", allowTraverse: false));
}
[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
public void ExtractingContentsWithExplicitlyAllowedTraversalPathSucceeds()
{
Assert.DoesNotThrow(() => ExtractTarOK("output", "../file", allowTraverse: true));
}
[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
[TestCase("output", "../file")]
[TestCase("output/", "../file")]
[TestCase("output", "../output.txt")]
public void ExtractingContentsWithDisallowedPathsFails(string outputDir, string fileName)
{
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
}
[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
[Platform(Include = "Win", Reason = "Backslashes are only treated as path separators on windows")]
[TestCase(@"output\", @"..\file")]
[TestCase(@"output/", @"..\file")]
[TestCase("output", @"..\output.txt")]
[TestCase(@"output\", @"..\output.txt")]
public void ExtractingContentsOnWindowsWithDisallowedPathsFails(string outputDir, string fileName)
{
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
}
public void ExtractTarOK(string outputDir, string fileName, bool allowTraverse)
{
var fileContent = Encoding.UTF8.GetBytes("file content");
using var tempDir = new TempDir();
var tempPath = tempDir.Fullpath;
var extractPath = Path.Combine(tempPath, outputDir);
var expectedOutputFile = Path.Combine(extractPath, fileName);
using var archiveStream = new MemoryStream();
Directory.CreateDirectory(extractPath);
using (var tos = new TarOutputStream(archiveStream, Encoding.UTF8){IsStreamOwner = false})
{
var entry = TarEntry.CreateTarEntry(fileName);
entry.Size = fileContent.Length;
tos.PutNextEntry(entry);
tos.Write(fileContent, 0, fileContent.Length);
tos.CloseEntry();
}
archiveStream.Position = 0;
using (var ta = TarArchive.CreateInputTarArchive(archiveStream, Encoding.UTF8))
{
ta.ProgressMessageEvent += (archive, entry, message)
=> TestContext.WriteLine($"{entry.Name} {entry.Size} {message}");
ta.ExtractContents(extractPath, allowTraverse);
}
Assert.That(File.Exists(expectedOutputFile));
}
}
}