From c58f107af3fb64bce4c338f6eb29d742f9d5a126 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Mon, 7 Oct 2024 13:28:00 +1300 Subject: [PATCH 1/4] Use simple using statements (IDE0063) --- .../CFSStreamExtensionsTest.cs | 46 ++- .../OLEPropertiesExtensionsTest.cs | 296 ++++++++---------- 2 files changed, 158 insertions(+), 184 deletions(-) diff --git a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs index eb596b72..6bcfb683 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs @@ -84,45 +84,37 @@ public void Test_AS_IOSTREAM_MULTISECTOR_WRITE() using (CompoundFile cf = new CompoundFile()) { - using (Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream()) - { - using (BinaryWriter bw = new BinaryWriter(s)) - { - bw.Write(data); - cf.SaveAs("$ACFFile2.cfs"); - cf.Close(); - } - } + using Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream(); + using BinaryWriter bw = new BinaryWriter(s); + bw.Write(data); + cf.SaveAs("$ACFFile2.cfs"); + cf.Close(); } // Works using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs")) { - using (BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream())) - { - byte[] readData = new byte[data.Length]; - int readCount = br.Read(readData, 0, readData.Length); - Assert.AreEqual(readData.Length, readCount); - CollectionAssert.AreEqual(data, readData); - cf.Close(); - } + using BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream()); + byte[] readData = new byte[data.Length]; + int readCount = br.Read(readData, 0, readData.Length); + Assert.AreEqual(readData.Length, readCount); + CollectionAssert.AreEqual(data, readData); + cf.Close(); } // Won't work until #88 is fixed. using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs")) { - using (Stream readStream = cf.RootStorage.GetStream("ANewStream").AsIOStream()) + using Stream readStream = cf.RootStorage.GetStream("ANewStream").AsIOStream(); + byte[] readData; + using (MemoryStream ms = new MemoryStream()) { - byte[] readData; - using (MemoryStream ms = new MemoryStream()) - { - readStream.CopyTo(ms); - readData = ms.ToArray(); - } - - CollectionAssert.AreEqual(data, readData); - cf.Close(); + readStream.CopyTo(ms); + readData = ms.ToArray(); } + + CollectionAssert.AreEqual(data, readData); + cf.Close(); } } } diff --git a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs index 3c901b93..add59820 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs @@ -49,45 +49,41 @@ public OLEPropertiesExtensionsTest() [TestMethod] public void Test_SUMMARY_INFO_READ() { - using (CompoundFile cf = new CompoundFile("_Test.ppt")) - { - var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + using CompoundFile cf = new CompoundFile("_Test.ppt"); + var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); - foreach (OLEProperty p in co.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + foreach (OLEProperty p in co.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - Assert.IsNotNull(co.Properties); + Assert.IsNotNull(co.Properties); - cf.Close(); - } + cf.Close(); } [TestMethod] public void Test_DOCUMENT_SUMMARY_INFO_READ() { - using (CompoundFile cf = new CompoundFile("_Test.ppt")) - { - var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + using CompoundFile cf = new CompoundFile("_Test.ppt"); + var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - foreach (OLEProperty p in co.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + foreach (OLEProperty p in co.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - Assert.IsNotNull(co.Properties); + Assert.IsNotNull(co.Properties); - cf.Close(); - } + cf.Close(); } [TestMethod] @@ -95,21 +91,19 @@ public void Test_DOCUMENT_SUMMARY_INFO_ROUND_TRIP() { File.Delete("test1.cfs"); - using (CompoundFile cf = new CompoundFile("_Test.ppt")) + using CompoundFile cf = new CompoundFile("_Test.ppt"); + var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + using (CompoundFile cf2 = new CompoundFile()) { - var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - using (CompoundFile cf2 = new CompoundFile()) - { - cf2.RootStorage.AddStream("\u0005DocumentSummaryInformation"); - - co.Save(cf2.RootStorage.GetStream("\u0005DocumentSummaryInformation")); + cf2.RootStorage.AddStream("\u0005DocumentSummaryInformation"); - cf2.SaveAs("test1.cfs"); - cf2.Close(); - } + co.Save(cf2.RootStorage.GetStream("\u0005DocumentSummaryInformation")); - cf.Close(); + cf2.SaveAs("test1.cfs"); + cf2.Close(); } + + cf.Close(); } // Modify some document summary information properties, save to a file, and then validate the expected results @@ -164,98 +158,92 @@ public void Test_DOCUMENT_SUMMARY_INFO_MODIFY() [TestMethod] public void Test_SUMMARY_INFO_READ_UTF8_ISSUE_33() { - using (CompoundFile cf = new CompoundFile("wstr_presets.doc")) - { - var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + using CompoundFile cf = new CompoundFile("wstr_presets.doc"); + var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); - foreach (OLEProperty p in co.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + foreach (OLEProperty p in co.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - Assert.IsNotNull(co.Properties); + Assert.IsNotNull(co.Properties); - var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - foreach (OLEProperty p in co2.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } - - Assert.IsNotNull(co2.Properties); + foreach (OLEProperty p in co2.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); } + + Assert.IsNotNull(co2.Properties); } [TestMethod] public void Test_SUMMARY_INFO_READ_UTF8_ISSUE_34() { - using (CompoundFile cf = new CompoundFile("2custom.doc")) + using CompoundFile cf = new CompoundFile("2custom.doc"); + var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + + foreach (OLEProperty p in co.Properties) { - var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - foreach (OLEProperty p in co.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + Assert.IsNotNull(co.Properties); - Assert.IsNotNull(co.Properties); + var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co2.Properties); + foreach (OLEProperty p in co2.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - Assert.IsNotNull(co2.Properties); - foreach (OLEProperty p in co2.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } - - Assert.IsNotNull(co2.UserDefinedProperties.Properties); - foreach (OLEProperty p in co2.UserDefinedProperties.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + Assert.IsNotNull(co2.UserDefinedProperties.Properties); + foreach (OLEProperty p in co2.UserDefinedProperties.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); } } [TestMethod] public void Test_SUMMARY_INFO_READ_LPWSTRING() { - using (CompoundFile cf = new CompoundFile("english.presets.doc")) - { - var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + using CompoundFile cf = new CompoundFile("english.presets.doc"); + var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); - foreach (OLEProperty p in co.Properties) - { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); - } + foreach (OLEProperty p in co.Properties) + { + Debug.Write(p.PropertyName); + Debug.Write(" - "); + Debug.Write(p.VTType); + Debug.Write(" - "); + Debug.WriteLine(p.Value); + } - Assert.IsNotNull(co.Properties); + Assert.IsNotNull(co.Properties); - cf.Close(); - } + cf.Close(); } // Test that we can modify an LPWSTR property, and the value is null terminated as required @@ -303,39 +291,37 @@ public void Test_SUMMARY_INFO_MODIFY_LPWSTRING() [TestMethod] public void Test_Read_Unicode_User_Properties_Dictionary() { - using (CompoundFile cf = new CompoundFile("winUnicodeDictionary.doc")) - { - var dsiStream = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation"); - var co = dsiStream.AsOLEPropertiesContainer(); - var userProps = co.UserDefinedProperties; - - // CodePage should be CP_WINUNICODE (1200) - Assert.AreEqual(1200, userProps.Context.CodePage); - - // There should be 5 property names present, and 6 properties (the properties include the code page) - Assert.AreEqual(5, userProps.PropertyNames.Count); - Assert.AreEqual(6, userProps.Properties.Count()); - - // Check for expected names and values - var propArray = userProps.Properties.ToArray(); - - // CodePage prop - Assert.AreEqual(1u, propArray[0].PropertyIdentifier); - Assert.AreEqual("0x00000001", propArray[0].PropertyName); - Assert.AreEqual((short)1200, propArray[0].Value); - - // String properties - Assert.AreEqual("A", propArray[1].PropertyName); - Assert.AreEqual("", propArray[1].Value); - Assert.AreEqual("AB", propArray[2].PropertyName); - Assert.AreEqual("X", propArray[2].Value); - Assert.AreEqual("ABC", propArray[3].PropertyName); - Assert.AreEqual("XY", propArray[3].Value); - Assert.AreEqual("ABCD", propArray[4].PropertyName); - Assert.AreEqual("XYZ", propArray[4].Value); - Assert.AreEqual("ABCDE", propArray[5].PropertyName); - Assert.AreEqual("XYZ!", propArray[5].Value); - } + using CompoundFile cf = new CompoundFile("winUnicodeDictionary.doc"); + var dsiStream = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation"); + var co = dsiStream.AsOLEPropertiesContainer(); + var userProps = co.UserDefinedProperties; + + // CodePage should be CP_WINUNICODE (1200) + Assert.AreEqual(1200, userProps.Context.CodePage); + + // There should be 5 property names present, and 6 properties (the properties include the code page) + Assert.AreEqual(5, userProps.PropertyNames.Count); + Assert.AreEqual(6, userProps.Properties.Count()); + + // Check for expected names and values + var propArray = userProps.Properties.ToArray(); + + // CodePage prop + Assert.AreEqual(1u, propArray[0].PropertyIdentifier); + Assert.AreEqual("0x00000001", propArray[0].PropertyName); + Assert.AreEqual((short)1200, propArray[0].Value); + + // String properties + Assert.AreEqual("A", propArray[1].PropertyName); + Assert.AreEqual("", propArray[1].Value); + Assert.AreEqual("AB", propArray[2].PropertyName); + Assert.AreEqual("X", propArray[2].Value); + Assert.AreEqual("ABC", propArray[3].PropertyName); + Assert.AreEqual("XY", propArray[3].Value); + Assert.AreEqual("ABCD", propArray[4].PropertyName); + Assert.AreEqual("XYZ", propArray[4].Value); + Assert.AreEqual("ABCDE", propArray[5].PropertyName); + Assert.AreEqual("XYZ!", propArray[5].Value); } // Test that we can add user properties of various types and then read them back @@ -419,32 +405,28 @@ public void Test_DOCUMENT_SUMMARY_INFO_ADD_CUSTOM() [TestMethod] public void Test_SUMMARY_INFO_READ_LPWSTRING_VECTOR() { - using (CompoundFile cf = new CompoundFile("SampleWorkBook_bug98.xls")) - { - var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + using CompoundFile cf = new CompoundFile("SampleWorkBook_bug98.xls"); + var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - var docPartsProperty = co.Properties.FirstOrDefault(property => property.PropertyIdentifier == 13); //13 == PIDDSI_DOCPARTS + var docPartsProperty = co.Properties.FirstOrDefault(property => property.PropertyIdentifier == 13); //13 == PIDDSI_DOCPARTS - Assert.IsNotNull(docPartsProperty); + Assert.IsNotNull(docPartsProperty); - var docPartsValues = docPartsProperty.Value as IList; - Assert.AreEqual(3, docPartsValues.Count); - Assert.AreEqual("Sheet1", docPartsValues[0]); - Assert.AreEqual("Sheet2", docPartsValues[1]); - Assert.AreEqual("Sheet3", docPartsValues[2]); - } + var docPartsValues = docPartsProperty.Value as IList; + Assert.AreEqual(3, docPartsValues.Count); + Assert.AreEqual("Sheet1", docPartsValues[0]); + Assert.AreEqual("Sheet2", docPartsValues[1]); + Assert.AreEqual("Sheet3", docPartsValues[2]); } [TestMethod] public void Test_CLSID_PROPERTY() { var guid = new Guid("15891a95-bf6e-4409-b7d0-3a31c391fa31"); - using (CompoundFile cf = new CompoundFile("CLSIDPropertyTest.file")) - { - var co = cf.RootStorage.GetStream("\u0005C3teagxwOttdbfkuIaamtae3Ie").AsOLEPropertiesContainer(); - var clsidProp = co.Properties.First(x => x.PropertyName == "DocumentID"); - Assert.AreEqual(guid, clsidProp.Value); - } + using CompoundFile cf = new CompoundFile("CLSIDPropertyTest.file"); + var co = cf.RootStorage.GetStream("\u0005C3teagxwOttdbfkuIaamtae3Ie").AsOLEPropertiesContainer(); + var clsidProp = co.Properties.First(x => x.PropertyName == "DocumentID"); + Assert.AreEqual(guid, clsidProp.Value); } // The test file 'report.xls' contains a DocumentSummaryInfo section, but no user defined properties. From a9485ef71da8e5ec02698aefb8dca27bb676faff Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Mon, 7 Oct 2024 13:33:10 +1300 Subject: [PATCH 2/4] Dispose objects before they lose scope (CA2000) --- .../CFSStreamExtensionsTest.cs | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs index 6bcfb683..098dd1be 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/CFSStreamExtensionsTest.cs @@ -46,10 +46,9 @@ public CFSStreamExtensionsTest() [TestMethod] public void Test_AS_IOSTREAM_READ() { - CompoundFile cf = new CompoundFile("MultipleStorage.cfs"); - - Stream s = cf.RootStorage.GetStorage("MyStorage").GetStream("MyStream").AsIOStream(); - BinaryReader br = new BinaryReader(s); + using CompoundFile cf = new("MultipleStorage.cfs"); + using Stream s = cf.RootStorage.GetStorage("MyStorage").GetStream("MyStream").AsIOStream(); + using BinaryReader br = new(s); byte[] result = br.ReadBytes(32); CollectionAssert.AreEqual(Helpers.GetBuffer(32, 1), result); } @@ -59,18 +58,21 @@ public void Test_AS_IOSTREAM_WRITE() { const string cmp = "Hello World of BinaryWriter !"; - CompoundFile cf = new CompoundFile(); - Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream(); - BinaryWriter bw = new BinaryWriter(s); - bw.Write(cmp); - cf.SaveAs("$ACFFile.cfs"); - cf.Close(); + using (CompoundFile cf = new()) + { + using Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream(); + using BinaryWriter bw = new(s); + bw.Write(cmp); + cf.SaveAs("$ACFFile.cfs"); + } - cf = new CompoundFile("$ACFFile.cfs"); - BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream()); - string st = br.ReadString(); - Assert.AreEqual(cmp, st); - cf.Close(); + using (CompoundFile cf = new("$ACFFile.cfs")) + { + using Stream s = cf.RootStorage.GetStream("ANewStream").AsIOStream(); + using BinaryReader br = new(s); + string st = br.ReadString(); + Assert.AreEqual(cmp, st); + } } [TestMethod] @@ -82,39 +84,32 @@ public void Test_AS_IOSTREAM_MULTISECTOR_WRITE() data[i] = (byte)(i % 255); } - using (CompoundFile cf = new CompoundFile()) + using (CompoundFile cf = new()) { using Stream s = cf.RootStorage.AddStream("ANewStream").AsIOStream(); - using BinaryWriter bw = new BinaryWriter(s); + using BinaryWriter bw = new(s); bw.Write(data); cf.SaveAs("$ACFFile2.cfs"); - cf.Close(); } // Works - using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs")) + using (CompoundFile cf = new("$ACFFile2.cfs")) { - using BinaryReader br = new BinaryReader(cf.RootStorage.GetStream("ANewStream").AsIOStream()); + using Stream s = cf.RootStorage.GetStream("ANewStream").AsIOStream(); + using BinaryReader br = new(s); byte[] readData = new byte[data.Length]; int readCount = br.Read(readData, 0, readData.Length); Assert.AreEqual(readData.Length, readCount); CollectionAssert.AreEqual(data, readData); - cf.Close(); } // Won't work until #88 is fixed. - using (CompoundFile cf = new CompoundFile("$ACFFile2.cfs")) + using (CompoundFile cf = new("$ACFFile2.cfs")) { using Stream readStream = cf.RootStorage.GetStream("ANewStream").AsIOStream(); - byte[] readData; - using (MemoryStream ms = new MemoryStream()) - { - readStream.CopyTo(ms); - readData = ms.ToArray(); - } - - CollectionAssert.AreEqual(data, readData); - cf.Close(); + using MemoryStream ms = new(); + readStream.CopyTo(ms); + CollectionAssert.AreEqual(data, ms.ToArray()); } } } From 792ebf8424bc07b581ed33c8c284ac22de175714 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Mon, 7 Oct 2024 13:39:55 +1300 Subject: [PATCH 3/4] Fix and simplify extensions tests --- .../OLEProperties/OLEProperty.cs | 4 +- .../Test/OpenMcdf.Extensions.Test/Helpers.cs | 23 ---- .../OLEPropertiesExtensionsTest.cs | 116 +++++------------- 3 files changed, 36 insertions(+), 107 deletions(-) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs index 8b2790da..9bfa0921 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEProperty.cs @@ -55,7 +55,7 @@ public object Value public override bool Equals(object obj) { - if (obj is not OLEProperty other) + if (obj is not OLEProperty other) return false; return other.PropertyIdentifier == PropertyIdentifier; @@ -65,5 +65,7 @@ public override int GetHashCode() { return (int)PropertyIdentifier; } + + public override string ToString() => $"{PropertyName} - {VTType} - {Value}"; } } \ No newline at end of file diff --git a/sources/Test/OpenMcdf.Extensions.Test/Helpers.cs b/sources/Test/OpenMcdf.Extensions.Test/Helpers.cs index 9581aef7..c622777c 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/Helpers.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/Helpers.cs @@ -22,28 +22,5 @@ public static byte[] GetBuffer(int count, byte c) return b; } - - public static bool CompareBuffer(byte[] b, byte[] p) - { - if (b == null && p == null) - throw new Exception("Null buffers"); - - if (b == null && p != null) - return false; - - if (b != null && p == null) - return false; - - if (b.Length != p.Length) - return false; - - for (int i = 0; i < b.Length; i++) - { - if (b[i] != p[i]) - return false; - } - - return true; - } } } diff --git a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs index add59820..ae994ba5 100644 --- a/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs +++ b/sources/Test/OpenMcdf.Extensions.Test/OLEPropertiesExtensionsTest.cs @@ -52,18 +52,12 @@ public void Test_SUMMARY_INFO_READ() using CompoundFile cf = new CompoundFile("_Test.ppt"); var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co.Properties); + foreach (OLEProperty p in co.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - - Assert.IsNotNull(co.Properties); - - cf.Close(); } [TestMethod] @@ -72,18 +66,12 @@ public void Test_DOCUMENT_SUMMARY_INFO_READ() using CompoundFile cf = new CompoundFile("_Test.ppt"); var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co.Properties); + foreach (OLEProperty p in co.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - - Assert.IsNotNull(co.Properties); - - cf.Close(); } [TestMethod] @@ -93,17 +81,13 @@ public void Test_DOCUMENT_SUMMARY_INFO_ROUND_TRIP() using CompoundFile cf = new CompoundFile("_Test.ppt"); var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); - using (CompoundFile cf2 = new CompoundFile()) - { - cf2.RootStorage.AddStream("\u0005DocumentSummaryInformation"); - co.Save(cf2.RootStorage.GetStream("\u0005DocumentSummaryInformation")); + using CompoundFile cf2 = new CompoundFile(); + cf2.RootStorage.AddStream("\u0005DocumentSummaryInformation"); - cf2.SaveAs("test1.cfs"); - cf2.Close(); - } + co.Save(cf2.RootStorage.GetStream("\u0005DocumentSummaryInformation")); - cf.Close(); + cf2.SaveAs("test1.cfs"); } // Modify some document summary information properties, save to a file, and then validate the expected results @@ -137,7 +121,6 @@ public void Test_DOCUMENT_SUMMARY_INFO_MODIFY() co.Save(dsiStream); cf.SaveAs(@"test_modify_summary.ppt"); - cf.Close(); } using (CompoundFile cf = new CompoundFile("test_modify_summary.ppt")) @@ -161,29 +144,21 @@ public void Test_SUMMARY_INFO_READ_UTF8_ISSUE_33() using CompoundFile cf = new CompoundFile("wstr_presets.doc"); var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co.Properties); + foreach (OLEProperty p in co.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - Assert.IsNotNull(co.Properties); - var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co2.Properties); + foreach (OLEProperty p in co2.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - - Assert.IsNotNull(co2.Properties); } [TestMethod] @@ -192,37 +167,24 @@ public void Test_SUMMARY_INFO_READ_UTF8_ISSUE_34() using CompoundFile cf = new CompoundFile("2custom.doc"); var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co.Properties); foreach (OLEProperty p in co.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - Assert.IsNotNull(co.Properties); - var co2 = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); Assert.IsNotNull(co2.Properties); foreach (OLEProperty p in co2.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } Assert.IsNotNull(co2.UserDefinedProperties.Properties); foreach (OLEProperty p in co2.UserDefinedProperties.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } } @@ -232,17 +194,13 @@ public void Test_SUMMARY_INFO_READ_LPWSTRING() using CompoundFile cf = new CompoundFile("english.presets.doc"); var co = cf.RootStorage.GetStream("\u0005SummaryInformation").AsOLEPropertiesContainer(); + Assert.IsNotNull(co.Properties); + foreach (OLEProperty p in co.Properties) { - Debug.Write(p.PropertyName); - Debug.Write(" - "); - Debug.Write(p.VTType); - Debug.Write(" - "); - Debug.WriteLine(p.Value); + Debug.WriteLine(p); } - Assert.IsNotNull(co.Properties); - cf.Close(); } @@ -374,7 +332,7 @@ public void Test_DOCUMENT_SUMMARY_INFO_ADD_CUSTOM() { var co = cf.RootStorage.GetStream("\u0005DocumentSummaryInformation").AsOLEPropertiesContainer(); var propArray = co.UserDefinedProperties.Properties.ToArray(); - Assert.AreEqual(propArray.Length, 6); + Assert.AreEqual(6, propArray.Length); // CodePage prop Assert.AreEqual(1u, propArray[0].PropertyIdentifier); @@ -488,13 +446,19 @@ public void Test_Retain_Dictionary_Property_In_AppSpecific_Streams() { File.Delete("Issue134RoundTrip.cfs"); + var expectedPropertyNames = new Dictionary() + { + [2] = "Document Number", + [3] = "Revision", + [4] = "Project Name" + }; + using (CompoundFile cf = new CompoundFile("Issue134.cfs")) { var testStream = cf.RootStorage.GetStream("Issue134"); var co = testStream.AsOLEPropertiesContainer(); - // Test initial contents are as expected - AssertExpectedProperties(co.PropertyNames); + CollectionAssert.AreEqual(expectedPropertyNames, co.PropertyNames); // Write test file co.Save(testStream); @@ -505,21 +469,7 @@ public void Test_Retain_Dictionary_Property_In_AppSpecific_Streams() using (CompoundFile cf = new CompoundFile("Issue134RoundTrip.cfs", CFSUpdateMode.ReadOnly, CFSConfiguration.Default)) { var co = cf.RootStorage.GetStream("Issue134").AsOLEPropertiesContainer(); - AssertExpectedProperties(co.PropertyNames); - } - - void AssertExpectedProperties(Dictionary actual) - { - Assert.IsNotNull(actual); - - var expected = new Dictionary() - { - [2] = "Document Number", - [3] = "Revision", - [4] = "Project Name" - }; - - CollectionAssert.AreEqual(expected, actual); + CollectionAssert.AreEqual(expectedPropertyNames, co.PropertyNames); } } } From 74489afc252378b9c098434715c7b1a3ef57c592 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Mon, 7 Oct 2024 13:48:42 +1300 Subject: [PATCH 4/4] Dispose objects before they lose scope (CA2000) --- .../OLEProperties/OLEPropertiesContainer.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs index c2fa0feb..a496c8b6 100644 --- a/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs +++ b/sources/OpenMcdf.Extensions/OLEProperties/OLEPropertiesContainer.cs @@ -84,7 +84,10 @@ internal OLEPropertiesContainer(CFStream cfStream) PropertySetStream pStream = new PropertySetStream(); this.cfStream = cfStream; - pStream.Read(new BinaryReader(new StreamDecorator(cfStream))); + + using StreamDecorator stream = new(cfStream); + using BinaryReader reader = new(stream); + pStream.Read(reader); ContainerType = pStream.FMTID0.ToString("B").ToUpperInvariant() switch { @@ -222,8 +225,8 @@ public void Save(CFStream cfStream) //throw new NotImplementedException("API Unstable - Work in progress - Milestone 2.3.0.0"); //properties.Sort((a, b) => a.PropertyIdentifier.CompareTo(b.PropertyIdentifier)); - Stream s = new StreamDecorator(cfStream); - BinaryWriter bw = new BinaryWriter(s); + using StreamDecorator s = new(cfStream); + using BinaryWriter bw = new BinaryWriter(s); Guid fmtId0 = FmtID0 ?? (ContainerType == ContainerType.SummaryInfo ? new Guid(WellKnownFMTID.FMTID_SummaryInformation) : new Guid(WellKnownFMTID.FMTID_DocSummaryInformation));