Skip to content

Commit fcc8aec

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

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public static void Create_Comment_Utf8EntryName_NullEncoding(string originalComm
3333
public static void Create_Comment_Utf8EntryName_Utf8Encoding(string originalComment, string expectedComment) =>
3434
Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, Encoding.UTF8);
3535

36+
[Theory]
37+
[MemberData(nameof(Utf8Comment_Data))]
38+
public static void Create_Comment_Utf8EntryName_Utf8Encoding_Default(string originalComment, string expectedComment) =>
39+
Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, expectedComment, Encoding.UTF8, null);
40+
3641
[Theory]
3742
[MemberData(nameof(Latin1Comment_Data))]
3843
public static void Create_Comment_Utf8EntryName_Latin1Encoding(string originalComment, string expectedComment) =>
@@ -41,30 +46,50 @@ public static void Create_Comment_Utf8EntryName_Latin1Encoding(string originalCo
4146

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

4870
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);
71+
=> Create_Comment_EntryName_Encoding_Internal(entryName, originalComment, expectedComment, expectedComment, encoding, encoding);
5072

51-
private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment, string expectedComment, Encoding creationEncoding, Encoding verificationEencoding)
73+
private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment,
74+
string expectedPreWriteComment, string expectedPostWriteComment,
75+
Encoding creationEncoding, Encoding verificationEncoding)
5276
{
5377
using var ms = new MemoryStream();
5478

5579
using (var zip = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true, creationEncoding))
5680
{
5781
ZipArchiveEntry entry = zip.CreateEntry(entryName, CompressionLevel.NoCompression);
5882
entry.Comment = originalComment;
59-
Assert.Equal(expectedComment, entry.Comment);
83+
// The expected pre-write and post-write comment can be different when testing encodings which vary between operations.
84+
Assert.Equal(expectedPreWriteComment, entry.Comment);
6085
}
6186

62-
using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: false, verificationEencoding))
87+
using (var zip = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: false, verificationEncoding))
6388
{
6489
foreach (ZipArchiveEntry entry in zip.Entries)
6590
{
6691
Assert.Equal(entryName, entry.Name);
67-
Assert.Equal(expectedComment, entry.Comment);
92+
Assert.Equal(expectedPostWriteComment, entry.Comment);
6893
}
6994
}
7095
}

0 commit comments

Comments
 (0)