forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssemblyDependencyResolverTests.cs
318 lines (278 loc) · 13.1 KB
/
AssemblyDependencyResolverTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using TestLibrary;
using Xunit;
using Assert = Xunit.Assert;
namespace AssemblyDependencyResolverTests
{
class AssemblyDependencyResolverTests : TestBase
{
string _componentDirectory;
string _componentAssemblyPath;
protected override void Initialize()
{
HostPolicyMock.Initialize(TestBasePath, CoreRoot);
_componentDirectory = Path.Combine(TestBasePath, $"TestComponent_{Guid.NewGuid().ToString().Substring(0, 8)}");
Directory.CreateDirectory(_componentDirectory);
_componentAssemblyPath = CreateMockAssembly("TestComponent.dll");
}
protected override void Cleanup()
{
if (Directory.Exists(_componentDirectory))
{
Directory.Delete(_componentDirectory, recursive: true);
}
}
public void TestComponentLoadFailure()
{
const string errorMessageFirstLine = "First line: failure";
const string errorMessageSecondLine = "Second line: value";
using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
HostPolicyMock.Mock_corehost_set_error_writer())
{
using (HostPolicyMock.MockValues_corehost_resolve_component_dependencies resolverMock =
HostPolicyMock.Mock_corehost_resolve_component_dependencies(
134,
"",
"",
""))
{
// When the resolver is called, emulate error behavior
// which is to write to the error writer some error message.
resolverMock.Callback = (string componentAssemblyPath) =>
{
Assert.NotNull(errorWriterMock.LastSetErrorWriter);
errorWriterMock.LastSetErrorWriter(errorMessageFirstLine);
errorWriterMock.LastSetErrorWriter(errorMessageSecondLine);
};
string message = Assert.Throws<InvalidOperationException>(() =>
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
}).Message;
Assert.Contains("134", message);
Assert.Contains(
errorMessageFirstLine + Environment.NewLine + errorMessageSecondLine,
message);
// After everything is done, the error writer should be reset.
Assert.Null(errorWriterMock.LastSetErrorWriter);
}
}
}
public void TestComponentLoadFailureWithPreviousErrorWriter()
{
IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(
(HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); }));
using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
HostPolicyMock.Mock_corehost_set_error_writer(previousWriter))
{
using (HostPolicyMock.MockValues_corehost_resolve_component_dependencies resolverMock =
HostPolicyMock.Mock_corehost_resolve_component_dependencies(
134,
"",
"",
""))
{
Assert.Throws<InvalidOperationException>(() =>
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
});
// After everything is done, the error writer should be reset to the original value.
Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr);
}
}
}
public void TestAssembly()
{
string assemblyDependencyPath = CreateMockAssembly("AssemblyDependency.dll");
IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(
(HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); }));
using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock =
HostPolicyMock.Mock_corehost_set_error_writer(previousWriter))
{
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
assemblyDependencyPath,
"",
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
assemblyDependencyPath,
resolver.ResolveAssemblyToPath(new AssemblyName("AssemblyDependency")));
// After everything is done, the error writer should be reset to the original value.
Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr);
}
}
}
public void TestAssemblyWithNoRecord()
{
// If the reqest is for assembly which is not listed in .deps.json
// the resolver should return null.
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
"",
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Null(resolver.ResolveAssemblyToPath(new AssemblyName("AssemblyWithNoRecord")));
}
}
public void TestAssemblyWithMissingFile()
{
// Even if the .deps.json can resolve the request, if the file is not present
// the resolution should still return null.
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
Path.Combine(_componentDirectory, "NonExistingAssembly.dll"),
"",
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Null(resolver.ResolveAssemblyToPath(new AssemblyName("NonExistingAssembly")));
}
}
public void TestSingleResource()
{
string enResourcePath = CreateMockAssembly($"en{Path.DirectorySeparatorChar}TestComponent.resources.dll");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
"",
_componentDirectory))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
enResourcePath,
resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=en")));
}
}
public void TestMultipleResourcesWithSameBasePath()
{
string enResourcePath = CreateMockAssembly($"en{Path.DirectorySeparatorChar}TestComponent.resources.dll");
string csResourcePath = CreateMockAssembly($"cs{Path.DirectorySeparatorChar}TestComponent.resources.dll");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
"",
_componentDirectory))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
enResourcePath,
resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=en")));
Assert.Equal(
csResourcePath,
resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=cs")));
}
}
public void TestMultipleResourcesWithDifferentBasePath()
{
string enResourcePath = CreateMockAssembly($"en{Path.DirectorySeparatorChar}TestComponent.resources.dll");
string frResourcePath = CreateMockAssembly($"SubComponent{Path.DirectorySeparatorChar}fr{Path.DirectorySeparatorChar}TestComponent.resources.dll");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
"",
$"{_componentDirectory}{Path.PathSeparator}{Path.GetDirectoryName(Path.GetDirectoryName(frResourcePath))}"))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
enResourcePath,
resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=en")));
Assert.Equal(
frResourcePath,
resolver.ResolveAssemblyToPath(new AssemblyName("TestComponent.resources, Culture=fr")));
}
}
public void TestAssemblyWithNeutralCulture()
{
string neutralAssemblyPath = CreateMockAssembly("NeutralAssembly.dll");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
neutralAssemblyPath,
"",
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
neutralAssemblyPath,
resolver.ResolveAssemblyToPath(new AssemblyName("NeutralAssembly, Culture=neutral")));
}
}
public void TestSingleNativeDependency()
{
string nativeLibraryPath = CreateMockStandardNativeLibrary("native", "Single");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
Path.GetDirectoryName(nativeLibraryPath),
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
nativeLibraryPath,
resolver.ResolveUnmanagedDllToPath("Single"));
}
}
public void TestMultipleNativeDependencies()
{
string oneNativeLibraryPath = CreateMockStandardNativeLibrary($"native{Path.DirectorySeparatorChar}one", "One");
string twoNativeLibraryPath = CreateMockStandardNativeLibrary($"native{Path.DirectorySeparatorChar}two", "Two");
using (HostPolicyMock.Mock_corehost_resolve_component_dependencies(
0,
"",
$"{Path.GetDirectoryName(oneNativeLibraryPath)}{Path.PathSeparator}{Path.GetDirectoryName(twoNativeLibraryPath)}",
""))
{
AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(
Path.Combine(TestBasePath, _componentAssemblyPath));
Assert.Equal(
oneNativeLibraryPath,
resolver.ResolveUnmanagedDllToPath("One"));
Assert.Equal(
twoNativeLibraryPath,
resolver.ResolveUnmanagedDllToPath("Two"));
}
}
private string CreateMockAssembly(string relativePath)
{
string fullPath = Path.Combine(_componentDirectory, relativePath);
if (!File.Exists(fullPath))
{
string directory = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
File.WriteAllText(fullPath, "Mock assembly");
}
return fullPath;
}
private string CreateMockStandardNativeLibrary(string relativePath, string simpleName)
{
return CreateMockAssembly(
relativePath + Path.DirectorySeparatorChar + XPlatformUtils.GetStandardNativeLibraryFileName(simpleName));
}
public static int Main()
{
return TestBase.RunTests(
typeof(AssemblyDependencyResolverTests),
typeof(NativeDependencyTests));
}
}
}