Skip to content

Commit 869d311

Browse files
committed
Added additional comment encoding tests
1 parent bbe41a2 commit 869d311

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Linq;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using Xunit;
910

@@ -499,5 +500,17 @@ public static IEnumerable<object[]> Latin1Comment_Data()
499500
yield return e;
500501
}
501502
}
503+
504+
// Returns pairs encoded with Latin1, but decoded with UTF8.
505+
// Returns: originalComment, expectedComment, transcoded expectedComment
506+
public static IEnumerable<object[]> MismatchingEncodingComment_Data()
507+
{
508+
foreach (object[] e in Latin1Comment_Data())
509+
{
510+
byte[] expectedBytes = Encoding.Latin1.GetBytes(e[1] as string);
511+
512+
yield return new object[] { e[0], e[1], Encoding.UTF8.GetString(expectedBytes) };
513+
}
514+
}
502515
}
503516
}

src/libraries/System.IO.Compression/tests/ZipArchive/zip_CreateTests.Comments.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,50 @@ public static void Create_Comment_Utf8EntryName_Latin1Encoding(string originalCo
4141

4242
[Theory]
4343
[MemberData(nameof(Utf8Comment_Data))]
44-
public static void Create_Comment_Utf8EntryName_Varying_Encodings(string originalComment, string expectedComment)
44+
public static void Create_Comment_Utf8EntryName_Utf8Encoding_Prioritised(string originalComment, string expectedComment)
4545
// UTF8 encoding bit is set in the general-purpose bit flags. The verification encoding of Latin1 should be ignored
46-
=> Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, Encoding.UTF8, Encoding.Latin1);
46+
=> Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, expectedComment, Encoding.UTF8, Encoding.Latin1);
47+
48+
[Theory]
49+
[MemberData(nameof(MismatchingEncodingComment_Data))]
50+
public static void Create_Comment_AsciiEntryName_Utf8Decoding_Invalid(string originalComment, string expectedPreWriteComment, string expectedPostWriteComment)
51+
// The UTF8 encoding bit in the general-purpose bit flags should not be set, filenames should be encoded with Latin1, and thus
52+
// decoding with UTF8 should result in incorrect filenames. This is because the filenames and comments contain code points in the
53+
// range 0xC0..0xFF (which Latin1 encodes in one byte, and UTF8 encodes in two bytes.)
54+
=> Create_Comment_EntryName_Encoding_Internal(AsciiFileName, originalComment, expectedPreWriteComment, expectedPostWriteComment, Encoding.Latin1, Encoding.UTF8);
55+
56+
[Theory]
57+
[MemberData(nameof(MismatchingEncodingComment_Data))]
58+
public static void Create_Comment_AsciiEntryName_DefaultDecoding_Utf8(string originalComment, string expectedPreWriteComment, string expectedPostWriteComment)
59+
// Filenames should be encoded with Latin1, resulting in the UTF8 encoding bit in the general-purpose bit flags not being set.
60+
// However, failing to specify an encoding (or specifying a null encoding) for the read should result in UTF8 being used anyway.
61+
// This should result in incorrect filenames, since the filenames and comments contain code points in the range 0xC0..0xFF (which
62+
// Latin1 encodes in one byte, and UTF8 encodes in two bytes.)
63+
=> Create_Comment_EntryName_Encoding_Internal(AsciiFileName, originalComment, expectedPreWriteComment, expectedPostWriteComment, Encoding.Latin1, null);
4764

4865
private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment, string expectedComment, Encoding encoding)
49-
=> Create_Comment_EntryName_Encoding_Internal(entryName, originalComment, expectedComment, encoding, encoding);
66+
=> Create_Comment_EntryName_Encoding_Internal(entryName, originalComment, expectedComment, expectedComment, encoding, encoding);
5067

51-
private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment, string expectedComment, Encoding creationEncoding, Encoding verificationEencoding)
68+
private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment,
69+
string expectedPreWriteComment, string expectedPostWriteComment,
70+
Encoding creationEncoding, Encoding verificationEncoding)
5271
{
5372
using var ms = new MemoryStream();
5473

5574
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true, creationEncoding))
5675
{
5776
ZipArchiveEntry entry = zip.CreateEntry(entryName, CompressionLevel.NoCompression);
5877
entry.Comment = originalComment;
59-
Assert.Equal(expectedComment, entry.Comment);
78+
// The expected pre-write and post-write comment can be different when testing encodings which vary between operations.
79+
Assert.Equal(expectedPreWriteComment, entry.Comment);
6080
}
6181

62-
using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: false, verificationEencoding))
82+
using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: false, verificationEncoding))
6383
{
6484
foreach (ZipArchiveEntry entry in zip.Entries)
6585
{
6686
Assert.Equal(entryName, entry.Name);
67-
Assert.Equal(expectedComment, entry.Comment);
87+
Assert.Equal(expectedPostWriteComment, entry.Comment);
6888
}
6989
}
7090
}

0 commit comments

Comments
 (0)