-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Nethost.cs
406 lines (363 loc) · 20.3 KB
/
Nethost.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using HostActivation.Tests;
using Microsoft.DotNet.Cli.Build.Framework;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Xunit;
namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
{
public class Nethost : IClassFixture<Nethost.SharedTestState>
{
private const string GetHostFxrPath = "get_hostfxr_path";
private static readonly string HostFxrName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostfxr");
private readonly SharedTestState sharedState;
public Nethost(SharedTestState sharedTestState)
{
sharedState = sharedTestState;
}
[Theory]
[InlineData(true, false, true)]
[InlineData(true, false, false)]
[InlineData(true, true, true)]
[InlineData(true, true, false)]
[InlineData(false, false, true)]
[InlineData(false, false, false)]
[InlineData(false, true, true)]
[InlineData(false, true, false)]
public void GetHostFxrPath_DotNetRootEnvironment(bool explicitLoad, bool useAssemblyPath, bool isValid)
{
string dotNetRoot = isValid ? Path.Combine(sharedState.ValidInstallRoot, "dotnet") : sharedState.InvalidInstallRoot;
CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
.EnableTracingAndCaptureOutputs()
.DotNetRoot(dotNetRoot)
.Execute();
result.Should().HaveStdErrContaining("Using environment variable");
if (isValid)
{
result.Should().Pass()
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
else
{
result.Should().Fail()
.And.ExitWith(1)
.And.HaveStdOutContaining($"{GetHostFxrPath} failed: 0x{Constants.ErrorCode.CoreHostLibMissingFailure.ToString("x")}")
.And.HaveStdErrContaining($"The required library {HostFxrName} could not be found");
}
}
[Theory]
[InlineData(true, false, true)]
[InlineData(true, false, false)]
[InlineData(true, true, true)]
[InlineData(true, true, false)]
[InlineData(false, false, true)]
[InlineData(false, false, false)]
[InlineData(false, true, true)]
[InlineData(false, true, false)]
public void GetHostFxrPath_DotNetRootParameter(bool explicitLoad, bool useAssemblyPath, bool isValid)
{
string dotNetRoot = isValid ? Path.Combine(sharedState.ValidInstallRoot, "dotnet") : sharedState.InvalidInstallRoot;
CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : "nullptr")} {dotNetRoot}")
.EnableTracingAndCaptureOutputs()
.DotNetRoot(null)
.Execute();
result.Should().HaveStdErrContaining("Using dotnet root parameter");
if (isValid)
{
result.Should().Pass()
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
else
{
result.Should().Fail()
.And.ExitWith(1)
.And.HaveStdOutContaining($"{GetHostFxrPath} failed: 0x{Constants.ErrorCode.CoreHostLibMissingFailure.ToString("x")}")
.And.HaveStdErrContaining($"The folder [{Path.Combine(dotNetRoot, "host", "fxr")}] does not exist");
}
}
[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/61131", TestPlatforms.OSX)]
[InlineData(true, false, true, false)]
[InlineData(true, false, true, true)]
[InlineData(true, false, false, false)]
[InlineData(true, false, false, true)]
[InlineData(true, true, true, false)]
[InlineData(true, true, true, true)]
[InlineData(true, true, false, false)]
[InlineData(true, true, false, true)]
[InlineData(false, false, true, false)]
[InlineData(false, false, true, true)]
[InlineData(false, false, false, false)]
[InlineData(false, false, false, true)]
[InlineData(false, true, true, false)]
[InlineData(false, true, true, true)]
[InlineData(false, true, false, false)]
[InlineData(false, true, false, true)]
public void GetHostFxrPath_GlobalInstallation(bool explicitLoad, bool useAssemblyPath, bool useRegisteredLocation, bool isValid)
{
// Override the registry key for self-registered global installs.
// If using the registered location, set the install location value to the valid/invalid root.
// If not using the registered location, do not set the value. When the value does not exist,
// the product falls back to the default install location.
CommandResult result;
string installLocation = Path.Combine(isValid ? sharedState.ValidInstallRoot : sharedState.InvalidInstallRoot, "dotnet");
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(sharedState.NethostPath))
{
if (useRegisteredLocation)
{
registeredInstallLocationOverride.SetInstallLocation((sharedState.RepoDirectories.BuildArchitecture, installLocation));
}
result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to a test directory
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
useRegisteredLocation ? sharedState.InvalidInstallRoot : installLocation)
.DotNetRoot(null)
.Execute();
}
result.Should().HaveStdErrContaining("Using global installation location");
if (isValid)
{
result.Should().Pass()
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
else
{
result.Should().Fail()
.And.ExitWith(1)
.And.HaveStdOutContaining($"{GetHostFxrPath} failed: 0x{Constants.ErrorCode.CoreHostLibMissingFailure.ToString("x")}")
.And.HaveStdErrContaining($"The required library {HostFxrName} could not be found");
}
}
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void GetHostFxrPath_WithAssemblyPath_AppLocalFxr(bool explicitLoad, bool useDotNetRoot)
{
string appLocalFxrDir = Path.Combine(sharedState.BaseDirectory, "appLocalFxr");
Directory.CreateDirectory(appLocalFxrDir);
string assemblyPath = Path.Combine(appLocalFxrDir, "AppLocalFxr.dll");
string hostFxrPath = Path.Combine(appLocalFxrDir, HostFxrName);
File.WriteAllText(assemblyPath, string.Empty);
File.WriteAllText(hostFxrPath, string.Empty);
string dotNetRoot = useDotNetRoot ? Path.Combine(sharedState.ValidInstallRoot, "dotnet") : string.Empty;
Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {assemblyPath} {dotNetRoot}")
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining($"hostfxr_path: {(useDotNetRoot ? sharedState.HostFxrPath : hostFxrPath)}".ToLower());
}
[Fact]
public void GetHostFxrPath_HostFxrAlreadyLoaded()
{
Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false {sharedState.TestAssemblyPath} nullptr {sharedState.ProductHostFxrPath}")
.EnableTracingAndCaptureOutputs()
.Execute()
.Should().Pass()
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.ProductHostFxrPath}".ToLower())
.And.HaveStdErrContaining($"Found previously loaded library {HostFxrName}");
}
[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/61131", TestPlatforms.OSX)]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
[InlineData("{0}", false, true)]
[InlineData("{0}\n", false, true)]
[InlineData("{0}\nSome other text", false, true)]
[InlineData("", false, false)]
[InlineData("\n{0}", false, false)]
[InlineData(" {0}", false, false)]
[InlineData("{0} \n", false, false)]
[InlineData("{0} ", false, false)]
[InlineData("{0}", true, true)]
[InlineData("{0}\n", true, true)]
[InlineData("{0}\nSome other text", true, true)]
[InlineData("", true, false)]
[InlineData("\n{0}", true, false)]
[InlineData(" {0}", true, false)]
[InlineData("{0} \n", true, false)]
[InlineData("{0} ", true, false)]
public void GetHostFxrPath_InstallLocationFile(string value, bool shouldUseArchSpecificInstallLocation, bool shouldPass)
{
string installLocation = Path.Combine(sharedState.ValidInstallRoot, "dotnet");
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(sharedState.NethostPath))
{
if (shouldUseArchSpecificInstallLocation)
registeredInstallLocationOverride.SetInstallLocation((sharedState.RepoDirectories.BuildArchitecture, string.Format(value, installLocation)));
else
registeredInstallLocationOverride.SetInstallLocation((string.Empty, string.Format(value, installLocation)));
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
.Execute();
if (shouldUseArchSpecificInstallLocation)
{
result.Should().HaveLookedForArchitectureSpecificInstallLocation(
registeredInstallLocationOverride.PathValueOverride,
sharedState.RepoDirectories.BuildArchitecture);
}
else
{
result.Should().HaveLookedForDefaultInstallLocation(registeredInstallLocationOverride.PathValueOverride);
}
if (shouldPass)
{
result.Should().Pass()
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
else
{
result.Should().Fail()
.And.ExitWith(1)
.And.HaveStdOutContaining($"{GetHostFxrPath} failed: 0x{Constants.ErrorCode.CoreHostLibMissingFailure.ToString("x")}")
.And.HaveStdErrContaining($"The required library {HostFxrName} could not be found");
}
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/61131", TestPlatforms.OSX)]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void GetHostFxrPath_GlobalInstallation_HasNoDefaultInstallationPath()
{
string installLocation = Path.Combine(sharedState.ValidInstallRoot, "dotnet");
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(sharedState.NethostPath))
{
registeredInstallLocationOverride.SetInstallLocation(new (string, string)[] {
(sharedState.RepoDirectories.BuildArchitecture, installLocation),
("someOtherArch", $"{installLocation}/invalid")
});
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
.Execute();
result.Should().Pass()
.And.HaveLookedForArchitectureSpecificInstallLocation(
registeredInstallLocationOverride.PathValueOverride,
sharedState.RepoDirectories.BuildArchitecture)
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/61131", TestPlatforms.OSX)]
[SkipOnPlatform(TestPlatforms.Windows, "This test targets the install_location config file which is only used on Linux and macOS.")]
public void GetHostFxrPath_GlobalInstallation_ArchitectureSpecificPathIsPickedOverDefaultPath()
{
string installLocation = Path.Combine(sharedState.ValidInstallRoot, "dotnet");
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(sharedState.NethostPath))
{
registeredInstallLocationOverride.SetInstallLocation(new (string, string)[] {
(string.Empty, $"{installLocation}/a/b/c"),
(sharedState.RepoDirectories.BuildArchitecture, installLocation)
});
CommandResult result = Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride)
.EnvironmentVariable( // Redirect the default install location to an invalid location so that it doesn't cause the test to pass
Constants.TestOnlyEnvironmentVariables.DefaultInstallPath,
sharedState.InvalidInstallRoot)
.DotNetRoot(null)
.Execute();
result.Should().Pass()
.And.HaveLookedForArchitectureSpecificInstallLocation(
registeredInstallLocationOverride.PathValueOverride,
sharedState.RepoDirectories.BuildArchitecture)
.And.HaveUsedConfigFileInstallLocation(installLocation)
.And.HaveStdOutContaining($"hostfxr_path: {sharedState.HostFxrPath}".ToLower());
}
}
[Fact]
public void GetHostFxrPath_InvalidParameters()
{
Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false [error]")
.EnableTracingAndCaptureOutputs()
.DotNetRoot(null)
.Execute()
.Should().Fail()
.And.HaveStdOutContaining($"{GetHostFxrPath} failed: 0x{Constants.ErrorCode.InvalidArgFailure.ToString("x")}")
.And.HaveStdErrContaining("Invalid size for get_hostfxr_parameters");
}
[Fact]
public void TracingNotBufferedByDefault()
{
CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false nullptr x")
.EnvironmentVariable("COREHOST_TRACE", "1")
.EnvironmentVariable("COREHOST_TRACEFILE", "Tracing.out")
.MultilevelLookup(true)
.DotNetRoot(null)
.Execute();
result.Should().Fail()
.And.FileExists("Tracing.out")
.And.FileContains("Tracing.out", "Tracing enabled");
}
[Fact]
public void TestOnlyDisabledByDefault()
{
// Intentionally not enabling test-only behavior. This test validates that even if the test-only env. variable is set
// it will not take effect on its own by default.
// To make sure the test is reliable, copy the product binary again into the test folder where we run it from.
// This is to make sure that we're using the unmodified product binary. If some previous test
// enabled test-only product behavior on the binary and didn't correctly cleanup, this test would fail.
File.Copy(
Path.Combine(sharedState.RepoDirectories.HostArtifacts, RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("nethost")),
sharedState.NethostPath,
overwrite: true);
Command.Create(sharedState.NativeHostPath, GetHostFxrPath)
.EnableTracingAndCaptureOutputs()
.EnvironmentVariable(Constants.TestOnlyEnvironmentVariables.GloballyRegisteredPath, sharedState.ValidInstallRoot)
.DotNetRoot(null)
.Execute()
.Should().NotHaveStdErrContaining($"Using global installation location [{sharedState.ValidInstallRoot}] as runtime location.");
}
public class SharedTestState : SharedTestStateBase
{
public string HostFxrPath { get; }
public string InvalidInstallRoot { get; }
public string ValidInstallRoot { get; }
public string TestAssemblyPath { get; }
public string ProductHostFxrPath { get; }
public SharedTestState()
{
InvalidInstallRoot = Path.Combine(BaseDirectory, "invalid");
Directory.CreateDirectory(InvalidInstallRoot);
ValidInstallRoot = Path.Combine(BaseDirectory, "valid");
HostFxrPath = CreateHostFxr(Path.Combine(ValidInstallRoot, "dotnet"));
string appDir = Path.Combine(BaseDirectory, "app");
Directory.CreateDirectory(appDir);
string assemblyPath = Path.Combine(appDir, "App.dll");
File.WriteAllText(assemblyPath, string.Empty);
TestAssemblyPath = assemblyPath;
string productDir = Path.Combine(BaseDirectory, "product");
Directory.CreateDirectory(productDir);
ProductHostFxrPath = Path.Combine(productDir, HostFxrName);
File.Copy(Path.Combine(RepoDirectories.HostArtifacts, HostFxrName), ProductHostFxrPath);
}
private string CreateHostFxr(string destinationDirectory)
{
string fxrRoot = Path.Combine(destinationDirectory, "host", "fxr");
Directory.CreateDirectory(fxrRoot);
string[] versions = new string[] { "1.1.0", "2.2.1", "2.3.0" };
foreach (string version in versions)
{
string versionDirectory = Path.Combine(fxrRoot, version);
Directory.CreateDirectory(versionDirectory);
File.WriteAllText(Path.Combine(versionDirectory, HostFxrName), string.Empty);
}
return Path.Combine(fxrRoot, "2.3.0", HostFxrName);
}
}
}
}