Add a prototype telemetry setting test.

This commit is contained in:
Marc Paine 2022-09-22 15:24:11 -07:00
parent 036f883b74
commit a3d39216d2
2 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,23 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
namespace EndToEnd.Tests
{
public class OfficialBuilderTests : TestBase
{
[Fact]
public void OfficialBuilderAtrribute()
{
var dotnetdir = Path.Combine(Path.GetDirectoryName(RepoDirectoriesProvider.DotnetUnderTest), "sdk", "7.0.100-dev");
var result = AssemblyInfo.Get(Path.Combine(dotnetdir, "dotnet.dll"), "AssemblyMetadataAttribute");
result.Should().Contain("OfficialBuilder:Microsoft");
}
}
}

View file

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public static class AssemblyInfo
{
public static IList<string> Get(string assemblyPath, string assemblyInfoName)
{
var list = new List<string>();
using (var stream = File.OpenRead(assemblyPath))
using (var peReader = new PEReader(stream))
{
var metadataReader = peReader.GetMetadataReader();
var assemblyDefinition = metadataReader.GetAssemblyDefinition();
foreach (var handle in assemblyDefinition.GetCustomAttributes())
{
var attribute = metadataReader.GetCustomAttribute(handle);
var constructor = metadataReader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);
var type = metadataReader.GetTypeReference((TypeReferenceHandle)constructor.Parent);
var name = metadataReader.GetString(type.Name);
if (name.Equals(assemblyInfoName))
{
var signature = metadataReader.GetBlobReader(constructor.Signature);
var value = metadataReader.GetBlobReader(attribute.Value);
var header = signature.ReadSignatureHeader();
const ushort prolog = 1; // two-byte "prolog" defined by ECMA-335 (II.23.3) to be at the beginning of attribute value blobs
if (value.ReadUInt16() != prolog || header.Kind != SignatureKind.Method || header.IsGeneric)
{
throw new BadImageFormatException();
}
var paramCount = signature.ReadCompressedInteger();
if (paramCount <= 0 || // must have at least 1 parameter
signature.ReadSignatureTypeCode() != SignatureTypeCode.Void) // return type must be void
{
continue;
}
var sb = new StringBuilder();
while (paramCount > 0 && sb != null)
{
switch (signature.ReadSignatureTypeCode())
{
case SignatureTypeCode.String:
sb.Append(value.ReadSerializedString());
break;
default:
sb = null;
break;
}
paramCount--;
if (paramCount != 0)
{
sb?.Append(':');
}
}
if (sb != null)
{
list.Add(sb.ToString());
}
}
}
}
return list;
}
}
}