PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/ModuleTests.cs

http://github.com/jbevain/cecil
C# | 328 lines | 263 code | 63 blank | 2 comment | 3 complexity | 7724840c0dd889a8c74a498046ea78e8 MD5 | raw file
  1using System;
  2using System.Diagnostics;
  3using System.IO;
  4using System.Linq;
  5
  6using Mono.Cecil;
  7
  8using NUnit.Framework;
  9
 10namespace Mono.Cecil.Tests {
 11
 12	[TestFixture]
 13	public class ModuleTests : BaseTestFixture {
 14
 15		[Test]
 16		public void CreateModuleEscapesAssemblyName ()
 17		{
 18			var module = ModuleDefinition.CreateModule ("Test.dll", ModuleKind.Dll);
 19			Assert.AreEqual ("Test", module.Assembly.Name.Name);
 20
 21			module = ModuleDefinition.CreateModule ("Test.exe", ModuleKind.Console);
 22			Assert.AreEqual ("Test", module.Assembly.Name.Name);
 23		}
 24
 25		[Test]
 26		public void SingleModule ()
 27		{
 28			TestModule ("hello.exe", module => {
 29				var assembly = module.Assembly;
 30
 31				Assert.AreEqual (1, assembly.Modules.Count);
 32				Assert.IsNotNull (assembly.MainModule);
 33			});
 34		}
 35
 36		[Test]
 37		public void EntryPoint ()
 38		{
 39			TestModule ("hello.exe", module => {
 40				var entry_point = module.EntryPoint;
 41				Assert.IsNotNull (entry_point);
 42
 43				Assert.AreEqual ("System.Void Program::Main()", entry_point.ToString ());
 44			});
 45		}
 46
 47		[Test]
 48		public void MultiModules ()
 49		{
 50			IgnoreOnCoreClr ();
 51
 52			TestModule("mma.exe", module => {
 53				var assembly = module.Assembly;
 54
 55				Assert.AreEqual (3, assembly.Modules.Count);
 56
 57				Assert.AreEqual ("mma.exe", assembly.Modules [0].Name);
 58				Assert.AreEqual (ModuleKind.Console, assembly.Modules [0].Kind);
 59
 60				Assert.AreEqual ("moda.netmodule", assembly.Modules [1].Name);
 61				Assert.AreEqual ("eedb4721-6c3e-4d9a-be30-49021121dd92", assembly.Modules [1].Mvid.ToString ());
 62				Assert.AreEqual (ModuleKind.NetModule, assembly.Modules [1].Kind);
 63
 64				Assert.AreEqual ("modb.netmodule", assembly.Modules [2].Name);
 65				Assert.AreEqual ("46c5c577-11b2-4ea0-bb3c-3c71f1331dd0", assembly.Modules [2].Mvid.ToString ());
 66				Assert.AreEqual (ModuleKind.NetModule, assembly.Modules [2].Kind);
 67			});
 68		}
 69
 70		[Test]
 71		public void ModuleInformation ()
 72		{
 73			TestModule ("hello.exe", module => {
 74				Assert.IsNotNull (module);
 75
 76				Assert.AreEqual ("hello.exe", module.Name);
 77				Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), module.Mvid);
 78			});
 79		}
 80
 81		[Test]
 82		public void AssemblyReferences ()
 83		{
 84			TestModule ("hello.exe", module => {
 85				Assert.AreEqual (1, module.AssemblyReferences.Count);
 86
 87				var reference = module.AssemblyReferences [0];
 88
 89				Assert.AreEqual ("mscorlib", reference.Name);
 90				Assert.AreEqual (new Version (2, 0, 0, 0), reference.Version);
 91				Assert.AreEqual (new byte [] { 0xB7, 0x7A, 0x5C, 0x56, 0x19, 0x34, 0xE0, 0x89 }, reference.PublicKeyToken);
 92			});
 93		}
 94
 95		[Test]
 96		public void ModuleReferences ()
 97		{
 98			TestModule ("pinvoke.exe", module => {
 99				Assert.AreEqual (2, module.ModuleReferences.Count);
100				Assert.AreEqual ("kernel32.dll", module.ModuleReferences [0].Name);
101				Assert.AreEqual ("shell32.dll", module.ModuleReferences [1].Name);
102			});
103		}
104
105		[Test]
106		public void Types ()
107		{
108			TestModule ("hello.exe", module => {
109				Assert.AreEqual (2, module.Types.Count);
110				Assert.AreEqual ("<Module>", module.Types [0].FullName);
111				Assert.AreEqual ("<Module>", module.GetType ("<Module>").FullName);
112				Assert.AreEqual ("Program", module.Types [1].FullName);
113				Assert.AreEqual ("Program", module.GetType ("Program").FullName);
114			});
115		}
116
117		[Test]
118		public void LinkedResource ()
119		{
120			TestModule ("libres.dll", module => {
121				var resource = module.Resources.Where (res => res.Name == "linked.txt").First () as LinkedResource;
122				Assert.IsNotNull (resource);
123
124				Assert.AreEqual ("linked.txt", resource.Name);
125				Assert.AreEqual ("linked.txt", resource.File);
126				Assert.AreEqual (ResourceType.Linked, resource.ResourceType);
127				Assert.IsTrue (resource.IsPublic);
128			});
129		}
130
131		[Test]
132		public void EmbeddedResource ()
133		{
134			TestModule ("libres.dll", module => {
135				var resource = module.Resources.Where (res => res.Name == "embedded1.txt").First () as EmbeddedResource;
136				Assert.IsNotNull (resource);
137
138				Assert.AreEqual ("embedded1.txt", resource.Name);
139				Assert.AreEqual (ResourceType.Embedded, resource.ResourceType);
140				Assert.IsTrue (resource.IsPublic);
141
142				using (var reader = new StreamReader (resource.GetResourceStream ()))
143				Assert.AreEqual ("Hello", reader.ReadToEnd ());
144
145				resource = module.Resources.Where (res => res.Name == "embedded2.txt").First () as EmbeddedResource;
146				Assert.IsNotNull (resource);
147
148				Assert.AreEqual ("embedded2.txt", resource.Name);
149				Assert.AreEqual (ResourceType.Embedded, resource.ResourceType);
150				Assert.IsTrue (resource.IsPublic);
151
152				using (var reader = new StreamReader (resource.GetResourceStream ()))
153				Assert.AreEqual ("World", reader.ReadToEnd ());
154			});
155		}
156
157		[Test]
158		public void ExportedTypeFromNetModule ()
159		{
160			IgnoreOnCoreClr ();
161
162			TestModule ("mma.exe", module => {
163				Assert.IsTrue (module.HasExportedTypes);
164				Assert.AreEqual (2, module.ExportedTypes.Count);
165
166				var exported_type = module.ExportedTypes [0];
167
168				Assert.AreEqual ("Module.A.Foo", exported_type.FullName);
169				Assert.AreEqual ("moda.netmodule", exported_type.Scope.Name);
170
171				exported_type = module.ExportedTypes [1];
172
173				Assert.AreEqual ("Module.B.Baz", exported_type.FullName);
174				Assert.AreEqual ("modb.netmodule", exported_type.Scope.Name);
175			});
176		}
177
178		[Test]
179		public void NestedTypeForwarder ()
180		{
181			TestCSharp ("CustomAttributes.cs", module => {
182				Assert.IsTrue (module.HasExportedTypes);
183				Assert.AreEqual (2, module.ExportedTypes.Count);
184
185				var exported_type = module.ExportedTypes [0];
186
187				Assert.AreEqual ("System.Diagnostics.DebuggableAttribute", exported_type.FullName);
188				Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", exported_type.Scope.Name);
189				Assert.IsTrue (exported_type.IsForwarder);
190
191				var nested_exported_type = module.ExportedTypes [1];
192
193				Assert.AreEqual ("System.Diagnostics.DebuggableAttribute/DebuggingModes", nested_exported_type.FullName);
194				Assert.AreEqual (exported_type, nested_exported_type.DeclaringType);
195				Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", nested_exported_type.Scope.Name);
196			});
197		}
198
199		[Test]
200		public void HasTypeReference ()
201		{
202			TestCSharp ("CustomAttributes.cs", module => {
203				Assert.IsTrue (module.HasTypeReference ("System.Attribute"));
204				Assert.IsTrue (module.HasTypeReference (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", "System.Attribute"));
205
206				Assert.IsFalse (module.HasTypeReference ("System.Core", "System.Attribute"));
207				Assert.IsFalse (module.HasTypeReference ("System.Linq.Enumerable"));
208			});
209		}
210
211		[Test]
212		public void Win32FileVersion ()
213		{
214			IgnoreOnCoreClr ();
215
216			TestModule ("libhello.dll", module => {
217				var version = FileVersionInfo.GetVersionInfo (module.FileName);
218
219				Assert.AreEqual ("0.0.0.0", version.FileVersion);
220			});
221		}
222
223		[Test]
224		public void ModuleWithoutBlob ()
225		{
226			TestModule ("noblob.dll", module => {
227				Assert.IsNull (module.Image.BlobHeap);
228			});
229		}
230
231		[Test]
232		public void MixedModeModule ()
233		{
234			using (var module = GetResourceModule ("cppcli.dll")) {
235				Assert.AreEqual (1, module.ModuleReferences.Count);
236				Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name);
237			}
238		}
239
240		[Test]
241		public void OpenIrrelevantFile ()
242		{
243			Assert.Throws<BadImageFormatException> (() => GetResourceModule ("text_file.txt"));
244		}
245
246		[Test]
247		public void GetTypeNamespacePlusName ()
248		{
249			using (var module = GetResourceModule ("moda.netmodule")) {
250				var type = module.GetType ("Module.A", "Foo");
251				Assert.IsNotNull (type);
252			}
253		}
254
255		[Test]
256		public void GetNonExistentTypeRuntimeName ()
257		{
258			using (var module = GetResourceModule ("libhello.dll")) {
259				var type = module.GetType ("DoesNotExist", runtimeName: true);
260				Assert.IsNull (type);
261			}
262		}
263
264		[Test]
265		public void OpenModuleImmediate ()
266		{
267			using (var module = GetResourceModule ("hello.exe", ReadingMode.Immediate)) {
268				Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode);
269			}
270		}
271
272		[Test]
273		public void OpenModuleDeferred ()
274		{
275			using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) {
276				Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode);
277			}
278		}
279
280		[Test]
281		public void OwnedStreamModuleFileName ()
282		{
283			var path = GetAssemblyResourcePath ("hello.exe");
284			using (var file = File.Open (path, FileMode.Open))
285			{
286				using (var module = ModuleDefinition.ReadModule (file))
287				{
288					Assert.IsNotNull (module.FileName);
289					Assert.IsNotEmpty (module.FileName);
290					Assert.AreEqual (path, module.FileName);
291				}
292			}
293		}
294
295		[Test]
296		public void ReadAndWriteFile ()
297		{
298			var path = Path.GetTempFileName ();
299
300			var original = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);
301			var type = new TypeDefinition ("Foo", "Foo", TypeAttributes.Abstract | TypeAttributes.Sealed);
302			original.Types.Add (type);
303			original.Write (path);
304
305			using (var module = ModuleDefinition.ReadModule (path, new ReaderParameters { ReadWrite = true })) {
306				module.Write ();
307			}
308
309			using (var module = ModuleDefinition.ReadModule (path))
310				Assert.AreEqual ("Foo.Foo", module.Types [1].FullName);
311		}
312
313		[Test]
314		public void ExceptionInWriteDoesNotKeepLockOnFile ()
315		{
316			var path = Path.GetTempFileName ();
317
318			var module = ModuleDefinition.CreateModule ("FooFoo", ModuleKind.Dll);
319			// Mixed mode module that Cecil can not write
320			module.Attributes = (ModuleAttributes) 0;
321
322			Assert.Throws<NotSupportedException>(() => module.Write (path));
323
324			// Ensure you can still delete the file
325			File.Delete (path);
326		}
327	}
328}