@@ -41,30 +41,50 @@ public static void Create_Comment_Utf8EntryName_Latin1Encoding(string originalCo
41
41
42
42
[ Theory ]
43
43
[ 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 )
45
45
// 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 ) ;
47
64
48
65
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 ) ;
50
67
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 )
52
71
{
53
72
using var ms = new MemoryStream ( ) ;
54
73
55
74
using ( var zip = new ZipArchive ( ms , ZipArchiveMode . Create , leaveOpen : true , creationEncoding ) )
56
75
{
57
76
ZipArchiveEntry entry = zip . CreateEntry ( entryName , CompressionLevel . NoCompression ) ;
58
77
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 ) ;
60
80
}
61
81
62
- using ( var zip = new ZipArchive ( ms , ZipArchiveMode . Read , leaveOpen : false , verificationEencoding ) )
82
+ using ( var zip = new ZipArchive ( ms , ZipArchiveMode . Read , leaveOpen : false , verificationEncoding ) )
63
83
{
64
84
foreach ( ZipArchiveEntry entry in zip . Entries )
65
85
{
66
86
Assert . Equal ( entryName , entry . Name ) ;
67
- Assert . Equal ( expectedComment , entry . Comment ) ;
87
+ Assert . Equal ( expectedPostWriteComment , entry . Comment ) ;
68
88
}
69
89
}
70
90
}
0 commit comments