PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System/AppDomainTest.cs

https://bitbucket.org/danipen/mono
C# | 3559 lines | 3382 code | 91 blank | 86 comment | 44 complexity | 46dc4c6e0d9c93802ee7af4c254a5a7e MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. //
  2. // AppDomainTest.cs - NUnit Test Cases for AppDomain
  3. //
  4. // Author:
  5. // Sebastien Pouliot (sebastien@ximian.com)
  6. //
  7. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  8. // Copyright 2011 Xamarin Inc (http://www.xamarin.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. #if !MOBILE
  30. using NUnit.Framework;
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using System.Configuration.Assemblies;
  35. using System.Globalization;
  36. using System.IO;
  37. using System.Reflection;
  38. using System.Reflection.Emit;
  39. using System.Runtime.InteropServices;
  40. using System.Security;
  41. using System.Security.Permissions;
  42. using System.Security.Policy;
  43. using System.Security.Principal;
  44. namespace MonoTests.System
  45. {
  46. [TestFixture]
  47. public class AppDomainTest
  48. {
  49. private AppDomain ad;
  50. private ArrayList files = new ArrayList ();
  51. private string tempDir;
  52. [SetUp]
  53. public void SetUp ()
  54. {
  55. tempDir = Path.Combine (Path.GetTempPath (), Environment.UserName);
  56. tempDir = Path.Combine (tempDir, "MonoTests.System.AppDomainTest");
  57. if (!Directory.Exists (tempDir)) {
  58. Directory.CreateDirectory (tempDir);
  59. }
  60. }
  61. [TearDown]
  62. public void TearDown ()
  63. {
  64. if (ad != null) {
  65. try {
  66. AppDomain.Unload (ad);
  67. ad = null;
  68. } catch { } // do not affect unit test results in TearDown
  69. }
  70. foreach (string fname in files) {
  71. File.Delete (fname);
  72. }
  73. files.Clear ();
  74. }
  75. [Test] // bug #80934
  76. public void ConfigurationFile_Relative ()
  77. {
  78. // Note:
  79. // We use Environment.GetCommandLineArgs () to get the location of
  80. // the entry assembly in the default domain (since the default domain
  81. // is not exposed by any API)
  82. //
  83. // MS returns a lower-case path in Environment.GetCommandLineArgs ()
  84. // and hence we need to perform a case-insensitive comparison
  85. // if the Assert involves that path
  86. string configFile = "test.config";
  87. string appBase = null;
  88. string expectedConfigFile = null;
  89. string expectedAppBase = null;
  90. // do not set ApplicationBase
  91. appBase = Path.GetDirectoryName (Environment.GetCommandLineArgs () [0]);
  92. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  93. appBase : appBase + Path.DirectorySeparatorChar;
  94. expectedConfigFile = Path.Combine (appBase, configFile);
  95. AppDomainSetup setup = new AppDomainSetup();
  96. setup.ConfigurationFile = configFile;
  97. ad = CreateTestDomain (setup, true);
  98. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  99. if (RunningOnUnix) {
  100. Assert.AreEqual (expectedConfigFile, cdt.GetConfigurationFile (), "#A1");
  101. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#A2");
  102. } else {
  103. Assert.IsTrue (string.Compare (expectedConfigFile, cdt.GetConfigurationFile (), true) == 0, "#A1");
  104. Assert.IsTrue (string.Compare (expectedAppBase, cdt.GetApplicationBase (), true) == 0, "#A2");
  105. }
  106. AppDomain.Unload (ad);
  107. // set ApplicationBase
  108. appBase = Path.GetTempPath ();
  109. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  110. appBase : appBase + Path.DirectorySeparatorChar;
  111. expectedConfigFile = Path.Combine (appBase, configFile);
  112. setup = new AppDomainSetup ();
  113. setup.ApplicationBase = appBase;
  114. setup.ConfigurationFile = configFile;
  115. ad = CreateTestDomain (setup, true);
  116. cdt = CreateCrossDomainTester (ad);
  117. Assert.AreEqual (expectedConfigFile, cdt.GetConfigurationFile (), "#B1");
  118. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#B2");
  119. AppDomain.Unload (ad);
  120. }
  121. [Test] // bug #80934
  122. public void ConfigurationFile_Absolute ()
  123. {
  124. // Note:
  125. // We use Environment.GetCommandLineArgs () to get the location of
  126. // the entry assembly in the default domain (since the default domain
  127. // is not exposed by any API)
  128. //
  129. // MS returns a lower-case path in Environment.GetCommandLineArgs ()
  130. // and hence on Windows we need to perform a case-insensitive
  131. // comparison if the Assert involves that path
  132. string configFile = Path.Combine (tempDir, "test.config");
  133. string appBase = null;
  134. string expectedAppBase = null;
  135. // do not set ApplicationBase
  136. appBase = Path.GetDirectoryName (Environment.GetCommandLineArgs () [0]);
  137. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  138. appBase : appBase + Path.DirectorySeparatorChar;
  139. AppDomainSetup setup = new AppDomainSetup ();
  140. setup.ConfigurationFile = configFile;
  141. ad = CreateTestDomain (setup, true);
  142. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  143. Assert.AreEqual (configFile, cdt.GetConfigurationFile (), "#A1");
  144. if (RunningOnUnix) {
  145. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#A2");
  146. } else {
  147. Assert.IsTrue (string.Compare (expectedAppBase, cdt.GetApplicationBase (), true) == 0, "#A2");
  148. }
  149. AppDomain.Unload (ad);
  150. // set ApplicationBase
  151. appBase = Path.GetTempPath ();
  152. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  153. appBase : appBase + Path.DirectorySeparatorChar;
  154. setup = new AppDomainSetup ();
  155. setup.ApplicationBase = appBase;
  156. setup.ConfigurationFile = configFile;
  157. ad = CreateTestDomain (setup, true);
  158. cdt = CreateCrossDomainTester (ad);
  159. Assert.AreEqual (configFile, cdt.GetConfigurationFile (), "#B1");
  160. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#B2");
  161. AppDomain.Unload (ad);
  162. }
  163. [Test] // bug #80934
  164. public void ConfigurationFile_Null ()
  165. {
  166. // Note:
  167. // We use Environment.GetCommandLineArgs () to get the location of
  168. // the entry assembly in the default domain (since the default domain
  169. // is not exposed by any API)
  170. //
  171. // MS returns a lower-case path in Environment.GetCommandLineArgs ()
  172. // and hence we need to perform a case-insensitive comparison
  173. // if the Assert involves that path
  174. string appBase = null;
  175. string expectedAppBase = null;
  176. string expectedConfigFile = null;
  177. // do not set ApplicationBase
  178. appBase = Path.GetDirectoryName (Environment.GetCommandLineArgs () [0]);
  179. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  180. appBase : appBase + Path.DirectorySeparatorChar;
  181. expectedConfigFile = Environment.GetCommandLineArgs () [0] + ".config";
  182. AppDomainSetup setup = new AppDomainSetup ();
  183. setup.ConfigurationFile = null;
  184. ad = CreateTestDomain (setup, true);
  185. CrossDomainTester cdt = CreateCrossDomainTester (ad);
  186. if (RunningOnUnix) {
  187. Assert.AreEqual (expectedConfigFile, cdt.GetConfigurationFile (), "#A1");
  188. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#A2");
  189. } else {
  190. Assert.IsTrue (string.Compare (expectedConfigFile, cdt.GetConfigurationFile (), true) == 0, "#A1");
  191. Assert.IsTrue (string.Compare (expectedAppBase, cdt.GetApplicationBase (), true) == 0, "#A2");
  192. }
  193. AppDomain.Unload (ad);
  194. // set ApplicationBase
  195. appBase = Path.GetTempPath ();
  196. expectedAppBase = appBase [appBase.Length - 1] == Path.DirectorySeparatorChar ?
  197. appBase : appBase + Path.DirectorySeparatorChar;
  198. expectedConfigFile = Path.Combine (appBase, Path.GetFileName (Environment.GetCommandLineArgs () [0]) + ".config");
  199. setup = new AppDomainSetup ();
  200. setup.ApplicationBase = appBase;
  201. setup.ConfigurationFile = null;
  202. ad = CreateTestDomain (setup, true);
  203. cdt = CreateCrossDomainTester (ad);
  204. if (RunningOnUnix) {
  205. Assert.AreEqual (expectedConfigFile, cdt.GetConfigurationFile (), "#B1");
  206. } else {
  207. Assert.IsTrue (string.Compare (expectedConfigFile, cdt.GetConfigurationFile (), true) == 0, "#B1");
  208. }
  209. Assert.AreEqual (expectedAppBase, cdt.GetApplicationBase (), "#B2");
  210. AppDomain.Unload (ad);
  211. }
  212. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess)
  213. public void DefineDynamicAssembly1_Access_Invalid ()
  214. {
  215. AssemblyName name = new AssemblyName ();
  216. name.Name = "DefineDynamicAssembly1";
  217. try {
  218. AppDomain.CurrentDomain.DefineDynamicAssembly (
  219. name, AssemblyBuilderAccess.Run |
  220. (AssemblyBuilderAccess) 666);
  221. Assert.Fail ("#1");
  222. } catch (ArgumentException ex) {
  223. // Illegal enum value: 667
  224. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  225. Assert.IsNull (ex.InnerException, "#3");
  226. Assert.IsNotNull (ex.Message, "#4");
  227. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  228. Assert.IsNotNull (ex.ParamName, "#6");
  229. Assert.AreEqual ("access", ex.ParamName, "#7");
  230. }
  231. }
  232. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess)
  233. public void DefineDynamicAssembly1_Name_InvalidChars ()
  234. {
  235. string [] invalid_char_names = new string [] {
  236. "\tAB",
  237. " AB",
  238. "\rAB",
  239. "A/B",
  240. ":AB",
  241. "B:A",
  242. "B\\A",
  243. "BA\\"};
  244. AssemblyName name = new AssemblyName ();
  245. foreach (string invalid_name in invalid_char_names) {
  246. name.Name = invalid_name;
  247. try {
  248. AppDomain.CurrentDomain.DefineDynamicAssembly (
  249. name,
  250. AssemblyBuilderAccess.Run);
  251. Assert.Fail ("#1:" + invalid_name);
  252. } catch (ArgumentException ex) {
  253. // Assembly names may not begin with whitespace
  254. // or contain the characters '/', '\' or ':'
  255. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  256. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  257. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  258. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  259. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  260. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  261. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  262. }
  263. }
  264. }
  265. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess)
  266. public void DefineDynamicAssembly1_Name_Null ()
  267. {
  268. try {
  269. AppDomain.CurrentDomain.DefineDynamicAssembly (
  270. (AssemblyName) null,
  271. AssemblyBuilderAccess.Run);
  272. Assert.Fail ("#A1");
  273. } catch (ArgumentNullException ex) {
  274. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  275. Assert.IsNull (ex.InnerException, "#A3");
  276. Assert.IsNotNull (ex.Message, "#A4");
  277. Assert.IsNotNull (ex.ParamName, "#A5");
  278. Assert.AreEqual ("name", ex.ParamName, "#A6");
  279. }
  280. AssemblyName name = new AssemblyName ();
  281. try {
  282. AppDomain.CurrentDomain.DefineDynamicAssembly (
  283. name,
  284. AssemblyBuilderAccess.Run);
  285. Assert.Fail ("#B1");
  286. } catch (ArgumentException ex) {
  287. // AssemblyName.Name cannot be null or an empty string
  288. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  289. Assert.IsNull (ex.InnerException, "#B3");
  290. Assert.IsNotNull (ex.Message, "#B4");
  291. Assert.IsNull (ex.ParamName, "#B5");
  292. }
  293. name.Name = string.Empty;
  294. try {
  295. AppDomain.CurrentDomain.DefineDynamicAssembly (
  296. name,
  297. AssemblyBuilderAccess.Run);
  298. Assert.Fail ("#C1");
  299. } catch (ArgumentException ex) {
  300. // AssemblyName.Name cannot be null or an empty string
  301. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  302. Assert.IsNull (ex.InnerException, "#C3");
  303. Assert.IsNotNull (ex.Message, "#C4");
  304. Assert.IsNull (ex.ParamName, "#C5");
  305. }
  306. }
  307. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence)
  308. public void DefineDynamicAssembly2_Access_Invalid ()
  309. {
  310. AssemblyName name = new AssemblyName ();
  311. name.Name = "DefineDynamicAssembly2";
  312. #if NET_2_0
  313. try {
  314. AppDomain.CurrentDomain.DefineDynamicAssembly (
  315. name, AssemblyBuilderAccess.Run |
  316. (AssemblyBuilderAccess) 666,
  317. AppDomain.CurrentDomain.Evidence);
  318. Assert.Fail ("#1");
  319. } catch (ArgumentException ex) {
  320. // Illegal enum value: 667
  321. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  322. Assert.IsNull (ex.InnerException, "#3");
  323. Assert.IsNotNull (ex.Message, "#4");
  324. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  325. Assert.IsNotNull (ex.ParamName, "#6");
  326. Assert.AreEqual ("access", ex.ParamName, "#7");
  327. }
  328. #else
  329. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  330. name, AssemblyBuilderAccess.Run |
  331. (AssemblyBuilderAccess) 666,
  332. AppDomain.CurrentDomain.Evidence);
  333. Assert.IsNotNull (ab, "#1");
  334. #endif
  335. }
  336. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence)
  337. public void DefineDynamicAssembly2_Name_InvalidChars ()
  338. {
  339. string [] invalid_char_names = new string [] {
  340. "\tAB",
  341. " AB",
  342. "\rAB",
  343. "A/B",
  344. ":AB",
  345. "B:A",
  346. "B\\A",
  347. "BA\\"};
  348. AssemblyName name = new AssemblyName ();
  349. foreach (string invalid_name in invalid_char_names) {
  350. name.Name = invalid_name;
  351. try {
  352. AppDomain.CurrentDomain.DefineDynamicAssembly (
  353. name,
  354. AssemblyBuilderAccess.Run,
  355. AppDomain.CurrentDomain.Evidence);
  356. Assert.Fail ("#1:" + invalid_name);
  357. } catch (ArgumentException ex) {
  358. // Assembly names may not begin with whitespace
  359. // or contain the characters '/', '\' or ':'
  360. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  361. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  362. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  363. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  364. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  365. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  366. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  367. }
  368. }
  369. }
  370. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence)
  371. public void DefineDynamicAssembly2_Name_Null ()
  372. {
  373. try {
  374. AppDomain.CurrentDomain.DefineDynamicAssembly (
  375. (AssemblyName) null,
  376. AssemblyBuilderAccess.Run,
  377. AppDomain.CurrentDomain.Evidence);
  378. Assert.Fail ("#A1");
  379. } catch (ArgumentNullException ex) {
  380. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  381. Assert.IsNull (ex.InnerException, "#A3");
  382. Assert.IsNotNull (ex.Message, "#A4");
  383. Assert.IsNotNull (ex.ParamName, "#A5");
  384. Assert.AreEqual ("name", ex.ParamName, "#A6");
  385. }
  386. AssemblyName name = new AssemblyName ();
  387. try {
  388. AppDomain.CurrentDomain.DefineDynamicAssembly (
  389. name,
  390. AssemblyBuilderAccess.Run,
  391. AppDomain.CurrentDomain.Evidence);
  392. Assert.Fail ("#B1");
  393. } catch (ArgumentException ex) {
  394. // AssemblyName.Name cannot be null or an empty string
  395. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  396. Assert.IsNull (ex.InnerException, "#B3");
  397. Assert.IsNotNull (ex.Message, "#B4");
  398. Assert.IsNull (ex.ParamName, "#B5");
  399. }
  400. name.Name = string.Empty;
  401. try {
  402. AppDomain.CurrentDomain.DefineDynamicAssembly (
  403. name,
  404. AssemblyBuilderAccess.Run,
  405. AppDomain.CurrentDomain.Evidence);
  406. Assert.Fail ("#C1");
  407. } catch (ArgumentException ex) {
  408. // AssemblyName.Name cannot be null or an empty string
  409. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  410. Assert.IsNull (ex.InnerException, "#C3");
  411. Assert.IsNotNull (ex.Message, "#C4");
  412. Assert.IsNull (ex.ParamName, "#C5");
  413. }
  414. }
  415. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String)
  416. public void DefineDynamicAssembly3_Access_Invalid ()
  417. {
  418. AssemblyName name = new AssemblyName ();
  419. name.Name = "DefineDynamicAssembly3";
  420. #if NET_2_0
  421. try {
  422. AppDomain.CurrentDomain.DefineDynamicAssembly (
  423. name, AssemblyBuilderAccess.Run |
  424. (AssemblyBuilderAccess) 666,
  425. Path.GetTempPath ());
  426. Assert.Fail ("#1");
  427. } catch (ArgumentException ex) {
  428. // Illegal enum value: 667
  429. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  430. Assert.IsNull (ex.InnerException, "#3");
  431. Assert.IsNotNull (ex.Message, "#4");
  432. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  433. Assert.IsNotNull (ex.ParamName, "#6");
  434. Assert.AreEqual ("access", ex.ParamName, "#7");
  435. }
  436. #else
  437. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  438. name, AssemblyBuilderAccess.Run |
  439. (AssemblyBuilderAccess) 666,
  440. Path.GetTempPath ());
  441. Assert.IsNotNull (ab, "#1");
  442. #endif
  443. }
  444. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String)
  445. public void DefineDynamicAssembly3_Name_InvalidChars ()
  446. {
  447. string [] invalid_char_names = new string [] {
  448. "\tAB",
  449. " AB",
  450. "\rAB",
  451. "A/B",
  452. ":AB",
  453. "B:A",
  454. "B\\A",
  455. "BA\\"};
  456. AssemblyName name = new AssemblyName ();
  457. foreach (string invalid_name in invalid_char_names) {
  458. name.Name = invalid_name;
  459. try {
  460. AppDomain.CurrentDomain.DefineDynamicAssembly (
  461. name,
  462. AssemblyBuilderAccess.Run,
  463. Path.GetTempPath ());
  464. Assert.Fail ("#1:" + invalid_name);
  465. } catch (ArgumentException ex) {
  466. // Assembly names may not begin with whitespace
  467. // or contain the characters '/', '\' or ':'
  468. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  469. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  470. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  471. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  472. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  473. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  474. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  475. }
  476. }
  477. }
  478. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String)
  479. public void DefineDynamicAssembly3_Name_Null ()
  480. {
  481. try {
  482. AppDomain.CurrentDomain.DefineDynamicAssembly (
  483. (AssemblyName) null,
  484. AssemblyBuilderAccess.Run,
  485. Path.GetTempPath ());
  486. Assert.Fail ("#A1");
  487. } catch (ArgumentNullException ex) {
  488. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  489. Assert.IsNull (ex.InnerException, "#A3");
  490. Assert.IsNotNull (ex.Message, "#A4");
  491. Assert.IsNotNull (ex.ParamName, "#A5");
  492. Assert.AreEqual ("name", ex.ParamName, "#A6");
  493. }
  494. AssemblyName name = new AssemblyName ();
  495. try {
  496. AppDomain.CurrentDomain.DefineDynamicAssembly (
  497. name,
  498. AssemblyBuilderAccess.Run,
  499. Path.GetTempPath ());
  500. Assert.Fail ("#B1");
  501. } catch (ArgumentException ex) {
  502. // AssemblyName.Name cannot be null or an empty string
  503. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  504. Assert.IsNull (ex.InnerException, "#B3");
  505. Assert.IsNotNull (ex.Message, "#B4");
  506. Assert.IsNull (ex.ParamName, "#B5");
  507. }
  508. name.Name = string.Empty;
  509. try {
  510. AppDomain.CurrentDomain.DefineDynamicAssembly (
  511. name,
  512. AssemblyBuilderAccess.Run,
  513. Path.GetTempPath ());
  514. Assert.Fail ("#C1");
  515. } catch (ArgumentException ex) {
  516. // AssemblyName.Name cannot be null or an empty string
  517. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  518. Assert.IsNull (ex.InnerException, "#C3");
  519. Assert.IsNotNull (ex.Message, "#C4");
  520. Assert.IsNull (ex.ParamName, "#C5");
  521. }
  522. }
  523. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence)
  524. public void DefineDynamicAssembly4_Access_Invalid ()
  525. {
  526. AssemblyName name = new AssemblyName ();
  527. name.Name = "DefineDynamicAssembly4";
  528. #if NET_2_0
  529. try {
  530. AppDomain.CurrentDomain.DefineDynamicAssembly (
  531. name, AssemblyBuilderAccess.Run |
  532. (AssemblyBuilderAccess) 666,
  533. Path.GetTempPath (),
  534. AppDomain.CurrentDomain.Evidence);
  535. Assert.Fail ("#1");
  536. } catch (ArgumentException ex) {
  537. // Illegal enum value: 667
  538. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  539. Assert.IsNull (ex.InnerException, "#3");
  540. Assert.IsNotNull (ex.Message, "#4");
  541. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  542. Assert.IsNotNull (ex.ParamName, "#6");
  543. Assert.AreEqual ("access", ex.ParamName, "#7");
  544. }
  545. #else
  546. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  547. name, AssemblyBuilderAccess.Run |
  548. (AssemblyBuilderAccess) 666,
  549. Path.GetTempPath (),
  550. AppDomain.CurrentDomain.Evidence);
  551. Assert.IsNotNull (ab, "#1");
  552. #endif
  553. }
  554. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence)
  555. public void DefineDynamicAssembly4_Name_InvalidChars ()
  556. {
  557. string [] invalid_char_names = new string [] {
  558. "\tAB",
  559. " AB",
  560. "\rAB",
  561. "A/B",
  562. ":AB",
  563. "B:A",
  564. "B\\A",
  565. "BA\\"};
  566. AssemblyName name = new AssemblyName ();
  567. foreach (string invalid_name in invalid_char_names) {
  568. name.Name = invalid_name;
  569. try {
  570. AppDomain.CurrentDomain.DefineDynamicAssembly (
  571. name,
  572. AssemblyBuilderAccess.Run,
  573. Path.GetTempPath (),
  574. AppDomain.CurrentDomain.Evidence);
  575. Assert.Fail ("#1:" + invalid_name);
  576. } catch (ArgumentException ex) {
  577. // Assembly names may not begin with whitespace
  578. // or contain the characters '/', '\' or ':'
  579. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  580. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  581. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  582. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  583. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  584. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  585. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  586. }
  587. }
  588. }
  589. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence)
  590. public void DefineDynamicAssembly4_Name_Null ()
  591. {
  592. try {
  593. AppDomain.CurrentDomain.DefineDynamicAssembly (
  594. (AssemblyName) null,
  595. AssemblyBuilderAccess.Run,
  596. Path.GetTempPath (),
  597. AppDomain.CurrentDomain.Evidence);
  598. Assert.Fail ("#A1");
  599. } catch (ArgumentNullException ex) {
  600. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  601. Assert.IsNull (ex.InnerException, "#A3");
  602. Assert.IsNotNull (ex.Message, "#A4");
  603. Assert.IsNotNull (ex.ParamName, "#A5");
  604. Assert.AreEqual ("name", ex.ParamName, "#A6");
  605. }
  606. AssemblyName name = new AssemblyName ();
  607. try {
  608. AppDomain.CurrentDomain.DefineDynamicAssembly (
  609. name,
  610. AssemblyBuilderAccess.Run,
  611. Path.GetTempPath (),
  612. AppDomain.CurrentDomain.Evidence);
  613. Assert.Fail ("#B1");
  614. } catch (ArgumentException ex) {
  615. // AssemblyName.Name cannot be null or an empty string
  616. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  617. Assert.IsNull (ex.InnerException, "#B3");
  618. Assert.IsNotNull (ex.Message, "#B4");
  619. Assert.IsNull (ex.ParamName, "#B5");
  620. }
  621. name.Name = string.Empty;
  622. try {
  623. AppDomain.CurrentDomain.DefineDynamicAssembly (
  624. name,
  625. AssemblyBuilderAccess.Run,
  626. Path.GetTempPath (),
  627. AppDomain.CurrentDomain.Evidence);
  628. Assert.Fail ("#C1");
  629. } catch (ArgumentException ex) {
  630. // AssemblyName.Name cannot be null or an empty string
  631. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  632. Assert.IsNull (ex.InnerException, "#C3");
  633. Assert.IsNotNull (ex.Message, "#C4");
  634. Assert.IsNull (ex.ParamName, "#C5");
  635. }
  636. }
  637. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)
  638. public void DefineDynamicAssembly5_Access_Invalid ()
  639. {
  640. AssemblyName name = new AssemblyName ();
  641. name.Name = "DefineDynamicAssembly5";
  642. #if NET_2_0
  643. try {
  644. AppDomain.CurrentDomain.DefineDynamicAssembly (
  645. name, AssemblyBuilderAccess.Run |
  646. (AssemblyBuilderAccess) 666,
  647. (PermissionSet) null,
  648. (PermissionSet) null,
  649. (PermissionSet) null);
  650. Assert.Fail ("#1");
  651. } catch (ArgumentException ex) {
  652. // Illegal enum value: 667
  653. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  654. Assert.IsNull (ex.InnerException, "#3");
  655. Assert.IsNotNull (ex.Message, "#4");
  656. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  657. Assert.IsNotNull (ex.ParamName, "#6");
  658. Assert.AreEqual ("access", ex.ParamName, "#7");
  659. }
  660. #else
  661. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  662. name, AssemblyBuilderAccess.Run |
  663. (AssemblyBuilderAccess) 666,
  664. (PermissionSet) null,
  665. (PermissionSet) null,
  666. (PermissionSet) null);
  667. Assert.IsNotNull (ab, "#1");
  668. #endif
  669. }
  670. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)
  671. public void DefineDynamicAssembly5_Name_InvalidChars ()
  672. {
  673. string [] invalid_char_names = new string [] {
  674. "\tAB",
  675. " AB",
  676. "\rAB",
  677. "A/B",
  678. ":AB",
  679. "B:A",
  680. "B\\A",
  681. "BA\\"};
  682. AssemblyName name = new AssemblyName ();
  683. foreach (string invalid_name in invalid_char_names) {
  684. name.Name = invalid_name;
  685. try {
  686. AppDomain.CurrentDomain.DefineDynamicAssembly (
  687. name,
  688. AssemblyBuilderAccess.Run,
  689. (PermissionSet) null,
  690. (PermissionSet) null,
  691. (PermissionSet) null);
  692. Assert.Fail ("#1:" + invalid_name);
  693. } catch (ArgumentException ex) {
  694. // Assembly names may not begin with whitespace
  695. // or contain the characters '/', '\' or ':'
  696. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  697. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  698. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  699. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  700. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  701. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  702. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  703. }
  704. }
  705. }
  706. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, PermissionSet, PermissionSet, PermissionSet)
  707. public void DefineDynamicAssembly5_Name_Null ()
  708. {
  709. try {
  710. AppDomain.CurrentDomain.DefineDynamicAssembly (
  711. (AssemblyName) null,
  712. AssemblyBuilderAccess.Run,
  713. (PermissionSet) null,
  714. (PermissionSet) null,
  715. (PermissionSet) null);
  716. Assert.Fail ("#A1");
  717. } catch (ArgumentNullException ex) {
  718. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  719. Assert.IsNull (ex.InnerException, "#A3");
  720. Assert.IsNotNull (ex.Message, "#A4");
  721. Assert.IsNotNull (ex.ParamName, "#A5");
  722. Assert.AreEqual ("name", ex.ParamName, "#A6");
  723. }
  724. AssemblyName name = new AssemblyName ();
  725. try {
  726. AppDomain.CurrentDomain.DefineDynamicAssembly (
  727. name,
  728. AssemblyBuilderAccess.Run,
  729. (PermissionSet) null,
  730. (PermissionSet) null,
  731. (PermissionSet) null);
  732. Assert.Fail ("#B1");
  733. } catch (ArgumentException ex) {
  734. // AssemblyName.Name cannot be null or an empty string
  735. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  736. Assert.IsNull (ex.InnerException, "#B3");
  737. Assert.IsNotNull (ex.Message, "#B4");
  738. Assert.IsNull (ex.ParamName, "#B5");
  739. }
  740. name.Name = string.Empty;
  741. try {
  742. AppDomain.CurrentDomain.DefineDynamicAssembly (
  743. name,
  744. AssemblyBuilderAccess.Run,
  745. (PermissionSet) null,
  746. (PermissionSet) null,
  747. (PermissionSet) null);
  748. Assert.Fail ("#C1");
  749. } catch (ArgumentException ex) {
  750. // AssemblyName.Name cannot be null or an empty string
  751. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  752. Assert.IsNull (ex.InnerException, "#C3");
  753. Assert.IsNotNull (ex.Message, "#C4");
  754. Assert.IsNull (ex.ParamName, "#C5");
  755. }
  756. }
  757. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
  758. public void DefineDynamicAssembly6_Access_Invalid ()
  759. {
  760. AssemblyName name = new AssemblyName ();
  761. name.Name = "DefineDynamicAssembly6";
  762. #if NET_2_0
  763. try {
  764. AppDomain.CurrentDomain.DefineDynamicAssembly (
  765. name, AssemblyBuilderAccess.Run |
  766. (AssemblyBuilderAccess) 666,
  767. AppDomain.CurrentDomain.Evidence,
  768. (PermissionSet) null,
  769. (PermissionSet) null,
  770. (PermissionSet) null);
  771. Assert.Fail ("#1");
  772. } catch (ArgumentException ex) {
  773. // Illegal enum value: 667
  774. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  775. Assert.IsNull (ex.InnerException, "#3");
  776. Assert.IsNotNull (ex.Message, "#4");
  777. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  778. Assert.IsNotNull (ex.ParamName, "#6");
  779. Assert.AreEqual ("access", ex.ParamName, "#7");
  780. }
  781. #else
  782. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  783. name, AssemblyBuilderAccess.Run |
  784. (AssemblyBuilderAccess) 666,
  785. AppDomain.CurrentDomain.Evidence,
  786. (PermissionSet) null,
  787. (PermissionSet) null,
  788. (PermissionSet) null);
  789. Assert.IsNotNull (ab, "#1");
  790. #endif
  791. }
  792. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
  793. public void DefineDynamicAssembly6_Name_InvalidChars ()
  794. {
  795. string [] invalid_char_names = new string [] {
  796. "\tAB",
  797. " AB",
  798. "\rAB",
  799. "A/B",
  800. ":AB",
  801. "B:A",
  802. "B\\A",
  803. "BA\\"};
  804. AssemblyName name = new AssemblyName ();
  805. foreach (string invalid_name in invalid_char_names) {
  806. name.Name = invalid_name;
  807. try {
  808. AppDomain.CurrentDomain.DefineDynamicAssembly (
  809. name,
  810. AssemblyBuilderAccess.Run,
  811. AppDomain.CurrentDomain.Evidence,
  812. (PermissionSet) null,
  813. (PermissionSet) null,
  814. (PermissionSet) null);
  815. Assert.Fail ("#1:" + invalid_name);
  816. } catch (ArgumentException ex) {
  817. // Assembly names may not begin with whitespace
  818. // or contain the characters '/', '\' or ':'
  819. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  820. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  821. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  822. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  823. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  824. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  825. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  826. }
  827. }
  828. }
  829. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, Evidence, PermissionSet, PermissionSet, PermissionSet)
  830. public void DefineDynamicAssembly6_Name_Null ()
  831. {
  832. try {
  833. AppDomain.CurrentDomain.DefineDynamicAssembly (
  834. (AssemblyName) null,
  835. AssemblyBuilderAccess.Run,
  836. AppDomain.CurrentDomain.Evidence,
  837. (PermissionSet) null,
  838. (PermissionSet) null,
  839. (PermissionSet) null);
  840. Assert.Fail ("#A1");
  841. } catch (ArgumentNullException ex) {
  842. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  843. Assert.IsNull (ex.InnerException, "#A3");
  844. Assert.IsNotNull (ex.Message, "#A4");
  845. Assert.IsNotNull (ex.ParamName, "#A5");
  846. Assert.AreEqual ("name", ex.ParamName, "#A6");
  847. }
  848. AssemblyName name = new AssemblyName ();
  849. try {
  850. AppDomain.CurrentDomain.DefineDynamicAssembly (
  851. name,
  852. AssemblyBuilderAccess.Run,
  853. AppDomain.CurrentDomain.Evidence,
  854. (PermissionSet) null,
  855. (PermissionSet) null,
  856. (PermissionSet) null);
  857. Assert.Fail ("#B1");
  858. } catch (ArgumentException ex) {
  859. // AssemblyName.Name cannot be null or an empty string
  860. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  861. Assert.IsNull (ex.InnerException, "#B3");
  862. Assert.IsNotNull (ex.Message, "#B4");
  863. Assert.IsNull (ex.ParamName, "#B5");
  864. }
  865. name.Name = string.Empty;
  866. try {
  867. AppDomain.CurrentDomain.DefineDynamicAssembly (
  868. name,
  869. AssemblyBuilderAccess.Run,
  870. AppDomain.CurrentDomain.Evidence,
  871. (PermissionSet) null,
  872. (PermissionSet) null,
  873. (PermissionSet) null);
  874. Assert.Fail ("#C1");
  875. } catch (ArgumentException ex) {
  876. // AssemblyName.Name cannot be null or an empty string
  877. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  878. Assert.IsNull (ex.InnerException, "#C3");
  879. Assert.IsNotNull (ex.Message, "#C4");
  880. Assert.IsNull (ex.ParamName, "#C5");
  881. }
  882. }
  883. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
  884. public void DefineDynamicAssembly7_Access_Invalid ()
  885. {
  886. AssemblyName name = new AssemblyName ();
  887. name.Name = "DefineDynamicAssembly7";
  888. #if NET_2_0
  889. try {
  890. AppDomain.CurrentDomain.DefineDynamicAssembly (
  891. name, AssemblyBuilderAccess.Run |
  892. (AssemblyBuilderAccess) 666,
  893. Path.GetTempPath (),
  894. (PermissionSet) null,
  895. (PermissionSet) null,
  896. (PermissionSet) null);
  897. Assert.Fail ("#1");
  898. } catch (ArgumentException ex) {
  899. // Illegal enum value: 667
  900. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  901. Assert.IsNull (ex.InnerException, "#3");
  902. Assert.IsNotNull (ex.Message, "#4");
  903. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  904. Assert.IsNotNull (ex.ParamName, "#6");
  905. Assert.AreEqual ("access", ex.ParamName, "#7");
  906. }
  907. #else
  908. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  909. name, AssemblyBuilderAccess.Run |
  910. (AssemblyBuilderAccess) 666,
  911. Path.GetTempPath (),
  912. (PermissionSet) null,
  913. (PermissionSet) null,
  914. (PermissionSet) null);
  915. Assert.IsNotNull (ab, "#1");
  916. #endif
  917. }
  918. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
  919. public void DefineDynamicAssembly7_Name_InvalidChars ()
  920. {
  921. string [] invalid_char_names = new string [] {
  922. "\tAB",
  923. " AB",
  924. "\rAB",
  925. "A/B",
  926. ":AB",
  927. "B:A",
  928. "B\\A",
  929. "BA\\"};
  930. AssemblyName name = new AssemblyName ();
  931. foreach (string invalid_name in invalid_char_names) {
  932. name.Name = invalid_name;
  933. try {
  934. AppDomain.CurrentDomain.DefineDynamicAssembly (
  935. name,
  936. AssemblyBuilderAccess.Run,
  937. Path.GetTempPath (),
  938. (PermissionSet) null,
  939. (PermissionSet) null,
  940. (PermissionSet) null);
  941. Assert.Fail ("#1:" + invalid_name);
  942. } catch (ArgumentException ex) {
  943. // Assembly names may not begin with whitespace
  944. // or contain the characters '/', '\' or ':'
  945. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  946. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  947. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  948. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  949. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  950. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  951. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  952. }
  953. }
  954. }
  955. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, PermissionSet, PermissionSet, PermissionSet)
  956. public void DefineDynamicAssembly7_Name_Null ()
  957. {
  958. try {
  959. AppDomain.CurrentDomain.DefineDynamicAssembly (
  960. (AssemblyName) null,
  961. AssemblyBuilderAccess.Run,
  962. Path.GetTempPath (),
  963. (PermissionSet) null,
  964. (PermissionSet) null,
  965. (PermissionSet) null);
  966. Assert.Fail ("#A1");
  967. } catch (ArgumentNullException ex) {
  968. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  969. Assert.IsNull (ex.InnerException, "#A3");
  970. Assert.IsNotNull (ex.Message, "#A4");
  971. Assert.IsNotNull (ex.ParamName, "#A5");
  972. Assert.AreEqual ("name", ex.ParamName, "#A6");
  973. }
  974. AssemblyName name = new AssemblyName ();
  975. try {
  976. AppDomain.CurrentDomain.DefineDynamicAssembly (
  977. name,
  978. AssemblyBuilderAccess.Run,
  979. Path.GetTempPath (),
  980. (PermissionSet) null,
  981. (PermissionSet) null,
  982. (PermissionSet) null);
  983. Assert.Fail ("#B1");
  984. } catch (ArgumentException ex) {
  985. // AssemblyName.Name cannot be null or an empty string
  986. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  987. Assert.IsNull (ex.InnerException, "#B3");
  988. Assert.IsNotNull (ex.Message, "#B4");
  989. Assert.IsNull (ex.ParamName, "#B5");
  990. }
  991. name.Name = string.Empty;
  992. try {
  993. AppDomain.CurrentDomain.DefineDynamicAssembly (
  994. name,
  995. AssemblyBuilderAccess.Run,
  996. Path.GetTempPath (),
  997. (PermissionSet) null,
  998. (PermissionSet) null,
  999. (PermissionSet) null);
  1000. Assert.Fail ("#C1");
  1001. } catch (ArgumentException ex) {
  1002. // AssemblyName.Name cannot be null or an empty string
  1003. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  1004. Assert.IsNull (ex.InnerException, "#C3");
  1005. Assert.IsNotNull (ex.Message, "#C4");
  1006. Assert.IsNull (ex.ParamName, "#C5");
  1007. }
  1008. }
  1009. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)
  1010. public void DefineDynamicAssembly8_Access_Invalid ()
  1011. {
  1012. AssemblyName name = new AssemblyName ();
  1013. name.Name = "DefineDynamicAssembly8";
  1014. #if NET_2_0
  1015. try {
  1016. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1017. name, AssemblyBuilderAccess.Run |
  1018. (AssemblyBuilderAccess) 666,
  1019. Path.GetTempPath (),
  1020. AppDomain.CurrentDomain.Evidence,
  1021. (PermissionSet) null,
  1022. (PermissionSet) null,
  1023. (PermissionSet) null);
  1024. Assert.Fail ("#1");
  1025. } catch (ArgumentException ex) {
  1026. // Illegal enum value: 667
  1027. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1028. Assert.IsNull (ex.InnerException, "#3");
  1029. Assert.IsNotNull (ex.Message, "#4");
  1030. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  1031. Assert.IsNotNull (ex.ParamName, "#6");
  1032. Assert.AreEqual ("access", ex.ParamName, "#7");
  1033. }
  1034. #else
  1035. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1036. name, AssemblyBuilderAccess.Run |
  1037. (AssemblyBuilderAccess) 666,
  1038. Path.GetTempPath (),
  1039. AppDomain.CurrentDomain.Evidence,
  1040. (PermissionSet) null,
  1041. (PermissionSet) null,
  1042. (PermissionSet) null);
  1043. Assert.IsNotNull (ab, "#1");
  1044. #endif
  1045. }
  1046. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)
  1047. public void DefineDynamicAssembly8_Name_InvalidChars ()
  1048. {
  1049. string [] invalid_char_names = new string [] {
  1050. "\tAB",
  1051. " AB",
  1052. "\rAB",
  1053. "A/B",
  1054. ":AB",
  1055. "B:A",
  1056. "B\\A",
  1057. "BA\\"};
  1058. AssemblyName name = new AssemblyName ();
  1059. foreach (string invalid_name in invalid_char_names) {
  1060. name.Name = invalid_name;
  1061. try {
  1062. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1063. name,
  1064. AssemblyBuilderAccess.Run,
  1065. Path.GetTempPath (),
  1066. AppDomain.CurrentDomain.Evidence,
  1067. (PermissionSet) null,
  1068. (PermissionSet) null,
  1069. (PermissionSet) null);
  1070. Assert.Fail ("#1:" + invalid_name);
  1071. } catch (ArgumentException ex) {
  1072. // Assembly names may not begin with whitespace
  1073. // or contain the characters '/', '\' or ':'
  1074. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  1075. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  1076. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  1077. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  1078. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  1079. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  1080. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  1081. }
  1082. }
  1083. }
  1084. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet)
  1085. public void DefineDynamicAssembly8_Name_Null ()
  1086. {
  1087. try {
  1088. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1089. (AssemblyName) null,
  1090. AssemblyBuilderAccess.Run,
  1091. Path.GetTempPath (),
  1092. AppDomain.CurrentDomain.Evidence,
  1093. (PermissionSet) null,
  1094. (PermissionSet) null,
  1095. (PermissionSet) null);
  1096. Assert.Fail ("#A1");
  1097. } catch (ArgumentNullException ex) {
  1098. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  1099. Assert.IsNull (ex.InnerException, "#A3");
  1100. Assert.IsNotNull (ex.Message, "#A4");
  1101. Assert.IsNotNull (ex.ParamName, "#A5");
  1102. Assert.AreEqual ("name", ex.ParamName, "#A6");
  1103. }
  1104. AssemblyName name = new AssemblyName ();
  1105. try {
  1106. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1107. name,
  1108. AssemblyBuilderAccess.Run,
  1109. Path.GetTempPath (),
  1110. AppDomain.CurrentDomain.Evidence,
  1111. (PermissionSet) null,
  1112. (PermissionSet) null,
  1113. (PermissionSet) null);
  1114. Assert.Fail ("#B1");
  1115. } catch (ArgumentException ex) {
  1116. // AssemblyName.Name cannot be null or an empty string
  1117. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  1118. Assert.IsNull (ex.InnerException, "#B3");
  1119. Assert.IsNotNull (ex.Message, "#B4");
  1120. Assert.IsNull (ex.ParamName, "#B5");
  1121. }
  1122. name.Name = string.Empty;
  1123. try {
  1124. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1125. name,
  1126. AssemblyBuilderAccess.Run,
  1127. Path.GetTempPath (),
  1128. AppDomain.CurrentDomain.Evidence,
  1129. (PermissionSet) null,
  1130. (PermissionSet) null,
  1131. (PermissionSet) null);
  1132. Assert.Fail ("#C1");
  1133. } catch (ArgumentException ex) {
  1134. // AssemblyName.Name cannot be null or an empty string
  1135. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  1136. Assert.IsNull (ex.InnerException, "#C3");
  1137. Assert.IsNotNull (ex.Message, "#C4");
  1138. Assert.IsNull (ex.ParamName, "#C5");
  1139. }
  1140. }
  1141. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
  1142. public void DefineDynamicAssembly9_Access_Invalid ()
  1143. {
  1144. AssemblyName name = new AssemblyName ();
  1145. name.Name = "DefineDynamicAssembly9";
  1146. #if NET_2_0
  1147. try {
  1148. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1149. name, AssemblyBuilderAccess.Run |
  1150. (AssemblyBuilderAccess) 666,
  1151. Path.GetTempPath (),
  1152. AppDomain.CurrentDomain.Evidence,
  1153. (PermissionSet) null,
  1154. (PermissionSet) null,
  1155. (PermissionSet) null,
  1156. true);
  1157. Assert.Fail ("#1");
  1158. } catch (ArgumentException ex) {
  1159. // Illegal enum value: 667
  1160. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1161. Assert.IsNull (ex.InnerException, "#3");
  1162. Assert.IsNotNull (ex.Message, "#4");
  1163. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  1164. Assert.IsNotNull (ex.ParamName, "#6");
  1165. Assert.AreEqual ("access", ex.ParamName, "#7");
  1166. }
  1167. #else
  1168. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1169. name, AssemblyBuilderAccess.Run |
  1170. (AssemblyBuilderAccess) 666,
  1171. Path.GetTempPath (),
  1172. AppDomain.CurrentDomain.Evidence,
  1173. (PermissionSet) null,
  1174. (PermissionSet) null,
  1175. (PermissionSet) null,
  1176. true);
  1177. Assert.IsNotNull (ab, "#1");
  1178. #endif
  1179. }
  1180. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
  1181. public void DefineDynamicAssembly9_Name_InvalidChars ()
  1182. {
  1183. string [] invalid_char_names = new string [] {
  1184. "\tAB",
  1185. " AB",
  1186. "\rAB",
  1187. "A/B",
  1188. ":AB",
  1189. "B:A",
  1190. "B\\A",
  1191. "BA\\"};
  1192. AssemblyName name = new AssemblyName ();
  1193. foreach (string invalid_name in invalid_char_names) {
  1194. name.Name = invalid_name;
  1195. try {
  1196. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1197. name,
  1198. AssemblyBuilderAccess.Run,
  1199. Path.GetTempPath (),
  1200. AppDomain.CurrentDomain.Evidence,
  1201. (PermissionSet) null,
  1202. (PermissionSet) null,
  1203. (PermissionSet) null,
  1204. true);
  1205. Assert.Fail ("#1:" + invalid_name);
  1206. } catch (ArgumentException ex) {
  1207. // Assembly names may not begin with whitespace
  1208. // or contain the characters '/', '\' or ':'
  1209. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  1210. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  1211. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  1212. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);
  1213. Assert.IsTrue (ex.Message.IndexOf ("'\\'") != -1, "#6:" + invalid_name);
  1214. Assert.IsTrue (ex.Message.IndexOf ("':'") != -1, "#7:" + invalid_name);
  1215. Assert.IsNull (ex.ParamName, "#8:" + invalid_name);
  1216. }
  1217. }
  1218. }
  1219. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean)
  1220. public void DefineDynamicAssembly9_Name_Null ()
  1221. {
  1222. try {
  1223. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1224. (AssemblyName) null,
  1225. AssemblyBuilderAccess.Run,
  1226. Path.GetTempPath (),
  1227. AppDomain.CurrentDomain.Evidence,
  1228. (PermissionSet) null,
  1229. (PermissionSet) null,
  1230. (PermissionSet) null,
  1231. true);
  1232. Assert.Fail ("#A1");
  1233. } catch (ArgumentNullException ex) {
  1234. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  1235. Assert.IsNull (ex.InnerException, "#A3");
  1236. Assert.IsNotNull (ex.Message, "#A4");
  1237. Assert.IsNotNull (ex.ParamName, "#A5");
  1238. Assert.AreEqual ("name", ex.ParamName, "#A6");
  1239. }
  1240. AssemblyName name = new AssemblyName ();
  1241. try {
  1242. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1243. name,
  1244. AssemblyBuilderAccess.Run,
  1245. Path.GetTempPath (),
  1246. AppDomain.CurrentDomain.Evidence,
  1247. (PermissionSet) null,
  1248. (PermissionSet) null,
  1249. (PermissionSet) null,
  1250. true);
  1251. Assert.Fail ("#B1");
  1252. } catch (ArgumentException ex) {
  1253. // AssemblyName.Name cannot be null or an empty string
  1254. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  1255. Assert.IsNull (ex.InnerException, "#B3");
  1256. Assert.IsNotNull (ex.Message, "#B4");
  1257. Assert.IsNull (ex.ParamName, "#B5");
  1258. }
  1259. name.Name = string.Empty;
  1260. try {
  1261. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1262. name,
  1263. AssemblyBuilderAccess.Run,
  1264. Path.GetTempPath (),
  1265. AppDomain.CurrentDomain.Evidence,
  1266. (PermissionSet) null,
  1267. (PermissionSet) null,
  1268. (PermissionSet) null,
  1269. true);
  1270. Assert.Fail ("#C1");
  1271. } catch (ArgumentException ex) {
  1272. // AssemblyName.Name cannot be null or an empty string
  1273. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  1274. Assert.IsNull (ex.InnerException, "#C3");
  1275. Assert.IsNotNull (ex.Message, "#C4");
  1276. Assert.IsNull (ex.ParamName, "#C5");
  1277. }
  1278. }
  1279. #if NET_2_0
  1280. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)
  1281. public void DefineDynamicAssembly10_Access_Invalid ()
  1282. {
  1283. AssemblyName name = new AssemblyName ();
  1284. name.Name = "DefineDynamicAssembly10";
  1285. #if NET_2_0
  1286. try {
  1287. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1288. name, AssemblyBuilderAccess.Run |
  1289. (AssemblyBuilderAccess) 666,
  1290. Path.GetTempPath (),
  1291. AppDomain.CurrentDomain.Evidence,
  1292. (PermissionSet) null,
  1293. (PermissionSet) null,
  1294. (PermissionSet) null,
  1295. true,
  1296. new List<CustomAttributeBuilder> ());
  1297. Assert.Fail ("#1");
  1298. } catch (ArgumentException ex) {
  1299. // Illegal enum value: 667
  1300. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  1301. Assert.IsNull (ex.InnerException, "#3");
  1302. Assert.IsNotNull (ex.Message, "#4");
  1303. Assert.IsTrue (ex.Message.IndexOf ("667") != -1, "#5");
  1304. Assert.IsNotNull (ex.ParamName, "#6");
  1305. Assert.AreEqual ("access", ex.ParamName, "#7");
  1306. }
  1307. #else
  1308. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1309. name, AssemblyBuilderAccess.Run |
  1310. (AssemblyBuilderAccess) 666,
  1311. Path.GetTempPath (),
  1312. AppDomain.CurrentDomain.Evidence,
  1313. (PermissionSet) null,
  1314. (PermissionSet) null,
  1315. (PermissionSet) null,
  1316. true,
  1317. new List<CustomAttributeBuilder> ());
  1318. Assert.IsNotNull (ab, "#1");
  1319. #endif
  1320. }
  1321. [Test] // DefineDynamicAssembly (AssemblyName, AssemblyBuilderAccess, String, Evidence, PermissionSet, PermissionSet, PermissionSet, Boolean, IEnumerable<CustomAttributeBuilder>)
  1322. public void DefineDynamicAssembly10_Name_InvalidChars ()
  1323. {
  1324. string [] invalid_char_names = new string [] {
  1325. "\tAB",
  1326. " AB",
  1327. "\rAB",
  1328. "A/B",
  1329. ":AB",
  1330. "B:A",
  1331. "B\\A",
  1332. "BA\\"};
  1333. AssemblyName name = new AssemblyName ();
  1334. foreach (string invalid_name in invalid_char_names) {
  1335. name.Name = invalid_name;
  1336. try {
  1337. AppDomain.CurrentDomain.DefineDynamicAssembly (
  1338. name,
  1339. AssemblyBuilderAccess.Run,
  1340. Path.GetTempPath (),
  1341. AppDomain.CurrentDomain.Evidence,
  1342. (PermissionSet) null,
  1343. (PermissionSet) null,
  1344. (PermissionSet) null,
  1345. true,
  1346. new List<CustomAttributeBuilder> ());
  1347. Assert.Fail ("#1:" + invalid_name);
  1348. } catch (ArgumentException ex) {
  1349. // Assembly names may not begin with whitespace
  1350. // or contain the characters '/', '\' or ':'
  1351. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2:" + invalid_name);
  1352. Assert.IsNull (ex.InnerException, "#3:" + invalid_name);
  1353. Assert.IsNotNull (ex.Message, "#4:" + invalid_name);
  1354. Assert.IsTrue (ex.Message.IndexOf ("'/'") != -1, "#5:" + invalid_name);

Large files files are truncated, but you can click here to view the full file