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