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
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
using Mono.Linker.Tests.Extensions;
using Mono.Linker.Tests.TestCases;
namespace Mono.Linker.Tests.TestCasesRunner
{
public class TestCaseMetadataProvider : BaseMetadataProvider
{
public TestCaseMetadataProvider (TestCase testCase, AssemblyDefinition fullTestCaseAssemblyDefinition)
: base (testCase, fullTestCaseAssemblyDefinition)
{
}
public virtual TestCaseLinkerOptions GetLinkerOptions (NPath inputPath)
{
var tclo = new TestCaseLinkerOptions {
Il8n = GetOptionAttributeValue (nameof (Il8nAttribute), "none"),
IgnoreDescriptors = GetOptionAttributeValue (nameof (IgnoreDescriptorsAttribute), true),
IgnoreSubstitutions = GetOptionAttributeValue (nameof (IgnoreSubstitutionsAttribute), true),
IgnoreLinkAttributes = GetOptionAttributeValue (nameof (IgnoreLinkAttributesAttribute), true),
KeepTypeForwarderOnlyAssemblies = GetOptionAttributeValue (nameof (KeepTypeForwarderOnlyAssembliesAttribute), string.Empty),
KeepDebugMembers = GetOptionAttributeValue (nameof (SetupLinkerKeepDebugMembersAttribute), string.Empty),
LinkSymbols = GetOptionAttributeValue (nameof (SetupLinkerLinkSymbolsAttribute), string.Empty),
TrimMode = GetOptionAttributeValue<string> (nameof (SetupLinkerTrimModeAttribute), null),
DefaultAssembliesAction = GetOptionAttributeValue<string> (nameof (SetupLinkerDefaultActionAttribute), null),
SkipUnresolved = GetOptionAttributeValue (nameof (SkipUnresolvedAttribute), false),
StripDescriptors = GetOptionAttributeValue (nameof (StripDescriptorsAttribute), true),
StripSubstitutions = GetOptionAttributeValue (nameof (StripSubstitutionsAttribute), true),
StripLinkAttributes = GetOptionAttributeValue (nameof (StripLinkAttributesAttribute), true),
};
foreach (var assemblyAction in _testCaseTypeDefinition.CustomAttributes.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerActionAttribute))) {
var ca = assemblyAction.ConstructorArguments;
tclo.AssembliesAction.Add (new KeyValuePair<string, string> ((string) ca[0].Value, (string) ca[1].Value));
}
foreach (var descFile in _testCaseTypeDefinition.CustomAttributes.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerDescriptorFile))) {
var ca = descFile.ConstructorArguments;
var file = (string) ca[0].Value;
tclo.Descriptors.Add (Path.Combine (inputPath, file));
}
foreach (var subsFile in _testCaseTypeDefinition.CustomAttributes.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerSubstitutionFileAttribute))) {
var ca = subsFile.ConstructorArguments;
var file = (string) ca[0].Value;
tclo.Substitutions.Add (Path.Combine (inputPath, file));
}
foreach (var linkAttrFile in _testCaseTypeDefinition.CustomAttributes.Where (attr => attr.AttributeType.Name == nameof (SetupLinkAttributesFile))) {
var ca = linkAttrFile.ConstructorArguments;
var file = (string) ca[0].Value;
tclo.LinkAttributes.Add (Path.Combine (inputPath, file));
}
foreach (var additionalArgumentAttr in _testCaseTypeDefinition.CustomAttributes.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerArgumentAttribute))) {
var ca = additionalArgumentAttr.ConstructorArguments;
var values = ((CustomAttributeArgument[]) ca[1].Value)?.Select (arg => arg.Value.ToString ()).ToArray ();
// Since custom attribute arguments need to be constant expressions, we need to add
// the path to the temp directory (where the custom assembly is located) here.
switch ((string) ca[0].Value) {
case "--custom-step":
int pos = values[0].IndexOf (",");
if (pos != -1) {
string custom_assembly_path = values[0].Substring (pos + 1);
if (!Path.IsPathRooted (custom_assembly_path))
values[0] = values[0].Substring (0, pos + 1) + Path.Combine (inputPath, custom_assembly_path);
}
break;
case "-a":
if (!Path.IsPathRooted (values[0]))
values[0] = Path.Combine (inputPath, values[0]);
break;
}
tclo.AdditionalArguments.Add (new KeyValuePair<string, string[]> ((string) ca[0].Value, values));
}
return tclo;
}
public virtual void CustomizeLinker (LinkerDriver linker, LinkerCustomizations customizations)
{
if (_testCaseTypeDefinition.CustomAttributes.Any (attr =>
attr.AttributeType.Name == nameof (DependencyRecordedAttribute))) {
customizations.DependencyRecorder = new TestDependencyRecorder ();
customizations.CustomizeContext += context => {
context.Tracer.AddRecorder (customizations.DependencyRecorder);
};
}
if (ValidatesReflectionAccessPatterns (_testCaseTypeDefinition)) {
customizations.ReflectionPatternRecorder = new TestReflectionPatternRecorder ();
customizations.CustomizeContext += context => {
customizations.ReflectionPatternRecorder.PreviousRecorder = context.ReflectionPatternRecorder;
context.ReflectionPatternRecorder = customizations.ReflectionPatternRecorder;
context.LogMessages = true;
};
}
if (ValidatesLogMessages (_testCaseTypeDefinition)) {
customizations.CustomizeContext += context => {
context.LogMessages = true;
};
}
}
bool ValidatesReflectionAccessPatterns (TypeDefinition testCaseTypeDefinition)
{
if (testCaseTypeDefinition.HasNestedTypes) {
var nestedTypes = new Queue<TypeDefinition> (testCaseTypeDefinition.NestedTypes.ToList ());
while (nestedTypes.Count > 0) {
if (ValidatesReflectionAccessPatterns (nestedTypes.Dequeue ()))
return true;
}
}
if (testCaseTypeDefinition.CustomAttributes.Any (attr =>
attr.AttributeType.Name == nameof (VerifyAllReflectionAccessPatternsAreValidatedAttribute))
|| testCaseTypeDefinition.AllMembers ().Concat (testCaseTypeDefinition.AllDefinedTypes ()).Any (m => m.CustomAttributes.Any (attr =>
attr.AttributeType.Name == nameof (RecognizedReflectionAccessPatternAttribute) ||
attr.AttributeType.Name == nameof (UnrecognizedReflectionAccessPatternAttribute))))
return true;
return false;
}
bool ValidatesLogMessages (TypeDefinition testCaseTypeDefinition)
{
if (testCaseTypeDefinition.HasNestedTypes) {
var nestedTypes = new Queue<TypeDefinition> (testCaseTypeDefinition.NestedTypes.ToList ());
while (nestedTypes.Count > 0) {
if (ValidatesLogMessages (nestedTypes.Dequeue ()))
return true;
}
}
if (testCaseTypeDefinition.AllMembers ().Concat (testCaseTypeDefinition.AllDefinedTypes ()).Append (testCaseTypeDefinition)
.Any (m => m.CustomAttributes.Any (attr =>
attr.AttributeType.Name == nameof (LogContainsAttribute) ||
attr.AttributeType.Name == nameof (LogDoesNotContainAttribute))))
return true;
return false;
}
public virtual IEnumerable<SourceAndDestinationPair> GetResponseFiles ()
{
return _testCaseTypeDefinition.CustomAttributes
.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerResponseFileAttribute))
.Select (GetSourceAndRelativeDestinationValue);
}
public virtual IEnumerable<SourceAndDestinationPair> GetDescriptorFiles ()
{
return _testCaseTypeDefinition.CustomAttributes
.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerDescriptorFile))
.Select (GetSourceAndRelativeDestinationValue);
}
public virtual IEnumerable<SourceAndDestinationPair> GetSubstitutionFiles ()
{
return _testCaseTypeDefinition.CustomAttributes
.Where (attr => attr.AttributeType.Name == nameof (SetupLinkerSubstitutionFileAttribute))
.Select (GetSourceAndRelativeDestinationValue);
}
public virtual IEnumerable<SourceAndDestinationPair> GetLinkAttributesFiles ()
{
return _testCaseTypeDefinition.CustomAttributes
.Where (attr => attr.AttributeType.Name == nameof (SetupLinkAttributesFile))
.Select (GetSourceAndRelativeDestinationValue);
}
public virtual IEnumerable<NPath> GetExtraLinkerReferences ()
{
var netcoreappDir = Path.GetDirectoryName (typeof (object).Assembly.Location);
foreach (var assembly in Directory.EnumerateFiles (netcoreappDir)) {
if (Path.GetExtension (assembly) != ".dll")
continue;
var assemblyName = Path.GetFileNameWithoutExtension (assembly);
if (assemblyName.Contains ("Native"))
continue;
if (assemblyName.StartsWith ("Microsoft") ||
assemblyName.StartsWith ("System") ||
assemblyName == "mscorlib" || assemblyName == "netstandard")
yield return assembly.ToNPath ();
}
}
public virtual bool LinkPublicAndFamily ()
{
return _testCaseTypeDefinition.CustomAttributes
.FirstOrDefault (attr => attr.AttributeType.Name == nameof (SetupLinkerLinkPublicAndFamilyAttribute)) != null;
}
}
}
|