Merge pull request #1142 from edbprx/master
[mono.git] / mcs / class / corlib / Test / System / BadImageFormatExceptionTest.cs
1 //
2 // BadImageFormatExceptionTest.cs - Unit tests for
3 //      System.BadImageFormatException
4 //
5 // Author:
6 //      Gert Driesen  <drieseng@users.sourceforge.net>
7 //
8 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System
35 {
36         [TestFixture]
37         public class BadImageFormatExceptionTest
38         {
39                 [Test]
40                 public void Constructor1 ()
41                 {
42                         BadImageFormatException bif = new BadImageFormatException ();
43
44 #if NET_2_0
45                         Assert.IsNotNull (bif.Data, "#1");
46 #endif
47                         Assert.IsNull (bif.FileName, "#2");
48                         Assert.IsNull (bif.InnerException, "#3");
49                         Assert.IsNotNull (bif.Message, "#4"); // Format of the executable (.exe) or library (.dll) is invalid
50                         Assert.IsNull (bif.FusionLog, "#5");
51                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName), "#6");
52                 }
53
54                 [Test]
55                 public void Constructor2 ()
56                 {
57                         BadImageFormatException bif = new BadImageFormatException ("message");
58
59 #if NET_2_0
60                         Assert.IsNotNull (bif.Data, "#1");
61 #endif
62                         Assert.IsNull (bif.FileName, "#2");
63                         Assert.IsNull (bif.InnerException, "#3");
64                         Assert.IsNotNull (bif.Message, "#4");
65                         Assert.AreEqual ("message", bif.Message, "#5");
66                         Assert.IsNull (bif.FusionLog, "#6");
67 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
68                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName + ": message"), "#7");
69 #else
70                         Assert.AreEqual (bif.GetType ().FullName + ": message",
71                                 bif.ToString (), "#7");
72 #endif // TARGET_JVM
73                 }
74
75                 [Test]
76                 public void Constructor2_Message_Empty ()
77                 {
78                         BadImageFormatException bif = new BadImageFormatException (string.Empty);
79
80 #if NET_2_0
81                         Assert.IsNotNull (bif.Data, "#1");
82 #endif
83                         Assert.IsNull (bif.FileName, "#2");
84                         Assert.IsNull (bif.InnerException, "#3");
85                         Assert.IsNotNull (bif.Message, "#4");
86                         Assert.AreEqual (string.Empty, bif.Message, "#5");
87                         Assert.IsNull (bif.FusionLog, "#6");
88 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
89                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": "), "#7");
90 #else
91                         Assert.AreEqual (bif.GetType ().FullName + ": ",
92                                 bif.ToString (), "#7");
93 #endif // TARGET_JVM
94                 }
95
96                 [Test]
97                 public void Constructor2_Message_Null ()
98                 {
99                         BadImageFormatException bif = new BadImageFormatException ((string) null);
100
101 #if NET_2_0
102                         Assert.IsNotNull (bif.Data, "#1");
103 #endif
104                         Assert.IsNull (bif.FileName, "#2");
105                         Assert.IsNull (bif.InnerException, "#3");
106 #if NET_2_0
107                         Assert.IsNotNull (bif.Message, "#4"); // Could not load file or assembly '' ...
108                         Assert.IsTrue (bif.Message.IndexOf ("''") != -1, "#5");
109 #else
110                         Assert.IsNotNull (bif.Message, "#4"); // Format of the executable (.exe) or library ...
111                         Assert.IsFalse (bif.Message.IndexOf ("''") != -1, "#5");
112 #endif
113                         Assert.IsNull (bif.FusionLog, "#5");
114                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName), "#6");
115 #if NET_2_0
116                         Assert.IsTrue (bif.ToString ().IndexOf ("''") != -1, "#7");
117 #else
118                         Assert.IsFalse (bif.ToString ().IndexOf ("''") != -1, "#7");
119 #endif
120                 }
121
122                 [Test]
123                 public void Constructor3 ()
124                 {
125                         ArithmeticException ame = new ArithmeticException ("something");
126                         BadImageFormatException bif = new BadImageFormatException ("message",
127                                 ame);
128
129 #if NET_2_0
130                         Assert.IsNotNull (bif.Data, "#1");
131 #endif
132                         Assert.IsNull (bif.FileName, "#2");
133                         Assert.IsNotNull (bif.InnerException, "#3");
134                         Assert.AreSame (ame, bif.InnerException, "#4");
135                         Assert.IsNotNull (bif.Message, "#5");
136                         Assert.AreEqual ("message", bif.Message, "#6");
137                         Assert.IsNull (bif.FusionLog, "#7");
138 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
139                         Assert.IsTrue (bif.ToString ().IndexOf (ame.GetType ().FullName + ": something") != -1, "#8");
140 #else
141                         Assert.AreEqual (bif.GetType ().FullName + ": message ---> "
142                                 + ame.GetType ().FullName + ": something", bif.ToString (), "#8");
143 #endif // TARGET_JVM
144                 }
145
146                 [Test]
147                 public void Constructor3_Message_Empty ()
148                 {
149                         ArithmeticException ame = new ArithmeticException ("something");
150                         BadImageFormatException bif = new BadImageFormatException (string.Empty, ame);
151
152 #if NET_2_0
153                         Assert.IsNotNull (bif.Data, "#1");
154 #endif
155                         Assert.IsNull (bif.FileName, "#2");
156                         Assert.IsNotNull (bif.InnerException, "#3");
157                         Assert.AreSame (ame, bif.InnerException, "#4");
158                         Assert.IsNotNull (bif.Message, "#5");
159                         Assert.AreEqual (string.Empty, bif.Message, "#6");
160                         Assert.IsNull (bif.FusionLog, "#7");
161 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
162                         Assert.IsTrue (bif.ToString ().IndexOf (ame.GetType ().FullName + ": something") != -1, "#8");
163 #else
164                         Assert.AreEqual (bif.GetType ().FullName + ":  ---> "
165                                 + ame.GetType ().FullName + ": something", bif.ToString (), "#8");
166 #endif // TARGET_JVM
167                 }
168
169                 [Test]
170                 public void Constructor3_Message_Null ()
171                 {
172                         ArithmeticException ame = new ArithmeticException ("something");
173                         BadImageFormatException bif = new BadImageFormatException ((string) null, ame);
174
175 #if NET_2_0
176                         Assert.IsNotNull (bif.Data, "#1");
177 #endif
178                         Assert.IsNull (bif.FileName, "#2");
179                         Assert.IsNotNull (bif.InnerException, "#3");
180                         Assert.AreSame (ame, bif.InnerException, "#4");
181 #if NET_2_0
182                         Assert.IsNotNull (bif.Message, "#5"); // Could not load file or assembly '' ...
183                         Assert.IsTrue (bif.Message.IndexOf ("''") != -1, "#6");
184 #else
185                         Assert.IsNotNull (bif.Message, "#5"); // Format of the executable (.exe) or library ...
186                         Assert.IsFalse (bif.Message.IndexOf ("''") != -1, "#6");
187 #endif
188                         Assert.IsNull (bif.FusionLog, "#7");
189                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName), "#8");
190                         Assert.IsTrue (bif.ToString ().IndexOf ("---> " + ame.GetType ().FullName) != -1, "#9");
191 #if !TARGET_JVM // ToString always has a stack trace under TARGET_JVM
192                         Assert.IsFalse (bif.ToString ().IndexOf (Environment.NewLine) != -1, "#10");
193 #endif // TARGET_JVM
194                 }
195
196                 [Test]
197                 public void Constructor3_InnerException_Null ()
198                 {
199                         BadImageFormatException bif = new BadImageFormatException ("message",
200                                 (Exception) null);
201
202 #if NET_2_0
203                         Assert.IsNotNull (bif.Data, "#1");
204 #endif
205                         Assert.IsNull (bif.FileName, "#2");
206                         Assert.IsNull (bif.InnerException, "#3");
207                         Assert.IsNotNull (bif.Message, "#4");
208                         Assert.AreEqual ("message", bif.Message, "#5");
209                         Assert.IsNull (bif.FusionLog, "#6");
210 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
211                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": message"), "#7");
212 #else
213                         Assert.AreEqual (bif.GetType ().FullName + ": message",
214                                 bif.ToString (), "#7");
215 #endif // TARGET_JVM
216                 }
217
218                 [Test]
219                 public void Constructor4 ()
220                 {
221                         BadImageFormatException bif = new BadImageFormatException ("message",
222                                 "file.txt");
223
224 #if NET_2_0
225                         Assert.IsNotNull (bif.Data, "#1");
226 #endif
227                         Assert.IsNotNull (bif.FileName, "#2");
228                         Assert.AreEqual ("file.txt", bif.FileName, "#3");
229                         Assert.IsNull (bif.InnerException, "#4");
230                         Assert.IsNotNull (bif.Message, "#5");
231                         Assert.AreEqual ("message", bif.Message, "#6");
232                         Assert.IsNull (bif.FusionLog, "#7");
233                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName
234                                 + ": message" + Environment.NewLine), "#8");
235 #if NET_2_0
236                         Assert.IsTrue (bif.ToString ().IndexOf ("'file.txt'") != -1, "#9");
237                         Assert.IsFalse (bif.ToString ().IndexOf ("\"file.txt\"") != -1, "#9");
238 #else
239                         Assert.IsFalse (bif.ToString ().IndexOf ("'file.txt'") != -1, "#9");
240                         Assert.IsTrue (bif.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
241 #endif
242                 }
243
244                 [Test]
245                 public void Constructor4_FileName_Empty ()
246                 {
247                         BadImageFormatException bif = new BadImageFormatException ("message",
248                                 string.Empty);
249
250 #if NET_2_0
251                         Assert.IsNotNull (bif.Data, "#1");
252 #endif
253                         Assert.IsNotNull (bif.FileName, "#2");
254                         Assert.AreEqual (string.Empty, bif.FileName, "#3");
255                         Assert.IsNull (bif.InnerException, "#4");
256                         Assert.IsNotNull (bif.Message, "#5");
257                         Assert.AreEqual ("message", bif.Message, "#6");
258                         Assert.IsNull (bif.FusionLog, "#7");
259 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
260                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": message"), "#8");
261 #else
262                         Assert.AreEqual (bif.GetType ().FullName + ": message",
263                                 bif.ToString (), "#8");
264 #endif // TARGET_JVM
265                 }
266
267                 [Test]
268                 public void Constructor4_FileName_Null ()
269                 {
270                         BadImageFormatException bif = new BadImageFormatException ("message",
271                                 (string) null);
272
273 #if NET_2_0
274                         Assert.IsNotNull (bif.Data, "#A1");
275 #endif
276                         Assert.IsNull (bif.FileName, "#A2");
277                         Assert.IsNull (bif.InnerException, "#A3");
278                         Assert.IsNotNull (bif.Message, "#A4");
279                         Assert.AreEqual ("message", bif.Message, "#A5");
280                         Assert.IsNull (bif.FusionLog, "#A6");
281 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
282                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": message"), "#A7");
283 #else
284                         Assert.AreEqual (bif.GetType ().FullName + ": message",
285                                 bif.ToString (), "#A7");
286 #endif // TARGET_JVM
287
288                         bif = new BadImageFormatException (string.Empty, (string) null);
289
290 #if NET_2_0
291                         Assert.IsNotNull (bif.Data, "#B1");
292 #endif
293                         Assert.IsNull (bif.FileName, "#B2");
294                         Assert.IsNull (bif.InnerException, "#B3");
295                         Assert.IsNotNull (bif.Message, "#B4");
296                         Assert.AreEqual (string.Empty, bif.Message, "#B5");
297                         Assert.IsNull (bif.FusionLog, "#B6");
298 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
299                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": "), "#B7");
300 #else
301                         Assert.AreEqual (bif.GetType ().FullName + ": ",
302                                 bif.ToString (), "#B7");
303 #endif // TARGET_JVM
304                 }
305
306                 [Test]
307                 public void Constructor4_FileNameAndMessage_Empty ()
308                 {
309                         BadImageFormatException bif = new BadImageFormatException (string.Empty,
310                                 string.Empty);
311
312 #if NET_2_0
313                         Assert.IsNotNull (bif.Data, "#1");
314 #endif
315                         Assert.IsNotNull (bif.FileName, "#2");
316                         Assert.AreEqual (string.Empty, bif.FileName, "#3");
317                         Assert.IsNull (bif.InnerException, "#4");
318                         Assert.IsNotNull (bif.Message, "#5");
319                         Assert.AreEqual (string.Empty, bif.Message, "#6");
320                         Assert.IsNull (bif.FusionLog, "#7");
321 #if TARGET_JVM // ToString always has a stack trace under TARGET_JVM
322                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType().FullName + ": "), "#8");
323 #else
324                         Assert.AreEqual (bif.GetType ().FullName + ": ", bif.ToString (), "#8");
325 #endif // TARGET_JVM
326                 }
327
328                 [Test]
329                 public void Constructor4_FileNameAndMessage_Null ()
330                 {
331                         BadImageFormatException bif = new BadImageFormatException ((string) null,
332                                 (string) null);
333
334 #if NET_2_0
335                         Assert.IsNotNull (bif.Data, "#1");
336 #endif
337                         Assert.IsNull (bif.FileName, "#2");
338                         Assert.IsNull (bif.InnerException, "#3");
339 #if NET_2_0
340                         Assert.IsNotNull (bif.Message, "#4"); // Could not load file or assembly '' ...
341                         Assert.IsTrue (bif.Message.IndexOf ("''") != -1, "#5");
342 #else
343                         Assert.IsNotNull (bif.Message, "#4"); // Format of the executable (.exe) or library ...
344                         Assert.IsFalse (bif.Message.IndexOf ("''") != -1, "#5");
345 #endif
346                         Assert.IsNull (bif.FusionLog, "#5");
347                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName
348                                 + ": "), "#6");
349 #if !TARGET_JVM // ToString always has a stack trace under TARGET_JVM
350                         Assert.IsFalse (bif.ToString ().IndexOf (Environment.NewLine) != -1, "#7");
351 #endif // TARGET_JVM
352                 }
353
354                 [Test]
355                 public void Constructor4_Message_Empty ()
356                 {
357                         BadImageFormatException bif = null;
358                         
359                         bif = new BadImageFormatException (string.Empty, "file.txt");
360
361 #if NET_2_0
362                         Assert.IsNotNull (bif.Data, "#1");
363 #endif
364                         Assert.IsNotNull (bif.FileName, "#2");
365                         Assert.AreEqual ("file.txt", bif.FileName, "#3");
366                         Assert.IsNull (bif.InnerException, "#4");
367                         Assert.IsNotNull (bif.Message, "#5");
368                         Assert.AreEqual (string.Empty, bif.Message, "#6");
369                         Assert.IsNull (bif.FusionLog, "#7");
370                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName
371                                 + ": " + Environment.NewLine), "#8");
372 #if NET_2_0
373                         Assert.IsTrue (bif.ToString ().IndexOf ("'file.txt'") != -1, "#9");
374                         Assert.IsFalse (bif.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
375 #else
376                         Assert.IsFalse (bif.ToString ().IndexOf ("'file.txt'") != -1, "#9");
377                         Assert.IsTrue (bif.ToString ().IndexOf ("\"file.txt\"") != -1, "#10");
378 #endif
379                 }
380
381                 [Test]
382                 public void Constructor4_Message_Null ()
383                 {
384                         BadImageFormatException bif = null;
385                         
386                         bif = new BadImageFormatException ((string) null, "file.txt");
387
388 #if NET_2_0
389                         Assert.IsNotNull (bif.Data, "#A1");
390 #endif
391                         Assert.IsNotNull (bif.FileName, "#A2");
392                         Assert.AreEqual ("file.txt", bif.FileName, "#A3");
393                         Assert.IsNull (bif.InnerException, "#A4");
394                         Assert.IsNotNull (bif.Message, "#A5");
395                         Assert.IsNull (bif.FusionLog, "#A6");
396                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName
397                                 + ": "), "#A7");
398                         Assert.IsTrue (bif.ToString ().IndexOf (Environment.NewLine) != -1, "#A8");
399                         Assert.IsTrue (bif.ToString ().IndexOf ("'file.txt'") != -1, "#A9");
400
401                         bif = new BadImageFormatException ((string) null, string.Empty);
402
403 #if NET_2_0
404                         Assert.IsNotNull (bif.Data, "#B1");
405 #endif
406                         Assert.IsNotNull (bif.FileName, "#B2");
407                         Assert.AreEqual (string.Empty, bif.FileName, "#B3");
408                         Assert.IsNull (bif.InnerException, "#B4");
409                         // .NET 1.1: The format of the file 'file.txt' is invalid
410                         // .NET 2.0: Could not load file or assembly 'file.txt' or one of its ...
411                         Assert.IsNotNull (bif.Message, "#B5");
412                         Assert.IsNull (bif.FusionLog, "#B6");
413                         Assert.IsTrue (bif.ToString ().StartsWith (bif.GetType ().FullName
414                                 + ": "), "#B7");
415 #if !TARGET_JVM // ToString always has a stack trace under TARGET_JVM
416                         Assert.IsFalse (bif.ToString ().IndexOf (Environment.NewLine) != -1, "#B8");
417 #endif // TARGET_JVM
418                         Assert.IsTrue (bif.ToString ().IndexOf ("''") != -1, "#B9");
419                 }
420         }
421 }