Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
08f2a8b
ref: Add public APIs.
carlossanlop Sep 22, 2021
973fc8c
src: Expose the archive and entry comments.
carlossanlop Sep 22, 2021
be54e4a
tests: Add update tests for archives and for entries. They cover crea…
carlossanlop Sep 22, 2021
28467ae
Fix encoding detection feedback
carlossanlop Sep 28, 2021
297b9e5
Fix encoding detection feedback
carlossanlop Sep 28, 2021
338b184
Address suggestions
carlossanlop Oct 6, 2021
31a2227
Switch names of archive comment fields.
carlossanlop Oct 18, 2021
78d7e37
Address unicode bit flag sharing problem.
carlossanlop Oct 19, 2021
0a29149
Add more test cases
carlossanlop Oct 19, 2021
b93305b
Adjust tests
carlossanlop Oct 19, 2021
7496628
Add newline so comment only applies to one line
carlossanlop Oct 19, 2021
a949883
Ensure string byte truncation is aligned to encoding's char size.
carlossanlop Nov 27, 2021
9738c30
Remove empty check for non-nullable string. Also remove unnecessary D…
carlossanlop Nov 29, 2021
5179ea2
Defer calculation of truncated encoding string to getter and to writ…
carlossanlop Dec 4, 2021
0e47de4
Rename test arguments
carlossanlop Dec 6, 2021
18bbcd4
Only use bytes[]
carlossanlop Dec 7, 2021
31602b3
Remove unnecessary bit comment
carlossanlop Dec 7, 2021
0ae8a64
Remove unnecessary length check
carlossanlop Dec 7, 2021
e9d5b67
Address feedback
carlossanlop Dec 8, 2021
77b6ee5
Suggestion by adamsitnik: write only if length > 0
carlossanlop Dec 7, 2021
8daedcf
Simplify EntryName code
carlossanlop Dec 8, 2021
1596bb3
Move entryName code to original location
carlossanlop Dec 8, 2021
3444631
In UTF8, use Runes to detect code point length to prevent truncating …
carlossanlop Jan 19, 2022
0749c84
Address suggestions
carlossanlop Feb 3, 2022
b08f0ac
Move ZipTestHelper back to its original position because S.IO.Compres…
carlossanlop Feb 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add more test cases
  • Loading branch information
carlossanlop committed Jan 18, 2022
commit 0a29149ce542ed2ae28cfc47c81b18edc7038450
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void UpdateUncompressedArchive()

[Theory]
[MemberData(nameof(TestComments))]
public static void Update_ZipArchive_Comment(string? comment)
public static void Update_ZipArchive_Comment(string? comment, Encoding? encoding)
{
string expectedComment = GetExpectedComment(comment);
string updatedComment = "Updated comment " + comment;
Expand All @@ -369,32 +369,32 @@ public static void Update_ZipArchive_Comment(string? comment)
var testStream = new WrappedStream(stream, true, true, true, null);

// Create
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Create, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Create, leaveOpen: true, encoding))
{
zip.Comment = comment;
Assert.Equal(expectedComment, zip.Comment);
}
// Read (verify creation)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: true, encoding))
{
Assert.Equal(expectedComment, zip.Comment);
}
// Update
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Update, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Update, leaveOpen: true, encoding))
{
zip.Comment = updatedComment;
Assert.Equal(expectedUpdatedComment, zip.Comment);
}
// Read (verify update)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read))
// Read (verify update) and autoclose
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: false, encoding))
{
Assert.Equal(expectedUpdatedComment, zip.Comment);
}
}

