PageRenderTime 43ms CodeModel.GetById 2ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/Test/Mono.Cecil.Tests/ImageReadTests.cs

http://github.com/jbevain/cecil
C# | 253 lines | 214 code | 39 blank | 0 comment | 11 complexity | fa93077855c7d47c2cbf8f12a7ef0c5e MD5 | raw file
  1using System;
  2using System.IO;
  3using System.Linq;
  4
  5using Mono.Cecil;
  6using Mono.Cecil.Cil;
  7using Mono.Cecil.PE;
  8using Mono.Cecil.Metadata;
  9
 10using NUnit.Framework;
 11
 12namespace Mono.Cecil.Tests {
 13
 14	[TestFixture]
 15	public class ImageReadTests : BaseTestFixture {
 16
 17		[Test]
 18		public void ImageSections ()
 19		{
 20			using (var image = GetResourceImage ("hello.exe")) {
 21				Assert.AreEqual (3, image.Sections.Length);
 22				Assert.AreEqual (".text", image.Sections [0].Name);
 23				Assert.AreEqual (".rsrc", image.Sections [1].Name);
 24				Assert.AreEqual (".reloc", image.Sections [2].Name);
 25			}
 26		}
 27
 28		[Test]
 29		public void ImageMetadataVersion ()
 30		{
 31			using (var image = GetResourceImage ("hello.exe"))
 32				Assert.AreEqual (TargetRuntime.Net_2_0, image.RuntimeVersion.ParseRuntime ());
 33
 34			using (var image = GetResourceImage ("hello1.exe"))
 35				Assert.AreEqual (TargetRuntime.Net_1_1, image.RuntimeVersion.ParseRuntime ());
 36		}
 37
 38		[Test]
 39		public void ImageModuleKind ()
 40		{
 41			using (var image = GetResourceImage ("hello.exe"))
 42				Assert.AreEqual (ModuleKind.Console, image.Kind);
 43
 44			using (var image = GetResourceImage ("libhello.dll"))
 45				Assert.AreEqual (ModuleKind.Dll, image.Kind);
 46
 47			using (var image = GetResourceImage ("hellow.exe"))
 48				Assert.AreEqual (ModuleKind.Windows, image.Kind);
 49		}
 50
 51		[Test]
 52		public void MetadataHeaps ()
 53		{
 54			using (var image = GetResourceImage ("hello.exe")) {
 55				Assert.IsNotNull (image.TableHeap);
 56
 57				Assert.IsNotNull (image.StringHeap);
 58				Assert.AreEqual (string.Empty, image.StringHeap.Read (0));
 59				Assert.AreEqual ("<Module>", image.StringHeap.Read (1));
 60
 61				Assert.IsNotNull (image.UserStringHeap);
 62				Assert.AreEqual (string.Empty, image.UserStringHeap.Read (0));
 63				Assert.AreEqual ("Hello Cecil World !", image.UserStringHeap.Read (1));
 64
 65				Assert.IsNotNull (image.GuidHeap);
 66				Assert.AreEqual (new Guid (), image.GuidHeap.Read (0));
 67				Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), image.GuidHeap.Read (1));
 68
 69				Assert.IsNotNull (image.BlobHeap);
 70				Assert.AreEqual (new byte [0], image.BlobHeap.Read (0));
 71			}
 72		}
 73
 74		[Test]
 75		public void TablesHeap ()
 76		{
 77			using (var image = GetResourceImage ("hello.exe")) {
 78				var heap = image.TableHeap;
 79
 80				Assert.IsNotNull (heap);
 81
 82				Assert.AreEqual (1, heap [Table.Module].Length);
 83				Assert.AreEqual (4, heap [Table.TypeRef].Length);
 84				Assert.AreEqual (2, heap [Table.TypeDef].Length);
 85				Assert.AreEqual (0, heap [Table.Field].Length);
 86				Assert.AreEqual (2, heap [Table.Method].Length);
 87				Assert.AreEqual (4, heap [Table.MemberRef].Length);
 88				Assert.AreEqual (2, heap [Table.CustomAttribute].Length);
 89				Assert.AreEqual (1, heap [Table.Assembly].Length);
 90				Assert.AreEqual (1, heap [Table.AssemblyRef].Length);
 91			}
 92		}
 93
 94		[Test]
 95		public void X64Module ()
 96		{
 97			TestModule ("hello.x64.exe", module => {
 98				Assert.AreEqual (TargetArchitecture.AMD64, module.Image.Architecture);
 99				Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
100			}, verify: !Platform.OnMono);
101		}
102
103		[Test]
104		public void X64ModuleTextOnlySection ()
105		{
106			TestModule ("hello.textonly.x64.exe", module => {
107				Assert.AreEqual (TargetArchitecture.AMD64, module.Image.Architecture);
108				Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
109			}, verify: !Platform.OnMono);
110		}
111
112		[Test]
113		public void IA64Module ()
114		{
115			TestModule ("hello.ia64.exe", module => {
116				Assert.AreEqual (TargetArchitecture.IA64, module.Image.Architecture);
117				Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
118			}, verify: !Platform.OnMono);
119		}
120
121		[Test]
122		public void X86Module ()
123		{
124			TestModule ("hello.x86.exe", module => {
125				Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture);
126				Assert.AreEqual (ModuleAttributes.ILOnly | ModuleAttributes.Required32Bit, module.Image.Attributes);
127			});
128		}
129
130		[Test]
131		public void AnyCpuModule ()
132		{
133			TestModule ("hello.anycpu.exe", module => {
134				Assert.AreEqual (TargetArchitecture.I386, module.Image.Architecture);
135				Assert.AreEqual (ModuleAttributes.ILOnly, module.Image.Attributes);
136			});
137		}
138
139		[Test]
140		public void DelaySignedAssembly ()
141		{
142			TestModule ("delay-signed.dll", module => {
143				Assert.IsNotNull (module.Assembly.Name.PublicKey);
144				Assert.AreNotEqual (0, module.Assembly.Name.PublicKey.Length);
145				Assert.AreNotEqual (ModuleAttributes.StrongNameSigned, module.Attributes & ModuleAttributes.StrongNameSigned);
146				Assert.AreNotEqual (0, module.Image.StrongName.VirtualAddress);
147				Assert.AreNotEqual (0, module.Image.StrongName.Size);
148			});
149		}
150
151		[Test]
152		public void WindowsPhoneNonSignedAssembly ()
153		{
154			TestModule ("wp7.dll", module => {
155				Assert.AreEqual (0, module.Assembly.Name.PublicKey.Length);
156				Assert.AreNotEqual (ModuleAttributes.StrongNameSigned, module.Attributes & ModuleAttributes.StrongNameSigned);
157				Assert.AreEqual (0, module.Image.StrongName.VirtualAddress);
158				Assert.AreEqual (0, module.Image.StrongName.Size);
159			}, verify: false);
160		}
161
162		[Test]
163		public void MetroAssembly ()
164		{
165			if (Platform.OnMono)
166				return;
167
168			TestModule ("metro.exe", module => {
169				Assert.AreEqual (ModuleCharacteristics.AppContainer, module.Characteristics & ModuleCharacteristics.AppContainer);
170			}, verify: false);
171		}
172
173#if !NET_CORE
174		[Test]
175		public void WindowsRuntimeComponentAssembly ()
176		{
177			var resolver = WindowsRuntimeAssemblyResolver.CreateInstance ();
178			if (resolver == null)
179				return;
180
181			TestModule("winrtcomp.winmd", module => {
182				Assert.IsTrue (module.Assembly.Name.IsWindowsRuntime);
183				Assert.AreEqual (6, module.Image.SubSystemMajor);
184				Assert.AreEqual (2, module.Image.SubSystemMinor);
185			}, verify: false, assemblyResolver: resolver);
186		}
187#endif
188		[Test]
189		public void DeterministicAssembly ()
190		{
191			TestModule ("Deterministic.dll", module => {
192				Assert.IsTrue (module.HasDebugHeader);
193
194				var header = module.GetDebugHeader ();
195
196				Assert.AreEqual (1, header.Entries.Length);
197				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.Deterministic));
198			});
199		}
200
201		[Test]
202		public void Net471TargetingAssembly ()
203		{
204			TestModule ("net471.exe", module => {
205				Assert.AreEqual (6, module.Image.SubSystemMajor);
206				Assert.AreEqual (0, module.Image.SubSystemMinor);
207			});
208		}
209
210		[Test]
211		public void LocallyScopedConstantArray ()
212		{
213			TestModule ("LocallyScopedConstantArray.dll", module => {
214				Assert.IsTrue (module.HasDebugHeader);
215				var method = module.Types
216					.Single (x => x.Name == "TestClass")
217					.Methods
218					.Single (x => x.Name == "TestMethod");
219				var debugInformation = method.DebugInformation;
220				Assert.IsNull (debugInformation.Scope.Constants.Single ().Value);
221			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
222		}
223
224		[Test]
225		public void ExternalPdbDeterministicAssembly ()
226		{
227			TestModule ("ExternalPdbDeterministic.dll", module => {
228				Assert.IsTrue (module.HasDebugHeader);
229
230				var header = module.GetDebugHeader ();
231
232				Assert.AreEqual (2, header.Entries.Length);
233				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.CodeView));
234				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.Deterministic));
235			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
236		}
237
238		[Test]
239		public void EmbeddedPdbDeterministicAssembly ()
240		{
241			TestModule ("EmbeddedPdbDeterministic.dll", module => {
242				Assert.IsTrue (module.HasDebugHeader);
243
244				var header = module.GetDebugHeader ();
245
246				Assert.AreEqual (3, header.Entries.Length);
247				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.CodeView));
248				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.Deterministic));
249				Assert.IsTrue (header.Entries.Any (e => e.Directory.Type == ImageDebugType.EmbeddedPortablePdb));
250			}, symbolReaderProvider: typeof (EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof (EmbeddedPortablePdbWriterProvider));
251		}
252	}
253}