Skip to content

Commit ac4fc5f

Browse files
authored
Merge pull request #22 from icarus-consulting/i21-RemovePasswordValidationFromClasses
refactor: remove password validation from classes
2 parents aeb25d1 + f3f546a commit ac4fc5f

File tree

5 files changed

+60
-56
lines changed

5 files changed

+60
-56
lines changed

src/Yaapii.Zip/ZipExtracted.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ public Stream Stream()
3333
{
3434
lock (zip)
3535
{
36-
new FailWhen(
37-
() => new HasPassword(this.zip, filePath).Value(),
38-
new InvalidOperationException(
39-
"Can not read content of the zip because the file is password protected"
40-
)
41-
).Go();
4236
Stream content;
4337
using (var archive = new ZipArchive(this.zip.Stream(), ZipArchiveMode.Read, leaveOpen))
4438
{
@@ -64,9 +58,16 @@ private Stream Content(ZipArchiveEntry zipEntry)
6458
MemoryStream content = new MemoryStream();
6559
using (Stream zipEntryStream = zipEntry.Open())
6660
{
67-
zipEntryStream.CopyTo(content);
68-
zipEntryStream.Close();
69-
content.Seek(0, SeekOrigin.Begin);
61+
try
62+
{
63+
zipEntryStream.CopyTo(content);
64+
zipEntryStream.Close();
65+
content.Seek(0, SeekOrigin.Begin);
66+
}
67+
catch (InvalidDataException ex)
68+
{
69+
throw new InvalidOperationException($"Unable to extract file '{this.filePath}'. Maybe the zip is encrypted", ex);
70+
}
7071
}
7172
return content;
7273
}

src/Yaapii.Zip/ZipPasswordExtracted.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,7 @@ public ZipPasswordExtracted(IInput zip, string virtualPath, string password)
2828

2929
public Stream Stream()
3030
{
31-
new FailWhen(
32-
() => !new IsZipArchive(this.zip).Value(),
33-
new ArgumentException(
34-
"Can not extract zip because no zip was provided."
35-
)
36-
).Go();
37-
new FailWhen(
38-
() => !new HasPassword(this.zip, this.virtualPath).Value(),
39-
new InvalidOperationException(
40-
"Can not extract zip because the file is not protected with a password."
41-
)
42-
).Go();
31+
Validate();
4332
zip.Stream().Seek(0, SeekOrigin.Begin);
4433
IInput result;
4534
using (var stream = new MemoryStream())
@@ -57,5 +46,21 @@ public Stream Stream()
5746
}
5847
return result.Stream();
5948
}
49+
50+
private void Validate()
51+
{
52+
new FailWhen(
53+
() => !new IsZipArchive(this.zip).Value(),
54+
new ArgumentException(
55+
"Can not extract zip because no zip was provided."
56+
)
57+
).Go();
58+
// ZipFile.ContainsEntry() by Ionic does not work with backslashes,
59+
// even if the path is normalized. Misterious
60+
new FailWhen(
61+
() => !new ZipContains(this.zip, this.virtualPath).Value(),
62+
new ArgumentException($"Cannot check for password because file '{this.virtualPath} doesn't exists in zip.")
63+
).Go();
64+
}
6065
}
6166
}

src/Yaapii.Zip/ZipUpdated.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ public ZipUpdated(IScalar<Stream> zip, string pathToUpdate, IInput update, bool
3737
var stream = zip.Value();
3838
lock (stream)
3939
{
40-
new FailWhen(
41-
() => ExistsAndCrypted(new InputOf(stream), pathToUpdate),
42-
new InvalidOperationException(
43-
$"Cannot update '{pathToUpdate}' because the file is password protected"
44-
)
45-
).Go();
46-
4740
stream.Seek(0, SeekOrigin.Begin);
4841
using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, leaveOpen))
4942
{
@@ -83,18 +76,5 @@ private string Normalized(string path)
8376
var file = Path.GetFileName(path).ToLower();
8477
return Path.Combine(dir, file);
8578
}
86-
87-
private bool ExistsAndCrypted(IInput zip, string filepath)
88-
{
89-
bool result = false;
90-
if(new ZipContains(zip, filepath).Value())
91-
{
92-
if(new HasPassword(zip, filepath).Value())
93-
{
94-
result = true;
95-
}
96-
}
97-
return result;
98-
}
9979
}
10080
}

tests/Test.Yaapii.Zip/ZipPasswordExtractedTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ public void ExtractsFromDifferentZips(string path)
6262
}
6363

6464
[Fact]
65-
public void FailsOnNoPassword()
65+
public void ExtractsUnencryptedZip()
6666
{
67-
Assert.Throws<InvalidOperationException>(() =>
67+
Assert.Equal(
68+
"unsafe",
6869
new TextOf(
6970
new ZipPasswordExtracted(
7071
new Zipped(
7172
"text.txt",
72-
new InputOf("safe")
73+
new InputOf("unsafe")
7374
),
7475
"text.txt",
7576
"password"

tests/Test.Yaapii.Zip/ZipUpdatedTests.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,35 @@ public void UpdatesFile()
2727
}
2828

2929
[Fact]
30-
public void ThrowsForPasswordFile()
30+
public void UpdatesPasswordProtectedZip()
3131
{
32-
Assert.Throws<InvalidOperationException>(() =>
33-
new ZipUpdated(
34-
new ZipWithPassword(
35-
"Brave Citizens.txt",
36-
"pwd",
37-
new InputOf("Empty data will not crypted!")
38-
),
39-
"Brave Citizens.txt",
40-
new InputOf("456")
41-
).Stream()
32+
//Assert.Throws<InvalidOperationException>(() =>
33+
// new ZipUpdated(
34+
// new ZipWithPassword(
35+
// "Brave Citizens.txt",
36+
// "pwd",
37+
// new InputOf("Empty data will not crypted!")
38+
// ),
39+
// "Brave Citizens.txt",
40+
// new InputOf("456")
41+
// ).Stream()
42+
//);
43+
Assert.Equal(
44+
"456",
45+
new TextOf(
46+
new ZipExtracted(
47+
new ZipUpdated(
48+
new ZipWithPassword(
49+
"Brave Citizens.txt",
50+
"pwd",
51+
new InputOf("Empty data will not crypted!")
52+
),
53+
"Brave Citizens.txt",
54+
new InputOf("456")
55+
),
56+
"Brave Citizens.txt"
57+
)
58+
).AsString()
4259
);
4360
}
4461

@@ -74,7 +91,7 @@ public void UpdatesFileInDifferentZips(string path)
7491
[InlineData("Datum/winrar_crypt_aes.zip")]
7592
public void ThrowsForDifferentCrypedZips(string path)
7693
{
77-
Assert.Throws<InvalidOperationException>(() =>
94+
Assert.Throws<ArgumentException>(() =>
7895
new ZipUpdated(
7996
new ResourceOf(path, this.GetType()),
8097
"c/Y/test-a-y-2.txt",

0 commit comments

Comments
 (0)