[Theory]
[MemberData(nameof(TestComments))]
public static void Update_ZipArchiveEntry_Comment(string? comment)
public static void Update_ZipArchiveEntry_Comment(string? comment, Encoding? encoding)
{
string expectedComment = GetExpectedComment(comment);
string updatedComment = "Updated comment " + comment;
Expand All @@ -404,32 +404,32 @@ public static void Update_ZipArchiveEntry_Comment(string? comment)
var testStream = new WrappedStream(stream, true, true, true, null);

// Create
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Create, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Create, leaveOpen: true, encoding))
{
ZipArchiveEntry entry = zip.CreateEntry("testfile.txt", CompressionLevel.NoCompression);

entry.Comment = comment;
Assert.Equal(expectedComment, entry.Comment);
}
// Read (verify creation)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: true, encoding))
{
foreach (ZipArchiveEntry entry in zip.Entries)
{
Assert.Equal(expectedComment, entry.Comment);
}
}
// Update
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Update, leaveOpen: true))
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Update, leaveOpen: true, encoding))
{
foreach (ZipArchiveEntry entry in zip.Entries)
{
entry.Comment = updatedComment;
Assert.Equal(expectedUpdatedComment, entry.Comment);
}
}
// Read (verify update)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read))
// Read (verify update) and autoclose
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: false, encoding))
{
foreach (ZipArchiveEntry entry in zip.Entries)
{
Expand All @@ -438,65 +438,51 @@ public static void Update_ZipArchiveEntry_Comment(string? comment)
}
}

[Fact]
// General purpose bit flag must get the appropriate bit set if a file comment is unicode
public static void Update_ZipArchive_And_ZipArchiveEntry_UnicodeComment()
{
string comment = "This Unicode string has 2 characters outside the " + "ASCII range:\n" + "Pi (\u03a0), and Sigma (\u03a3).";
string updatedComment = "Updated comment " + comment;

var stream = new MemoryStream();
var testStream = new WrappedStream(stream, true, true, true, null);
private static Encoding[] s_encodings = new[] { Encoding.ASCII, Encoding.Latin1, Encoding.UTF8 };

// Create with UTF8 encoding
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Create, leaveOpen: true, Encoding.UTF8))
// entryName, encoding
public static IEnumerable<object[]> TestEntryNames()
{
var entryNames = new string[]
{
zip.Comment = comment;
ZipArchiveEntry entry = zip.CreateEntry("testfile.txt", CompressionLevel.NoCompression);
entry.Comment = comment;
"entry.txt",
"��������.txt",
new string('x', ushort.MaxValue),
};

Assert.Equal(comment, zip.Comment);
Assert.Equal(comment, entry.Comment);
}
// Read with UTF8 encoding (verify creation)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: true, Encoding.UTF8))
foreach (string entryName in entryNames)
{
Assert.Equal(comment, zip.Comment);
foreach (ZipArchiveEntry entry in zip.Entries)
foreach (Encoding encoding in s_encodings)
{
Assert.Equal(comment, entry.Comment);
yield return new object[] { entryName, encoding };
}
}
// Update with UTF8 encoding
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Update, leaveOpen: true, Encoding.UTF8))
{
zip.Comment = updatedComment;
Assert.Equal(updatedComment, zip.Comment);
}

foreach (ZipArchiveEntry entry in zip.Entries)
{
entry.Comment = updatedComment;
Assert.Equal(updatedComment, entry.Comment);
}
}
// Read with UTF8 encoding (verify update)
using (var zip = new ZipArchive(testStream, ZipArchiveMode.Read, leaveOpen: false, Encoding.UTF8))
// comment, encoding
public static IEnumerable<object[]> TestComments()
{
var comments = new string[] {
"",
"1",
new string('x', ushort.MaxValue),
new string('y', ushort.MaxValue + 1), // Should get truncated
};

yield return new object[] { null, null }; // Should get saved as empty string.

foreach (string comment in comments)
{
Assert.Equal(updatedComment, zip.Comment);
foreach (ZipArchiveEntry entry in zip.Entries)
foreach (Encoding encoding in s_encodings)
{
Assert.Equal(updatedComment, entry.Comment);
yield return new object[] { comment, encoding };
}
}
}

public static IEnumerable<object[]> TestComments()
{
yield return new object[] { null }; // Should get saved as empty string.
yield return new object[] { "" };
yield return new object[] { "1" };
yield return new object[] { new string('x', ushort.MaxValue) };
yield return new object[] { new string('y', ushort.MaxValue + 1) }; // Should get truncated.
foreach (Encoding encoding in new[] { Encoding.Latin1, Encoding.UTF8 })
{
yield return new object[] { "��������", encoding };
}
}

// Ensures the specified comment is returned as a non-null string no longer than ushort.MaxValue.
Expand Down