-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
360 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
src/Microsoft.DiaSymReader.PortablePdb.Tests/EmbeddedSourceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Roslyn.Test.Utilities; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Reflection.Metadata; | ||
using Xunit; | ||
|
||
namespace Microsoft.DiaSymReader.PortablePdb.UnitTests | ||
{ | ||
using static SymTestHelpers; | ||
|
||
public class EmbeddedSourceTests | ||
{ | ||
[Fact] | ||
public void EmbeddedSource_Portable() | ||
{ | ||
EmbeddedSource(TestResources.EmbeddedSource.PortableDllAndPdb); | ||
} | ||
|
||
[Fact] | ||
public void EmbeddedSource_Native() | ||
{ | ||
NativeNotYetImplemented(); | ||
//EmbeddedSource(TestResources.EmbeddedSource.DllAndPdb); | ||
} | ||
|
||
private void EmbeddedSource(KeyValuePair<byte[], byte[]> dllAndPdb) | ||
{ | ||
ISymUnmanagedReader symReader = CreateSymReaderFromResource(dllAndPdb); | ||
|
||
foreach (string file in new[] { @"C:\EmbeddedSource.cs", @"C:\EmbeddedSourceSmall.cs" }) | ||
{ | ||
bool hasEmbeddedSource; | ||
int length, bytesRead; | ||
ISymUnmanagedDocument doc; | ||
Assert.Equal(HResult.S_OK, symReader.GetDocument(file, default(Guid), default(Guid), default(Guid), out doc)); | ||
Assert.Equal(HResult.S_OK, doc.HasEmbeddedSource(out hasEmbeddedSource)); | ||
Assert.Equal(HResult.S_OK, doc.GetSourceLength(out length)); | ||
Assert.True(hasEmbeddedSource); | ||
|
||
bool isCompressed; | ||
var content = new byte[length]; | ||
Assert.Equal(HResult.S_OK, doc.GetSourceRange(0, 0, int.MaxValue, int.MaxValue, length, out bytesRead, content)); | ||
Assert.Equal(length, bytesRead); | ||
|
||
if (content[0] == 0x1F && content[1] == 0x8B) // gzip signature | ||
{ | ||
isCompressed = true; | ||
Assert.Equal(@"C:\EmbeddedSource.cs", file); | ||
} | ||
else | ||
{ | ||
isCompressed = false; | ||
Assert.Equal(@"C:\EmbeddedSourceSmall.cs", file); | ||
} | ||
|
||
if (isCompressed) | ||
{ | ||
content = Decompress(content); | ||
} | ||
|
||
if (dllAndPdb.Equals(TestResources.EmbeddedSource.PortableDllAndPdb)) | ||
{ | ||
CheckPortableFormatHeader(dllAndPdb.Value, file, isCompressed); | ||
} | ||
|
||
byte[] expectedContent = isCompressed ? TestResources.EmbeddedSource.CS : TestResources.EmbeddedSource.CSSmall; | ||
AssertEx.Equal(expectedContent, content); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void NoEmbeddedSource_Portable() | ||
{ | ||
NoEmbeddedSource(TestResources.Documents.PortableDllAndPdb); | ||
} | ||
|
||
[Fact] | ||
public void NoEmbeddedSource_Native() | ||
{ | ||
NativeNotYetImplemented(); | ||
//NoEmbeddedSource(TestResources.Documents.DllAndPdb); | ||
} | ||
|
||
private void NoEmbeddedSource(KeyValuePair<byte[], byte[]> dllAndPdb) | ||
{ | ||
ISymUnmanagedReader symReader = CreateSymReaderFromResource(dllAndPdb); | ||
|
||
ISymUnmanagedDocument doc; | ||
Assert.Equal(HResult.S_OK, symReader.GetDocument(@"C:\Documents.cs", default(Guid), default(Guid), default(Guid), out doc)); | ||
|
||
bool hasEmbeddedSource; | ||
Assert.Equal(HResult.S_OK, doc.HasEmbeddedSource(out hasEmbeddedSource)); | ||
Assert.False(hasEmbeddedSource); | ||
|
||
int length; | ||
Assert.Equal(HResult.S_FALSE, doc.GetSourceLength(out length)); | ||
Assert.Equal(HResult.S_FALSE, doc.GetSourceRange(0, 0, -1, -1, 0, out length, null)); | ||
Assert.Equal(HResult.S_FALSE, doc.GetSourceRange(0, 0, int.MaxValue, int.MaxValue, 0, out length, null)); | ||
} | ||
|
||
[Fact] | ||
public void BadArgs_Portable() | ||
{ | ||
BadArgs(TestResources.EmbeddedSource.PortableDllAndPdb); | ||
} | ||
|
||
[Fact] | ||
public void BadArgs_Native() | ||
{ | ||
NativeNotYetImplemented(); | ||
//EmbeddedSource(TestResources.EmbeddedSource.DllAndPdb); | ||
} | ||
|
||
private void BadArgs(KeyValuePair<byte[], byte[]> dllAndPdb) | ||
{ | ||
ISymUnmanagedReader symReader = CreateSymReaderFromResource(dllAndPdb); | ||
|
||
ISymUnmanagedDocument doc; | ||
Assert.Equal(HResult.S_OK, symReader.GetDocument(@"C:\EmbeddedSource.cs", default(Guid), default(Guid), default(Guid), out doc)); | ||
|
||
int count; | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(-1, 0, int.MaxValue, int.MaxValue, 0, out count, null)); | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(0, -1, int.MaxValue, int.MaxValue, 0, out count, null)); | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(0, 0, 1, int.MaxValue, 0, out count, null)); | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(0, 0, int.MaxValue, 1, 0, out count, null)); | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(0, 0, int.MaxValue, int.MaxValue, 1, out count, null)); | ||
|
||
// negative bufferLength test does not apply to native as it uses uint arguments. | ||
if (dllAndPdb.Equals(TestResources.EmbeddedSource.PortableDllAndPdb)) | ||
{ | ||
Assert.Equal(HResult.E_INVALIDARG, doc.GetSourceRange(0, 0, int.MaxValue, int.MaxValue, -1, out count, new byte[1])); | ||
} | ||
} | ||
|
||
private byte[] Decompress(byte[] content) | ||
{ | ||
using (var compressed = new MemoryStream(content)) | ||
using (var decompressor = new GZipStream(compressed, CompressionMode.Decompress)) | ||
using (var decompressed = new MemoryStream()) | ||
{ | ||
decompressor.CopyTo(decompressed); | ||
return decompressed.ToArray(); | ||
} | ||
} | ||
|
||
// Portable PDB has a leading format indicator that is not exposed via the COM API. Check it for consistency. | ||
private static void CheckPortableFormatHeader(byte[] portablePdb, string file, bool isCompressed) | ||
{ | ||
using (var provider = MetadataReaderProvider.FromPortablePdbImage(ImmutableArray.Create(portablePdb))) | ||
{ | ||
var portablePdbReader = provider.GetMetadataReader(); | ||
var document = GetDocumentHandle(portablePdbReader, file); | ||
var customDebugInfo = portablePdbReader.GetCustomDebugInformation(document, MetadataUtilities.EmbeddedSourceId); | ||
var blobReader = portablePdbReader.GetBlobReader(customDebugInfo); | ||
Assert.Equal(isCompressed ? 1 : 0, blobReader.ReadUInt16()); | ||
} | ||
} | ||
|
||
private static DocumentHandle GetDocumentHandle(MetadataReader portablePdbReader, string file) | ||
{ | ||
foreach (var handle in portablePdbReader.Documents) | ||
{ | ||
var document = portablePdbReader.GetDocument(handle); | ||
if (portablePdbReader.StringComparer.Equals(document.Name, file)) | ||
{ | ||
return handle; | ||
} | ||
} | ||
|
||
Assert.False(true, "Document not found."); | ||
throw null; | ||
} | ||
|
||
private void NativeNotYetImplemented() | ||
{ | ||
// TODO: This checks the behavior of Microsoft.DiaSymReader.Native before the update that | ||
// has native code support. When this starts to fail, remove this and uncomment the | ||
// tests that pass native PDB to same worker routines as portable PDB. | ||
ISymUnmanagedReader symReader = CreateSymReaderFromResource(TestResources.EmbeddedSource.DllAndPdb); | ||
|
||
ISymUnmanagedDocument doc; | ||
Assert.Equal(HResult.S_OK, symReader.GetDocument(@"C:\EmbeddedSource.cs", default(Guid), default(Guid), default(Guid), out doc)); | ||
|
||
bool hasEmbeddedSource; | ||
Assert.Equal(HResult.E_NOTIMPL, doc.HasEmbeddedSource(out hasEmbeddedSource)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.cmd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@REM -- TODO: Compiled with WIP writer and preliminary command-line. | ||
@setlocal | ||
@set CSC=D:\src\roslyn\binaries\debug\csc.exe | ||
|
||
%CSC% /target:library /debug:portable /optimize- /deterministic /pathmap:%~dp0=C:\ /embedsourceinpdb EmbeddedSource.cs EmbeddedSourceSmall.cs | ||
copy /y EmbeddedSource.pdb EmbeddedSource.pdbx | ||
copy /y EmbeddedSource.dll EmbeddedSource.dllx | ||
|
||
%CSC% /target:library /debug+ /optimize- /deterministic /pathmap:%~dp0=C:\ /embedsourceinpdb EmbeddedSource.cs EmbeddedSourceSmall.cs |
19 changes: 19 additions & 0 deletions
19
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// should be higher than compression threshold (200 chars) | ||
|
||
using System; | ||
|
||
namespace Test | ||
{ | ||
public static class SomeCode | ||
{ | ||
public static int SomeMethod(int value) | ||
{ | ||
if (value < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(value)); | ||
} | ||
|
||
return checked(value + 42); | ||
} | ||
} | ||
} |
Binary file added
BIN
+3.5 KB
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.dll
Binary file not shown.
Binary file added
BIN
+3.5 KB
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.dllx
Binary file not shown.
Binary file added
BIN
+11.5 KB
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.pdb
Binary file not shown.
Binary file added
BIN
+900 Bytes
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSource.pdbx
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
src/Microsoft.DiaSymReader.PortablePdb.Tests/Resources/EmbeddedSourceSmall.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// should be less than compression threshold (200 chars) | ||
public class Small | ||
{ | ||
public Small() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.