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

Moved the workspace to the root, Added the build scripts, ARM64 releases and the Docker image

Source/'s content is at the root now, Source/README.md merged into
readme.md. Scripts at the root: build.ps1, test.ps1 (was check.ps1),
release.ps1 (over release.py) and clean.ps1. Everything is LF except the
.ps1 files.

CI (.github/workflows/ci.yml) runs on a v* tag or by hand: the checks,
then a release for Windows x64 and ARM64 and Linux x64 and ARM64, each on
its own kind of runner, the archives kept as artifacts. release.py names
the platform from the Rust host, make_wheel.py tags the wheel from the
library's header.

docker/: the server's image (built from source, the tools beside it, uid
10001), compose.yml and test.sh. CI builds amd64 and arm64 natively,
checks them, and on a tag pushes them to ghcr.io with an index for
<version> and latest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 01:13:44 +01:00
parent 17ec69c03c
commit 5045019df4
93 changed files with 634 additions and 277 deletions
+95
View File
@@ -0,0 +1,95 @@
//! A server on a free port, in a folder of its own with its CA and the
//! clients' certificates.
use bakashrot_config::Config;
use bakashrot_pki::{Ca, Identity, Usage};
use bakashrot_server::Server;
use std::path::PathBuf;
const CONFIG: &str = r#"
[server]
listen = "127.0.0.1:0"
ca = "ca.crt"
certificate = "server.crt"
key = "server.key"
topics = ["logs", "secret"]
[roles.home]
push_topics = ["logs", "secret"]
read_topics = ["*"]
query = true
[roles.lounge]
read_topics = ["logs"]
"#;
pub struct Running {
pub folder: PathBuf,
pub port: u16,
ca: Ca,
// Kept for the server's tasks to run.
_runtime: tokio::runtime::Runtime,
}
impl Running {
pub fn start(name: &str) -> Running {
let folder = PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
.join(format!("capi-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&folder);
std::fs::create_dir_all(&folder).unwrap();
let issued = Ca::create("Test CA", 10).unwrap();
let ca = Ca::load(&issued.certificate, &issued.key).unwrap();
let server = ca
.issue(
&Identity::new("server-1", &["peer"]).unwrap(),
Usage::Server,
10,
)
.unwrap();
std::fs::write(folder.join("ca.crt"), &issued.certificate).unwrap();
std::fs::write(folder.join("server.crt"), &server.certificate).unwrap();
std::fs::write(folder.join("server.key"), &server.key).unwrap();
// `SYNC=normal` to measure without a disk flush per push.
let sync = std::env::var("SYNC").unwrap_or_else(|_| "full".into());
let config = CONFIG.replace(
"[roles.home]",
&format!(
"synchronous = \"{sync}\"
[roles.home]"
),
);
std::fs::write(folder.join("server.toml"), config).unwrap();
let config = Config::load(&folder.join("server.toml")).unwrap();
let server = Server::start(config).unwrap();
let runtime = tokio::runtime::Runtime::new().unwrap();
let listener = runtime
.block_on(tokio::net::TcpListener::bind("127.0.0.1:0"))
.unwrap();
let port = listener.local_addr().unwrap().port();
runtime.spawn(async move { server.serve(listener).await });
Running {
folder,
port,
ca,
_runtime: runtime,
}
}
/// Issues a client's certificate, written as `<name>.crt` and
/// `<name>.key`, with a `<name>.toml` to connect with.
pub fn issue(&self, name: &str, roles: &[&str]) {
let issued = self
.ca
.issue(&Identity::new(name, roles).unwrap(), Usage::Client, 10)
.unwrap();
std::fs::write(self.folder.join(format!("{name}.crt")), &issued.certificate).unwrap();
std::fs::write(self.folder.join(format!("{name}.key")), &issued.key).unwrap();
let toml = format!(
"server = \"127.0.0.1:{}\"\nca = \"ca.crt\"\ncertificate = \"{name}.crt\"\nkey = \"{name}.key\"\n",
self.port
);
std::fs::write(self.folder.join(format!("{name}.toml")), toml).unwrap();
}
}
+341
View File
@@ -0,0 +1,341 @@
//! The exports called as C would, against a server: the `W` ones (what
//! PureBasic uses; Python's check covers the `A` ones), the buffers, the
//! errors. One test: the DLL holds one connection per process.
#![allow(unsafe_code)]
mod common;
use bakashrot::*;
use bakashrot_proto::ErrorCode;
use std::ffi::{CString, c_char};
fn w(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
fn a(text: &str) -> CString {
CString::new(text).unwrap()
}
/// A `W` getter's string: asks for the size, then copies.
fn get_w(getter: unsafe extern "C" fn(*mut u16, isize) -> isize) -> String {
let size = unsafe { getter(std::ptr::null_mut(), 0) };
let mut buffer = vec![0u16; size as usize / 2];
unsafe { getter(buffer.as_mut_ptr(), size) };
String::from_utf16(&buffer[..buffer.len() - 1]).unwrap()
}
fn get_a(getter: unsafe extern "C" fn(*mut c_char, isize) -> isize) -> String {
let size = unsafe { getter(std::ptr::null_mut(), 0) };
let mut buffer = vec![0u8; size as usize];
unsafe { getter(buffer.as_mut_ptr().cast(), size) };
String::from_utf8(buffer[..buffer.len() - 1].to_vec()).unwrap()
}
/// The last error's code and reason.
fn error() -> (u32, String) {
(
BakaShrot_GetLastError() as u32,
get_w(BakaShrot_GetRefusalReasonW),
)
}
fn code(code: ErrorCode) -> u32 {
code.code()
}
#[test]
fn exports() {
let running = common::Running::start("exports");
running.issue("kitchen", &["home"]);
running.issue("lounge", &["lounge"]);
let file = |name: &str| running.folder.join(name).display().to_string();
let pem = |name: &str| std::fs::read_to_string(running.folder.join(name)).unwrap();
let localhost = w("127.0.0.1");
let port = i32::from(running.port);
unsafe {
// Nothing set, not connected.
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), w("{}").as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::NotConnected));
assert_eq!(BakaShrot_ConnectW(localhost.as_ptr(), port), 0);
let (failed, why) = error();
assert_eq!(failed, code(ErrorCode::InvalidArgument));
assert!(why.contains("BakaShrot_SetCaCertificate"), "{why}");
// Files or PEM text; a missing file, an empty setting.
assert_eq!(
BakaShrot_SetCaCertificateW(w(&file("missing.crt")).as_ptr()),
0
);
assert_eq!(error().0, code(ErrorCode::FileError));
assert_eq!(BakaShrot_SetCertificateW(std::ptr::null()), 0);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(BakaShrot_SetCaCertificateW(w(&file("ca.crt")).as_ptr()), 1);
assert_eq!(BakaShrot_GetLastError(), 0);
assert_eq!(
BakaShrot_SetCertificateW(w(&pem("kitchen.crt")).as_ptr()),
1
);
assert_eq!(
BakaShrot_SetPrivateKeyA(a(&file("kitchen.key")).as_ptr()),
1
);
assert_eq!(BakaShrot_SetCrlW(std::ptr::null()), 1);
assert_eq!(BakaShrot_ConnectW(localhost.as_ptr(), 0), 0);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(
BakaShrot_ConnectW(localhost.as_ptr(), port),
1,
"{:?}",
error()
);
assert_eq!(BakaShrot_IsConnected(), 1);
assert_eq!(get_w(BakaShrot_GetNodeW), "kitchen");
assert_eq!(BakaShrot_ConnectW(localhost.as_ptr(), port), 0);
assert_eq!(error().0, code(ErrorCode::AlreadyConnected));
assert_eq!(BakaShrot_SetCaCertificateW(w(&file("ca.crt")).as_ptr()), 0);
assert_eq!(error().0, code(ErrorCode::AlreadyConnected));
// Pushes: UTF-16, a null message is an empty one.
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), w(r#"{"i":1}"#).as_ptr()),
1
);
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), w("héllo 🌍").as_ptr()),
2
);
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), std::ptr::null()),
3
);
assert_eq!(
BakaShrot_PushMessageToW(std::ptr::null(), w("{}").as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(
BakaShrot_PushMessageToW(w("nothing").as_ptr(), w("{}").as_ptr()),
-1
);
let (failed, why) = error();
assert_eq!(failed, code(ErrorCode::UnknownName));
assert!(why.contains("unknown topic"), "{why}");
// The latest; none yet in another topic.
assert_eq!(
BakaShrot_GetLatestMessageOfW(w("logs").as_ptr(), w("kitchen").as_ptr()),
1
);
assert_eq!(
(BakaShrot_GetMessageCount(), BakaShrot_GetMessageId()),
(1, 3)
);
assert_eq!(get_w(BakaShrot_GetMessageDataW), "");
assert!(BakaShrot_GetMessagePushedAt() > 1_700_000_000_000);
assert_eq!(BakaShrot_GetMessageAttachmentSize(), 0);
assert_eq!(BakaShrot_IsMessageAttachmentReady(), 0);
assert_eq!(
BakaShrot_GetLatestMessageOfW(w("secret").as_ptr(), w("kitchen").as_ptr()),
0
);
assert_eq!(error().0, code(ErrorCode::NotFound));
assert_eq!(BakaShrot_GetMessageCount(), 0);
// A run of messages, selected one by one.
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 2),
2
);
assert_eq!(BakaShrot_HasMoreMessages(), 1);
assert_eq!(get_w(BakaShrot_GetMessageTopicW), "logs");
assert_eq!(get_w(BakaShrot_GetMessageNodeW), "kitchen");
assert_eq!(BakaShrot_GetMessageId(), 1);
assert_eq!(BakaShrot_SelectMessage(1), 1);
assert_eq!(get_w(BakaShrot_GetMessageDataW), "héllo 🌍");
assert_eq!(get_a(BakaShrot_GetMessageDataA), "héllo 🌍");
assert_eq!(BakaShrot_SelectMessage(2), 0);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(BakaShrot_GetMessageId(), 2, "the selection stays");
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), 3, 10),
0
);
assert_eq!(BakaShrot_HasMoreMessages(), 0);
assert_eq!(BakaShrot_GetMessageId(), 0);
for (after, limit) in [(-1, 10), (0, 0), (0, 10_001)] {
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), after, limit),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
}
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("nobody").as_ptr(), 0, 10),
-1
);
assert_eq!(error().0, code(ErrorCode::UnknownName));
assert_eq!(BakaShrot_IsConnected(), 1, "a refusal keeps the connection");
// The buffers: the size needed, nothing copied if it doesn't fit.
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 1);
assert_eq!(BakaShrot_GetMessageDataW(std::ptr::null_mut(), 0), 16);
assert_eq!(BakaShrot_GetMessageDataA(std::ptr::null_mut(), 0), 8);
let mut small = [7u16; 7];
assert_eq!(BakaShrot_GetMessageDataW(small.as_mut_ptr(), 14), 16);
assert_eq!(small, [7; 7]);
// Unaligned: a byte buffer.
let mut bytes = [0u8; 17];
assert_eq!(
BakaShrot_GetMessageDataW(bytes[1..].as_mut_ptr().cast(), 16),
16
);
assert_eq!(bytes[1..3], [b'{', 0]);
// Names, queries.
assert_eq!(BakaShrot_ListTopics(), 2);
assert_eq!(
get_w(BakaShrot_GetNamesListW),
r#"{"kind":"topic","names":["logs","secret"],"truncated":false}"#
);
assert_eq!(BakaShrot_ListNodes(), 1);
assert_eq!(
BakaShrot_QueryW(w("SELECT COUNT(*) AS n FROM data").as_ptr()),
1
);
assert_eq!(
get_w(BakaShrot_GetQueryResultW),
r#"{"columns":["n"],"rows":[["3"]]}"#
);
assert_eq!(BakaShrot_QueryW(w("SELECT nope").as_ptr()), 0);
assert_eq!(error().0, code(ErrorCode::QueryFailed));
assert_eq!(get_w(BakaShrot_GetQueryResultW), "");
// Attachments: from a file or memory, saved or loaded back.
let (logs, kitchen) = (w("logs"), w("kitchen"));
let photo: Vec<u8> = (0..1_500_000u32).map(|i| (i % 251) as u8).collect();
std::fs::write(running.folder.join("photo.bin"), &photo).unwrap();
let photo_file = w(&file("photo.bin"));
let pushed = BakaShrot_PushMessageWithFileW(
logs.as_ptr(),
w(r#"{"photo":1}"#).as_ptr(),
photo_file.as_ptr(),
);
assert_eq!(pushed, 4, "{:?}", error());
let tiny = b"tiny";
let pushed = BakaShrot_PushMessageWithDataW(
logs.as_ptr(),
std::ptr::null(),
tiny.as_ptr().cast(),
4,
);
assert_eq!(pushed, 5, "{:?}", error());
assert_eq!(
BakaShrot_GetLatestMessageOfW(logs.as_ptr(), kitchen.as_ptr()),
1
);
assert_eq!(
(
BakaShrot_GetMessageId(),
BakaShrot_GetMessageAttachmentSize(),
BakaShrot_IsMessageAttachmentReady()
),
(5, 4, 1)
);
let saved = w(&file("saved.bin"));
assert_eq!(
BakaShrot_SaveAttachmentW(logs.as_ptr(), kitchen.as_ptr(), 4, saved.as_ptr()),
1_500_000
);
assert_eq!(
std::fs::read(running.folder.join("saved.bin")).unwrap(),
photo
);
assert_eq!(
BakaShrot_LoadAttachmentW(logs.as_ptr(), kitchen.as_ptr(), 5),
4
);
assert_eq!(BakaShrot_GetAttachmentData(std::ptr::null_mut(), 0), 4);
let mut small = [9u8; 3];
assert_eq!(BakaShrot_GetAttachmentData(small.as_mut_ptr().cast(), 3), 4);
assert_eq!(small, [9; 3], "nothing copied if it doesn't fit");
let mut data = [0u8; 4];
assert_eq!(BakaShrot_GetAttachmentData(data.as_mut_ptr().cast(), 4), 4);
assert_eq!(&data, tiny);
assert_eq!(BakaShrot_FreeAttachmentData(), 1);
assert_eq!(BakaShrot_GetAttachmentData(std::ptr::null_mut(), 0), 0);
assert_eq!(
BakaShrot_LoadAttachmentA(a("logs").as_ptr(), a("kitchen").as_ptr(), 4),
1_500_000
);
BakaShrot_FreeAttachmentData();
// Refused, or the client's own failures: -1, no file.
let none = w(&file("none.bin"));
assert_eq!(
BakaShrot_SaveAttachmentW(logs.as_ptr(), kitchen.as_ptr(), 1, none.as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::NotFound));
assert!(!running.folder.join("none.bin").exists());
assert_eq!(
BakaShrot_SaveAttachmentW(logs.as_ptr(), kitchen.as_ptr(), -1, none.as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
let missing = w(&file("missing.bin"));
assert_eq!(
BakaShrot_PushMessageWithFileW(logs.as_ptr(), std::ptr::null(), missing.as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::FileError));
for (pointer, size) in [(std::ptr::null(), 4), (tiny.as_ptr(), 0)] {
let pushed = BakaShrot_PushMessageWithDataW(
logs.as_ptr(),
std::ptr::null(),
pointer.cast(),
size,
);
assert_eq!(pushed, -1);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
}
assert_eq!(BakaShrot_IsConnected(), 1);
// Closed: requests refused, the settings kept.
assert_eq!(BakaShrot_Disconnect(), 1);
assert_eq!(BakaShrot_Disconnect(), 0);
assert_eq!(BakaShrot_IsConnected(), 0);
assert_eq!(get_w(BakaShrot_GetNodeW), "");
assert_eq!(BakaShrot_QueryW(w("SELECT 1").as_ptr()), 0);
assert_eq!(error().0, code(ErrorCode::NotConnected));
// Another identity, by the A exports: not allowed to query.
assert_eq!(
BakaShrot_SetCertificateA(a(&file("lounge.crt")).as_ptr()),
1
);
assert_eq!(BakaShrot_SetPrivateKeyA(a(&pem("lounge.key")).as_ptr()), 1);
assert_eq!(
BakaShrot_ConnectA(a("localhost").as_ptr(), port),
1,
"{:?}",
error()
);
assert_eq!(get_a(BakaShrot_GetNodeA), "lounge");
assert_eq!(BakaShrot_QueryA(a("SELECT 1").as_ptr()), 0);
let (failed, why) = error();
assert_eq!(failed, code(ErrorCode::NotAllowed));
assert_eq!(get_a(BakaShrot_GetRefusalReasonA), why);
BakaShrot_Disconnect();
// Nothing listening there.
assert_eq!(BakaShrot_ConnectA(a("127.0.0.1").as_ptr(), 1), 0);
assert_eq!(error().0, code(ErrorCode::ConnectFailed));
}
}
+20
View File
@@ -0,0 +1,20 @@
//! `bindings/c/bakashrot.h` matches the exports: rewritten when it doesn't
//! (the test fails then, check the new one in).
use std::path::Path;
#[test]
fn the_header_is_current() {
let crate_dir = env!("CARGO_MANIFEST_DIR");
let path = Path::new(crate_dir).join("../../bindings/c/bakashrot.h");
let mut generated = Vec::new();
cbindgen::generate(crate_dir)
.expect("cbindgen reads the crate")
.write(&mut generated);
let current = std::fs::read(&path).unwrap_or_default();
if current != generated {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, &generated).unwrap();
panic!("{} was out of date: rewritten, check it in", path.display());
}
}
+66
View File
@@ -0,0 +1,66 @@
//! `bindings/python/check_bakashrot.py` on the DLL against a server: the
//! Python module and the DLL as an app loads it. Skipped (said so) without
//! a Python 3.11+ on the PATH.
mod common;
use std::path::{Path, PathBuf};
use std::process::Command;
#[test]
fn check_bakashrot_py() {
let Some(python) = ["python3", "python"].into_iter().find(|python| {
Command::new(python)
.args(["-c", "import sys, tomllib; sys.exit(0)"])
.output()
.is_ok_and(|output| output.status.success())
}) else {
eprintln!("SKIPPED: no Python 3.11+ on the PATH");
return;
};
let dll = built_dll();
let running = common::Running::start("python");
running.issue("kitchen", &["home"]);
running.issue("lounge", &["lounge"]);
running.issue("guest", &["guest"]);
let bindings = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../bindings/python");
let output = Command::new(python)
.arg(bindings.join("check_bakashrot.py"))
.arg(&dll)
.arg(&running.folder)
.env("PYTHONPATH", &bindings)
.env("PYTHONIOENCODING", "utf-8")
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&output.stdout);
println!("{stdout}");
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
assert!(output.status.success(), "check_bakashrot.py failed");
assert!(stdout.contains("All checks passed"));
}
/// The DLL cargo built with this test: next to it (cargo's older layout),
/// or the newest in this crate's build folders (`build/bakashrot-capi/*/out/`).
fn built_dll() -> PathBuf {
let name = format!(
"{}bakashrot{}",
std::env::consts::DLL_PREFIX,
std::env::consts::DLL_SUFFIX
);
let exe = std::env::current_exe().unwrap();
let here = exe.parent().unwrap();
if here.join(&name).exists() {
return here.join(name);
}
let builds = here.ancestors().nth(2).unwrap();
std::fs::read_dir(builds)
.unwrap()
.filter_map(|entry| Some(entry.ok()?.path().join("out").join(&name)))
.filter_map(|dll| Some((dll.metadata().ok()?.modified().ok()?, dll)))
.max()
.map(|(_, dll)| dll)
.unwrap_or_else(|| panic!("no {name} built near {}", here.display()))
}