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

Made the DLL handle-based: several clients per process

- BakaShrot_CreateClient() returns a handle, the first argument of every
  other export (same names); BakaShrot_FreeClient() frees it. No default
  client, no exports without a handle: apps built for the old DLL need
  updating
- Each client has its own settings, connection, answers and errors;
  different clients run at once from threads. A bad handle (freed, made
  up, 0) fails with InvalidArgument, never crashes; handles are never
  reused
- Python: each Client has its own handle, so several can be connected at
  once (the one-Client restriction is gone)
- PureBasic: the prototypes take the handle first; the example runs a
  second client beside the first. Passed with 5.73 LTS and 6.41
- Tests: exports.rs `clients` (two identities at once, bad and freed
  handles, 4 threads on 4 clients, a client freed mid-call); the Python
  check connects two Clients and pushes from two threads

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 23:17:48 +01:00
parent 5b41a06d0c
commit d061ff6f80
10 changed files with 1074 additions and 561 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "bakashrot-capi"
description = "BakaShrot's C DLL: the BakaShrot_* exports over bakashrot-client, one connection per process."
description = "BakaShrot's C DLL: the BakaShrot_* exports over bakashrot-client, as many clients as needed."
version.workspace = true
edition.workspace = true
rust-version.workspace = true
+317 -153
View File
@@ -1,27 +1,29 @@
//! BakaShrot's C DLL (`bakashrot.dll`, `libbakashrot.so`): the old
//! BakaShrot's C DLL (`bakashrot.dll`, `libbakashrot.so`): the
//! `BakaShrot_*` exports over [`bakashrot_client`], for C, PureBasic,
//! Python (ctypes) and anything that loads a DLL. `docs/client.md`
//! has the whole API; `bindings/c/bakashrot.h` is generated from this file.
//!
//! The conventions:
//!
//! - **One connection per process:** set the certificates, connect, send
//! requests, one at a time (calls from several threads wait for each
//! other).
//! - **Clients:** `BakaShrot_CreateClient()` makes one; its handle is the
//! first argument of every other export. Each has its own connection,
//! answers and errors; different clients can be used from different
//! threads at once.
//! - **`A` and `W` exports:** strings in and out as UTF-8 (`char*`) or
//! UTF-16 (`uint16_t*`, `wchar_t*` on Windows), null-terminated.
//! - **Strings out** are copied into the caller's buffer of `BufferSize`
//! bytes: the getter returns the bytes needed, terminator included, and
//! copies nothing if they don't fit (a null buffer asks for the size).
//! - **Failures:** 0 (or -1 where a count or an id is returned), then
//! `BakaShrot_GetLastError()` gives the code (1-99 a server's refusal, the
//! connection still open; 100 and up the client's own) and
//! `BakaShrot_GetLastError(Client)` gives the code (1-99 a server's
//! refusal, the connection still open; 100 and up the client's own) and
//! `BakaShrot_GetRefusalReason()` why, in words. After `ConnectionLost`
//! or `ProtocolError` the connection is closed: connect again.
//! - **Safety:** pointers are null or valid for what's said, strings
//! terminated, buffers writable for their size. A panic inside is caught
//! and reported as `Internal`; nothing allocated here is freed by the
//! caller, or the other way.
//! terminated, buffers writable for their size. A bad handle fails
//! (`InvalidArgument`). A panic inside is caught and reported as
//! `Internal`; nothing allocated here is freed by the caller, or the
//! other way.
#![allow(unsafe_code, non_snake_case, clippy::missing_safety_doc)]
@@ -30,20 +32,24 @@ mod text;
use bakashrot_client::{Client, Error, Settings};
use bakashrot_proto::ErrorCode;
use bakashrot_proto::packets::{MessageEntry, NameKind};
use std::collections::BTreeMap;
use std::ffi::{c_char, c_void};
use std::fs::File;
use std::io::Cursor;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::path::Path;
use std::sync::{Mutex, MutexGuard, PoisonError};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use text::{Text, Utf8, Utf16};
type Result<T> = std::result::Result<T, Error>;
/// Everything the DLL holds: its one connection, its settings, the last
/// answers.
static STATE: Mutex<State> = Mutex::new(State::new());
/// The next handle, and the clients by handle. A call holds its client's
/// `Arc`, so a client freed during a call goes when the call ends.
static CLIENTS: Mutex<(i64, BTreeMap<i64, Shared>)> = Mutex::new((1, BTreeMap::new()));
type Shared = Arc<Mutex<State>>;
/// A client: its settings, its connection, the last answers.
struct State {
ca: Option<String>,
certificate: Option<String>,
@@ -124,15 +130,22 @@ impl State {
}
}
fn state() -> MutexGuard<'static, State> {
STATE.lock().unwrap_or_else(PoisonError::into_inner)
fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
mutex.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Runs a call that can fail: the last error reset, then set if it fails
/// (or panics), `failed` returned then.
fn call<T>(failed: T, run: impl FnOnce(&mut State) -> Result<T>) -> T {
fn find(client: i64) -> Option<Shared> {
lock(&CLIENTS).1.get(&client).cloned()
}
/// Runs a call that can fail on a client: its last error reset, then set
/// if it fails (or panics). `failed` then, or for a bad handle.
fn call<T>(client: i64, failed: T, run: impl FnOnce(&mut State) -> Result<T>) -> T {
let Some(shared) = find(client) else {
return failed;
};
let outcome = catch_unwind(AssertUnwindSafe(|| {
let mut state = state();
let mut state = lock(&shared);
state.last_error = 0;
state.reason.clear();
let outcome = run(&mut state);
@@ -142,20 +155,29 @@ fn call<T>(failed: T, run: impl FnOnce(&mut State) -> Result<T>) -> T {
Ok(Ok(value)) => value,
Ok(Err(())) => failed,
Err(_) => {
state().fail(Error::new(ErrorCode::Internal, "the DLL panicked"));
lock(&shared).fail(Error::new(ErrorCode::Internal, "the DLL panicked"));
failed
}
}
}
/// Runs a getter: the last error stays as it is.
fn get<T>(default: T, read: impl FnOnce(&State) -> T) -> T {
catch_unwind(AssertUnwindSafe(|| read(&state()))).unwrap_or(default)
/// Runs a getter on a client, its last error unchanged. `default` for a
/// bad handle.
fn get<T>(client: i64, default: T, read: impl FnOnce(&mut State) -> T) -> T {
let Some(shared) = find(client) else {
return default;
};
catch_unwind(AssertUnwindSafe(|| read(&mut lock(&shared)))).unwrap_or(default)
}
/// Copies a string of the state into the caller's buffer.
unsafe fn get_text<T: Text>(buffer: *mut T::Unit, size: isize, text: fn(&State) -> &str) -> isize {
get(0, |state| unsafe {
/// Copies a string of a client's state into the caller's buffer.
unsafe fn get_text<T: Text>(
client: i64,
buffer: *mut T::Unit,
size: isize,
text: fn(&State) -> &str,
) -> isize {
get(client, 0, |state| unsafe {
text::copy::<T>(text(state), buffer, size)
})
}
@@ -172,29 +194,71 @@ fn bool_result(ok: bool) -> isize {
isize::from(ok)
}
// --- Clients -----------------------------------------------------------------
/// Makes a client: its handle (1 and up, never reused), 0 if it failed.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_CreateClient() -> i64 {
let made = catch_unwind(|| {
let (next, clients) = &mut *lock(&CLIENTS);
clients.insert(*next, Arc::new(Mutex::new(State::new())));
*next += 1;
*next - 1
});
made.unwrap_or(0)
}
/// Frees a client and closes its connection (once a call on it in another
/// thread ends): 1, or 0 if it wasn't one.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_FreeClient(client: i64) -> isize {
let freed = catch_unwind(|| lock(&CLIENTS).1.remove(&client).is_some());
bool_result(freed.unwrap_or(false))
}
// --- Errors ------------------------------------------------------------------
/// Why the last call failed: a protocol error code (1-99 a server's
/// refusal, the connection still open; 100 and up the client's own), 0 if
/// it succeeded. Getters don't change it.
/// Why the client's last call failed: a protocol error code (1-99 a
/// server's refusal, the connection still open; 100 and up the client's
/// own), 0 if it succeeded. Getters don't change it. `InvalidArgument` for
/// a bad handle.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_GetLastError() -> i32 {
get(ErrorCode::Internal.code() as i32, |state| {
pub extern "C" fn BakaShrot_GetLastError(client: i64) -> i32 {
get(client, ErrorCode::InvalidArgument.code() as i32, |state| {
state.last_error as i32
})
}
/// Why the last call failed, in words: the server's reason for a refusal,
/// the client's otherwise. Empty if it succeeded.
/// The client's reason, or why its handle is bad.
unsafe fn refusal_reason<T: Text>(client: i64, buffer: *mut T::Unit, size: isize) -> isize {
match find(client) {
Some(_) => unsafe { get_text::<T>(client, buffer, size, |state| &state.reason) },
None => {
let why = format!("no client {client}: never made, or freed");
unsafe { text::copy::<T>(&why, buffer, size) }
}
}
}
/// Why the client's last call failed, in words: the server's reason for a
/// refusal, the client's otherwise. Empty if it succeeded.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetRefusalReasonA(buffer: *mut c_char, size: isize) -> isize {
unsafe { get_text::<Utf8>(buffer.cast(), size, |state| &state.reason) }
pub unsafe extern "C" fn BakaShrot_GetRefusalReasonA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe { refusal_reason::<Utf8>(client, buffer.cast(), size) }
}
/// The same as `BakaShrot_GetRefusalReasonA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetRefusalReasonW(buffer: *mut u16, size: isize) -> isize {
unsafe { get_text::<Utf16>(buffer, size, |state| &state.reason) }
pub unsafe extern "C" fn BakaShrot_GetRefusalReasonW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe { refusal_reason::<Utf16>(client, buffer, size) }
}
// --- Settings ----------------------------------------------------------------
@@ -218,12 +282,13 @@ fn pem_or_file(value: Option<String>, what: &str, optional: bool) -> Result<Opti
/// Sets one of the certificates' settings, before connecting.
unsafe fn set<T: Text>(
client: i64,
value: *const T::Unit,
what: &str,
optional: bool,
slot: fn(&mut State) -> &mut Option<String>,
) -> isize {
call(0, |state| {
call(client, 0, |state| {
if state.client.is_some() {
return Err(Error::new(
ErrorCode::AlreadyConnected,
@@ -239,26 +304,43 @@ unsafe fn set<T: Text>(
/// Sets the CA's certificate, which the server's must be issued by: PEM
/// text, or the path of a file holding it. Before connecting.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCaCertificateA(certificate: *const c_char) -> isize {
pub unsafe extern "C" fn BakaShrot_SetCaCertificateA(
client: i64,
certificate: *const c_char,
) -> isize {
unsafe {
set::<Utf8>(certificate.cast(), "CA certificate", false, |state| {
&mut state.ca
})
set::<Utf8>(
client,
certificate.cast(),
"CA certificate",
false,
|state| &mut state.ca,
)
}
}
/// The same as `BakaShrot_SetCaCertificateA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCaCertificateW(certificate: *const u16) -> isize {
unsafe { set::<Utf16>(certificate, "CA certificate", false, |state| &mut state.ca) }
pub unsafe extern "C" fn BakaShrot_SetCaCertificateW(
client: i64,
certificate: *const u16,
) -> isize {
unsafe {
set::<Utf16>(client, certificate, "CA certificate", false, |state| {
&mut state.ca
})
}
}
/// Sets this client's certificate, which says its node and its roles: PEM
/// Sets the client's certificate, which says its node and its roles: PEM
/// text, or the path of a file holding it. Before connecting.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCertificateA(certificate: *const c_char) -> isize {
pub unsafe extern "C" fn BakaShrot_SetCertificateA(
client: i64,
certificate: *const c_char,
) -> isize {
unsafe {
set::<Utf8>(certificate.cast(), "certificate", false, |state| {
set::<Utf8>(client, certificate.cast(), "certificate", false, |state| {
&mut state.certificate
})
}
@@ -266,45 +348,49 @@ pub unsafe extern "C" fn BakaShrot_SetCertificateA(certificate: *const c_char) -
/// The same as `BakaShrot_SetCertificateA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCertificateW(certificate: *const u16) -> isize {
pub unsafe extern "C" fn BakaShrot_SetCertificateW(client: i64, certificate: *const u16) -> isize {
unsafe {
set::<Utf16>(certificate, "certificate", false, |state| {
set::<Utf16>(client, certificate, "certificate", false, |state| {
&mut state.certificate
})
}
}
/// Sets this client's private key, its certificate's: PEM text, or the path
/// Sets the client's private key, its certificate's: PEM text, or the path
/// of a file holding it. Before connecting.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetPrivateKeyA(key: *const c_char) -> isize {
unsafe { set::<Utf8>(key.cast(), "private key", false, |state| &mut state.key) }
pub unsafe extern "C" fn BakaShrot_SetPrivateKeyA(client: i64, key: *const c_char) -> isize {
unsafe {
set::<Utf8>(client, key.cast(), "private key", false, |state| {
&mut state.key
})
}
}
/// The same as `BakaShrot_SetPrivateKeyA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetPrivateKeyW(key: *const u16) -> isize {
unsafe { set::<Utf16>(key, "private key", false, |state| &mut state.key) }
pub unsafe extern "C" fn BakaShrot_SetPrivateKeyW(client: i64, key: *const u16) -> isize {
unsafe { set::<Utf16>(client, key, "private key", false, |state| &mut state.key) }
}
/// Sets the CA's CRL, optional: servers it revokes are refused. PEM text,
/// or the path of a file holding it (one that isn't there is a
/// `FileError`); null or empty for none. Before connecting.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCrlA(crl: *const c_char) -> isize {
unsafe { set::<Utf8>(crl.cast(), "CRL", true, |state| &mut state.crl) }
pub unsafe extern "C" fn BakaShrot_SetCrlA(client: i64, crl: *const c_char) -> isize {
unsafe { set::<Utf8>(client, crl.cast(), "CRL", true, |state| &mut state.crl) }
}
/// The same as `BakaShrot_SetCrlA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SetCrlW(crl: *const u16) -> isize {
unsafe { set::<Utf16>(crl, "CRL", true, |state| &mut state.crl) }
pub unsafe extern "C" fn BakaShrot_SetCrlW(client: i64, crl: *const u16) -> isize {
unsafe { set::<Utf16>(client, crl, "CRL", true, |state| &mut state.crl) }
}
// --- The connection ----------------------------------------------------------
unsafe fn connect<T: Text>(host: *const T::Unit, port: i32) -> isize {
call(0, |state| {
unsafe fn connect<T: Text>(client: i64, host: *const T::Unit, port: i32) -> isize {
call(client, 0, |state| {
if state.client.is_some() {
return Err(Error::new(
ErrorCode::AlreadyConnected,
@@ -349,44 +435,47 @@ unsafe fn connect<T: Text>(host: *const T::Unit, port: i32) -> isize {
/// the server refused the certificate, `ServerNotVerified` this client
/// refused the server's, `NotAllowed` none of its roles is the server's).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_ConnectA(host: *const c_char, port: i32) -> isize {
unsafe { connect::<Utf8>(host.cast(), port) }
pub unsafe extern "C" fn BakaShrot_ConnectA(client: i64, host: *const c_char, port: i32) -> isize {
unsafe { connect::<Utf8>(client, host.cast(), port) }
}
/// The same as `BakaShrot_ConnectA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_ConnectW(host: *const u16, port: i32) -> isize {
unsafe { connect::<Utf16>(host, port) }
pub unsafe extern "C" fn BakaShrot_ConnectW(client: i64, host: *const u16, port: i32) -> isize {
unsafe { connect::<Utf16>(client, host, port) }
}
/// 1 while connected: notices when the server closed the connection
/// (forgotten then: connect again).
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_IsConnected() -> isize {
let open = catch_unwind(AssertUnwindSafe(|| {
let mut state = state();
pub extern "C" fn BakaShrot_IsConnected(client: i64) -> isize {
get(client, 0, |state| {
let open = state.client.as_ref().is_some_and(Client::is_open);
if !open {
state.client = None;
}
open
}));
bool_result(open.unwrap_or(false))
bool_result(open)
})
}
/// Closes the connection: 0 if there was none. The settings stay.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_Disconnect() -> isize {
let closed = catch_unwind(AssertUnwindSafe(|| state().client.take().is_some()));
bool_result(closed.unwrap_or(false))
pub extern "C" fn BakaShrot_Disconnect(client: i64) -> isize {
get(client, 0, |state| {
bool_result(state.client.take().is_some())
})
}
/// The node this client is, as the server said when connecting: its
/// certificate's name. Empty when not connected.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetNodeA(buffer: *mut c_char, size: isize) -> isize {
pub unsafe extern "C" fn BakaShrot_GetNodeA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe {
get_text::<Utf8>(buffer.cast(), size, |state| {
get_text::<Utf8>(client, buffer.cast(), size, |state| {
state.client.as_ref().map_or("", Client::node)
})
}
@@ -394,9 +483,9 @@ pub unsafe extern "C" fn BakaShrot_GetNodeA(buffer: *mut c_char, size: isize) ->
/// The same as `BakaShrot_GetNodeA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetNodeW(buffer: *mut u16, size: isize) -> isize {
pub unsafe extern "C" fn BakaShrot_GetNodeW(client: i64, buffer: *mut u16, size: isize) -> isize {
unsafe {
get_text::<Utf16>(buffer, size, |state| {
get_text::<Utf16>(client, buffer, size, |state| {
state.client.as_ref().map_or("", Client::node)
})
}
@@ -415,8 +504,8 @@ unsafe fn topic_and_data<T: Text>(
Ok((topic, data))
}
unsafe fn push_to<T: Text>(topic: *const T::Unit, data: *const T::Unit) -> i64 {
call(-1, |state| {
unsafe fn push_to<T: Text>(client: i64, topic: *const T::Unit, data: *const T::Unit) -> i64 {
call(client, -1, |state| {
let (topic, data) = unsafe { topic_and_data::<T>(topic, data)? };
let id = state.client()?.push(&topic, &data)?;
Ok(id as i64)
@@ -428,16 +517,21 @@ unsafe fn push_to<T: Text>(topic: *const T::Unit, data: *const T::Unit) -> i64 {
/// (`ConnectionLost`) may have been stored all the same.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageToA(
client: i64,
topic: *const c_char,
data: *const c_char,
) -> i64 {
unsafe { push_to::<Utf8>(topic.cast(), data.cast()) }
unsafe { push_to::<Utf8>(client, topic.cast(), data.cast()) }
}
/// The same as `BakaShrot_PushMessageToA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageToW(topic: *const u16, data: *const u16) -> i64 {
unsafe { push_to::<Utf16>(topic, data) }
pub unsafe extern "C" fn BakaShrot_PushMessageToW(
client: i64,
topic: *const u16,
data: *const u16,
) -> i64 {
unsafe { push_to::<Utf16>(client, topic, data) }
}
// --- Reading -----------------------------------------------------------------
@@ -452,8 +546,8 @@ unsafe fn topic_and_node<T: Text>(
Ok((topic, node))
}
unsafe fn latest_of<T: Text>(topic: *const T::Unit, node: *const T::Unit) -> isize {
call(0, |state| {
unsafe fn latest_of<T: Text>(client: i64, topic: *const T::Unit, node: *const T::Unit) -> isize {
call(client, 0, |state| {
let (topic, node) = unsafe { topic_and_node::<T>(topic, node)? };
state.clear_messages(&topic, &node);
let answer = state.client()?.get_messages(&topic, &node, None, 1)?;
@@ -472,28 +566,31 @@ unsafe fn latest_of<T: Text>(topic: *const T::Unit, node: *const T::Unit) -> isi
/// `BakaShrot_GetMessage*()` getters. `NotFound` if there's none yet.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetLatestMessageOfA(
client: i64,
topic: *const c_char,
node: *const c_char,
) -> isize {
unsafe { latest_of::<Utf8>(topic.cast(), node.cast()) }
unsafe { latest_of::<Utf8>(client, topic.cast(), node.cast()) }
}
/// The same as `BakaShrot_GetLatestMessageOfA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetLatestMessageOfW(
client: i64,
topic: *const u16,
node: *const u16,
) -> isize {
unsafe { latest_of::<Utf16>(topic, node) }
unsafe { latest_of::<Utf16>(client, topic, node) }
}
unsafe fn messages<T: Text>(
client: i64,
topic: *const T::Unit,
node: *const T::Unit,
after: i64,
limit: i32,
) -> i32 {
call(-1, |state| {
call(client, -1, |state| {
let (topic, node) = unsafe { topic_and_node::<T>(topic, node)? };
state.clear_messages(&topic, &node);
let after =
@@ -515,36 +612,38 @@ unsafe fn messages<T: Text>(
/// `BakaShrot_HasMoreMessages()` says whether more follow the last one.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessagesA(
client: i64,
topic: *const c_char,
node: *const c_char,
after: i64,
limit: i32,
) -> i32 {
unsafe { messages::<Utf8>(topic.cast(), node.cast(), after, limit) }
unsafe { messages::<Utf8>(client, topic.cast(), node.cast(), after, limit) }
}
/// The same as `BakaShrot_GetMessagesA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessagesW(
client: i64,
topic: *const u16,
node: *const u16,
after: i64,
limit: i32,
) -> i32 {
unsafe { messages::<Utf16>(topic, node, after, limit) }
unsafe { messages::<Utf16>(client, topic, node, after, limit) }
}
/// How many messages the last `GetMessages` or `GetLatestMessageOf` got.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_GetMessageCount() -> i32 {
get(0, |state| state.messages.len() as i32)
pub extern "C" fn BakaShrot_GetMessageCount(client: i64) -> i32 {
get(client, 0, |state| state.messages.len() as i32)
}
/// Selects the message at `index` (0 to the count - 1) for the getters: 0
/// if there's none there (`InvalidArgument`), the selection unchanged.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_SelectMessage(index: i32) -> isize {
call(0, |state| {
pub extern "C" fn BakaShrot_SelectMessage(client: i64, index: i32) -> isize {
call(client, 0, |state| {
let index = usize::try_from(index)
.ok()
.filter(|index| *index < state.messages.len())
@@ -557,14 +656,14 @@ pub extern "C" fn BakaShrot_SelectMessage(index: i32) -> isize {
/// 1 if more messages follow the last one the last `GetMessages` got: ask
/// again after its id.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_HasMoreMessages() -> isize {
get(0, |state| bool_result(state.more))
pub extern "C" fn BakaShrot_HasMoreMessages(client: i64) -> isize {
get(client, 0, |state| bool_result(state.more))
}
/// The selected message's id, 0 if none.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_GetMessageId() -> i64 {
get(0, |state| {
pub extern "C" fn BakaShrot_GetMessageId(client: i64) -> i64 {
get(client, 0, |state| {
state.message().map_or(0, |message| message.id as i64)
})
}
@@ -572,8 +671,8 @@ pub extern "C" fn BakaShrot_GetMessageId() -> i64 {
/// When the selected message was pushed, by the server it was pushed to:
/// milliseconds since 1970-01-01 UTC, 0 if none.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_GetMessagePushedAt() -> i64 {
get(0, |state| {
pub extern "C" fn BakaShrot_GetMessagePushedAt(client: i64) -> i64 {
get(client, 0, |state| {
state
.message()
.map_or(0, |message| message.pushed_at as i64)
@@ -582,8 +681,8 @@ pub extern "C" fn BakaShrot_GetMessagePushedAt() -> i64 {
/// The selected message's attachment's size, 0 without one.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_GetMessageAttachmentSize() -> i64 {
get(0, |state| {
pub extern "C" fn BakaShrot_GetMessageAttachmentSize(client: i64) -> i64 {
get(client, 0, |state| {
state
.message()
.and_then(|message| message.attachment.as_ref())
@@ -594,8 +693,8 @@ pub extern "C" fn BakaShrot_GetMessageAttachmentSize() -> i64 {
/// 1 if the selected message's attachment is all on this server (a
/// replicated one may still be on its way).
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_IsMessageAttachmentReady() -> isize {
get(0, |state| {
pub extern "C" fn BakaShrot_IsMessageAttachmentReady(client: i64) -> isize {
get(client, 0, |state| {
let attachment = state
.message()
.and_then(|message| message.attachment.as_ref());
@@ -605,9 +704,13 @@ pub extern "C" fn BakaShrot_IsMessageAttachmentReady() -> isize {
/// The selected message's data, as pushed (JSON or not).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageDataA(buffer: *mut c_char, size: isize) -> isize {
pub unsafe extern "C" fn BakaShrot_GetMessageDataA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe {
get_text::<Utf8>(buffer.cast(), size, |state| {
get_text::<Utf8>(client, buffer.cast(), size, |state| {
state.message().map_or("", |message| &message.data)
})
}
@@ -615,9 +718,13 @@ pub unsafe extern "C" fn BakaShrot_GetMessageDataA(buffer: *mut c_char, size: is
/// The same as `BakaShrot_GetMessageDataA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageDataW(buffer: *mut u16, size: isize) -> isize {
pub unsafe extern "C" fn BakaShrot_GetMessageDataW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe {
get_text::<Utf16>(buffer, size, |state| {
get_text::<Utf16>(client, buffer, size, |state| {
state.message().map_or("", |message| &message.data)
})
}
@@ -625,32 +732,48 @@ pub unsafe extern "C" fn BakaShrot_GetMessageDataW(buffer: *mut u16, size: isize
/// The topic the last `GetMessages` or `GetLatestMessageOf` asked for.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageTopicA(buffer: *mut c_char, size: isize) -> isize {
unsafe { get_text::<Utf8>(buffer.cast(), size, |state| &state.topic) }
pub unsafe extern "C" fn BakaShrot_GetMessageTopicA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe { get_text::<Utf8>(client, buffer.cast(), size, |state| &state.topic) }
}
/// The same as `BakaShrot_GetMessageTopicA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageTopicW(buffer: *mut u16, size: isize) -> isize {
unsafe { get_text::<Utf16>(buffer, size, |state| &state.topic) }
pub unsafe extern "C" fn BakaShrot_GetMessageTopicW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe { get_text::<Utf16>(client, buffer, size, |state| &state.topic) }
}
/// The node the last `GetMessages` or `GetLatestMessageOf` asked for.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageNodeA(buffer: *mut c_char, size: isize) -> isize {
unsafe { get_text::<Utf8>(buffer.cast(), size, |state| &state.node) }
pub unsafe extern "C" fn BakaShrot_GetMessageNodeA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe { get_text::<Utf8>(client, buffer.cast(), size, |state| &state.node) }
}
/// The same as `BakaShrot_GetMessageNodeA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetMessageNodeW(buffer: *mut u16, size: isize) -> isize {
unsafe { get_text::<Utf16>(buffer, size, |state| &state.node) }
pub unsafe extern "C" fn BakaShrot_GetMessageNodeW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe { get_text::<Utf16>(client, buffer, size, |state| &state.node) }
}
// --- Names -------------------------------------------------------------------
fn list(kind: NameKind) -> i32 {
call(-1, |state| {
fn list(client: i64, kind: NameKind) -> i32 {
call(client, -1, |state| {
state.names.clear();
let list = state.client()?.list_names(kind)?;
let kind = match kind {
@@ -672,36 +795,44 @@ fn list(kind: NameKind) -> i32 {
/// (every one if its roles have `list_topics` or `query`): how many, -1 on
/// failure. Then `BakaShrot_GetNamesList()`.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_ListTopics() -> i32 {
list(NameKind::Topic)
pub extern "C" fn BakaShrot_ListTopics(client: i64) -> i32 {
list(client, NameKind::Topic)
}
/// Lists the nodes with messages in the topics this client may read (every
/// one if its roles have `list_nodes` or `query`): how many, -1 on failure. Then
/// `BakaShrot_GetNamesList()`.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_ListNodes() -> i32 {
list(NameKind::Node)
pub extern "C" fn BakaShrot_ListNodes(client: i64) -> i32 {
list(client, NameKind::Node)
}
/// The last list, as JSON:
/// `{"kind":"topic","truncated":false,"names":["logs",...]}`, `truncated`
/// if some didn't fit.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetNamesListA(buffer: *mut c_char, size: isize) -> isize {
unsafe { get_text::<Utf8>(buffer.cast(), size, |state| &state.names) }
pub unsafe extern "C" fn BakaShrot_GetNamesListA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe { get_text::<Utf8>(client, buffer.cast(), size, |state| &state.names) }
}
/// The same as `BakaShrot_GetNamesListA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetNamesListW(buffer: *mut u16, size: isize) -> isize {
unsafe { get_text::<Utf16>(buffer, size, |state| &state.names) }
pub unsafe extern "C" fn BakaShrot_GetNamesListW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe { get_text::<Utf16>(client, buffer, size, |state| &state.names) }
}
// --- Queries -----------------------------------------------------------------
unsafe fn query<T: Text>(sql: *const T::Unit) -> isize {
call(0, |state| {
unsafe fn query<T: Text>(client: i64, sql: *const T::Unit) -> isize {
call(client, 0, |state| {
state.query.clear();
let sql = required(unsafe { text::read::<T>(sql, "query")? }, "query")?;
state.query = state.client()?.query(&sql)?;
@@ -712,27 +843,35 @@ unsafe fn query<T: Text>(sql: *const T::Unit) -> isize {
/// Runs read-only SQL on the server (one SELECT or WITH), if this client
/// may query. Then `BakaShrot_GetQueryResult()`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_QueryA(sql: *const c_char) -> isize {
unsafe { query::<Utf8>(sql.cast()) }
pub unsafe extern "C" fn BakaShrot_QueryA(client: i64, sql: *const c_char) -> isize {
unsafe { query::<Utf8>(client, sql.cast()) }
}
/// The same as `BakaShrot_QueryA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_QueryW(sql: *const u16) -> isize {
unsafe { query::<Utf16>(sql) }
pub unsafe extern "C" fn BakaShrot_QueryW(client: i64, sql: *const u16) -> isize {
unsafe { query::<Utf16>(client, sql) }
}
/// The last query's result, as JSON:
/// `{"columns":[...],"rows":[[...],...]}`, every value a string or null.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetQueryResultA(buffer: *mut c_char, size: isize) -> isize {
unsafe { get_text::<Utf8>(buffer.cast(), size, |state| &state.query) }
pub unsafe extern "C" fn BakaShrot_GetQueryResultA(
client: i64,
buffer: *mut c_char,
size: isize,
) -> isize {
unsafe { get_text::<Utf8>(client, buffer.cast(), size, |state| &state.query) }
}
/// The same as `BakaShrot_GetQueryResultA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetQueryResultW(buffer: *mut u16, size: isize) -> isize {
unsafe { get_text::<Utf16>(buffer, size, |state| &state.query) }
pub unsafe extern "C" fn BakaShrot_GetQueryResultW(
client: i64,
buffer: *mut u16,
size: isize,
) -> isize {
unsafe { get_text::<Utf16>(client, buffer, size, |state| &state.query) }
}
// --- Attachments -------------------------------------------------------------
@@ -743,11 +882,12 @@ fn message_id(id: i64) -> Result<u64> {
}
unsafe fn push_with_file<T: Text>(
client: i64,
topic: *const T::Unit,
data: *const T::Unit,
path: *const T::Unit,
) -> i64 {
call(-1, |state| {
call(client, -1, |state| {
let (topic, data) = unsafe { topic_and_data::<T>(topic, data)? };
let path = required(unsafe { text::read::<T>(path, "the file")? }, "file")?;
let mut file = File::open(&path)
@@ -766,30 +906,33 @@ unsafe fn push_with_file<T: Text>(
/// (`DataMismatch`).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageWithFileA(
client: i64,
topic: *const c_char,
data: *const c_char,
file_path: *const c_char,
) -> i64 {
unsafe { push_with_file::<Utf8>(topic.cast(), data.cast(), file_path.cast()) }
unsafe { push_with_file::<Utf8>(client, topic.cast(), data.cast(), file_path.cast()) }
}
/// The same as `BakaShrot_PushMessageWithFileA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageWithFileW(
client: i64,
topic: *const u16,
data: *const u16,
file_path: *const u16,
) -> i64 {
unsafe { push_with_file::<Utf16>(topic, data, file_path) }
unsafe { push_with_file::<Utf16>(client, topic, data, file_path) }
}
unsafe fn push_with_data<T: Text>(
client: i64,
topic: *const T::Unit,
data: *const T::Unit,
attachment: *const c_void,
size: i64,
) -> i64 {
call(-1, |state| {
call(client, -1, |state| {
let (topic, data) = unsafe { topic_and_data::<T>(topic, data)? };
if attachment.is_null() {
return Err(invalid("no attachment (a null pointer)"));
@@ -810,32 +953,35 @@ unsafe fn push_with_data<T: Text>(
/// `BakaShrot_PushMessageWithFileA` does.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageWithDataA(
client: i64,
topic: *const c_char,
data: *const c_char,
attachment: *const c_void,
size: i64,
) -> i64 {
unsafe { push_with_data::<Utf8>(topic.cast(), data.cast(), attachment, size) }
unsafe { push_with_data::<Utf8>(client, topic.cast(), data.cast(), attachment, size) }
}
/// The same as `BakaShrot_PushMessageWithDataA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_PushMessageWithDataW(
client: i64,
topic: *const u16,
data: *const u16,
attachment: *const c_void,
size: i64,
) -> i64 {
unsafe { push_with_data::<Utf16>(topic, data, attachment, size) }
unsafe { push_with_data::<Utf16>(client, topic, data, attachment, size) }
}
unsafe fn save<T: Text>(
client: i64,
topic: *const T::Unit,
node: *const T::Unit,
id: i64,
path: *const T::Unit,
) -> i64 {
call(-1, |state| {
call(client, -1, |state| {
let (topic, node) = unsafe { topic_and_node::<T>(topic, node)? };
let path = required(unsafe { text::read::<T>(path, "the file")? }, "file")?;
let size =
@@ -853,27 +999,37 @@ unsafe fn save<T: Text>(
/// failed one leaves no file.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SaveAttachmentA(
client: i64,
topic: *const c_char,
node: *const c_char,
message_id: i64,
file_path: *const c_char,
) -> i64 {
unsafe { save::<Utf8>(topic.cast(), node.cast(), message_id, file_path.cast()) }
unsafe {
save::<Utf8>(
client,
topic.cast(),
node.cast(),
message_id,
file_path.cast(),
)
}
}
/// The same as `BakaShrot_SaveAttachmentA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_SaveAttachmentW(
client: i64,
topic: *const u16,
node: *const u16,
message_id: i64,
file_path: *const u16,
) -> i64 {
unsafe { save::<Utf16>(topic, node, message_id, file_path) }
unsafe { save::<Utf16>(client, topic, node, message_id, file_path) }
}
unsafe fn load<T: Text>(topic: *const T::Unit, node: *const T::Unit, id: i64) -> i64 {
call(-1, |state| {
unsafe fn load<T: Text>(client: i64, topic: *const T::Unit, node: *const T::Unit, id: i64) -> i64 {
call(client, -1, |state| {
state.attachment = Vec::new();
let (topic, node) = unsafe { topic_and_node::<T>(topic, node)? };
let mut attachment = Vec::new();
@@ -891,29 +1047,35 @@ unsafe fn load<T: Text>(topic: *const T::Unit, node: *const T::Unit, id: i64) ->
/// ones): its size, -1 on failure. Then `BakaShrot_GetAttachmentData()`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_LoadAttachmentA(
client: i64,
topic: *const c_char,
node: *const c_char,
message_id: i64,
) -> i64 {
unsafe { load::<Utf8>(topic.cast(), node.cast(), message_id) }
unsafe { load::<Utf8>(client, topic.cast(), node.cast(), message_id) }
}
/// The same as `BakaShrot_LoadAttachmentA`, in UTF-16.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_LoadAttachmentW(
client: i64,
topic: *const u16,
node: *const u16,
message_id: i64,
) -> i64 {
unsafe { load::<Utf16>(topic, node, message_id) }
unsafe { load::<Utf16>(client, topic, node, message_id) }
}
/// Copies what the last `BakaShrot_LoadAttachment()` read into `Buffer`, if
/// `BufferSize` is enough: returns its size (0 if none), copies nothing if
/// it doesn't fit (a null `Buffer` asks for the size).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn BakaShrot_GetAttachmentData(buffer: *mut c_void, buffer_size: i64) -> i64 {
get(0, |state| {
pub unsafe extern "C" fn BakaShrot_GetAttachmentData(
client: i64,
buffer: *mut c_void,
buffer_size: i64,
) -> i64 {
get(client, 0, |state| {
let data = &state.attachment;
if !buffer.is_null() && usize::try_from(buffer_size).is_ok_and(|size| size >= data.len()) {
unsafe { std::ptr::copy_nonoverlapping(data.as_ptr(), buffer.cast(), data.len()) };
@@ -924,7 +1086,9 @@ pub unsafe extern "C" fn BakaShrot_GetAttachmentData(buffer: *mut c_void, buffer
/// Frees what `BakaShrot_LoadAttachment()` read: 1.
#[unsafe(no_mangle)]
pub extern "C" fn BakaShrot_FreeAttachmentData() -> isize {
let freed = catch_unwind(AssertUnwindSafe(|| state().attachment = Vec::new()));
bool_result(freed.is_ok())
pub extern "C" fn BakaShrot_FreeAttachmentData(client: i64) -> isize {
get(client, 0, |state| {
state.attachment = Vec::new();
1
})
}
+348 -127
View File
@@ -1,6 +1,7 @@
//! 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.
//! The exports called as C would, against a server. `exports`: one client,
//! the `W` exports (what PureBasic uses; Python's check covers the `A`
//! ones), the buffers, the errors. `clients`: several clients at once, bad
//! handles, threads.
#![allow(unsafe_code)]
@@ -9,6 +10,8 @@ mod common;
use bakashrot::*;
use bakashrot_proto::ErrorCode;
use std::ffi::{CString, c_char};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
fn w(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
@@ -19,25 +22,28 @@ fn a(text: &str) -> CString {
}
/// 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) };
fn get_w(client: i64, getter: unsafe extern "C" fn(i64, *mut u16, isize) -> isize) -> String {
let size = unsafe { getter(client, std::ptr::null_mut(), 0) };
if size == 0 {
return String::new();
}
let mut buffer = vec![0u16; size as usize / 2];
unsafe { getter(buffer.as_mut_ptr(), size) };
unsafe { getter(client, 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) };
fn get_a(client: i64, getter: unsafe extern "C" fn(i64, *mut c_char, isize) -> isize) -> String {
let size = unsafe { getter(client, std::ptr::null_mut(), 0) };
let mut buffer = vec![0u8; size as usize];
unsafe { getter(buffer.as_mut_ptr().cast(), size) };
unsafe { getter(client, 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) {
/// The client's last error: its code and reason.
fn error(client: i64) -> (u32, String) {
(
BakaShrot_GetLastError() as u32,
get_w(BakaShrot_GetRefusalReasonW),
BakaShrot_GetLastError(client) as u32,
get_w(client, BakaShrot_GetRefusalReasonW),
)
}
@@ -54,172 +60,205 @@ fn exports() {
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);
let client = BakaShrot_CreateClient();
assert!(client >= 1);
unsafe {
// Nothing set, not connected.
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), w("{}").as_ptr()),
BakaShrot_PushMessageToW(client, 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!(error(client).0, code(ErrorCode::NotConnected));
assert_eq!(BakaShrot_ConnectW(client, localhost.as_ptr(), port), 0);
let (failed, why) = error(client);
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()),
BakaShrot_SetCaCertificateW(client, 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!(error(client).0, code(ErrorCode::FileError));
assert_eq!(BakaShrot_SetCertificateW(client, std::ptr::null()), 0);
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
assert_eq!(
BakaShrot_SetCertificateW(w(&pem("kitchen.crt")).as_ptr()),
BakaShrot_SetCaCertificateW(client, w(&file("ca.crt")).as_ptr()),
1
);
assert_eq!(BakaShrot_GetLastError(client), 0);
assert_eq!(
BakaShrot_SetCertificateW(client, w(&pem("kitchen.crt")).as_ptr()),
1
);
assert_eq!(
BakaShrot_SetPrivateKeyA(a(&file("kitchen.key")).as_ptr()),
BakaShrot_SetPrivateKeyA(client, a(&file("kitchen.key")).as_ptr()),
1
);
// A CRL file asked for is needed (client.toml's may be missing).
assert_eq!(BakaShrot_SetCrlW(w(&file("missing.crl")).as_ptr()), 0);
assert_eq!(error().0, code(ErrorCode::FileError));
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_SetCrlW(client, w(&file("missing.crl")).as_ptr()),
0
);
assert_eq!(error(client).0, code(ErrorCode::FileError));
assert_eq!(BakaShrot_SetCrlW(client, std::ptr::null()), 1);
assert_eq!(BakaShrot_ConnectW(client, localhost.as_ptr(), 0), 0);
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
assert_eq!(
BakaShrot_ConnectW(localhost.as_ptr(), port),
BakaShrot_ConnectW(client, localhost.as_ptr(), port),
1,
"{:?}",
error()
error(client)
);
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));
assert_eq!(BakaShrot_IsConnected(client), 1);
assert_eq!(get_w(client, BakaShrot_GetNodeW), "kitchen");
assert_eq!(BakaShrot_ConnectW(client, localhost.as_ptr(), port), 0);
assert_eq!(error(client).0, code(ErrorCode::AlreadyConnected));
assert_eq!(
BakaShrot_SetCaCertificateW(client, w(&file("ca.crt")).as_ptr()),
0
);
assert_eq!(error(client).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()),
BakaShrot_PushMessageToW(client, w("logs").as_ptr(), w(r#"{"i":1}"#).as_ptr()),
1
);
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), w("héllo 🌍").as_ptr()),
BakaShrot_PushMessageToW(client, w("logs").as_ptr(), w("héllo 🌍").as_ptr()),
2
);
assert_eq!(
BakaShrot_PushMessageToW(w("logs").as_ptr(), std::ptr::null()),
BakaShrot_PushMessageToW(client, w("logs").as_ptr(), std::ptr::null()),
3
);
assert_eq!(
BakaShrot_PushMessageToW(std::ptr::null(), w("{}").as_ptr()),
BakaShrot_PushMessageToW(client, std::ptr::null(), w("{}").as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
assert_eq!(
BakaShrot_PushMessageToW(w("nothing").as_ptr(), w("{}").as_ptr()),
BakaShrot_PushMessageToW(client, w("nothing").as_ptr(), w("{}").as_ptr()),
-1
);
let (failed, why) = error();
let (failed, why) = error(client);
assert_eq!(failed, code(ErrorCode::NotAllowed));
assert!(why.contains("may not push to 'nothing'"), "{why}");
// The latest; none yet in another topic.
assert_eq!(
BakaShrot_GetLatestMessageOfW(w("logs").as_ptr(), w("kitchen").as_ptr()),
BakaShrot_GetLatestMessageOfW(client, w("logs").as_ptr(), w("kitchen").as_ptr()),
1
);
assert_eq!(
(BakaShrot_GetMessageCount(), BakaShrot_GetMessageId()),
(
BakaShrot_GetMessageCount(client),
BakaShrot_GetMessageId(client)
),
(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!(get_w(client, BakaShrot_GetMessageDataW), "");
assert!(BakaShrot_GetMessagePushedAt(client) > 1_700_000_000_000);
assert_eq!(BakaShrot_GetMessageAttachmentSize(client), 0);
assert_eq!(BakaShrot_IsMessageAttachmentReady(client), 0);
assert_eq!(
BakaShrot_GetLatestMessageOfW(w("secret").as_ptr(), w("kitchen").as_ptr()),
BakaShrot_GetLatestMessageOfW(client, w("secret").as_ptr(), w("kitchen").as_ptr()),
0
);
assert_eq!(error().0, code(ErrorCode::NotFound));
assert_eq!(BakaShrot_GetMessageCount(), 0);
assert_eq!(error(client).0, code(ErrorCode::NotFound));
assert_eq!(BakaShrot_GetMessageCount(client), 0);
// A run of messages, selected one by one.
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 2),
BakaShrot_GetMessagesW(client, 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_HasMoreMessages(client), 1);
assert_eq!(get_w(client, BakaShrot_GetMessageTopicW), "logs");
assert_eq!(get_w(client, BakaShrot_GetMessageNodeW), "kitchen");
assert_eq!(BakaShrot_GetMessageId(client), 1);
assert_eq!(BakaShrot_SelectMessage(client, 1), 1);
assert_eq!(get_w(client, BakaShrot_GetMessageDataW), "héllo 🌍");
assert_eq!(get_a(client, BakaShrot_GetMessageDataA), "héllo 🌍");
assert_eq!(BakaShrot_SelectMessage(client, 2), 0);
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
assert_eq!(BakaShrot_GetMessageId(client), 2, "the selection stays");
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("kitchen").as_ptr(), 3, 10),
BakaShrot_GetMessagesW(client, w("logs").as_ptr(), w("kitchen").as_ptr(), 3, 10),
0
);
assert_eq!(BakaShrot_HasMoreMessages(), 0);
assert_eq!(BakaShrot_GetMessageId(), 0);
assert_eq!(BakaShrot_HasMoreMessages(client), 0);
assert_eq!(BakaShrot_GetMessageId(client), 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),
BakaShrot_GetMessagesW(
client,
w("logs").as_ptr(),
w("kitchen").as_ptr(),
after,
limit
),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
}
assert_eq!(
BakaShrot_GetMessagesW(w("logs").as_ptr(), w("nobody").as_ptr(), 0, 10),
BakaShrot_GetMessagesW(client, 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");
assert_eq!(error(client).0, code(ErrorCode::UnknownName));
assert_eq!(
BakaShrot_IsConnected(client),
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);
BakaShrot_GetMessagesW(client, w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 1);
assert_eq!(
BakaShrot_GetMessageDataW(client, std::ptr::null_mut(), 0),
16
);
assert_eq!(
BakaShrot_GetMessageDataA(client, std::ptr::null_mut(), 0),
8
);
let mut small = [7u16; 7];
assert_eq!(BakaShrot_GetMessageDataW(small.as_mut_ptr(), 14), 16);
assert_eq!(
BakaShrot_GetMessageDataW(client, 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),
BakaShrot_GetMessageDataW(client, bytes[1..].as_mut_ptr().cast(), 16),
16
);
assert_eq!(bytes[1..3], [b'{', 0]);
// Names, queries.
// Topics: those with messages (nothing in `secret` yet).
assert_eq!(BakaShrot_ListTopics(), 1);
assert_eq!(BakaShrot_ListTopics(client), 1);
assert_eq!(
get_w(BakaShrot_GetNamesListW),
get_w(client, BakaShrot_GetNamesListW),
r#"{"kind":"topic","names":["logs"],"truncated":false}"#
);
assert_eq!(BakaShrot_ListNodes(), 1);
assert_eq!(BakaShrot_ListNodes(client), 1);
assert_eq!(
BakaShrot_QueryW(w("SELECT COUNT(*) AS n FROM data").as_ptr()),
BakaShrot_QueryW(client, w("SELECT COUNT(*) AS n FROM data").as_ptr()),
1
);
assert_eq!(
get_w(BakaShrot_GetQueryResultW),
get_w(client, 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), "");
assert_eq!(BakaShrot_QueryW(client, w("SELECT nope").as_ptr()), 0);
assert_eq!(error(client).0, code(ErrorCode::QueryFailed));
assert_eq!(get_w(client, BakaShrot_GetQueryResultW), "");
// Attachments: from a file or memory, saved or loaded back.
let (logs, kitchen) = (w("logs"), w("kitchen"));
@@ -227,34 +266,36 @@ fn exports() {
std::fs::write(running.folder.join("photo.bin"), &photo).unwrap();
let photo_file = w(&file("photo.bin"));
let pushed = BakaShrot_PushMessageWithFileW(
client,
logs.as_ptr(),
w(r#"{"photo":1}"#).as_ptr(),
photo_file.as_ptr(),
);
assert_eq!(pushed, 4, "{:?}", error());
assert_eq!(pushed, 4, "{:?}", error(client));
let tiny = b"tiny";
let pushed = BakaShrot_PushMessageWithDataW(
client,
logs.as_ptr(),
std::ptr::null(),
tiny.as_ptr().cast(),
4,
);
assert_eq!(pushed, 5, "{:?}", error());
assert_eq!(pushed, 5, "{:?}", error(client));
assert_eq!(
BakaShrot_GetLatestMessageOfW(logs.as_ptr(), kitchen.as_ptr()),
BakaShrot_GetLatestMessageOfW(client, logs.as_ptr(), kitchen.as_ptr()),
1
);
assert_eq!(
(
BakaShrot_GetMessageId(),
BakaShrot_GetMessageAttachmentSize(),
BakaShrot_IsMessageAttachmentReady()
BakaShrot_GetMessageId(client),
BakaShrot_GetMessageAttachmentSize(client),
BakaShrot_IsMessageAttachmentReady(client)
),
(5, 4, 1)
);
let saved = w(&file("saved.bin"));
assert_eq!(
BakaShrot_SaveAttachmentW(logs.as_ptr(), kitchen.as_ptr(), 4, saved.as_ptr()),
BakaShrot_SaveAttachmentW(client, logs.as_ptr(), kitchen.as_ptr(), 4, saved.as_ptr()),
1_500_000
);
assert_eq!(
@@ -262,84 +303,264 @@ fn exports() {
photo
);
assert_eq!(
BakaShrot_LoadAttachmentW(logs.as_ptr(), kitchen.as_ptr(), 5),
BakaShrot_LoadAttachmentW(client, logs.as_ptr(), kitchen.as_ptr(), 5),
4
);
assert_eq!(
BakaShrot_GetAttachmentData(client, std::ptr::null_mut(), 0),
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!(
BakaShrot_GetAttachmentData(client, 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),
BakaShrot_GetAttachmentData(client, data.as_mut_ptr().cast(), 4),
4
);
assert_eq!(&data, tiny);
assert_eq!(BakaShrot_FreeAttachmentData(client), 1);
assert_eq!(
BakaShrot_GetAttachmentData(client, std::ptr::null_mut(), 0),
0
);
assert_eq!(
BakaShrot_LoadAttachmentA(client, a("logs").as_ptr(), a("kitchen").as_ptr(), 4),
1_500_000
);
BakaShrot_FreeAttachmentData();
BakaShrot_FreeAttachmentData(client);
// 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()),
BakaShrot_SaveAttachmentW(client, logs.as_ptr(), kitchen.as_ptr(), 1, none.as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::NotFound));
assert_eq!(error(client).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()),
BakaShrot_SaveAttachmentW(client, logs.as_ptr(), kitchen.as_ptr(), -1, none.as_ptr()),
-1
);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
let missing = w(&file("missing.bin"));
assert_eq!(
BakaShrot_PushMessageWithFileW(logs.as_ptr(), std::ptr::null(), missing.as_ptr()),
BakaShrot_PushMessageWithFileW(
client,
logs.as_ptr(),
std::ptr::null(),
missing.as_ptr()
),
-1
);
assert_eq!(error().0, code(ErrorCode::FileError));
assert_eq!(error(client).0, code(ErrorCode::FileError));
for (pointer, size) in [(std::ptr::null(), 4), (tiny.as_ptr(), 0)] {
let pushed = BakaShrot_PushMessageWithDataW(
client,
logs.as_ptr(),
std::ptr::null(),
pointer.cast(),
size,
);
assert_eq!(pushed, -1);
assert_eq!(error().0, code(ErrorCode::InvalidArgument));
assert_eq!(error(client).0, code(ErrorCode::InvalidArgument));
}
assert_eq!(BakaShrot_IsConnected(), 1);
assert_eq!(BakaShrot_IsConnected(client), 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));
assert_eq!(BakaShrot_Disconnect(client), 1);
assert_eq!(BakaShrot_Disconnect(client), 0);
assert_eq!(BakaShrot_IsConnected(client), 0);
assert_eq!(get_w(client, BakaShrot_GetNodeW), "");
assert_eq!(BakaShrot_QueryW(client, w("SELECT 1").as_ptr()), 0);
assert_eq!(error(client).0, code(ErrorCode::NotConnected));
// Another identity, by the A exports: not allowed to query.
assert_eq!(
BakaShrot_SetCertificateA(a(&file("lounge.crt")).as_ptr()),
BakaShrot_SetCertificateA(client, 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),
BakaShrot_SetPrivateKeyA(client, a(&pem("lounge.key")).as_ptr()),
1
);
assert_eq!(
BakaShrot_ConnectA(client, a("localhost").as_ptr(), port),
1,
"{:?}",
error()
error(client)
);
assert_eq!(get_a(BakaShrot_GetNodeA), "lounge");
assert_eq!(BakaShrot_QueryA(a("SELECT 1").as_ptr()), 0);
let (failed, why) = error();
assert_eq!(get_a(client, BakaShrot_GetNodeA), "lounge");
assert_eq!(BakaShrot_QueryA(client, a("SELECT 1").as_ptr()), 0);
let (failed, why) = error(client);
assert_eq!(failed, code(ErrorCode::NotAllowed));
assert_eq!(get_a(BakaShrot_GetRefusalReasonA), why);
BakaShrot_Disconnect();
assert_eq!(get_a(client, BakaShrot_GetRefusalReasonA), why);
BakaShrot_Disconnect(client);
// Nothing listening there.
assert_eq!(BakaShrot_ConnectA(a("127.0.0.1").as_ptr(), 1), 0);
assert_eq!(error().0, code(ErrorCode::ConnectFailed));
assert_eq!(BakaShrot_ConnectA(client, a("127.0.0.1").as_ptr(), 1), 0);
assert_eq!(error(client).0, code(ErrorCode::ConnectFailed));
}
assert_eq!(BakaShrot_FreeClient(client), 1);
}
/// A new client, connected as `name` (its certificate and key in files).
fn connected(running: &common::Running, name: &str) -> i64 {
let client = BakaShrot_CreateClient();
assert!(client >= 1);
let file = |name: &str| w(&running.folder.join(name).display().to_string());
unsafe {
assert_eq!(
BakaShrot_SetCaCertificateW(client, file("ca.crt").as_ptr()),
1
);
assert_eq!(
BakaShrot_SetCertificateW(client, file(&format!("{name}.crt")).as_ptr()),
1
);
assert_eq!(
BakaShrot_SetPrivateKeyW(client, file(&format!("{name}.key")).as_ptr()),
1
);
let host = w("127.0.0.1");
let connected = BakaShrot_ConnectW(client, host.as_ptr(), i32::from(running.port));
assert_eq!(connected, 1, "{name}: {:?}", error(client));
}
client
}
#[test]
fn clients() {
let running = common::Running::start("clients");
running.issue("kitchen", &["home"]);
running.issue("lounge", &["lounge"]);
unsafe {
// Handles that aren't a client's: refused, never a crash.
assert_eq!(
BakaShrot_PushMessageToW(99, w("logs").as_ptr(), w("{}").as_ptr()),
-1
);
let (failed, why) = error(99);
assert_eq!(failed, code(ErrorCode::InvalidArgument));
assert!(why.contains("no client 99"), "{why}");
assert_eq!(BakaShrot_IsConnected(99), 0);
assert_eq!(BakaShrot_GetMessageId(-5), 0);
assert_eq!(get_w(99, BakaShrot_GetMessageDataW), "");
assert_eq!(BakaShrot_FreeClient(99), 0);
assert_eq!(BakaShrot_FreeClient(0), 0);
let (failed, why) = error(0);
assert_eq!(failed, code(ErrorCode::InvalidArgument), "0 is never one");
assert!(why.contains("no client 0"), "{why}");
// Two identities connected at once.
let kitchen = connected(&running, "kitchen");
let lounge = connected(&running, "lounge");
assert_ne!(kitchen, lounge);
assert_eq!(get_w(kitchen, BakaShrot_GetNodeW), "kitchen");
assert_eq!(get_w(lounge, BakaShrot_GetNodeW), "lounge");
for i in 1..=3 {
let data = w(&format!(r#"{{"i":{i}}}"#));
assert_eq!(
BakaShrot_PushMessageToW(kitchen, w("logs").as_ptr(), data.as_ptr()),
i
);
}
// Each its answers: one reads, the other's selection stays.
assert_eq!(
BakaShrot_GetLatestMessageOfW(kitchen, w("logs").as_ptr(), w("kitchen").as_ptr()),
1
);
assert_eq!(
BakaShrot_GetMessagesW(lounge, w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 10),
3
);
assert_eq!(BakaShrot_SelectMessage(lounge, 1), 1);
assert_eq!(get_w(lounge, BakaShrot_GetMessageDataW), r#"{"i":2}"#);
assert_eq!(BakaShrot_GetMessageId(kitchen), 3);
assert_eq!(get_w(kitchen, BakaShrot_GetMessageDataW), r#"{"i":3}"#);
assert_eq!(BakaShrot_GetMessageCount(kitchen), 1);
// Each its errors: lounge may only read logs.
assert_eq!(
BakaShrot_PushMessageToW(lounge, w("logs").as_ptr(), w("{}").as_ptr()),
-1
);
let (failed, why) = error(lounge);
assert_eq!(failed, code(ErrorCode::NotAllowed));
assert!(why.contains("may not push"), "{why}");
assert_eq!(error(kitchen), (0, String::new()));
// Freed: closed, its handle refused, never given again.
assert_eq!(BakaShrot_FreeClient(lounge), 1);
assert_eq!(BakaShrot_FreeClient(lounge), 0);
assert_eq!(BakaShrot_IsConnected(lounge), 0);
assert_eq!(
BakaShrot_GetMessagesW(lounge, w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 10),
-1
);
assert_eq!(error(lounge).0, code(ErrorCode::InvalidArgument));
assert!(BakaShrot_CreateClient() > lounge.max(kitchen));
assert_eq!(BakaShrot_IsConnected(kitchen), 1, "the other stays");
assert_eq!(BakaShrot_Disconnect(kitchen), 1);
assert_eq!(BakaShrot_IsConnected(kitchen), 0);
assert_eq!(BakaShrot_FreeClient(kitchen), 1);
}
// Threads, each with its client, pushing at once: every push stored,
// each id once.
let threads: Vec<_> = (0..4)
.map(|_| {
let client = connected(&running, "kitchen");
std::thread::spawn(move || {
let ids: Vec<i64> = (0..50)
.map(|i| unsafe {
let data = w(&format!(r#"{{"t":{i}}}"#));
BakaShrot_PushMessageToW(client, w("logs").as_ptr(), data.as_ptr())
})
.collect();
assert_eq!(BakaShrot_FreeClient(client), 1);
ids
})
})
.collect();
let mut ids: Vec<i64> = threads
.into_iter()
.flat_map(|thread| thread.join().unwrap())
.collect();
ids.sort_unstable();
assert_eq!(ids, (4..=203).collect::<Vec<i64>>());
// A client freed while another thread uses it: that thread's calls
// fail from then on, nothing crashes.
let client = connected(&running, "kitchen");
let started = Arc::new(AtomicBool::new(false));
let reader = {
let started = started.clone();
std::thread::spawn(move || {
let mut reads = 0;
loop {
let count = unsafe {
BakaShrot_GetMessagesW(client, w("logs").as_ptr(), w("kitchen").as_ptr(), 0, 50)
};
started.store(true, Ordering::SeqCst);
if count < 0 {
return (reads, error(client).0);
}
reads += 1;
}
})
};
while !started.load(Ordering::SeqCst) {
std::thread::yield_now();
}
assert_eq!(BakaShrot_FreeClient(client), 1);
let (reads, failed) = reader.join().unwrap();
assert!(reads >= 1);
assert_eq!(failed, code(ErrorCode::InvalidArgument));
}