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