1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

Added a C# binding, packaged as NibblePoker.BakaShrot.Client on NuGet

- bindings/csharp/NibblePoker.BakaShrot.Client: P/Invoke over the DLL's
  UTF-16 exports, one Client per handle (namespace NibblePoker.BakaShrot).
  C# 2 and the .NET 2.0 BCL only: net35 (.NET Framework 2.0 and up) and
  netstandard2.0 (.NET Core 2.0 and up, .NET 5 and up)
- No JSON or TOML parsing: ListTopics, ListNodes and Query return the raw
  JSON string for the caller to parse; settings go to the constructor
- The package: the four native libraries under runtimes/<rid>/native/,
  their THIRD-PARTY.txt under licenses/<rid>/, and a build/net35 .targets
  copying the right one next to .NET Framework apps (which ignore
  runtimes/)
- CI: a nuget job after the four release builds packs it with the
  release's version and libraries, checks the listing and keeps it as
  the bakashrot-nuget artifact
- Tests: an NUnit project (net48 and net8.0) against a real server with
  certificates from `bakashrot cert`, 13 tests
- release.py ships BakaShrot.cs in csharp/; docs/client.md, building.md,
  readme.md, revamp.md (step 14) updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 16:50:05 +01:00
parent d061ff6f80
commit 1a1603f3f3
13 changed files with 1075 additions and 5 deletions
@@ -0,0 +1,144 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using NUnit.Framework;
namespace NibblePoker.BakaShrot.Tests
{
/// <summary>A server for the tests, on a free port, in its own folder
/// under target/csharp-tests/ with its certificates: made with the
/// programs cargo built (target/debug, or BAKASHROT_BIN).</summary>
[SetUpFixture]
public class Server
{
public static string Folder = "";
public static int Port;
private static Process? process;
private static readonly bool Windows = Path.DirectorySeparatorChar == '\\';
[OneTimeSetUp]
public void Start()
{
string root = FindRepository();
string bin = Environment.GetEnvironmentVariable("BAKASHROT_BIN") ?? Path.Combine(root, "target", "debug");
string exe = Windows ? ".exe" : "";
string library = Windows ? "bakashrot.dll" : "libbakashrot.so";
if (!File.Exists(Path.Combine(bin, library)))
Assert.Fail($"No {library} in {bin}: cargo build first (or set BAKASHROT_BIN)");
// Before any call into it: DllImport finds it next to the tests.
File.Copy(Path.Combine(bin, library), Path.Combine(TestContext.CurrentContext.TestDirectory, library), true);
Folder = Path.Combine(root, "target", "csharp-tests", Process.GetCurrentProcess().Id.ToString());
if (Directory.Exists(Folder))
Directory.Delete(Folder, true);
Directory.CreateDirectory(Folder);
string tool = Path.Combine(bin, "bakashrot" + exe);
Run(tool, "cert ca --name test-ca");
Run(tool, "cert issue --server --name server-1");
Run(tool, "cert issue --name kitchen --role home");
Run(tool, "cert issue --name lounge --role lounge");
Run(tool, "cert issue --name guest --role guest");
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
Port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
File.WriteAllText(Path.Combine(Folder, "server.toml"), $@"[server]
listen = ""127.0.0.1:{Port}""
ca = ""ca.crt""
certificate = ""server-1.crt""
key = ""server-1.key""
database = ""bakashrot.sqlite""
[roles.home]
push_topics = [""*""]
read_topics = [""*""]
query = true
[roles.lounge]
read_topics = [""logs""]
");
process = Process.Start(new ProcessStartInfo(Path.Combine(bin, "bakashrot-server" + exe), "server.toml")
{
WorkingDirectory = Folder,
UseShellExecute = false,
CreateNoWindow = true,
});
DateTime until = DateTime.UtcNow.AddSeconds(15);
while (true)
{
if (process!.HasExited)
Assert.Fail($"bakashrot-server exited ({process.ExitCode})");
try
{
using (TcpClient client = new TcpClient())
{
client.Connect(IPAddress.Loopback, Port);
return;
}
}
catch (SocketException)
{
if (DateTime.UtcNow > until)
Assert.Fail("bakashrot-server isn't listening after 15 s");
Thread.Sleep(100);
}
}
}
[OneTimeTearDown]
public void Stop()
{
if (process != null && !process.HasExited)
{
process.Kill();
process.WaitForExit(10000);
}
try
{
Directory.Delete(Folder, true);
}
catch (IOException)
{
}
}
public static string In(string name) => Path.Combine(Folder, name);
public static Client Connect(string name) =>
new Client("127.0.0.1", Port, In("ca.crt"), In(name + ".crt"), In(name + ".key"), null).Connect();
private static void Run(string exe, string arguments)
{
using Process run = Process.Start(new ProcessStartInfo(exe, arguments)
{
WorkingDirectory = Folder,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
})!;
string output = run.StandardOutput.ReadToEnd() + run.StandardError.ReadToEnd();
run.WaitForExit();
if (run.ExitCode != 0)
Assert.Fail($"{Path.GetFileName(exe)} {arguments}: {output}");
}
// The folder with Cargo.toml, above the tests' build folder.
private static string FindRepository()
{
DirectoryInfo? folder = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
while (folder != null && !File.Exists(Path.Combine(folder.FullName, "Cargo.toml")))
folder = folder.Parent;
if (folder == null)
Assert.Fail("No Cargo.toml above " + TestContext.CurrentContext.TestDirectory);
return folder!.FullName;
}
}
}