Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PortablePdbTests.cs « Mono.Cecil.Tests « Test - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9786cb373362a245b217dae8ee29091f5d45a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using System;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;

using Mono.Cecil.Cil;
using Mono.Cecil.Pdb;
using Mono.Cecil.PE;

namespace Mono.Cecil.Tests {

	[TestFixture]
	public class PortablePdbTests : BaseTestFixture {

		[Test]
		public void SequencePoints ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.Program");
				var main = type.GetMethod ("Main");

				AssertCode (@"
	.locals init (System.Int32 a, System.String[] V_1, System.Int32 V_2, System.String arg)
	.line 21,21:3,4 'C:\sources\PdbTarget\Program.cs'
	IL_0000: nop
	.line 22,22:4,11 'C:\sources\PdbTarget\Program.cs'
	IL_0001: nop
	.line 22,22:24,28 'C:\sources\PdbTarget\Program.cs'
	IL_0002: ldarg.0
	IL_0003: stloc.1
	IL_0004: ldc.i4.0
	IL_0005: stloc.2
	.line hidden 'C:\sources\PdbTarget\Program.cs'
	IL_0006: br.s IL_0017
	.line 22,22:13,20 'C:\sources\PdbTarget\Program.cs'
	IL_0008: ldloc.1
	IL_0009: ldloc.2
	IL_000a: ldelem.ref
	IL_000b: stloc.3
	.line 23,23:5,20 'C:\sources\PdbTarget\Program.cs'
	IL_000c: ldloc.3
	IL_000d: call System.Void System.Console::WriteLine(System.String)
	IL_0012: nop
	.line hidden 'C:\sources\PdbTarget\Program.cs'
	IL_0013: ldloc.2
	IL_0014: ldc.i4.1
	IL_0015: add
	IL_0016: stloc.2
	.line 22,22:21,23 'C:\sources\PdbTarget\Program.cs'
	IL_0017: ldloc.2
	IL_0018: ldloc.1
	IL_0019: ldlen
	IL_001a: conv.i4
	IL_001b: blt.s IL_0008
	.line 25,25:4,22 'C:\sources\PdbTarget\Program.cs'
	IL_001d: ldc.i4.1
	IL_001e: ldc.i4.2
	IL_001f: call System.Int32 System.Math::Min(System.Int32,System.Int32)
	IL_0024: stloc.0
	.line 26,26:3,4 'C:\sources\PdbTarget\Program.cs'
	IL_0025: ret
", main);
			});
		}

		[Test]
		public void SequencePointsMultipleDocument ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.B");
				var main = type.GetMethod (".ctor");

				AssertCode (@"
	.locals ()
	.line 7,7:3,25 'C:\sources\PdbTarget\B.cs'
	IL_0000: ldarg.0
	IL_0001: ldstr """"
	IL_0006: stfld System.String PdbTarget.B::s
	.line 110,110:3,21 'C:\sources\PdbTarget\Program.cs'
	IL_000b: ldarg.0
	IL_000c: ldc.i4.2
	IL_000d: stfld System.Int32 PdbTarget.B::a
	.line 111,111:3,21 'C:\sources\PdbTarget\Program.cs'
	IL_0012: ldarg.0
	IL_0013: ldc.i4.3
	IL_0014: stfld System.Int32 PdbTarget.B::b
	.line 9,9:3,13 'C:\sources\PdbTarget\B.cs'
	IL_0019: ldarg.0
	IL_001a: call System.Void System.Object::.ctor()
	IL_001f: nop
	.line 10,10:3,4 'C:\sources\PdbTarget\B.cs'
	IL_0020: nop
	.line 11,11:4,19 'C:\sources\PdbTarget\B.cs'
	IL_0021: ldstr ""B""
	IL_0026: call System.Void System.Console::WriteLine(System.String)
	IL_002b: nop
	.line 12,12:3,4 'C:\sources\PdbTarget\B.cs'
	IL_002c: ret
", main);
			});
		}

		[Test]
		public void LocalVariables ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.Program");
				var method = type.GetMethod ("Bar");
				var debug_info = method.DebugInformation;

				Assert.IsNotNull (debug_info.Scope);
				Assert.IsTrue (debug_info.Scope.HasScopes);
				Assert.AreEqual (2, debug_info.Scope.Scopes.Count);

				var scope = debug_info.Scope.Scopes [0];

				Assert.IsNotNull (scope);
				Assert.IsTrue (scope.HasVariables);
				Assert.AreEqual (1, scope.Variables.Count);

				var variable = scope.Variables [0];

				Assert.AreEqual ("s", variable.Name);
				Assert.IsFalse (variable.IsDebuggerHidden);
				Assert.AreEqual (2, variable.Index);

				scope = debug_info.Scope.Scopes [1];

				Assert.IsNotNull (scope);
				Assert.IsTrue (scope.HasVariables);
				Assert.AreEqual (1, scope.Variables.Count);

				variable = scope.Variables [0];

				Assert.AreEqual ("s", variable.Name);
				Assert.IsFalse (variable.IsDebuggerHidden);
				Assert.AreEqual (3, variable.Index);

				Assert.IsTrue (scope.HasScopes);
				Assert.AreEqual (1, scope.Scopes.Count);

				scope = scope.Scopes [0];

				Assert.IsNotNull (scope);
				Assert.IsTrue (scope.HasVariables);
				Assert.AreEqual (1, scope.Variables.Count);

				variable = scope.Variables [0];

				Assert.AreEqual ("u", variable.Name);
				Assert.IsFalse (variable.IsDebuggerHidden);
				Assert.AreEqual (5, variable.Index);
			});
		}

		[Test]
		public void LocalConstants ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.Program");
				var method = type.GetMethod ("Bar");
				var debug_info = method.DebugInformation;

				Assert.IsNotNull (debug_info.Scope);
				Assert.IsTrue (debug_info.Scope.HasScopes);
				Assert.AreEqual (2, debug_info.Scope.Scopes.Count);

				var scope = debug_info.Scope.Scopes [1];

				Assert.IsNotNull (scope);
				Assert.IsTrue (scope.HasConstants);
				Assert.AreEqual (2, scope.Constants.Count);

				var constant = scope.Constants [0];

				Assert.AreEqual ("b", constant.Name);
				Assert.AreEqual (12, constant.Value);
				Assert.AreEqual (MetadataType.Int32, constant.ConstantType.MetadataType);

				constant = scope.Constants [1];
				Assert.AreEqual ("c", constant.Name);
				Assert.AreEqual ((decimal) 74, constant.Value);
				Assert.AreEqual (MetadataType.ValueType, constant.ConstantType.MetadataType);

				method = type.GetMethod ("Foo");
				debug_info = method.DebugInformation;

				Assert.IsNotNull (debug_info.Scope);
				Assert.IsTrue (debug_info.Scope.HasConstants);
				Assert.AreEqual (4, debug_info.Scope.Constants.Count);

				constant = debug_info.Scope.Constants [0];
				Assert.AreEqual ("s", constant.Name);
				Assert.AreEqual ("const string", constant.Value);
				Assert.AreEqual (MetadataType.String, constant.ConstantType.MetadataType);

				constant = debug_info.Scope.Constants [1];
				Assert.AreEqual ("f", constant.Name);
				Assert.AreEqual (1, constant.Value);
				Assert.AreEqual (MetadataType.Int32, constant.ConstantType.MetadataType);

				constant = debug_info.Scope.Constants [2];
				Assert.AreEqual ("o", constant.Name);
				Assert.AreEqual (null, constant.Value);
				Assert.AreEqual (MetadataType.Object, constant.ConstantType.MetadataType);

				constant = debug_info.Scope.Constants [3];
				Assert.AreEqual ("u", constant.Name);
				Assert.AreEqual (null, constant.Value);
				Assert.AreEqual (MetadataType.String, constant.ConstantType.MetadataType);
			});
		}

		[Test]
		public void ImportScope ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.Program");
				var method = type.GetMethod ("Bar");
				var debug_info = method.DebugInformation;

				Assert.IsNotNull (debug_info.Scope);

				var import = debug_info.Scope.Import;
				Assert.IsNotNull (import);

				Assert.IsFalse (import.HasTargets);
				Assert.IsNotNull (import.Parent);

				import = import.Parent;

				Assert.IsTrue (import.HasTargets);
				Assert.AreEqual (9, import.Targets.Count);
				var target = import.Targets [0];

				Assert.AreEqual (ImportTargetKind.ImportAlias, target.Kind);
				Assert.AreEqual ("XML", target.Alias);

				target = import.Targets [1];

				Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
				Assert.AreEqual ("System", target.Namespace);

				target = import.Targets [2];

				Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
				Assert.AreEqual ("System.Collections.Generic", target.Namespace);

				target = import.Targets [3];

				Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
				Assert.AreEqual ("System.IO", target.Namespace);

				target = import.Targets [4];

				Assert.AreEqual (ImportTargetKind.ImportNamespace, target.Kind);
				Assert.AreEqual ("System.Threading.Tasks", target.Namespace);

				target = import.Targets [5];

				Assert.AreEqual (ImportTargetKind.ImportNamespaceInAssembly, target.Kind);
				Assert.AreEqual ("System.Xml.Resolvers", target.Namespace);
				Assert.AreEqual ("System.Xml", target.AssemblyReference.Name);


				target = import.Targets [6];

				Assert.AreEqual (ImportTargetKind.ImportType, target.Kind);
				Assert.AreEqual ("System.Console", target.Type.FullName);

				target = import.Targets [7];

				Assert.AreEqual (ImportTargetKind.ImportType, target.Kind);
				Assert.AreEqual ("System.Math", target.Type.FullName);

				target = import.Targets [8];

				Assert.AreEqual (ImportTargetKind.DefineTypeAlias, target.Kind);
				Assert.AreEqual ("Foo", target.Alias);
				Assert.AreEqual ("System.Xml.XmlDocumentType", target.Type.FullName);

				Assert.IsNotNull (import.Parent);

				import = import.Parent;

				Assert.IsTrue (import.HasTargets);
				Assert.AreEqual (1, import.Targets.Count);
				Assert.IsNull (import.Parent);

				target = import.Targets [0];

				Assert.AreEqual (ImportTargetKind.DefineAssemblyAlias, target.Kind);
				Assert.AreEqual ("XML", target.Alias);
				Assert.AreEqual ("System.Xml", target.AssemblyReference.Name);
			});
		}

		[Test]
		public void StateMachineKickOff ()
		{
			TestPortablePdbModule (module => {
				var state_machine = module.GetType ("PdbTarget.Program/<Baz>d__7");
				var main = state_machine.GetMethod ("MoveNext");
				var symbol = main.DebugInformation;

				Assert.IsNotNull (symbol);
				Assert.IsNotNull (symbol.StateMachineKickOffMethod);
				Assert.AreEqual ("System.Threading.Tasks.Task PdbTarget.Program::Baz(System.IO.StreamReader)", symbol.StateMachineKickOffMethod.FullName);
			});
		}

		[Test]
		public void StateMachineCustomDebugInformation ()
		{
			TestPortablePdbModule (module => {
				var state_machine = module.GetType ("PdbTarget.Program/<Baz>d__7");
				var move_next = state_machine.GetMethod ("MoveNext");

				Assert.IsTrue (move_next.HasCustomDebugInformations);

				var state_machine_scope = move_next.CustomDebugInformations.OfType<StateMachineScopeDebugInformation> ().FirstOrDefault ();
				Assert.IsNotNull (state_machine_scope);
				Assert.AreEqual (3, state_machine_scope.Scopes.Count);
				Assert.AreEqual (0, state_machine_scope.Scopes [0].Start.Offset);
				Assert.IsTrue (state_machine_scope.Scopes [0].End.IsEndOfMethod);

				Assert.AreEqual (0, state_machine_scope.Scopes [1].Start.Offset);
				Assert.AreEqual (0, state_machine_scope.Scopes [1].End.Offset);

				Assert.AreEqual (184, state_machine_scope.Scopes [2].Start.Offset);
				Assert.AreEqual (343, state_machine_scope.Scopes [2].End.Offset);

				var async_body = move_next.CustomDebugInformations.OfType<AsyncMethodBodyDebugInformation> ().FirstOrDefault ();
				Assert.IsNotNull (async_body);
				Assert.AreEqual (-1, async_body.CatchHandler.Offset);

				Assert.AreEqual (2, async_body.Yields.Count);
				Assert.AreEqual (61, async_body.Yields [0].Offset);
				Assert.AreEqual (221, async_body.Yields [1].Offset);

				Assert.AreEqual (2, async_body.Resumes.Count);
				Assert.AreEqual (91, async_body.Resumes [0].Offset);
				Assert.AreEqual (252, async_body.Resumes [1].Offset);

				Assert.AreEqual (move_next, async_body.ResumeMethods [0]);
				Assert.AreEqual (move_next, async_body.ResumeMethods [1]);
			});
		}

		[Test]
		public void EmbeddedCompressedPortablePdb ()
		{
			TestModule("EmbeddedCompressedPdbTarget.exe", module => {
				Assert.IsTrue (module.HasDebugHeader);

				var header = module.GetDebugHeader ();

				Assert.IsNotNull (header);
				Assert.AreEqual (2, header.Entries.Length);

				var cv = header.Entries [0];
				Assert.AreEqual (ImageDebugType.CodeView, cv.Directory.Type);

				var eppdb = header.Entries [1];
				Assert.AreEqual (ImageDebugType.EmbeddedPortablePdb, eppdb.Directory.Type);
				Assert.AreEqual (0x0100, eppdb.Directory.MajorVersion);
				Assert.AreEqual (0x0100, eppdb.Directory.MinorVersion);
			}, symbolReaderProvider: typeof (EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof (EmbeddedPortablePdbWriterProvider));
		}

		[Test]
		public void EmbeddedCompressedPortablePdbFromStream ()
		{
			var bytes = File.ReadAllBytes (GetAssemblyResourcePath ("EmbeddedCompressedPdbTarget.exe"));
			var parameters = new ReaderParameters {
				ReadSymbols = true,
				SymbolReaderProvider = new PdbReaderProvider ()
			};

			var module = ModuleDefinition.ReadModule (new MemoryStream(bytes), parameters);
			Assert.IsTrue (module.HasDebugHeader);

			var header = module.GetDebugHeader ();

			Assert.IsNotNull (header);
			Assert.AreEqual (2, header.Entries.Length);

			var cv = header.Entries [0];
			Assert.AreEqual (ImageDebugType.CodeView, cv.Directory.Type);

			var eppdb = header.Entries [1];
			Assert.AreEqual (ImageDebugType.EmbeddedPortablePdb, eppdb.Directory.Type);
			Assert.AreEqual (0x0100, eppdb.Directory.MajorVersion);
			Assert.AreEqual (0x0100, eppdb.Directory.MinorVersion);
		}


		void TestPortablePdbModule (Action<ModuleDefinition> test)
		{
			TestModule ("PdbTarget.exe", test, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
			TestModule ("EmbeddedPdbTarget.exe", test, verify: !Platform.OnMono);
			TestModule ("EmbeddedCompressedPdbTarget.exe", test, symbolReaderProvider: typeof(EmbeddedPortablePdbReaderProvider), symbolWriterProvider: typeof(EmbeddedPortablePdbWriterProvider));
		}

		[Test]
		public void RoundTripCecilPortablePdb ()
		{
			TestModule ("cecil.dll", module => {
				Assert.IsTrue (module.HasSymbols);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void RoundTripLargePortablePdb ()
		{
			TestModule ("Mono.Android.dll", module => {
				Assert.IsTrue (module.HasSymbols);
			}, verify: false, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void EmptyPortablePdb ()
		{
			TestModule ("EmptyPdb.dll", module => {
				Assert.IsTrue (module.HasSymbols);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void NullClassConstant ()
		{
			TestModule ("xattr.dll", module => {
				var type = module.GetType ("Library");
				var method = type.GetMethod ("NullXAttributeConstant");
				var symbol = method.DebugInformation;

				Assert.IsNotNull (symbol);
				Assert.AreEqual (1, symbol.Scope.Constants.Count);

				var a = symbol.Scope.Constants [0];
				Assert.AreEqual ("a", a.Name);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void InvalidConstantRecord ()
		{
			using (var module = GetResourceModule ("mylib.dll", new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
				var type = module.GetType ("mylib.Say");
				var method = type.GetMethod ("hello");
				var symbol = method.DebugInformation;

				Assert.IsNotNull (symbol);
				Assert.AreEqual (0, symbol.Scope.Constants.Count);
			}
		}

		[Test]
		public void GenericInstConstantRecord ()
		{
			using (var module = GetResourceModule ("ReproConstGenericInst.dll", new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
				var type = module.GetType ("ReproConstGenericInst.Program");
				var method = type.GetMethod ("Main");
				var symbol = method.DebugInformation;

				Assert.IsNotNull (symbol);
				Assert.AreEqual (1, symbol.Scope.Constants.Count);

				var list = symbol.Scope.Constants [0];
				Assert.AreEqual ("list", list.Name);

				Assert.AreEqual ("System.Collections.Generic.List`1<System.String>", list.ConstantType.FullName);
			}
		}

		[Test]
		public void EmptyStringLocalConstant ()
		{
			TestModule ("empty-str-const.exe", module => {
				var type = module.GetType ("<Program>$");
				var method = type.GetMethod ("<Main>$");
				var symbol = method.DebugInformation;

				Assert.IsNotNull (symbol);
				Assert.AreEqual (1, symbol.Scope.Constants.Count);

				var a = symbol.Scope.Constants [0];
				Assert.AreEqual ("value", a.Name);
				Assert.AreEqual ("", a.Value);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void SourceLink ()
		{
			TestModule ("TargetLib.dll", module => {
				Assert.IsTrue (module.HasCustomDebugInformations);
				Assert.AreEqual (1, module.CustomDebugInformations.Count);

				var source_link = module.CustomDebugInformations [0] as SourceLinkDebugInformation;
				Assert.IsNotNull (source_link);
				Assert.AreEqual ("{\"documents\":{\"C:\\\\tmp\\\\SourceLinkProblem\\\\*\":\"https://raw.githubusercontent.com/bording/SourceLinkProblem/197d965ee7f1e7f8bd3cea55b5f904aeeb8cd51e/*\"}}", source_link.Content);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		[Test]
		public void EmbeddedSource ()
		{
			TestModule ("embedcs.exe", module => {
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));

			TestModule ("embedcs.exe", module => {
				var program = GetDocument (module.GetType ("Program"));
				var program_src = GetSourceDebugInfo (program);
				Assert.IsTrue (program_src.Compress);
				var program_src_content = Encoding.UTF8.GetString (program_src.Content);
				Assert.AreEqual (Normalize (@"using System;

class Program
{
    static void Main()
    {
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        // Hello hello hello hello hello hello
        Console.WriteLine(B.Do());
        Console.WriteLine(A.Do());
    }
}
"), Normalize (program_src_content));

				var a = GetDocument (module.GetType ("A"));
				var a_src = GetSourceDebugInfo (a);
				Assert.IsFalse (a_src.Compress);
				var a_src_content = Encoding.UTF8.GetString (a_src.Content);
				Assert.AreEqual (Normalize (@"class A
{
    public static string Do()
    {
        return ""A::Do"";
    }
}"), Normalize (a_src_content));

				var b = GetDocument(module.GetType ("B"));
				var b_src = GetSourceDebugInfo (b);
				Assert.IsFalse (b_src.compress);
				var b_src_content = Encoding.UTF8.GetString (b_src.Content);
				Assert.AreEqual (Normalize (@"class B
{
    public static string Do()
    {
        return ""B::Do"";
    }
}"), Normalize (b_src_content));
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		static Document GetDocument (TypeDefinition type)
		{
			foreach (var method in type.Methods) {
				if (!method.HasBody)
					continue;

				foreach (var instruction in method.Body.Instructions) {
					var sp = method.DebugInformation.GetSequencePoint (instruction);
					if (sp != null && sp.Document != null)
						return sp.Document;
				}
			}

			return null;
		}

		static EmbeddedSourceDebugInformation GetSourceDebugInfo (Document document)
		{
			Assert.IsTrue (document.HasCustomDebugInformations);
			Assert.AreEqual (1, document.CustomDebugInformations.Count);

			var source = document.CustomDebugInformations [0] as EmbeddedSourceDebugInformation;
			Assert.IsNotNull (source);
			return source;
		}

		[Test]
		public void PortablePdbLineInfo  ()
		{
			TestModule ("line.exe", module => {
				var type = module.GetType ("Tests");
				var main = type.GetMethod ("Main");

				AssertCode (@"
	.locals ()
	.line 4,4:42,43 '/foo/bar.cs'
	IL_0000: nop
	.line 5,5:2,3 '/foo/bar.cs'
	IL_0001: ret", main);
			}, symbolReaderProvider: typeof (PortablePdbReaderProvider), symbolWriterProvider: typeof (PortablePdbWriterProvider));
		}

		public sealed class SymbolWriterProvider : ISymbolWriterProvider {

			readonly DefaultSymbolWriterProvider writer_provider = new DefaultSymbolWriterProvider ();

			public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
			{
				return new SymbolWriter (writer_provider.GetSymbolWriter (module, fileName));
			}

			public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
			{
				return new SymbolWriter (writer_provider.GetSymbolWriter (module, symbolStream));
			}
		}

		public sealed class SymbolWriter : ISymbolWriter {

			readonly ISymbolWriter symbol_writer;

			public SymbolWriter (ISymbolWriter symbolWriter)
			{
				this.symbol_writer = symbolWriter;
			}

			public ImageDebugHeader GetDebugHeader ()
			{
				var header = symbol_writer.GetDebugHeader ();
				if (!header.HasEntries)
					return header;

				for (int i = 0; i < header.Entries.Length; i++) {
					header.Entries [i] = ProcessEntry (header.Entries [i]);
				}

				return header;
			}

			private static ImageDebugHeaderEntry ProcessEntry (ImageDebugHeaderEntry entry)
			{
				if (entry.Directory.Type != ImageDebugType.CodeView)
					return entry;

				var reader = new ByteBuffer (entry.Data);
				var writer = new ByteBuffer ();

				var sig = reader.ReadUInt32 ();
				if (sig != 0x53445352)
					return entry;

				writer.WriteUInt32 (sig); // RSDS
				writer.WriteBytes (reader.ReadBytes (16)); // MVID
				writer.WriteUInt32 (reader.ReadUInt32 ()); // Age

				var length = Array.IndexOf (entry.Data, (byte) 0, reader.position) - reader.position;

				var fullPath = Encoding.UTF8.GetString (reader.ReadBytes (length));

				writer.WriteBytes (Encoding.UTF8.GetBytes (Path.GetFileName (fullPath)));
				writer.WriteByte (0);

				var newData = new byte [writer.length];
				Buffer.BlockCopy (writer.buffer, 0, newData, 0, writer.length);

				var directory = entry.Directory;
				directory.SizeOfData = newData.Length;

				return new ImageDebugHeaderEntry (directory, newData);
			}

			public ISymbolReaderProvider GetReaderProvider ()
			{
				return symbol_writer.GetReaderProvider ();
			}

			public void Write (MethodDebugInformation info)
			{
				symbol_writer.Write (info);
			}

			public void Dispose ()
			{
				symbol_writer.Dispose ();
			}
		}

		static string GetDebugHeaderPdbPath (ModuleDefinition module)
		{
			var header = module.GetDebugHeader ();
			var cv = Mixin.GetCodeViewEntry (header);
			Assert.IsNotNull (cv);
			var length = Array.IndexOf (cv.Data, (byte)0, 24) - 24;
			var bytes = new byte [length];
			Buffer.BlockCopy (cv.Data, 24, bytes, 0, length);
			return Encoding.UTF8.GetString (bytes);
		}

		[Test]
		public void UseCustomSymbolWriterToChangeDebugHeaderPdbPath ()
		{
			const string resource = "mylib.dll";

			string debug_header_pdb_path;
			string dest = Path.Combine (Path.GetTempPath (), resource);

			using (var module = GetResourceModule (resource, new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
				debug_header_pdb_path = GetDebugHeaderPdbPath (module);
				Assert.IsTrue (Path.IsPathRooted (debug_header_pdb_path));
				module.Write (dest, new WriterParameters { SymbolWriterProvider = new SymbolWriterProvider () });
			}

			using (var module = ModuleDefinition.ReadModule (dest, new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
				var pdb_path = GetDebugHeaderPdbPath (module);
				Assert.IsFalse (Path.IsPathRooted (pdb_path));
				Assert.AreEqual (Path.GetFileName (debug_header_pdb_path), pdb_path);
			}
		}

		[Test]
		public void WriteAndReadAgainModuleWithDeterministicMvid ()
		{
			const string resource = "mylib.dll";
			string destination = Path.GetTempFileName ();

			using (var module = GetResourceModule (resource, new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
				module.Write (destination, new WriterParameters { DeterministicMvid = true, SymbolWriterProvider = new SymbolWriterProvider () });
			}

			using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { SymbolReaderProvider = new PortablePdbReaderProvider () })) {
			}
		}

		[Test]
		public void DoubleWriteAndReadAgainModuleWithDeterministicMvid ()
		{
			Guid mvid1_in, mvid1_out, mvid2_in, mvid2_out;

			{
				const string resource = "foo.dll";
				string destination = Path.GetTempFileName ();

				using (var module = GetResourceModule (resource, new ReaderParameters {  })) {
					mvid1_in = module.Mvid;
					module.Write (destination, new WriterParameters { DeterministicMvid = true });
				}

				using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { })) {
					mvid1_out = module.Mvid;
				}
			}

			{
				const string resource = "hello2.exe";
				string destination = Path.GetTempFileName ();

				using (var module = GetResourceModule (resource, new ReaderParameters {  })) {
					mvid2_in = module.Mvid;
					module.Write (destination, new WriterParameters { DeterministicMvid = true });
				}

				using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { })) {
					mvid2_out = module.Mvid;
				}
			}

			Assert.AreNotEqual (mvid1_in, mvid2_in);
			Assert.AreNotEqual (mvid1_out, mvid2_out);
		}

		[Test]
		public void ClearSequencePoints ()
		{
			TestPortablePdbModule (module => {
				var type = module.GetType ("PdbTarget.Program");
				var main = type.GetMethod ("Main");

				main.DebugInformation.SequencePoints.Clear ();

				var destination = Path.Combine (Path.GetTempPath (), "mylib.dll");
				module.Write(destination, new WriterParameters { WriteSymbols = true });

				Assert.Zero (main.DebugInformation.SequencePoints.Count);

				using (var resultModule = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) {
					type = resultModule.GetType ("PdbTarget.Program");
					main = type.GetMethod ("Main");

					Assert.Zero (main.DebugInformation.SequencePoints.Count);
				}
			});
		}
	}
}