Skip to content

Commit 13f6cee

Browse files
author
koeeenig
authored
Merge pull request #15 from icarus-consulting/i13-check-unknown-filepath
Add check for missing file entries
2 parents ebc4787 + 46fdd25 commit 13f6cee

File tree

5 files changed

+60
-10
lines changed

5 files changed

+60
-10
lines changed

build.cake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#tool nuget:?package=OpenCover
33
#tool nuget:?package=xunit.runner.console
44
#tool nuget:?package=Codecov
5-
#tool nuget:?package=ReportGenerator
5+
#tool nuget:?package=ReportGenerator&version=4.2.20
66
#addin nuget:?package=Cake.Codecov&version=0.5.0
77

88
var target = Argument("target", "Default");

src/Yaapii.Zip/HasPassword.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ public HasPassword(IInput zip, string virtualPath)
2525

2626
public bool Value()
2727
{
28-
var stream = zip.Stream();
29-
stream.Seek(0, SeekOrigin.Begin);
3028
new FailWhen(
3129
() => !new IsZipArchive(this.zip).Value(),
32-
new ArgumentException(
33-
"Can not extract zip because no zip was provided."
34-
)
30+
new ArgumentException("Cannot check for password because no zip was provided.")
3531
).Go();
36-
stream.Seek(0, SeekOrigin.Begin);
32+
33+
// ZipFile.ContainsEntry() by Ionic does not work with backslashes,
34+
// even if the path is normalized. Misterious
35+
new FailWhen(
36+
() => !new ZipContains(this.zip, this.virtualPath).Value(),
37+
new ArgumentException($"Cannot check for password because file '{this.virtualPath} doesn't exists in zip.")
38+
).Go();
39+
3740
bool result;
38-
using (var zip = ZipFile.Read(this.zip.Stream()))
41+
var stream = this.zip.Stream();
42+
stream.Seek(0, SeekOrigin.Begin);
43+
using (var zip = ZipFile.Read(stream))
3944
{
40-
result = zip[virtualPath].UsesEncryption;
45+
result = zip[this.virtualPath].UsesEncryption;
4146
}
4247
return result;
4348
}

tests/Test.Yaapii.Zip/HasPasswordTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,19 @@ public void ThrowsOnNoZip()
7171
new HasPassword(new InputOf("test"), "a path").Value()
7272
);
7373
}
74+
75+
[Fact]
76+
public void ThrowsOnMissingEntry()
77+
{
78+
Assert.Throws<ArgumentException>(() =>
79+
new HasPassword(
80+
new Zipped(
81+
"filename.txt",
82+
new InputOf("a input")
83+
),
84+
"otherfile.txt"
85+
).Value()
86+
);
87+
}
7488
}
7589
}

tests/Test.Yaapii.Zip/ZipExtractedTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using System;
2+
using Xunit;
23
using Yaapii.Atoms.IO;
34
using Yaapii.Atoms.Text;
45

@@ -39,6 +40,20 @@ public void WorksWithSlashAndBacklashSeparation(string path)
3940
);
4041
}
4142

43+
[Fact]
44+
public void FailsOnMissingEntry()
45+
{
46+
Assert.Throws<ArgumentException>(() =>
47+
new ZipExtracted(
48+
new Zipped(
49+
"filename.txt",
50+
new InputOf("a input")
51+
),
52+
"otherfile.txt"
53+
).Stream()
54+
);
55+
}
56+
4257
[Theory]
4358
[InlineData("Datum/windows.zip")]
4459
[InlineData("Datum/7zip.zip")]

tests/Test.Yaapii.Zip/ZipPasswordExtractedTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,21 @@ public void FailsOnNoPassword()
7777
).AsString()
7878
);
7979
}
80+
81+
[Fact]
82+
public void FailsOnMissingEntry()
83+
{
84+
Assert.Throws<ArgumentException>(() =>
85+
new ZipPasswordExtracted(
86+
new ZipWithPassword(
87+
"filename.txt",
88+
"pwd",
89+
new InputOf("a input")
90+
),
91+
"otherfile.txt",
92+
"pwd"
93+
).Stream()
94+
);
95+
}
8096
}
8197
}

0 commit comments

Comments
 (0)