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