-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Host tests for using host-runtime contract
- Loading branch information
1 parent
5d744ee
commit 4e6645c
Showing
4 changed files
with
106 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/installer/tests/Assets/TestProjects/HostApiInvokerApp/HostRuntimeContract.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// 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.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace HostApiInvokerApp | ||
{ | ||
public static unsafe class HostRuntimeContract | ||
{ | ||
internal struct host_runtime_contract | ||
{ | ||
public void* context; | ||
public IntPtr bundle_probe; | ||
public IntPtr pinvoke_override; | ||
public delegate* unmanaged[Stdcall]<byte*, byte*, nint, void*, nint> get_runtime_property; | ||
} | ||
|
||
private static host_runtime_contract GetContract() | ||
{ | ||
string contractString = (string)AppContext.GetData("HOST_RUNTIME_CONTRACT"); | ||
if (string.IsNullOrEmpty(contractString)) | ||
throw new Exception("HOST_RUNTIME_CONTRACT not found"); | ||
|
||
host_runtime_contract* contract = (host_runtime_contract*)Convert.ToUInt64(contractString, 16); | ||
return *contract; | ||
} | ||
|
||
private static void Test_get_runtime_property(string[] args) | ||
{ | ||
host_runtime_contract contract = GetContract(); | ||
|
||
foreach (string name in args) | ||
{ | ||
string value = GetProperty(name, contract); | ||
Console.WriteLine($"{nameof(host_runtime_contract.get_runtime_property)}: {name} = {(value == null ? "<none>" : value)}"); | ||
} | ||
|
||
static string GetProperty(string name, host_runtime_contract contract) | ||
{ | ||
Span<byte> nameSpan = stackalloc byte[Encoding.UTF8.GetMaxByteCount(name.Length)]; | ||
byte* namePtr = (byte*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(nameSpan)); | ||
int nameLen = Encoding.UTF8.GetBytes(name, nameSpan); | ||
nameSpan[nameLen] = 0; | ||
|
||
nint len = 256; | ||
byte* buffer = stackalloc byte[(int)len]; | ||
nint lenActual = contract.get_runtime_property(namePtr, buffer, len, contract.context); | ||
if (lenActual <= 0) | ||
{ | ||
Console.WriteLine($"No value for {name} - {nameof(host_runtime_contract.get_runtime_property)} returned {lenActual}"); | ||
return null; | ||
} | ||
|
||
if (lenActual <= len) | ||
return Encoding.UTF8.GetString(buffer, (int)lenActual); | ||
|
||
len = lenActual; | ||
byte* expandedBuffer = stackalloc byte[(int)len]; | ||
lenActual = contract.get_runtime_property(namePtr, expandedBuffer, len, contract.context); | ||
return Encoding.UTF8.GetString(expandedBuffer, (int)lenActual); | ||
} | ||
} | ||
|
||
public static bool RunTest(string apiToTest, string[] args) | ||
{ | ||
switch (apiToTest) | ||
{ | ||
case $"{nameof(host_runtime_contract)}.{nameof(host_runtime_contract.get_runtime_property)}": | ||
Test_get_runtime_property(args); | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters