Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[mono.git] / mcs / class / corlib / Test / System.Resources / ResourceReaderTest.cs
1 //
2 // MonoTests.System.Resources.ResourceReaderTest.cs
3 //
4 // Author:
5 //   Nick Drochak (ndrochak@gol.com)
6 //
7 // (C) 2001 Nick Drochak II
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Reflection;
15 using System.Resources;
16
17 using NUnit.Framework;
18
19 namespace MonoTests.System.Resources
20 {
21         [TestFixture]
22         public class ResourceReaderTest
23         {
24                 internal static string m_ResourceFile;
25                 private static string m_BadResourceFile;
26                 private string _tempResourceFile;
27
28                 [TestFixtureSetUp]
29                 public void FixtureSetUp ()
30                 {
31                         string base_path = Path.Combine (Directory.GetCurrentDirectory (), Path.Combine ("Test", "resources"));
32                         m_ResourceFile = Path.Combine (base_path, "MyResources.resources");
33                         m_BadResourceFile = Path.Combine (base_path, "Empty.resources");
34                 }
35
36                 [SetUp]
37                 public void SetUp ()
38                 {
39                         _tempResourceFile = Path.GetTempFileName ();
40                 }
41
42                 [TearDown]
43                 public void TearDown ()
44                 {
45                         File.Delete (_tempResourceFile);
46                 }
47
48                 [Test]
49                 public void ConstructorString_Path_Null ()
50                 {
51                         try {
52                                 new ResourceReader ((string) null);
53                                 Assert.Fail ("#1");
54                         } catch (ArgumentNullException ex) {
55                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
56                                 Assert.IsNull (ex.InnerException, "#3");
57                                 Assert.IsNotNull (ex.Message, "#4");
58                                 Assert.IsNotNull (ex.ParamName, "#5");
59                                 Assert.AreEqual ("path", ex.ParamName, "#6");
60                         }
61                 }
62
63                 [Test]
64                 public void ConstructorString_Path_Empty ()
65                 {
66                         try {
67                                 new ResourceReader (String.Empty);
68                                 Assert.Fail ("#1");
69                         } catch (ArgumentException ex) {
70                                 // Empty path name is not legal
71                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
72                                 Assert.IsNull (ex.InnerException, "#3");
73                                 Assert.IsNotNull (ex.Message, "#4");
74                                 Assert.IsNull (ex.ParamName, "#5");
75                         }
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (FileNotFoundException))]
80                 public void ConstructorString_NotFound ()
81                 {
82                         // use a file name that is *very* unlikely to exsist
83                         new ResourceReader ("j38f8axvnn9h38hfa9nxn93f8hav4zvag87vvah32o");
84                 }
85
86                 [Test]
87                 [Category ("MobileNotWorking")]
88                 [ExpectedException (typeof (BadImageFormatException))]
89                 public void ConstructorString_Bad ()
90                 {
91                         Assert.IsTrue (File.Exists (m_BadResourceFile));
92                         new ResourceReader (m_BadResourceFile);
93                 }
94
95                 [Test]
96                 [Category ("MobileNotWorking")]
97                 public void ConstructorString ()
98                 {
99                         if (!File.Exists (m_ResourceFile)) {
100                                 Assert.Fail ("Resource file is not where it should be:" + Path.Combine (Directory.GetCurrentDirectory (), m_ResourceFile));
101                         }
102                         ResourceReader r = new ResourceReader (m_ResourceFile);
103                         Assert.IsNotNull (r, "ResourceReader");
104                         r.Close ();
105                 }
106
107                 [Test]
108                 public void ConstructorStream_Null ()
109                 {
110                         try {
111                                 new ResourceReader ((Stream) null);
112                                 Assert.Fail ("#1");
113                         } catch (ArgumentNullException ex) {
114                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
115                                 Assert.IsNull (ex.InnerException, "#3");
116                                 Assert.IsNotNull (ex.Message, "#4");
117                                 Assert.IsNotNull (ex.ParamName, "#5");
118                                 Assert.AreEqual ("stream", ex.ParamName, "#6");
119                         }
120                 }
121
122                 [Test]
123                 [Category ("MobileNotWorking")]
124                 public void ConstructorStream_Closed ()
125                 {
126                         Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
127                         stream.Close ();
128
129                         try {
130                                 new ResourceReader (stream);
131                                 Assert.Fail ("#1");
132                         } catch (ArgumentException ex) {
133                                 // Stream was not readable
134                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
135                                 Assert.IsNull (ex.InnerException, "#3");
136                                 Assert.IsNotNull (ex.Message, "#4");
137                                 Assert.IsNull (ex.ParamName, "#5");
138                         }
139                 }
140
141                 [Test]
142                 [Category ("MobileNotWorking")]
143                 public void Stream ()
144                 {
145                         Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
146                         ResourceReader r = new ResourceReader (stream);
147                         Assert.IsNotNull (r, "ResourceReader");
148                         r.Close ();
149                 }
150
151                 [Test]
152                 [Category ("MobileNotWorking")]
153                 public void Close ()
154                 {
155                         Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
156                         ResourceReader r = new ResourceReader (stream);
157                         r.Close ();
158
159                         stream = new FileStream (m_ResourceFile, FileMode.Open);
160                         Assert.IsNotNull (stream, "FileStream");
161                         stream.Close ();
162                 }
163
164                 [Test]
165                 [Category ("MobileNotWorking")]
166                 public void Enumerator ()
167                 {
168                         Stream stream = new FileStream (m_ResourceFile, FileMode.Open);
169                         ResourceReader reader = new ResourceReader (stream);
170
171                         IDictionaryEnumerator en = reader.GetEnumerator ();
172                         // Goes through the enumerator, printing out the key and value pairs.
173                         while (en.MoveNext ()) {
174                                 DictionaryEntry de = (DictionaryEntry) en.Current;
175                                 Assert.IsTrue (String.Empty != (string) de.Key, "Current.Key should not be empty");
176                                 Assert.IsTrue (String.Empty != (string) de.Value, "Current.Value should not be empty");
177                                 Assert.IsTrue (String.Empty != (string) en.Key, "Entry.Key should not be empty");
178                                 Assert.IsTrue (String.Empty != (string) en.Value, "Entry.Value should not be empty");
179                         }
180                         reader.Close ();
181                 }
182
183                 [Test] // bug #81757
184                 public void ReadNullResource ()
185                 {
186                         MemoryStream stream = new MemoryStream ();
187                         object value = null;
188                         ResourceWriter rw = new ResourceWriter (stream);
189                         rw.AddResource ("NullTest", value);
190                         rw.Generate ();
191                         stream.Position = 0;
192
193                         using (ResourceReader rr = new ResourceReader (stream)) {
194                                 int entryCount = 0;
195                                 foreach (DictionaryEntry de in rr) {
196                                         Assert.AreEqual ("NullTest", de.Key, "#1");
197                                         Assert.IsNull (de.Value, "#2");
198                                         Assert.AreEqual (0, entryCount, "#3");
199                                         entryCount++;
200                                 }
201                                 Assert.AreEqual (1, entryCount, "#4");
202                         }
203                 }
204
205                 [Test] // bug #79976
206                 public void ByteArray ()
207                 {
208                         byte [] content = new byte [] { 1, 2, 3, 4, 5, 6 };
209
210                         Stream stream = null;
211
212                         // we currently do not support writing v2 resource files
213                         stream = new MemoryStream ();
214                         stream.Write (byte_resource_v2, 0, byte_resource_v2.Length);
215                         stream.Position = 0;
216
217                         using (stream) {
218                                 int entryCount = 0;
219                                 using (IResourceReader rr = new ResourceReader (stream)) {
220                                         foreach (DictionaryEntry de in rr) {
221                                                 Assert.AreEqual ("byteArrayTest", de.Key, "#1");
222                                                 Assert.AreEqual (content, de.Value, "#2");
223                                                 entryCount++;
224                                         }
225                                 }
226                                 Assert.AreEqual (1, entryCount, "#3");
227                         }
228                 }
229
230                 [Test]
231                 [Category ("MobileNotWorking")]
232                 public void GetResourceDataNullName ()
233                 {
234                         ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
235                         string type;
236                         byte [] bytes;
237
238                         try {
239                                 r.GetResourceData (null, out type, out bytes);
240                                 Assert.Fail ("#1");
241                         } catch (ArgumentNullException ex) {
242                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
243                                 Assert.IsNull (ex.InnerException, "#3");
244                                 Assert.IsNotNull (ex.Message, "#4");
245                                 Assert.IsNotNull (ex.ParamName, "#5");
246                                 Assert.AreEqual ("resourceName", ex.ParamName, "#6");
247                         } finally {
248                                 r.Close ();
249                         }
250                 }
251
252                 [Test]
253                 [Category ("MobileNotWorking")]
254                 public void GetResourceData ()
255                 {
256                         byte [] t1 = new byte [] {0x16, 0x00, 0x00, 0x00, 0x76, 0x65, 0x72, 0x69, 0x74, 0x61, 0x73, 0x20, 0x76, 0x6F, 0x73, 0x20, 0x6C, 0x69, 0x62, 0x65, 0x72, 0x61, 0x62, 0x69, 0x74, 0x0A};
257                         byte [] t2 = new byte [] {0x0A, 0x73, 0x6F, 0x6D, 0x65, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67};
258                         byte [] t3 = new byte [] {0x0E, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x72, 0x64, 0x65, 0x6E, 0x66, 0x72, 0x65, 0x75, 0x64, 0x65, 0x0A};
259
260                         ResourceReader r = new ResourceReader ("Test/resources/StreamTest.resources");
261                         Hashtable items = new Hashtable ();
262                         foreach (DictionaryEntry de in r) {
263                                 string type;
264                                 byte [] bytes;
265                                 r.GetResourceData ((string) de.Key, out type, out bytes);
266                                 items [de.Key] = new DictionaryEntry (type, bytes);
267                         }
268
269                         DictionaryEntry p = (DictionaryEntry) items ["test"];
270                         Assert.AreEqual ("ResourceTypeCode.Stream", p.Key as string, "#1-1");
271                         Assert.AreEqual (t1, p.Value as byte [], "#1-2");
272
273                         p = (DictionaryEntry) items ["test2"];
274                         Assert.AreEqual ("ResourceTypeCode.String", p.Key as string, "#2-1");
275                         Assert.AreEqual (t2, p.Value as byte [], "#2-2");
276
277                         p = (DictionaryEntry) items ["test3"];
278                         Assert.AreEqual ("ResourceTypeCode.ByteArray", p.Key as string, "#3-1");
279                         Assert.AreEqual (t3, p.Value as byte [], "#3-2");
280
281                         r.Close ();
282                 }
283
284                 [Test]
285                 [Category ("MobileNotWorking")]
286                 public void GetResourceData2 ()
287                 {
288                         byte [] expected = new byte [] {
289                                 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
290                                 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
291                                 0x00, 0x0C, 0x02, 0x00, 0x00, 0x00, 0x51, 0x53,
292                                 0x79, 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72,
293                                 0x61, 0x77, 0x69, 0x6E, 0x67, 0x2C, 0x20, 0x56,
294                                 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x32,
295                                 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x30, 0x2C, 0x20,
296                                 0x43, 0x75, 0x6C, 0x74, 0x75, 0x72, 0x65, 0x3D,
297                                 0x6E, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6C, 0x2C,
298                                 0x20, 0x50, 0x75, 0x62, 0x6C, 0x69, 0x63, 0x4B,
299                                 0x65, 0x79, 0x54, 0x6F, 0x6B, 0x65, 0x6E, 0x3D,
300                                 0x62, 0x30, 0x33, 0x66, 0x35, 0x66, 0x37, 0x66,
301                                 0x31, 0x31, 0x64, 0x35, 0x30, 0x61, 0x33, 0x61,
302                                 0x05, 0x01, 0x00, 0x00, 0x00, 0x13, 0x53, 0x79,
303                                 0x73, 0x74, 0x65, 0x6D, 0x2E, 0x44, 0x72, 0x61,
304                                 0x77, 0x69, 0x6E, 0x67, 0x2E, 0x53, 0x69, 0x7A,
305                                 0x65, 0x02, 0x00, 0x00, 0x00, 0x05, 0x77, 0x69,
306                                 0x64, 0x74, 0x68, 0x06, 0x68, 0x65, 0x69, 0x67,
307                                 0x68, 0x74, 0x00, 0x00, 0x08, 0x08, 0x02, 0x00,
308                                 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00,
309                                 0x00, 0x00, 0x0B};
310                         ResourceReader r = new ResourceReader ("Test/resources/bug81759.resources");
311                         string type;
312                         byte [] bytes;
313                         r.GetResourceData ("imageList.ImageSize", out type, out bytes);
314                         // Note that const should not be used here.
315                         Assert.AreEqual ("System.Drawing.Size, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", type, "#1");
316                         Assert.AreEqual (expected, bytes, "#2");
317                         r.Close ();
318                 }
319
320                 // we currently do not support writing v2 resource files
321                 private static readonly byte [] byte_resource_v2 = new byte [] {
322                         0xce, 0xca, 0xef, 0xbe, 0x01, 0x00, 0x00, 0x00, 0x91, 0x00, 0x00,
323                         0x00, 0x6c, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65,
324                         0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x65, 0x73,
325                         0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x65, 0x61, 0x64, 0x65, 0x72,
326                         0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72, 0x6c, 0x69, 0x62, 0x2c,
327                         0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x32, 0x2e,
328                         0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x43, 0x75, 0x6c, 0x74,
329                         0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75, 0x74, 0x72, 0x61, 0x6c,
330                         0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x4b, 0x65, 0x79,
331                         0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62, 0x37, 0x37, 0x61, 0x35,
332                         0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34, 0x65, 0x30, 0x38, 0x39,
333                         0x23, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x65, 0x73,
334                         0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x2e, 0x52, 0x75, 0x6e, 0x74,
335                         0x69, 0x6d, 0x65, 0x52, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65,
336                         0x53, 0x65, 0x74, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
337                         0x00, 0x00, 0x00, 0x00, 0x50, 0x41, 0x44, 0x50, 0x41, 0x44, 0x50,
338                         0x80, 0x88, 0x5a, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xdb, 0x00, 0x00,
339                         0x00, 0x1a, 0x62, 0x00, 0x79, 0x00, 0x74, 0x00, 0x65, 0x00, 0x41,
340                         0x00, 0x72, 0x00, 0x72, 0x00, 0x61, 0x00, 0x79, 0x00, 0x54, 0x00,
341                         0x65, 0x00, 0x73, 0x00, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
342                         0x06, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
343         }
344 }