|
| 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 | +} |
0 commit comments