-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expose Comment in ZipArchive and ZipArchiveEntry #59442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
08f2a8b
973fc8c
be54e4a
28467ae
297b9e5
338b184
31a2227
78d7e37
0a29149
b93305b
7496628
a949883
9738c30
5179ea2
0e47de4
18bbcd4
31602b3
0ae8a64
e9d5b67
77b6ee5
8daedcf
1596bb3
3444631
0749c84
b08f0ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,33 +11,33 @@ public partial class zip_CreateTests : ZipFileTestBase | |
| [Theory] | ||
| [MemberData(nameof(Utf8Comment_Data))] | ||
| public static void Create_Comment_AsciiEntryName_NullEncoding(string originalComment, string expectedComment) => | ||
| Create_Comment_EntryName_Encoding_Internal("file.txt", originalComment, expectedComment, null); | ||
| Create_Comment_EntryName_Encoding_Internal(AsciiFileName, originalComment, expectedComment, null); | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(Utf8Comment_Data))] | ||
| public static void Create_Comment_AsciiEntryName_Utf8Encoding(string originalComment, string expectedComment) => | ||
| Create_Comment_EntryName_Encoding_Internal("file.txt", originalComment, expectedComment, Encoding.UTF8); | ||
| Create_Comment_EntryName_Encoding_Internal(AsciiFileName, originalComment, expectedComment, Encoding.UTF8); | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(Latin1Comment_Data))] | ||
| public static void Create_Comment_AsciiEntryName_Latin1Encoding(string originalComment, string expectedComment) => | ||
| Create_Comment_EntryName_Encoding_Internal("file.txt", originalComment, expectedComment, Encoding.Latin1); | ||
| Create_Comment_EntryName_Encoding_Internal(AsciiFileName, originalComment, expectedComment, Encoding.Latin1); | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(Utf8Comment_Data))] | ||
| public static void Create_Comment_Utf8EntryName_NullEncoding(string originalComment, string expectedComment) => | ||
| Create_Comment_EntryName_Encoding_Internal($"{SmileyEmoji}.txt", originalComment, expectedComment, null); | ||
| Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, null); | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(Utf8Comment_Data))] | ||
| public static void Create_Comment_Utf8EntryName_Utf8Encoding(string originalComment, string expectedComment) => | ||
| Create_Comment_EntryName_Encoding_Internal($"{SmileyEmoji}.txt", originalComment, expectedComment, Encoding.UTF8); | ||
| Create_Comment_EntryName_Encoding_Internal(Utf8FileName, originalComment, expectedComment, Encoding.UTF8); | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(Latin1Comment_Data))] | ||
| public static void Create_Comment_Utf8EntryName_Latin1Encoding(string originalComment, string expectedComment) => | ||
| // Emoji not supported by latin1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if you pass an emoji with latin1 encoding? I assume it just encodes it as something meaningless. We should assert that the latin1-encoded-emoji still gets truncated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how I would assert that in the tests. I just wanted to make sure the string got truncated, even if the final outputted string, when viewed by the user, will be garbage where the emoji was saved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On My point being that the truncation logic only runs when it makes sense (UTF8). |
||
| Create_Comment_EntryName_Encoding_Internal($"{LowerCaseOUmlautChar}.txt", originalComment, expectedComment, Encoding.Latin1); | ||
| Create_Comment_EntryName_Encoding_Internal(Utf8AndLatin1FileName, originalComment, expectedComment, Encoding.Latin1); | ||
|
|
||
| private static void Create_Comment_EntryName_Encoding_Internal(string entryName, string originalComment, string expectedComment, Encoding encoding) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -384,9 +384,18 @@ internal static void AddEntry(ZipArchive archive, string name, string contents, | |
| } | ||
| } | ||
|
|
||
| protected const string SmileyEmoji = "\ud83d\ude04"; | ||
| protected const string LowerCaseOUmlautChar = "\u00F6"; | ||
| protected const string CopyrightChar = "\u00A9"; | ||
| protected const string Utf8SmileyEmoji = "\ud83d\ude04"; | ||
| protected const string Utf8LowerCaseOUmlautChar = "\u00F6"; | ||
| protected const string Utf8CopyrightChar = "\u00A9"; | ||
| protected const string AsciiFileName = "file.txt"; | ||
| // The o with umlaut is a character that exists in both latin1 and utf8 | ||
| protected const string Utf8AndLatin1FileName = $"{Utf8LowerCaseOUmlautChar}.txt"; | ||
| // emojis only make sense in utf8 | ||
| protected const string Utf8FileName = $"{Utf8SmileyEmoji}.txt"; | ||
| protected static readonly string ALettersUShortMaxValueMinusOne = new string('a', ushort.MaxValue - 1); | ||
| protected static readonly string ALettersUShortMaxValue = ALettersUShortMaxValueMinusOne + 'a'; | ||
| protected static readonly string ALettersUShortMaxValueMinusOneAndCopyRightChar = ALettersUShortMaxValueMinusOne + Utf8CopyrightChar; | ||
| protected static readonly string ALettersUShortMaxValueMinusOneAndTwoCopyRightChars = ALettersUShortMaxValueMinusOneAndCopyRightChar + Utf8CopyrightChar; | ||
|
|
||
| // Returns pairs that are returned the same way by Utf8 and Latin1 | ||
| // Returns: originalComment, expectedComment | ||
|
|
@@ -395,28 +404,31 @@ private static IEnumerable<object[]> SharedComment_Data() | |
| yield return new object[] { null, string.Empty }; | ||
| yield return new object[] { string.Empty, string.Empty }; | ||
| yield return new object[] { "a", "a" }; | ||
| yield return new object[] { LowerCaseOUmlautChar, LowerCaseOUmlautChar }; | ||
| yield return new object[] { Utf8LowerCaseOUmlautChar, Utf8LowerCaseOUmlautChar }; | ||
| } | ||
|
|
||
| // Returns pairs as expected by Utf8 | ||
| // Returns: originalComment, expectedComment | ||
| public static IEnumerable<object[]> Utf8Comment_Data() | ||
| { | ||
| string asciiExpectedExactMaxLength = new('a', ushort.MaxValue); | ||
| string asciiOriginalOverMaxLength = asciiExpectedExactMaxLength + "aaa"; | ||
| string asciiOriginalOverMaxLength = ALettersUShortMaxValue + "aaa"; | ||
|
|
||
| // A smiley emoji code point consists of two characters, | ||
| // meaning the whole emoji should be fully truncated | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add a case where the emoji does not get truncated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only case where the string is truncated is when it's length is greater than expected. The contents don't matter. So if a string is smaller than the max length, it should remain untouched, regardless of its contents.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
| string utf8ExpectedJustALetters = new('a', ushort.MaxValue - 1); | ||
| string utf8OriginalALettersAndOneEmoji = utf8ExpectedJustALetters + SmileyEmoji; | ||
| string utf8OriginalALettersAndOneEmojiDoesNotFit = ALettersUShortMaxValueMinusOne + Utf8SmileyEmoji; | ||
|
|
||
| // A smiley emoji code point consists of two characters, | ||
| // so it should not be truncated if it's the last character and the total length is not over the limit. | ||
| string utf8OriginalALettersAndOneEmojiFits = "aaaaa" + Utf8SmileyEmoji; | ||
|
|
||
| yield return new object[] { asciiOriginalOverMaxLength, ALettersUShortMaxValue }; | ||
| yield return new object[] { utf8OriginalALettersAndOneEmojiDoesNotFit, ALettersUShortMaxValueMinusOne }; | ||
| yield return new object[] { utf8OriginalALettersAndOneEmojiFits, utf8OriginalALettersAndOneEmojiFits }; | ||
|
|
||
| foreach (object[] e in SharedComment_Data()) | ||
| { | ||
| yield return e; | ||
| } | ||
|
|
||
| yield return new object[] { asciiOriginalOverMaxLength, asciiExpectedExactMaxLength }; | ||
| yield return new object[] { utf8OriginalALettersAndOneEmoji, utf8ExpectedJustALetters }; | ||
| } | ||
|
|
||
| // Returns pairs as expected by Latin1 | ||
|
|
@@ -425,15 +437,15 @@ public static IEnumerable<object[]> Latin1Comment_Data() | |
| { | ||
| // In Latin1, all characters are exactly 1 byte | ||
|
|
||
| string latin1ExpectedALettersAndOneOUmlaut = new string('a', ushort.MaxValue - 1) + LowerCaseOUmlautChar; | ||
| string latin1OriginalALettersAndTwoOUmlauts = latin1ExpectedALettersAndOneOUmlaut + LowerCaseOUmlautChar; | ||
| string latin1ExpectedALettersAndOneOUmlaut = ALettersUShortMaxValueMinusOne + Utf8LowerCaseOUmlautChar; | ||
| string latin1OriginalALettersAndTwoOUmlauts = latin1ExpectedALettersAndOneOUmlaut + Utf8LowerCaseOUmlautChar; | ||
|
|
||
| yield return new object[] { latin1OriginalALettersAndTwoOUmlauts, latin1ExpectedALettersAndOneOUmlaut }; | ||
|
|
||
| foreach (object[] e in SharedComment_Data()) | ||
| { | ||
| yield return e; | ||
| } | ||
|
|
||
| yield return new object[] { latin1OriginalALettersAndTwoOUmlauts, latin1ExpectedALettersAndOneOUmlaut }; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.