Skip to content

Commit d7a52b2

Browse files
authored
Add System.IO.Packaging test that checks with reflection that the ZipArchiveEntry general purpose bit flag unicode value is unset. (#88978)
1 parent 4fbd7c5 commit d7a52b2

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO.Compression;
5+
using System.Reflection;
6+
using Xunit;
7+
8+
namespace System.IO.Packaging.Tests;
9+
10+
public class ReflectionTests
11+
{
12+
[Fact]
13+
public void Verify_GeneralPurposeBitFlag_NotSetTo_Unicode()
14+
{
15+
using MemoryStream ms = new();
16+
17+
using (ZipPackage package = (ZipPackage)Package.Open(ms, FileMode.Create, FileAccess.Write))
18+
{
19+
Uri uri = PackUriHelper.CreatePartUri(new Uri("document.xml", UriKind.Relative));
20+
ZipPackagePart part = (ZipPackagePart)package.CreatePart(uri, Tests.Mime_MediaTypeNames_Text_Xml, CompressionOption.NotCompressed);
21+
using (Stream partStream = part.GetStream())
22+
{
23+
using StreamWriter sw = new(partStream);
24+
sw.Write(Tests.s_DocumentXml);
25+
}
26+
package.CreateRelationship(part.Uri, TargetMode.Internal, "http://packageRelType", "rId1234");
27+
}
28+
29+
ms.Position = 0;
30+
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read, leaveOpen: false))
31+
{
32+
foreach (ZipArchiveEntry entry in archive.Entries)
33+
{
34+
FieldInfo fieldInfo = typeof(ZipArchiveEntry).GetField("_generalPurposeBitFlag", BindingFlags.Instance | BindingFlags.NonPublic);
35+
object fieldObject = fieldInfo.GetValue(entry);
36+
ushort shortField = (ushort)fieldObject;
37+
Assert.Equal(0, shortField); // If it was UTF8, we would set the general purpose bit flag to 0x800 (UnicodeFileNameAndComment)
38+
CheckCharacters(entry.Name);
39+
CheckCharacters(entry.Comment); // Unavailable in .NET Framework
40+
}
41+
}
42+
43+
void CheckCharacters(string value)
44+
{
45+
for (int i = 0; i < value.Length; i++)
46+
{
47+
char c = value[i];
48+
Assert.True(c >= 32 && c <= 126, $"ZipArchiveEntry name character {c} requires UTF8");
49+
}
50+
}
51+
}
52+
53+
}

src/libraries/System.IO.Packaging/tests/System.IO.Packaging.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<ItemGroup>
66
<Compile Include="Tests.cs" />
77
</ItemGroup>
8+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETFramework'">
9+
<Compile Include="ReflectionTests.cs" />
10+
</ItemGroup>
811
<ItemGroup>
912
<PackageReference Include="System.IO.Packaging.TestData" Version="$(SystemIOPackagingTestDataVersion)" />
1013
<ProjectReference Include="..\src\System.IO.Packaging.csproj" />

src/libraries/System.IO.Packaging/tests/Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace System.IO.Packaging.Tests
1212
{
1313
public class Tests : FileCleanupTestBase
1414
{
15-
private const string Mime_MediaTypeNames_Text_Xml = "text/xml";
15+
internal const string Mime_MediaTypeNames_Text_Xml = "text/xml";
1616
private const string Mime_MediaTypeNames_Image_Jpeg = "image/jpeg"; // System.Net.Mime.MediaTypeNames.Image.Jpeg
17-
private const string s_DocumentXml = @"<Hello>Test</Hello>";
17+
internal const string s_DocumentXml = @"<Hello>Test</Hello>";
1818
private const string s_ResourceXml = @"<Resource>Test</Resource>";
1919

2020
private FileInfo GetTempFileInfoFromExistingFile(string existingFileName, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0)

0 commit comments

Comments
 (0)