-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathAssemblyLoadContextTest.cs
279 lines (233 loc) · 11.9 KB
/
AssemblyLoadContextTest.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using Xunit;
namespace System.Runtime.Loader.Tests
{
public partial class AssemblyLoadContextTest
{
private const string TestAssembly = "System.Runtime.Loader.Test.Assembly";
private const string TestAssembly2 = "System.Runtime.Loader.Test.Assembly2";
[Fact]
public static void GetAssemblyNameTest_ValidAssembly()
{
var expectedName = typeof(AssemblyLoadContextTest).Assembly.GetName();
var actualAsmName = AssemblyLoadContext.GetAssemblyName("System.Runtime.Loader.Tests.dll");
Assert.Equal(expectedName.FullName, actualAsmName.FullName);
// Verify that the AssemblyName returned by GetAssemblyName can be used to load an assembly. System.Runtime would
// already be loaded, but this is just verifying it does not throw some other unexpected exception.
var asm = Assembly.Load(actualAsmName);
Assert.NotNull(asm);
Assert.Equal(asm, typeof(AssemblyLoadContextTest).Assembly);
}
[Fact]
public static void GetAssemblyNameTest_AssemblyNotFound()
{
Assert.Throws<FileNotFoundException>(() => AssemblyLoadContext.GetAssemblyName("Non.Existing.Assembly.dll"));
}
[Fact]
public static void GetAssemblyNameTest_NullParameter()
{
Assert.Throws<ArgumentNullException>(() => AssemblyLoadContext.GetAssemblyName(null));
}
[Fact]
public static void LoadFromAssemblyPath_PartiallyQualifiedPath_ThrowsArgumentException()
{
string path = Path.Combine("foo", "bar.dll");
ArgumentException ex = AssertExtensions.Throws<ArgumentException>("assemblyPath", () => (new AssemblyLoadContext("alc")).LoadFromAssemblyPath(path));
Assert.Contains(path, ex.Message);
}
[Fact]
public static void LoadFromNativeImagePath_PartiallyQualifiedPath_ThrowsArgumentException()
{
string path = Path.Combine("foo", "bar.dll");
ArgumentException ex = AssertExtensions.Throws<ArgumentException>("nativeImagePath", () => (new AssemblyLoadContext("alc")).LoadFromNativeImagePath(path, null));
Assert.Contains(path, ex.Message);
}
[Fact]
public static void LoadFromNativeImagePath_PartiallyQualifiedPath_ThrowsArgumentException2()
{
string path = Path.Combine("foo", "bar.dll");
string rootedPath = Path.GetFullPath(Guid.NewGuid().ToString("N"));
ArgumentException ex = AssertExtensions.Throws<ArgumentException>("assemblyPath", () => (new AssemblyLoadContext("alc")).LoadFromNativeImagePath(rootedPath, path));
Assert.Contains(path, ex.Message);
}
[Fact]
[PlatformSpecific(~(TestPlatforms.iOS | TestPlatforms.tvOS))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/51893", typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltWithAggressiveTrimming), nameof(PlatformDetection.IsBrowser))]
public static void LoadAssemblyByPath_ValidUserAssembly()
{
var asmName = new AssemblyName(TestAssembly);
var loadContext = new ResourceAssemblyLoadContext();
loadContext.LoadBy = LoadBy.Path;
var asm = loadContext.LoadFromAssemblyName(asmName);
Assert.NotNull(asm);
Assert.Same(loadContext, AssemblyLoadContext.GetLoadContext(asm));
Assert.Contains(asm.DefinedTypes, t => t.Name == "TestClass");
}
[Fact]
[PlatformSpecific(~(TestPlatforms.iOS | TestPlatforms.tvOS))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/51893", typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltWithAggressiveTrimming), nameof(PlatformDetection.IsBrowser))]
public static void LoadAssemblyByStream_ValidUserAssembly()
{
var asmName = new AssemblyName(TestAssembly);
var loadContext = new ResourceAssemblyLoadContext();
loadContext.LoadBy = LoadBy.Stream;
var asm = loadContext.LoadFromAssemblyName(asmName);
Assert.NotNull(asm);
Assert.Same(loadContext, AssemblyLoadContext.GetLoadContext(asm));
Assert.Contains(asm.DefinedTypes, t => t.Name == "TestClass");
}
[Fact]
public static void LoadFromAssemblyName_AssemblyNotFound()
{
var asmName = new AssemblyName("Non.Existing.Assembly.dll");
var loadContext = new ResourceAssemblyLoadContext();
loadContext.LoadBy = LoadBy.Path;
Assert.Throws<FileNotFoundException>(() => loadContext.LoadFromAssemblyName(asmName));
}
[Fact]
[SkipOnPlatform(TestPlatforms.Browser, "Corelib does not exist on disc for Browser builds")]
public static void LoadFromAssemblyName_ValidTrustedPlatformAssembly()
{
var asmName = typeof(System.Linq.Enumerable).Assembly.GetName();
var loadContext = new CustomTPALoadContext();
// We should be able to override (and thus, load) assemblies that were
// loaded in TPA load context.
var asm = loadContext.LoadFromAssemblyName(asmName);
Assert.NotNull(asm);
var loadedContext = AssemblyLoadContext.GetLoadContext(asm);
Assert.NotNull(loadedContext);
Assert.Same(loadContext, loadedContext);
}
[Fact]
public static void LoadFromAssemblyName_FallbackToDefaultContext()
{
var asmName = typeof(System.Linq.Enumerable).Assembly.GetName();
var loadContext = new AssemblyLoadContext("FallbackToDefaultContextTest");
// This should not have any special handlers, so it should just find the version in the default context
var asm = loadContext.LoadFromAssemblyName(asmName);
Assert.NotNull(asm);
var loadedContext = AssemblyLoadContext.GetLoadContext(asm);
Assert.NotNull(loadedContext);
Assert.Same(AssemblyLoadContext.Default, loadedContext);
Assert.NotEqual(loadContext, loadedContext);
Assert.Same(typeof(System.Linq.Enumerable).Assembly, asm);
}
[Fact]
[PlatformSpecific(~(TestPlatforms.iOS | TestPlatforms.tvOS))]
public static void GetLoadContextTest_ValidUserAssembly()
{
var asmName = new AssemblyName(TestAssembly);
var expectedContext = new ResourceAssemblyLoadContext();
expectedContext.LoadBy = LoadBy.Stream;
var asm = expectedContext.LoadFromAssemblyName(asmName);
var actualContext = AssemblyLoadContext.GetLoadContext(asm);
Assert.Equal(expectedContext, actualContext);
}
[Fact]
public static void GetLoadContextTest_ValidTrustedPlatformAssembly()
{
var asm = typeof(System.Linq.Enumerable).GetTypeInfo().Assembly;
var context = AssemblyLoadContext.GetLoadContext(asm);
Assert.NotNull(context);
Assert.Same(AssemblyLoadContext.Default, context);
}
[Fact]
public static void GetLoadContextTest_SystemPrivateCorelibAssembly()
{
// System.Private.Corelib is a special case
// `int` is defined in S.P.C
var asm = typeof(int).Assembly;
var context = AssemblyLoadContext.GetLoadContext(asm);
Assert.NotNull(context);
Assert.Same(AssemblyLoadContext.Default, context);
}
[Fact]
public static void DefaultAssemblyLoadContext_Properties()
{
AssemblyLoadContext alc = AssemblyLoadContext.Default;
Assert.False(alc.IsCollectible);
Assert.Equal("Default", alc.Name);
Assert.Contains("\"Default\"", alc.ToString());
Assert.Contains("System.Runtime.Loader.DefaultAssemblyLoadContext", alc.ToString());
Assert.Contains(alc, AssemblyLoadContext.All);
Assert.Contains(typeof(int).Assembly, alc.Assemblies);
}
[Fact]
public static void PublicConstructor_Default()
{
AssemblyLoadContext alc = new AssemblyLoadContext("PublicConstructor");
Assert.False(alc.IsCollectible);
Assert.Equal("PublicConstructor", alc.Name);
Assert.Contains("PublicConstructor", alc.ToString());
Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
Assert.Contains(alc, AssemblyLoadContext.All);
Assert.Empty(alc.Assemblies);
}
[Theory]
[InlineData("AssemblyLoadContextCollectible", true)]
[InlineData("AssemblyLoadContextNonCollectible", false)]
public static void PublicConstructor_Theory(string name, bool isCollectible)
{
AssemblyLoadContext alc = new AssemblyLoadContext(name, isCollectible);
Assert.Equal(isCollectible, alc.IsCollectible);
Assert.Equal(name, alc.Name);
Assert.Contains(name, alc.ToString());
Assert.Contains("System.Runtime.Loader.AssemblyLoadContext", alc.ToString());
Assert.Contains(alc, AssemblyLoadContext.All);
Assert.Empty(alc.Assemblies);
}
[Fact]
public static void SubclassAssemblyLoadContext_Properties()
{
AssemblyLoadContext alc = new ResourceAssemblyLoadContext();
Assert.False(alc.IsCollectible);
Assert.Null(alc.Name);
Assert.Contains("\"\"", alc.ToString());
Assert.Contains(typeof(ResourceAssemblyLoadContext).ToString(), alc.ToString());
Assert.Contains(alc, AssemblyLoadContext.All);
Assert.Empty(alc.Assemblies);
}
class RefEmitLoadContext : AssemblyLoadContext
{
protected override Assembly? Load(AssemblyName assemblyName)
{
return AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31804", TestRuntimes.Mono)]
public static void LoadRefEmitAssembly()
{
RefEmitLoadContext alc = new();
alc.Resolving += (sender, assembly) => { Assert.Fail("Resolving event not expected"); return null; };
Exception error = Assert.Throws<FileLoadException>(() => alc.LoadFromAssemblyName(new AssemblyName("MyAssembly")));
Assert.IsType<InvalidOperationException>(error.InnerException);
}
class NonRuntimeAssemblyContext : AssemblyLoadContext
{
class NonRuntimeAssembly : Assembly
{
private AssemblyName _name;
public NonRuntimeAssembly(AssemblyName name) => _name = name;
public override AssemblyName GetName(bool copiedName) => _name;
}
protected override Assembly? Load(AssemblyName assemblyName)
{
return new NonRuntimeAssembly(assemblyName);
}
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31804", TestRuntimes.Mono)]
public static void LoadNonRuntimeAssembly()
{
NonRuntimeAssemblyContext alc = new();
alc.Resolving += (sender, assembly) => { Assert.Fail("Resolving event not expected"); return null; };
Exception error = Assert.Throws<FileLoadException>(() => alc.LoadFromAssemblyName(new AssemblyName("MyAssembly")));
Assert.IsType<InvalidOperationException>(error.InnerException);
}
}
}