1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
|
//
// System.Reflection.Assembly Test Cases
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Philippe Lavoie (philippe.lavoie@cactus.ca)
// Sebastien Pouliot (sebastien@ximian.com)
//
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.Configuration.Assemblies;
using System.Globalization;
using System.IO;
using System.Reflection;
#if !MONOTOUCH
using System.Reflection.Emit;
#endif
using System.Threading;
using System.Runtime.Serialization;
using System.Security;
using System.Linq;
namespace MonoTests.System.Reflection
{
[TestFixture]
public class AssemblyTest
{
static string TempFolder = Path.Combine (Path.GetTempPath (),
"MonoTests.System.Reflection.AssemblyTest");
[SetUp]
public void SetUp ()
{
while (Directory.Exists (TempFolder))
TempFolder = Path.Combine (TempFolder, "2");
Directory.CreateDirectory (TempFolder);
}
[TearDown]
public void TearDown ()
{
try {
// This throws an exception under MS.NET, since the directory contains loaded
// assemblies.
Directory.Delete (TempFolder, true);
} catch (Exception) {
}
}
[Test]
public void CreateInstance()
{
Type type = typeof (AssemblyTest);
Object obj = type.Assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
Assert.IsNotNull (obj, "#01");
Assert.AreEqual (GetType (), obj.GetType (), "#02");
}
[Test]
public void CreateInvalidInstance()
{
Type type = typeof (AssemblyTest);
Object obj = type.Assembly.CreateInstance("NunitTests.ThisTypeDoesNotExist");
Assert.IsNull (obj, "#03");
}
[Test] // bug #49114
[Category ("NotWorking")]
[ExpectedException (typeof (ArgumentException))]
public void GetType_TypeName_Invalid ()
{
typeof (int).Assembly.GetType ("&blabla", true, true);
}
[Test] // bug #334203
public void GetType_TypeName_AssemblyName ()
{
Assembly a = typeof (int).Assembly;
string typeName = typeof (string).AssemblyQualifiedName;
try {
a.GetType (typeName, true, false);
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.IsNull (ex.ParamName, "#A5");
}
Type type = a.GetType (typeName, false);
Assert.IsNull (type, "#B1");
type = a.GetType (typeName, false, true);
Assert.IsNull (type, "#B2");
}
[Test]
public void GetEntryAssembly ()
{
// note: only available in default appdomain
// http://weblogs.asp.net/asanto/archive/2003/09/08/26710.aspx
// Not sure we should emulate this behavior.
string fname = AppDomain.CurrentDomain.FriendlyName;
if (fname.EndsWith (".dll")) { // nunit-console
Assert.IsNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
Assert.IsFalse (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
} else { // gnunit
Assert.IsNotNull (Assembly.GetEntryAssembly (), "GetEntryAssembly");
Assert.IsTrue (AppDomain.CurrentDomain.IsDefaultAppDomain (), "!default appdomain");
}
}
#if !MONOTOUCH // Reflection.Emit is not supported.
[Test]
public void GetModules_MissingFile ()
{
AssemblyName newName = new AssemblyName ();
newName.Name = "AssemblyTest";
AssemblyBuilder ab = Thread.GetDomain().DefineDynamicAssembly (newName, AssemblyBuilderAccess.RunAndSave, TempFolder);
ModuleBuilder mb = ab.DefineDynamicModule ("myDynamicModule1", "myDynamicModule.dll", true);
ab.Save ("test_assembly.dll");
File.Delete (Path.Combine (TempFolder, "myDynamicModule.dll"));
Assembly ass = Assembly.LoadFrom (Path.Combine (TempFolder, "test_assembly.dll"));
try {
ass.GetModules ();
Assert.Fail ();
} catch (FileNotFoundException ex) {
Assert.AreEqual ("myDynamicModule.dll", ex.FileName);
}
}
#endif
[Category ("NotWorking")]
[Test]
public void Corlib ()
{
Assembly corlib = typeof (int).Assembly;
Assert.IsTrue (corlib.CodeBase.EndsWith ("mscorlib.dll"), "CodeBase");
Assert.IsNull (corlib.EntryPoint, "EntryPoint");
Assert.IsTrue (corlib.EscapedCodeBase.EndsWith ("mscorlib.dll"), "EscapedCodeBase");
Assert.IsNotNull (corlib.Evidence, "Evidence");
Assert.IsTrue (corlib.Location.EndsWith ("mscorlib.dll"), "Location");
// corlib doesn't reference anything
Assert.AreEqual (0, corlib.GetReferencedAssemblies ().Length, "GetReferencedAssemblies");
Assert.AreEqual ("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", corlib.FullName, "FullName");
// not really "true" but it's even more trusted so...
Assert.IsTrue (corlib.GlobalAssemblyCache, "GlobalAssemblyCache");
Assert.AreEqual (0, corlib.HostContext, "HostContext");
Assert.AreEqual ("v2.0.50727", corlib.ImageRuntimeVersion, "ImageRuntimeVersion");
Assert.IsFalse (corlib.ReflectionOnly, "ReflectionOnly");
Assert.AreEqual (0x1, corlib.ManifestModule.MetadataToken);
}
[Test]
public void Corlib_test ()
{
Assembly corlib_test = Assembly.GetExecutingAssembly ();
#if MOBILE
Assert.IsNotNull (corlib_test.EntryPoint, "EntryPoint");
Assert.IsNull (corlib_test.Evidence, "Evidence");
#else
Assert.IsNull (corlib_test.EntryPoint, "EntryPoint");
Assert.IsNotNull (corlib_test.Evidence, "Evidence");
#endif
Assert.IsFalse (corlib_test.GlobalAssemblyCache, "GlobalAssemblyCache");
Assert.IsTrue (corlib_test.GetReferencedAssemblies ().Length > 0, "GetReferencedAssemblies");
Assert.AreEqual (0, corlib_test.HostContext, "HostContext");
#if NET_4_0 && !MOBILE
Assert.AreEqual ("v4.0.30319", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
#else
Assert.AreEqual ("v2.0.50727", corlib_test.ImageRuntimeVersion, "ImageRuntimeVersion");
#endif
Assert.IsNotNull (corlib_test.ManifestModule, "ManifestModule");
Assert.IsFalse (corlib_test.ReflectionOnly, "ReflectionOnly");
}
[Test]
public void GetAssembly ()
{
Assert.IsTrue (Assembly.GetAssembly (typeof (int)).FullName.StartsWith ("mscorlib"), "GetAssembly(int)");
Assert.AreEqual (this.GetType ().Assembly.FullName, Assembly.GetAssembly (this.GetType ()).FullName, "GetAssembly(this)");
}
[Test]
public void GetFile_Null ()
{
try {
Assembly.GetExecutingAssembly ().GetFile (null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
[Test]
public void GetFile_Empty ()
{
try {
Assembly.GetExecutingAssembly ().GetFile (
String.Empty);
Assert.Fail ("#1");
} catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNull (ex.ParamName, "#5");
}
}
[Test]
public void GetFiles_False ()
{
Assembly corlib = typeof (int).Assembly;
FileStream[] fss = corlib.GetFiles ();
Assert.AreEqual (fss.Length, corlib.GetFiles (false).Length, "corlib.GetFiles (false)");
Assembly corlib_test = Assembly.GetExecutingAssembly ();
fss = corlib_test.GetFiles ();
Assert.AreEqual (fss.Length, corlib_test.GetFiles (false).Length, "test.GetFiles (false)");
}
[Test]
public void GetFiles_True ()
{
Assembly corlib = typeof (int).Assembly;
FileStream[] fss = corlib.GetFiles ();
Assert.IsTrue (fss.Length <= corlib.GetFiles (true).Length, "corlib.GetFiles (true)");
Assembly corlib_test = Assembly.GetExecutingAssembly ();
fss = corlib_test.GetFiles ();
Assert.IsTrue (fss.Length <= corlib_test.GetFiles (true).Length, "test.GetFiles (true)");
}
[Test]
public void GetManifestResourceStream_Name_Empty ()
{
Assembly corlib = typeof (int).Assembly;
try {
corlib.GetManifestResourceStream (string.Empty);
Assert.Fail ("#A1");
} catch (ArgumentException ex) {
// String cannot have zero length
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
}
corlib.GetManifestResourceStream (typeof (int), string.Empty);
try {
corlib.GetManifestResourceStream ((Type) null, string.Empty);
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// String cannot have zero length
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
}
}
[Test]
public void GetManifestResourceStream_Name_Null ()
{
Assembly corlib = typeof (int).Assembly;
try {
corlib.GetManifestResourceStream ((string) null);
Assert.Fail ("#A1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
}
corlib.GetManifestResourceStream (typeof (int), (string) null);
try {
corlib.GetManifestResourceStream ((Type) null, (string) null);
Assert.Fail ("#B1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsNotNull (ex.ParamName, "#B5");
Assert.AreEqual ("type", ex.ParamName, "#B6");
}
}
[Test]
public void IsDefined_AttributeType_Null ()
{
Assembly corlib = typeof (int).Assembly;
try {
corlib.IsDefined ((Type) null, false);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("attributeType", ex.ParamName, "#6");
}
}
[Test] // bug #78517
#if ONLY_1_1
[Category ("NotDotNet")] // MS.NET 1.x throws FileLoadException
#endif
public void LoadFrom_Empty_Assembly ()
{
string tempFile = Path.GetTempFileName ();
try {
Assembly.LoadFrom (tempFile);
Assert.Fail ("#1");
} catch (BadImageFormatException ex) {
Assert.IsNull (ex.InnerException, "#2");
} finally {
File.Delete (tempFile);
}
}
[Test] // bug #78517
public void LoadFrom_Invalid_Assembly ()
{
string tempFile = Path.GetTempFileName ();
using (StreamWriter sw = File.CreateText (tempFile)) {
sw.WriteLine ("foo");
sw.Close ();
}
try {
Assembly.LoadFrom (tempFile);
Assert.Fail ("#1");
} catch (BadImageFormatException ex) {
Assert.IsNull (ex.InnerException, "#2");
} finally {
File.Delete (tempFile);
}
}
[Test]
public void LoadFrom_NonExisting_Assembly ()
{
string tempFile = Path.GetTempFileName ();
File.Delete (tempFile);
try {
Assembly.LoadFrom (tempFile);
Assert.Fail ("#1");
} catch (FileNotFoundException ex) {
Assert.IsNull (ex.InnerException, "#2");
} finally {
File.Delete (tempFile);
}
}
[Test]
public void LoadWithPartialName ()
{
string [] names = { "corlib_test_net_1_1", "corlib_test_net_2_0", "corlib_test_net_4_0", "corlib_test_net_4_5", "corlib_plattest", "mscorlibtests" };
foreach (string s in names)
if (Assembly.LoadWithPartialName (s) != null)
return;
Assert.Fail ("Was not able to load any corlib test");
}
[Test]
public void GetObjectData_Info_Null ()
{
Assembly corlib = typeof (int).Assembly;
try {
corlib.GetObjectData (null, new StreamingContext (
StreamingContextStates.All));
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("info", ex.ParamName, "#6");
}
}
[Test]
public void GetReferencedAssemblies ()
{
Assembly corlib_test = Assembly.GetExecutingAssembly ();
AssemblyName[] names = corlib_test.GetReferencedAssemblies ();
foreach (AssemblyName an in names) {
Assert.IsNull (an.CodeBase, "CodeBase");
Assert.IsNotNull (an.CultureInfo, "CultureInfo");
Assert.IsNull (an.EscapedCodeBase, "EscapedCodeBase");
Assert.AreEqual (AssemblyNameFlags.None, an.Flags, "Flags");
Assert.IsNotNull (an.FullName, "FullName");
Assert.AreEqual (AssemblyHashAlgorithm.SHA1, an.HashAlgorithm, "HashAlgorithm");
Assert.IsNull (an.KeyPair, "KeyPair");
Assert.IsNotNull (an.Name, "Name");
Assert.IsNotNull (an.Version, "Version");
Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
an.VersionCompatibility, "VersionCompatibility");
}
}
#if !MONOTOUCH // Reflection.Emit is not supported.
[Test]
public void Location_Empty() {
string assemblyFileName = Path.Combine (
Path.GetTempPath (), "AssemblyLocation.dll");
try {
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "AssemblyLocation";
AssemblyBuilder ab = AppDomain.CurrentDomain
.DefineDynamicAssembly (assemblyName,
AssemblyBuilderAccess.Save,
Path.GetTempPath (),
AppDomain.CurrentDomain.Evidence);
ab.Save (Path.GetFileName (assemblyFileName));
using (FileStream fs = File.OpenRead (assemblyFileName)) {
byte[] buffer = new byte[fs.Length];
fs.Read (buffer, 0, buffer.Length);
Assembly assembly = Assembly.Load (buffer);
Assert.AreEqual (string.Empty, assembly.Location);
fs.Close ();
}
} finally {
File.Delete (assemblyFileName);
}
}
[Test]
[Category ("NotWorking")]
public void bug78464 ()
{
string assemblyFileName = Path.Combine (
Path.GetTempPath (), "bug78464.dll");
try {
// execute test in separate appdomain to allow assembly to be unloaded
AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
try {
crossDomainTester.bug78464 (assemblyFileName);
} finally {
AppDomain.Unload (testDomain);
}
} finally {
File.Delete (assemblyFileName);
}
}
[Test]
public void bug78465 ()
{
string assemblyFileName = Path.Combine (
Path.GetTempPath (), "bug78465.dll");
try {
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "bug78465";
AssemblyBuilder ab = AppDomain.CurrentDomain
.DefineDynamicAssembly (assemblyName,
AssemblyBuilderAccess.Save,
Path.GetDirectoryName (assemblyFileName),
AppDomain.CurrentDomain.Evidence);
ab.Save (Path.GetFileName (assemblyFileName));
using (FileStream fs = File.OpenRead (assemblyFileName)) {
byte[] buffer = new byte[fs.Length];
fs.Read (buffer, 0, buffer.Length);
Assembly assembly = Assembly.Load (buffer);
Assert.AreEqual (string.Empty, assembly.Location, "#1");
fs.Close ();
}
AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
try {
crossDomainTester.bug78465 (assemblyFileName);
} finally {
AppDomain.Unload (testDomain);
}
} finally {
File.Delete (assemblyFileName);
}
}
[Test]
public void bug78468 ()
{
string assemblyFileNameA = Path.Combine (Path.GetTempPath (),
"bug78468a.dll");
string resourceFileName = Path.Combine (Path.GetTempPath (),
"readme.txt");
using (StreamWriter sw = File.CreateText (resourceFileName)) {
sw.WriteLine ("FOO");
sw.Close ();
}
try {
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "bug78468a";
AssemblyBuilder ab = AppDomain.CurrentDomain
.DefineDynamicAssembly (assemblyName,
AssemblyBuilderAccess.Save,
Path.GetTempPath (),
AppDomain.CurrentDomain.Evidence);
ab.AddResourceFile ("read", "readme.txt");
ab.Save (Path.GetFileName (assemblyFileNameA));
Assembly assembly;
using (FileStream fs = File.OpenRead (assemblyFileNameA)) {
byte[] buffer = new byte[fs.Length];
fs.Read (buffer, 0, buffer.Length);
assembly = Assembly.Load (buffer);
fs.Close ();
}
Assert.AreEqual (string.Empty, assembly.Location, "#A1");
string[] resNames = assembly.GetManifestResourceNames ();
Assert.IsNotNull (resNames, "#A2");
Assert.AreEqual (1, resNames.Length, "#A3");
Assert.AreEqual ("read", resNames[0], "#A4");
ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
Assert.IsNotNull (resInfo, "#A5");
Assert.AreEqual ("readme.txt", resInfo.FileName, "#A6");
Assert.IsNull (resInfo.ReferencedAssembly, "#A7");
Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#A8");
try {
assembly.GetManifestResourceStream ("read");
Assert.Fail ("#A9");
} catch (FileNotFoundException) {
}
try {
assembly.GetFile ("readme.txt");
Assert.Fail ("#A10");
} catch (FileNotFoundException) {
}
string assemblyFileNameB = Path.Combine (Path.GetTempPath (),
"bug78468b.dll");
AppDomain testDomain = CreateTestDomain (AppDomain.CurrentDomain.BaseDirectory, false);
CrossDomainTester crossDomainTester = CreateCrossDomainTester (testDomain);
try {
crossDomainTester.bug78468 (assemblyFileNameB);
} finally {
AppDomain.Unload (testDomain);
File.Delete (assemblyFileNameB);
}
} finally {
File.Delete (assemblyFileNameA);
File.Delete (resourceFileName);
}
}
[Test]
[Category ("NotWorking")]
public void ReflectionOnlyLoad ()
{
Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
Assert.IsNotNull (assembly);
Assert.IsTrue (assembly.ReflectionOnly);
}
[Test]
public void ReflectionOnlyLoadFrom ()
{
string loc = typeof (AssemblyTest).Assembly.Location;
string filename = Path.GetFileName (loc);
Assembly assembly = Assembly.ReflectionOnlyLoadFrom (filename);
Assert.IsNotNull (assembly);
Assert.IsTrue (assembly.ReflectionOnly);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void CreateInstanceOnRefOnly ()
{
Assembly assembly = Assembly.ReflectionOnlyLoad (typeof (AssemblyTest).Assembly.FullName);
assembly.CreateInstance ("MonoTests.System.Reflection.AssemblyTest");
}
[Test]
[Category ("NotWorking")] // patch for bug #79720 must be committed first
public void Load_Culture ()
{
string tempDir = Path.Combine (Path.GetTempPath (),
"MonoTests.System.Reflection.AssemblyTest");
string cultureTempDir = Path.Combine (tempDir, "nl-BE");
if (!Directory.Exists (cultureTempDir))
Directory.CreateDirectory (cultureTempDir);
cultureTempDir = Path.Combine (tempDir, "en-US");
if (!Directory.Exists (cultureTempDir))
Directory.CreateDirectory (cultureTempDir);
AppDomain ad = CreateTestDomain (tempDir, true);
try {
CrossDomainTester cdt = CreateCrossDomainTester (ad);
// PART A
AssemblyName aname = new AssemblyName ();
aname.Name = "culturea";
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "culturea.dll"));
aname = new AssemblyName ();
aname.Name = "culturea";
Assert.IsTrue (cdt.AssertLoad(aname), "#A1");
aname = new AssemblyName ();
aname.Name = "culturea";
aname.CultureInfo = new CultureInfo ("nl-BE");
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A2");
aname = new AssemblyName ();
aname.Name = "culturea";
aname.CultureInfo = CultureInfo.InvariantCulture;
Assert.IsTrue (cdt.AssertLoad(aname), "#A3");
// PART B
aname = new AssemblyName ();
aname.Name = "cultureb";
aname.CultureInfo = new CultureInfo ("nl-BE");
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "cultureb.dll"));
aname = new AssemblyName ();
aname.Name = "cultureb";
aname.CultureInfo = new CultureInfo ("nl-BE");
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
aname = new AssemblyName ();
aname.Name = "cultureb";
Assert.IsTrue (cdt.AssertLoad (aname), "#B2");
aname = new AssemblyName ();
aname.Name = "cultureb";
aname.CultureInfo = new CultureInfo ("en-US");
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B3");
// PART C
aname = new AssemblyName ();
aname.Name = "culturec";
aname.CultureInfo = new CultureInfo ("nl-BE");
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "nl-BE/culturec.dll"));
aname = new AssemblyName ();
aname.Name = "culturec";
aname.CultureInfo = new CultureInfo ("nl-BE");
Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
aname = new AssemblyName ();
aname.Name = "culturec";
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C2");
aname = new AssemblyName ();
aname.Name = "culturec";
aname.CultureInfo = CultureInfo.InvariantCulture;
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#C3");
// PART D
aname = new AssemblyName ();
aname.Name = "cultured";
aname.CultureInfo = new CultureInfo ("nl-BE");
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/cultured.dll"));
aname = new AssemblyName ();
aname.Name = "cultured";
aname.CultureInfo = new CultureInfo ("nl-BE");
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D1");
aname = new AssemblyName ();
aname.Name = "cultured";
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D2");
aname = new AssemblyName ();
aname.Name = "cultured";
aname.CultureInfo = CultureInfo.InvariantCulture;
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#D3");
} finally {
AppDomain.Unload (ad);
if (Directory.Exists (tempDir))
Directory.Delete (tempDir, true);
}
}
[Test] // bug #79712
[Category ("NotWorking")] // in non-default domain, MS throws FileNotFoundException
public void Load_Culture_Mismatch ()
{
string tempDir = Path.Combine (Path.GetTempPath (),
"MonoTests.System.Reflection.AssemblyTest");
string cultureTempDir = Path.Combine (tempDir, "en-US");
if (!Directory.Exists (cultureTempDir))
Directory.CreateDirectory (cultureTempDir);
AppDomain ad = CreateTestDomain (tempDir, true);
try {
CrossDomainTester cdt = CreateCrossDomainTester (ad);
// PART A
AssemblyName aname = new AssemblyName ();
aname.Name = "bug79712a";
aname.CultureInfo = new CultureInfo ("nl-BE");
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79712a.dll"));
aname = new AssemblyName ();
aname.Name = "bug79712a";
aname.CultureInfo = CultureInfo.InvariantCulture;
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#A1");
// PART B
aname = new AssemblyName ();
aname.Name = "bug79712b";
aname.CultureInfo = new CultureInfo ("nl-BE");
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "en-US/bug79712b.dll"));
aname = new AssemblyName ();
aname.Name = "bug79712b";
aname.CultureInfo = new CultureInfo ("en-US");
Assert.IsTrue (cdt.AssertFileNotFoundException (aname), "#B1");
} finally {
AppDomain.Unload (ad);
if (Directory.Exists (tempDir))
Directory.Delete (tempDir, true);
}
}
[Test] // bug #79715
public void Load_PartialVersion ()
{
string tempDir = Path.Combine (Path.GetTempPath (),
"MonoTests.System.Reflection.AssemblyTest");
if (!Directory.Exists (tempDir))
Directory.CreateDirectory (tempDir);
AppDomain ad = CreateTestDomain (tempDir, true);
try {
CrossDomainTester cdt = CreateCrossDomainTester (ad);
AssemblyName aname = new AssemblyName ();
aname.Name = "bug79715";
aname.Version = new Version (1, 2, 3, 4);
cdt.GenerateAssembly (aname, Path.Combine (tempDir, "bug79715.dll"));
aname = new AssemblyName ();
aname.Name = "bug79715";
aname.Version = new Version (1, 2);
Assert.IsTrue (cdt.AssertLoad (aname), "#A1");
Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#A2");
aname = new AssemblyName ();
aname.Name = "bug79715";
aname.Version = new Version (1, 2, 3);
Assert.IsTrue (cdt.AssertLoad (aname), "#B1");
Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#B2");
aname = new AssemblyName ();
aname.Name = "bug79715";
aname.Version = new Version (1, 2, 3, 4);
Assert.IsTrue (cdt.AssertLoad (aname), "#C1");
Assert.IsTrue (cdt.AssertLoad (aname.FullName), "#C2");
} finally {
AppDomain.Unload (ad);
if (Directory.Exists (tempDir))
Directory.Delete (tempDir, true);
}
}
private static AppDomain CreateTestDomain (string baseDirectory, bool assemblyResolver)
{
AppDomainSetup setup = new AppDomainSetup ();
setup.ApplicationBase = baseDirectory;
setup.ApplicationName = "testdomain";
AppDomain ad = AppDomain.CreateDomain ("testdomain",
AppDomain.CurrentDomain.Evidence, setup);
if (assemblyResolver) {
Assembly ea = Assembly.GetExecutingAssembly ();
ad.CreateInstanceFrom (ea.CodeBase,
typeof (AssemblyResolveHandler).FullName,
false,
BindingFlags.Public | BindingFlags.Instance,
null,
new object [] { ea.Location, ea.FullName },
CultureInfo.InvariantCulture,
null,
null);
}
return ad;
}
private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
{
Type testerType = typeof (CrossDomainTester);
return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
testerType.Assembly.FullName, testerType.FullName, false,
BindingFlags.Public | BindingFlags.Instance, null, new object[0],
CultureInfo.InvariantCulture, new object[0], null);
}
private class CrossDomainTester : MarshalByRefObject
{
public void GenerateAssembly (AssemblyName aname, string path)
{
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
aname, AssemblyBuilderAccess.Save, Path.GetDirectoryName (path));
ab.Save (Path.GetFileName (path));
}
public void Load (AssemblyName assemblyRef)
{
Assembly.Load (assemblyRef);
}
public void LoadFrom (string assemblyFile)
{
Assembly.LoadFrom (assemblyFile);
}
public bool AssertLoad (AssemblyName assemblyRef)
{
try {
Assembly.Load (assemblyRef);
return true;
} catch {
return false;
}
}
public bool AssertLoad (string assemblyString)
{
try {
Assembly.Load (assemblyString);
return true;
} catch {
return false;
}
}
public bool AssertFileLoadException (AssemblyName assemblyRef)
{
try {
Assembly.Load (assemblyRef);
return false;
} catch (FileLoadException) {
return true;
}
}
public bool AssertFileNotFoundException (AssemblyName assemblyRef)
{
try {
Assembly.Load (assemblyRef);
return false;
} catch (FileNotFoundException) {
return true;
}
}
public void bug78464 (string assemblyFileName)
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "bug78464";
AssemblyBuilder ab = AppDomain.CurrentDomain
.DefineDynamicAssembly (assemblyName,
AssemblyBuilderAccess.Save,
Path.GetDirectoryName (assemblyFileName),
AppDomain.CurrentDomain.Evidence);
ab.Save (Path.GetFileName (assemblyFileName));
Assembly assembly;
using (FileStream fs = File.OpenRead (assemblyFileName)) {
byte[] buffer = new byte[fs.Length];
fs.Read (buffer, 0, buffer.Length);
assembly = Assembly.Load (buffer);
fs.Close ();
}
Assert.AreEqual (string.Empty, assembly.Location, "#1");
assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
Assert.IsFalse (assembly.Location == string.Empty, "#2");
Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName(assembly.Location), "#3");
// note: we cannot check if directory names match, as MS.NET seems to
// convert directory part of assembly location to lowercase
Assert.IsFalse (Path.GetDirectoryName(assembly.Location) == string.Empty, "#4");
}
public void bug78465 (string assemblyFileName)
{
Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
Assert.IsFalse (assembly.Location == string.Empty, "#2");
Assert.AreEqual (Path.GetFileName (assemblyFileName), Path.GetFileName (assembly.Location), "#3");
// note: we cannot check if directory names match, as MS.NET seems to
// convert directory part of assembly location to lowercase
Assert.IsFalse (Path.GetDirectoryName (assembly.Location) == string.Empty, "#4");
}
public void bug78468 (string assemblyFileName)
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = "bug78468b";
AssemblyBuilder ab = AppDomain.CurrentDomain
.DefineDynamicAssembly (assemblyName,
AssemblyBuilderAccess.Save,
Path.GetDirectoryName (assemblyFileName),
AppDomain.CurrentDomain.Evidence);
ab.AddResourceFile ("read", "readme.txt");
ab.Save (Path.GetFileName (assemblyFileName));
Assembly assembly = Assembly.LoadFrom (assemblyFileName, AppDomain.CurrentDomain.Evidence);
Assert.IsTrue (assembly.Location != string.Empty, "#B1");
string[] resNames = assembly.GetManifestResourceNames ();
Assert.IsNotNull (resNames, "#B2");
Assert.AreEqual (1, resNames.Length, "#B3");
Assert.AreEqual ("read", resNames[0], "#B4");
ManifestResourceInfo resInfo = assembly.GetManifestResourceInfo ("read");
Assert.IsNotNull (resInfo, "#B5");
Assert.AreEqual ("readme.txt", resInfo.FileName, "#B6");
Assert.IsNull (resInfo.ReferencedAssembly, "#B7");
Assert.AreEqual ((ResourceLocation) 0, resInfo.ResourceLocation, "#B8");
Stream s = assembly.GetManifestResourceStream ("read");
Assert.IsNotNull (s, "#B9");
s.Close ();
s = assembly.GetFile ("readme.txt");
Assert.IsNotNull (s, "#B10");
s.Close ();
}
}
[Test]
public void bug79872 ()
{
Random rnd = new Random ();
string outdir;
int tries = 0;
retry:
outdir = Path.Combine (Path.GetTempPath (), "bug79872-" + rnd.Next (10000, 99999));
if (Directory.Exists (outdir)) {
try {
Directory.Delete (outdir, true);
} catch {
if (++tries <= 100)
goto retry;
}
}
Directory.CreateDirectory (outdir);
AssemblyName an = new AssemblyName ();
an.Name = "bug79872";
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, outdir);
string dllname = "bug79872.dll";
ModuleBuilder mb1 = ab.DefineDynamicModule ("bug79872", dllname);
string netmodule = "bug79872.netmodule";
ModuleBuilder mb2 = ab.DefineDynamicModule (netmodule, netmodule);
TypeBuilder a1 = mb2.DefineType ("A");
a1.CreateType ();
ab.Save (dllname);
bool ok = true;
try {
Assembly.LoadFrom (Path.Combine (outdir, dllname));
} catch {
ok = false;
}
Assert.IsTrue (ok, "Should load a .NET metadata file with an assembly manifest");
ok = false;
try {
Assembly.LoadFrom (Path.Combine (outdir, netmodule));
} catch (BadImageFormatException) {
ok = true; // mono and .net 2.0 throw this
} catch (FileLoadException) {
ok = true; // .net 1.1 throws this
} catch {
// swallow the rest
}
Assert.IsTrue (ok, "Complain on loading a .NET metadata file without an assembly manifest");
Directory.Delete (outdir, true);
}
#endif
[Test]
public void ManifestModule ()
{
Assembly assembly = typeof (int).Assembly;
Module module = assembly.ManifestModule;
Assert.IsNotNull (module, "#1");
#if NET_4_0
Assert.AreEqual ("MonoModule", module.GetType ().Name, "#2");
#else
Assert.AreEqual (typeof (Module), module.GetType (), "#2");
#endif
#if !MONOTOUCH
Assert.AreEqual ("mscorlib.dll", module.Name, "#3");
#endif
Assert.IsFalse (module.IsResource (), "#4");
Assert.IsTrue (assembly.GetModules ().Length > 0, "#5");
Assert.AreSame (module, assembly.GetModules () [0], "#6");
Assert.AreSame (module, assembly.ManifestModule, "#7");
}
[Serializable ()]
private class AssemblyResolveHandler
{
public AssemblyResolveHandler (string assemblyFile, string assemblyName)
{
_assemblyFile = assemblyFile;
_assemblyName = assemblyName;
AppDomain.CurrentDomain.AssemblyResolve +=
new ResolveEventHandler (ResolveAssembly);
}
private Assembly ResolveAssembly (Object sender, ResolveEventArgs args)
{
if (args.Name == _assemblyName)
return Assembly.LoadFrom (_assemblyFile);
return null;
}
private readonly string _assemblyFile;
private readonly string _assemblyName;
}
protected internal class Bug328812_NestedFamORAssem { };
[Test]
public void bug328812 ()
{
Assembly corlib_test = Assembly.GetExecutingAssembly ();
Assert.IsNull (corlib_test.GetType ("Bug328812_NestedFamORAssem"));
// Just a sanity check, in case the above passed for some other reason
Assert.IsNotNull (corlib_test.GetType ("MonoTests.System.Reflection.AssemblyTest+Bug328812_NestedFamORAssem"));
}
[Test]
public void GetCustomAttributes_AttributeType_Null ()
{
Assembly a = typeof (int).Assembly;
try {
a.GetCustomAttributes (null, false);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("attributeType", ex.ParamName, "#6");
}
}
[Test]
public void GetTypeWithEmptyStringShouldThrow ()
{
try {
typeof (string).Assembly.GetType ("");
Assert.Fail ("#1");
} catch (ArgumentException) {}
}
#if NET_4_5
[Test]
public void DefinedTypes_Equality ()
{
var x1 = Assembly.GetExecutingAssembly ().DefinedTypes.Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
var x2 = Assembly.GetExecutingAssembly ().GetTypes ().Where (l => l.FullName == "MonoTests.System.Reflection.TestDefinedTypes").Single ();
Assert.AreSame (x1, x2, "#1");
}
#endif
}
public class TestDefinedTypes
{
}
}
|