refactor: drop internal bridge/ffi layers; exporter family lands
Single-lib cleanup: the per-crate src/bridge/ and src/ffi.rs layers are gone (oakundo/oakcommon/oaknode/oaktimeline/oakcodec/oakaudio/ oakrender/oaktask/oakplugin/oakstorage); cross-crate calls are plain Rust, CHandle marshalling shrinks to the oakengine boundary, and tests call the Rust APIs directly (pure C-ABI wrapper tests removed where the domain layer already covers the behavior). exporter.h family implemented: oakengine_export_render (CLI contract), oakengine_export_render_with_params (was a stub), last_error and progress callback; synchronous path reuses task_create_export + start_sync. Fixes on the way: oaktask video ticket self-deadlock, audio params dropped on the export path, codec encoder AAC slicing and H.264 time base. Real-mp4 tests cover both entry points, progress and the illegal-argument matrix. Also: oakstorage session maps null project handles to None (version- info path), configstore test double literal 3.14 -> 3.15 (clippy PI lint), oakaudio output callback scratch buffer + env-aware P1 test, cli media round-trip test uses a generated 16-frame clip (no more minute-long debug runs).
This commit is contained in:
@@ -729,7 +729,7 @@ mod tests {
|
||||
assert_eq!(format_g(-0.0), "-0");
|
||||
assert_eq!(format_g(1920.0), "1920");
|
||||
assert_eq!(format_g(100.0), "100");
|
||||
assert_eq!(format_g(3.14), "3.14");
|
||||
assert_eq!(format_g(3.15), "3.15");
|
||||
assert_eq!(format_g(1.5), "1.5");
|
||||
assert_eq!(format_g(0.1), "0.1");
|
||||
assert_eq!(format_g(-3.5), "-3.5");
|
||||
@@ -848,9 +848,9 @@ mod tests {
|
||||
let _g = test_lock().lock().unwrap();
|
||||
let s = ConfigStore::instance();
|
||||
s.reset_defaults().unwrap();
|
||||
s.set_double(None, "MyD", 3.14);
|
||||
assert_eq!(s.get_double(None, "MyD", -1.0), 3.14);
|
||||
assert_eq!(s.get(None, "MyD").unwrap(), "3.14");
|
||||
s.set_double(None, "MyD", 3.15);
|
||||
assert_eq!(s.get_double(None, "MyD", -1.0), 3.15);
|
||||
assert_eq!(s.get(None, "MyD").unwrap(), "3.15");
|
||||
assert_eq!(s.entry_type(None, "MyD").unwrap(), EntryType::Double);
|
||||
|
||||
assert_eq!(s.get_double(None, "MissingD", 2.5), 2.5);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -27,7 +27,6 @@ pub mod commandlineparser;
|
||||
pub mod configstore;
|
||||
pub mod debug;
|
||||
pub mod error;
|
||||
pub mod ffi;
|
||||
pub mod ffmpegutils;
|
||||
pub mod filefunctions;
|
||||
pub mod handle;
|
||||
|
||||
@@ -41,6 +41,7 @@ pub struct Subtitle {
|
||||
}
|
||||
|
||||
/// `olive::SubtitleParams` — a handle-wrapped subtitle parameter set.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct SubtitleParams {
|
||||
/// Stream index within the source file.
|
||||
stream_index: i32,
|
||||
|
||||
@@ -56,7 +56,7 @@ pub enum ColorRange {
|
||||
}
|
||||
|
||||
/// `olive::VideoParams` — a handle-wrapped video parameter set.
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct VideoParams {
|
||||
/// Frame width in pixels.
|
||||
width: i32,
|
||||
|
||||
@@ -1,354 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! FFI-level integration tests for the C-ABI `colortransform` exports in
|
||||
//! `oakcommon::ffi::colortransform`, asserted against the C++ oracle
|
||||
//! `src/common/c_api/colortransform.cpp` and the Rust domain
|
||||
//! `src/common/rust/src/colortransform.rs`.
|
||||
//!
|
||||
//! The crate exports the two constructors, `free`, `is_display`, and the
|
||||
//! four two-stage getters; only the C++-typed `init_from_native` /
|
||||
//! `get_native` adapters stay with the C++ adapter layer. The tests
|
||||
//! cover:
|
||||
//! - constructor success: a stamped (`OAKCOMMON_ABI_VERSION`), non-null
|
||||
//! handle with live `addref`/`release` callbacks, whose boxed
|
||||
//! [`ColorTransform`] carries the right fields (peeked via
|
||||
//! `oakcommon::handle::get`);
|
||||
//! - constructor failure: a null string argument yields a null handle
|
||||
//! (guard semantics: any error or panic also collapses to a null
|
||||
//! handle);
|
||||
//! - the free contract (release once, nullify, no-op on null);
|
||||
//! - `is_display` / `get_display` / `get_output` / `get_view` /
|
||||
//! `get_look` happy paths, two-stage short-buffer behavior, and
|
||||
//! empty-handle error codes;
|
||||
//! - distinct handles plus the ownership contract (init yields reference
|
||||
//! count 1; addref/release adjust it atomically; both free cleanly).
|
||||
|
||||
use std::ffi::{c_char, CString};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use oakcommon::colortransform::ColorTransform;
|
||||
use oakcommon::ffi::colortransform::*;
|
||||
use oakcommon::handle::{get, CHandle, RefBox, OAKCOMMON_ABI_VERSION};
|
||||
|
||||
/// Convert a string slice to a NUL-terminated C string for FFI inputs.
|
||||
fn to_cstring(s: &str) -> CString {
|
||||
CString::new(s).expect("test string must not contain NUL")
|
||||
}
|
||||
|
||||
/// Cheap struct copy: `CHandle` is neither `Clone` nor `Copy`; rebuilding
|
||||
/// from the same fields duplicates only the handle value — the box stays
|
||||
/// alive as long as the original handle lives.
|
||||
fn dup(h: &CHandle) -> CHandle {
|
||||
CHandle {
|
||||
ctx: h.ctx,
|
||||
addref: h.addref,
|
||||
release: h.release,
|
||||
abi_version: h.abi_version,
|
||||
}
|
||||
}
|
||||
|
||||
/// Release one reference and nullify the handle. `oakcommon_colortransform_free`
|
||||
/// is declared in the header but not exported by this crate, so the free
|
||||
/// contract is driven through this exact replica of the crate-private
|
||||
/// `free_handle` (and the C++ oracle): release once, write `CHandle::null()`.
|
||||
fn free(h: &mut CHandle) {
|
||||
if let Some(rel) = h.release {
|
||||
unsafe {
|
||||
rel(h.ctx);
|
||||
}
|
||||
}
|
||||
*h = CHandle::null();
|
||||
}
|
||||
|
||||
/// Pointer form of [`free`], mirroring `free_handle(*mut CHandle)`: a null
|
||||
/// pointer is a no-op.
|
||||
fn free_ptr(p: *mut CHandle) {
|
||||
if p.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
if let Some(h) = p.as_ref() {
|
||||
if let Some(rel) = h.release {
|
||||
rel(h.ctx);
|
||||
}
|
||||
}
|
||||
p.write(CHandle::null());
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the reference count behind an owned handle (test-only peek into
|
||||
/// the crate-public `RefBox` layout; the box is guaranteed alive here).
|
||||
unsafe fn refs_of(h: &CHandle) -> u32 {
|
||||
unsafe {
|
||||
(*(h.ctx as *const RefBox<ColorTransform>))
|
||||
.refs
|
||||
.load(Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
/// A successful `init_output` yields a stamped, non-null handle whose boxed
|
||||
/// transform is an output transform with the requested name; `free`
|
||||
/// nullifies it.
|
||||
#[test]
|
||||
fn init_output_success() {
|
||||
let mut h = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(h.addref.is_some());
|
||||
assert!(h.release.is_some());
|
||||
|
||||
let t: &ColorTransform = unsafe { get::<ColorTransform>(&h) }.expect("boxed transform");
|
||||
assert!(!t.is_display());
|
||||
assert_eq!(t.output(), "sRGB");
|
||||
assert_eq!(t.display(), "");
|
||||
assert_eq!(t.view(), "");
|
||||
assert_eq!(t.look(), "");
|
||||
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
}
|
||||
|
||||
/// A null output string yields a null handle with no callbacks.
|
||||
#[test]
|
||||
fn init_output_null_yields_null_handle() {
|
||||
let h = oakcommon_colortransform_init_output(std::ptr::null::<c_char>());
|
||||
assert!(h.is_null());
|
||||
assert!(h.ctx.is_null());
|
||||
assert!(h.addref.is_none());
|
||||
assert!(h.release.is_none());
|
||||
// The shared `null()` stamps no ABI version (single-lib unification).
|
||||
assert_eq!(h.abi_version, 0);
|
||||
}
|
||||
|
||||
/// An empty output name is a valid value: the transform is still an output
|
||||
/// transform (C++ default-constructor parity).
|
||||
#[test]
|
||||
fn init_output_empty_string_succeeds() {
|
||||
let mut h = oakcommon_colortransform_init_output(to_cstring("").as_ptr());
|
||||
assert!(!h.is_null());
|
||||
let t: &ColorTransform = unsafe { get::<ColorTransform>(&h) }.expect("boxed transform");
|
||||
assert!(!t.is_display());
|
||||
assert_eq!(t.output(), "");
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
}
|
||||
|
||||
/// A successful `init_display` yields a stamped, non-null handle whose
|
||||
/// boxed transform carries the display/view/look names.
|
||||
#[test]
|
||||
fn init_display_success() {
|
||||
let mut h = oakcommon_colortransform_init_display(
|
||||
to_cstring("DCI-P3").as_ptr(),
|
||||
to_cstring("standard").as_ptr(),
|
||||
to_cstring("soft").as_ptr(),
|
||||
);
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(h.addref.is_some());
|
||||
assert!(h.release.is_some());
|
||||
|
||||
let t: &ColorTransform = unsafe { get::<ColorTransform>(&h) }.expect("boxed transform");
|
||||
assert!(t.is_display());
|
||||
assert_eq!(t.display(), "DCI-P3");
|
||||
assert_eq!(t.view(), "standard");
|
||||
assert_eq!(t.look(), "soft");
|
||||
assert_eq!(t.output(), "");
|
||||
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
}
|
||||
|
||||
/// Any single null argument — and all-null — yields a null handle.
|
||||
#[test]
|
||||
fn init_display_null_any_arg_yields_null_handle() {
|
||||
let d = to_cstring("DCI-P3");
|
||||
let v = to_cstring("standard");
|
||||
let l = to_cstring("soft");
|
||||
let cases = [
|
||||
(d.as_ptr(), v.as_ptr(), std::ptr::null::<c_char>()),
|
||||
(d.as_ptr(), std::ptr::null::<c_char>(), l.as_ptr()),
|
||||
(std::ptr::null::<c_char>(), v.as_ptr(), l.as_ptr()),
|
||||
(
|
||||
std::ptr::null::<c_char>(),
|
||||
std::ptr::null::<c_char>(),
|
||||
std::ptr::null::<c_char>(),
|
||||
),
|
||||
];
|
||||
for (display, view, look) in cases {
|
||||
let h = oakcommon_colortransform_init_display(display, view, look);
|
||||
assert!(h.is_null());
|
||||
assert!(h.ctx.is_null());
|
||||
assert!(h.addref.is_none());
|
||||
assert!(h.release.is_none());
|
||||
// The shared `null()` stamps no ABI version (single-lib unification).
|
||||
assert_eq!(h.abi_version, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Empty display/view/look strings are valid; the explicit `is_display`
|
||||
/// flag keeps the transform identifiable as a display transform.
|
||||
#[test]
|
||||
fn init_display_empty_strings_succeed() {
|
||||
let mut h = oakcommon_colortransform_init_display(
|
||||
to_cstring("").as_ptr(),
|
||||
to_cstring("").as_ptr(),
|
||||
to_cstring("").as_ptr(),
|
||||
);
|
||||
assert!(!h.is_null());
|
||||
let t: &ColorTransform = unsafe { get::<ColorTransform>(&h) }.expect("boxed transform");
|
||||
assert!(t.is_display());
|
||||
assert_eq!(t.display(), "");
|
||||
assert_eq!(t.view(), "");
|
||||
assert_eq!(t.look(), "");
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
}
|
||||
|
||||
/// The free contract: nullify, idempotent, and no-op on a null handle or a
|
||||
/// null pointer.
|
||||
#[test]
|
||||
fn free_contract() {
|
||||
// A real handle: free nullifies it and a second free is safe.
|
||||
let mut h = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
assert!(!h.is_null());
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
assert!(h.ctx.is_null());
|
||||
assert!(h.release.is_none());
|
||||
free(&mut h);
|
||||
assert!(h.is_null());
|
||||
|
||||
// Freeing an already-null handle is a no-op.
|
||||
free(&mut CHandle::null());
|
||||
|
||||
// Freeing a null pointer is a no-op.
|
||||
free_ptr(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
/// Two output transforms are distinct handles (different boxes); init
|
||||
/// yields reference count 1, addref/release adjust it atomically, and both
|
||||
/// free cleanly. `dup` copies the handle value but shares the same box.
|
||||
#[test]
|
||||
fn init_outputs_are_distinct_and_release_cleanly() {
|
||||
let mut a = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
let mut b = oakcommon_colortransform_init_output(to_cstring("Display P3").as_ptr());
|
||||
assert!(!a.is_null());
|
||||
assert!(!b.is_null());
|
||||
assert_ne!(a.ctx, b.ctx);
|
||||
|
||||
// dup copies the handle value and shares the same underlying box.
|
||||
let c = dup(&a);
|
||||
assert_eq!(c.ctx, a.ctx);
|
||||
|
||||
// Ownership contract: count 1 at init, atomic addref/release.
|
||||
unsafe {
|
||||
assert_eq!(refs_of(&a), 1);
|
||||
assert_eq!(refs_of(&b), 1);
|
||||
(a.addref.unwrap())(a.ctx);
|
||||
assert_eq!(refs_of(&a), 2);
|
||||
(a.release.unwrap())(a.ctx);
|
||||
assert_eq!(refs_of(&a), 1);
|
||||
}
|
||||
|
||||
// Both release cleanly (single release + nullify, no double-free).
|
||||
free(&mut a);
|
||||
assert!(a.is_null());
|
||||
free(&mut b);
|
||||
assert!(b.is_null());
|
||||
}
|
||||
|
||||
/// `is_display` distinguishes display transforms from output transforms
|
||||
/// and reports OAKCOMMON_E_INVALID on an empty handle.
|
||||
#[test]
|
||||
fn is_display_paths() {
|
||||
let mut d = oakcommon_colortransform_init_display(
|
||||
to_cstring("sRGB").as_ptr(),
|
||||
to_cstring("standard").as_ptr(),
|
||||
to_cstring("").as_ptr(),
|
||||
);
|
||||
let mut o = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
assert_eq!(oakcommon_colortransform_is_display(dup(&d)), 1);
|
||||
assert_eq!(oakcommon_colortransform_is_display(dup(&o)), 0);
|
||||
assert_eq!(
|
||||
oakcommon_colortransform_is_display(CHandle::null()),
|
||||
oakcommon::error::OAKCOMMON_E_INVALID
|
||||
);
|
||||
free(&mut d);
|
||||
free(&mut o);
|
||||
}
|
||||
|
||||
/// Two-stage getters: exact-fit returns content, short buffer leaves the
|
||||
/// buffer untouched but reports the required size, empty handle yields
|
||||
/// OAKCOMMON_E_INVALID.
|
||||
#[test]
|
||||
fn getters_two_stage_and_empty_handle() {
|
||||
let mut d = oakcommon_colortransform_init_display(
|
||||
to_cstring("P3").as_ptr(),
|
||||
to_cstring("std").as_ptr(),
|
||||
to_cstring("soft").as_ptr(),
|
||||
);
|
||||
|
||||
let mut buf = [0i8; 32];
|
||||
let need = oakcommon_colortransform_get_display(dup(&d), buf.as_mut_ptr(), 32);
|
||||
assert_eq!(need, 3); // "P3" + NUL
|
||||
assert_eq!(
|
||||
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
"P3"
|
||||
);
|
||||
|
||||
let need = oakcommon_colortransform_get_view(dup(&d), buf.as_mut_ptr(), 2);
|
||||
assert_eq!(need, 4); // "std" + NUL, too small: nothing written
|
||||
|
||||
let need = oakcommon_colortransform_get_look(dup(&d), buf.as_mut_ptr(), 32);
|
||||
assert_eq!(need, 5);
|
||||
assert_eq!(
|
||||
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
"soft"
|
||||
);
|
||||
|
||||
let o = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
let need = oakcommon_colortransform_get_output(o, buf.as_mut_ptr(), 32);
|
||||
assert_eq!(need, 5);
|
||||
assert_eq!(
|
||||
unsafe { std::ffi::CStr::from_ptr(buf.as_ptr()) }
|
||||
.to_str()
|
||||
.unwrap(),
|
||||
"sRGB"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_colortransform_get_display(CHandle::null(), buf.as_mut_ptr(), 32),
|
||||
oakcommon::error::OAKCOMMON_E_INVALID
|
||||
);
|
||||
free(&mut d);
|
||||
}
|
||||
|
||||
/// The real `oakcommon_colortransform_free` export: releases once,
|
||||
/// nullifies, no-ops on a null pointer.
|
||||
#[test]
|
||||
fn free_export_contract() {
|
||||
let mut h = oakcommon_colortransform_init_output(to_cstring("sRGB").as_ptr());
|
||||
assert!(!h.is_null());
|
||||
oakcommon_colortransform_free(&mut h);
|
||||
assert!(h.is_null());
|
||||
oakcommon_colortransform_free(&mut h); // already null: no-op
|
||||
oakcommon_colortransform_free(std::ptr::null_mut()); // null pointer: no-op
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,519 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! FFI integration tests for the `config` submodule of the C ABI layer
|
||||
//! (`oakcommon::ffi::config`, backed by `include/common/config.h`).
|
||||
//!
|
||||
//! Every exported symbol is exercised with both a success and a failure
|
||||
//! path. The store is a process-wide singleton and the C-ABI `config`
|
||||
//! functions have no handle, so the failure paths are the documented
|
||||
//! behaviors: null/empty keys, invalid buffers, and absent entries.
|
||||
//!
|
||||
//! Tests use per-test unique keys so they stay independent of each other
|
||||
//! and of the domain unit tests, and every test that mutates the singleton
|
||||
//! (or the `OAK_CONFIG_DIR` env override) serializes on a local mutex. The
|
||||
//! crate's `test_support::env_lock` is `#[cfg(test)]` and therefore not
|
||||
//! visible to this integration-test crate, and the domain unit tests run in
|
||||
//! a separate process anyway, so a local lock is sufficient.
|
||||
|
||||
use std::ffi::{c_char, c_void, CStr, CString};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::ptr::{null, null_mut};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
use oakcommon::error::{
|
||||
OAKCOMMON_E_FAILED, OAKCOMMON_E_INVALID, OAKCOMMON_E_NOT_FOUND, OAKCOMMON_OK,
|
||||
};
|
||||
use oakcommon::ffi::config::*;
|
||||
|
||||
// ---- Test infrastructure --------------------------------------------------
|
||||
|
||||
/// Serializes tests that mutate the process-wide singleton store or the
|
||||
/// `OAK_CONFIG_DIR` env override. Without this, a `reset_defaults`/`load`
|
||||
/// running between another test's `set` and `get` would clear its key.
|
||||
static LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
fn lock() -> MutexGuard<'static, ()> {
|
||||
LOCK.lock().unwrap()
|
||||
}
|
||||
|
||||
static KEY_SEQ: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
/// A per-test unique key, so parallel tests cannot collide on the shared
|
||||
/// store.
|
||||
fn unique_key(name: &str) -> CString {
|
||||
let n = KEY_SEQ.fetch_add(1, Ordering::Relaxed);
|
||||
CString::new(format!("itest_{}_{}_{}", name, std::process::id(), n)).unwrap()
|
||||
}
|
||||
|
||||
fn cstr(s: &str) -> CString {
|
||||
CString::new(s).unwrap()
|
||||
}
|
||||
|
||||
/// Cleans up the `OAK_CONFIG_DIR` override and its temp dir even when the
|
||||
/// test body panics.
|
||||
struct TempConfigDir(PathBuf);
|
||||
|
||||
impl Drop for TempConfigDir {
|
||||
fn drop(&mut self) {
|
||||
std::env::remove_var("OAK_CONFIG_DIR");
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Clears the process-global error handler on drop so a panicking test
|
||||
/// cannot leak it to the rest of the suite.
|
||||
struct HandlerGuard;
|
||||
|
||||
impl Drop for HandlerGuard {
|
||||
fn drop(&mut self) {
|
||||
oakcommon_config_set_error_handler(None, std::ptr::null_mut());
|
||||
}
|
||||
}
|
||||
|
||||
/// Point `OAK_CONFIG_DIR` at an isolated temp dir, run `f`, then clean up.
|
||||
/// Mirrors the `with_temp_config` pattern of the domain unit tests.
|
||||
fn with_temp_config<T>(f: impl FnOnce(&Path) -> T) -> T {
|
||||
let _guard = lock();
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("oakcommon_ffi_config_test_{}", std::process::id()));
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
std::env::set_var("OAK_CONFIG_DIR", &dir);
|
||||
let _cleanup = TempConfigDir(dir.clone());
|
||||
f(&dir)
|
||||
}
|
||||
|
||||
// ---- Error handler recording ---------------------------------------------
|
||||
|
||||
static HANDLER_HITS: AtomicUsize = AtomicUsize::new(0);
|
||||
static HANDLER_USERDATA: AtomicUsize = AtomicUsize::new(0);
|
||||
static HANDLER_TITLE: Mutex<Option<String>> = Mutex::new(None);
|
||||
|
||||
unsafe extern "C" fn record_handler(
|
||||
title: *const c_char,
|
||||
_message: *const c_char,
|
||||
userdata: *mut c_void,
|
||||
) {
|
||||
HANDLER_HITS.fetch_add(1, Ordering::SeqCst);
|
||||
HANDLER_USERDATA.store(userdata as usize, Ordering::SeqCst);
|
||||
if !title.is_null() {
|
||||
let s = unsafe { CStr::from_ptr(title) }
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
*HANDLER_TITLE.lock().unwrap() = Some(s);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- load / save ----------------------------------------------------------
|
||||
|
||||
/// `oakcommon_config_load` treats a missing `config.ini` as a non-error.
|
||||
#[test]
|
||||
fn config_load_missing_file_is_ok() {
|
||||
with_temp_config(|_| {
|
||||
assert_eq!(oakcommon_config_load(), OAKCOMMON_OK);
|
||||
});
|
||||
}
|
||||
|
||||
/// `save` persists the store; `load` re-reads it (resetting to defaults
|
||||
/// first), so the round trip restores the custom key.
|
||||
#[test]
|
||||
fn config_save_load_roundtrip() {
|
||||
with_temp_config(|_| {
|
||||
let key = unique_key("save_roundtrip");
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr("persisted").as_ptr());
|
||||
assert_eq!(oakcommon_config_save(), OAKCOMMON_OK);
|
||||
assert_eq!(oakcommon_config_load(), OAKCOMMON_OK);
|
||||
let mut buf = [0i8; 32];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), 32);
|
||||
assert_eq!(n, 10); // "persisted" + NUL
|
||||
assert_eq!(
|
||||
unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap(),
|
||||
"persisted"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// A save that cannot write reports through the registered error handler
|
||||
/// and returns `OAKCOMMON_E_FAILED`. `OAK_CONFIG_DIR` points at a regular
|
||||
/// file so `"<dir>/config.ini.tmp"` cannot be created (mirrors the domain
|
||||
/// unit test).
|
||||
#[test]
|
||||
fn config_save_failure_reports_and_returns_failed() {
|
||||
let _guard = lock();
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"oakcommon_ffi_config_blocked_{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = std::fs::create_dir_all(&dir);
|
||||
let blocker = dir.join("not_a_dir");
|
||||
std::fs::write(&blocker, b"x").unwrap();
|
||||
std::env::set_var("OAK_CONFIG_DIR", &blocker);
|
||||
let _cleanup = TempConfigDir(dir);
|
||||
|
||||
HANDLER_HITS.store(0, Ordering::SeqCst);
|
||||
HANDLER_USERDATA.store(0, Ordering::SeqCst);
|
||||
*HANDLER_TITLE.lock().unwrap() = None;
|
||||
let userdata: usize = 0xdead_beef;
|
||||
let _handler_guard = HandlerGuard;
|
||||
assert_eq!(
|
||||
oakcommon_config_set_error_handler(Some(record_handler), userdata as *mut c_void),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
|
||||
assert_eq!(oakcommon_config_save(), OAKCOMMON_E_FAILED);
|
||||
assert_eq!(HANDLER_HITS.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(HANDLER_USERDATA.load(Ordering::SeqCst), userdata);
|
||||
assert_eq!(
|
||||
HANDLER_TITLE.lock().unwrap().as_deref(),
|
||||
Some("Error saving settings")
|
||||
);
|
||||
}
|
||||
|
||||
// ---- reset_defaults -------------------------------------------------------
|
||||
|
||||
/// `reset_defaults` drops custom keys and restores the compiled-in
|
||||
/// defaults.
|
||||
#[test]
|
||||
fn config_reset_defaults_restores_defaults() {
|
||||
let _guard = lock();
|
||||
let custom = unique_key("reset_custom");
|
||||
oakcommon_config_set_int(null(), custom.as_ptr(), 1);
|
||||
assert_eq!(oakcommon_config_get_int(null(), custom.as_ptr(), -1), 1);
|
||||
|
||||
assert_eq!(oakcommon_config_reset_defaults(), OAKCOMMON_OK);
|
||||
|
||||
// The custom key is gone...
|
||||
assert_eq!(oakcommon_config_get_int(null(), custom.as_ptr(), -1), -1);
|
||||
assert_eq!(
|
||||
oakcommon_config_entry_type(null(), custom.as_ptr()),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
// ...and the compiled-in defaults are back.
|
||||
assert_eq!(
|
||||
oakcommon_config_get_int(null(), cstr("DefaultSequenceWidth").as_ptr(), -1),
|
||||
1920
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_config_get_bool(null(), cstr("UseProxyMedia").as_ptr(), -1),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
// ---- string set / get -----------------------------------------------------
|
||||
|
||||
/// Null keys/values are silently ignored by `oakcommon_config_set`.
|
||||
#[test]
|
||||
fn config_set_null_key_is_noop() {
|
||||
let _guard = lock();
|
||||
oakcommon_config_set(null(), null(), cstr("v").as_ptr());
|
||||
oakcommon_config_set(null(), cstr("k").as_ptr(), null());
|
||||
}
|
||||
|
||||
/// Two-stage getter, stage 1: a null buffer with size 0 returns the
|
||||
/// required size (length + NUL) without writing.
|
||||
#[test]
|
||||
fn config_get_size_query_returns_required() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("size_query");
|
||||
let value = "hello";
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr(value).as_ptr());
|
||||
let required = oakcommon_config_get(null(), key.as_ptr(), null_mut(), 0);
|
||||
assert_eq!(required, value.len() as i32 + 1);
|
||||
}
|
||||
|
||||
/// Two-stage getter, short buffer: the full required size is returned but
|
||||
/// the buffer is NOT touched (non-truncating, unlike `copy_setting`).
|
||||
#[test]
|
||||
fn config_get_short_buffer_is_non_truncating() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("short_buf");
|
||||
let value = "hello world"; // required = 12
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr(value).as_ptr());
|
||||
let mut buf = [0x7a as c_char; 8];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), buf.len() as i32);
|
||||
assert_eq!(n, value.len() as i32 + 1);
|
||||
assert_eq!(buf, [0x7a as c_char; 8]); // sentinel untouched
|
||||
}
|
||||
|
||||
/// Two-stage getter, exact fit: a buffer of exactly `len + 1` receives the
|
||||
/// string plus NUL terminator.
|
||||
#[test]
|
||||
fn config_get_exact_fit_buffer() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("exact_fit");
|
||||
let value = "hello";
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr(value).as_ptr());
|
||||
let required = value.len() as i32 + 1;
|
||||
let mut buf = vec![0i8; required as usize];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), required);
|
||||
assert_eq!(n, required);
|
||||
assert_eq!(
|
||||
unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap(),
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
/// A missing key yields `OAKCOMMON_E_NOT_FOUND`.
|
||||
#[test]
|
||||
fn config_get_missing_key_returns_not_found() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("missing");
|
||||
let mut buf = [0i8; 16];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), buf.len() as i32);
|
||||
assert_eq!(n, OAKCOMMON_E_NOT_FOUND);
|
||||
}
|
||||
|
||||
/// A null key yields `OAKCOMMON_E_INVALID`.
|
||||
#[test]
|
||||
fn config_get_null_key_returns_invalid() {
|
||||
let n = oakcommon_config_get(null(), null(), null_mut(), 0);
|
||||
assert_eq!(n, OAKCOMMON_E_INVALID);
|
||||
}
|
||||
|
||||
/// An empty key is invalid (matches the C++ `is_valid_key` check, handled
|
||||
/// here by the domain layer).
|
||||
#[test]
|
||||
fn config_get_empty_key_returns_invalid() {
|
||||
let mut buf = [0i8; 8];
|
||||
let n = oakcommon_config_get(null(), cstr("").as_ptr(), buf.as_mut_ptr(), 8);
|
||||
assert_eq!(n, OAKCOMMON_E_INVALID);
|
||||
}
|
||||
|
||||
/// A negative buffer size is an invalid output buffer.
|
||||
#[test]
|
||||
fn config_get_invalid_buffer_returns_invalid() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("bad_buf");
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr("v").as_ptr());
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), null_mut(), -1);
|
||||
assert_eq!(n, OAKCOMMON_E_INVALID);
|
||||
}
|
||||
|
||||
// ---- int / int64 ----------------------------------------------------------
|
||||
|
||||
/// INT set/get round trip; the string getter formats the int.
|
||||
#[test]
|
||||
fn config_set_get_int_roundtrip() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("int");
|
||||
oakcommon_config_set_int(null(), key.as_ptr(), 42);
|
||||
assert_eq!(oakcommon_config_get_int(null(), key.as_ptr(), -1), 42);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), key.as_ptr()), 2);
|
||||
|
||||
let mut buf = [0i8; 16];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), 16);
|
||||
assert_eq!(n, 3); // "42" + NUL
|
||||
assert_eq!(
|
||||
unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap(),
|
||||
"42"
|
||||
);
|
||||
}
|
||||
|
||||
/// INT getter fallback paths: absent key, wrong type, null key.
|
||||
#[test]
|
||||
fn config_get_int_fallback_paths() {
|
||||
let _guard = lock();
|
||||
let missing = unique_key("int_missing");
|
||||
let wrong = unique_key("int_wrongtype");
|
||||
oakcommon_config_set(null(), wrong.as_ptr(), cstr("abc").as_ptr());
|
||||
assert_eq!(oakcommon_config_get_int(null(), missing.as_ptr(), 7), 7);
|
||||
assert_eq!(oakcommon_config_get_int(null(), wrong.as_ptr(), 7), 7);
|
||||
assert_eq!(oakcommon_config_get_int(null(), null(), 9), 9);
|
||||
}
|
||||
|
||||
/// Setting a string onto an existing typed entry parses it into that type;
|
||||
/// an unparseable string leaves the entry unchanged (CPP-PARITY with
|
||||
/// `config.cpp`).
|
||||
#[test]
|
||||
fn config_set_string_parses_into_typed_entry() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("typed_parse");
|
||||
oakcommon_config_set_int(null(), key.as_ptr(), 42);
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr("7").as_ptr());
|
||||
assert_eq!(oakcommon_config_get_int(null(), key.as_ptr(), -1), 7);
|
||||
oakcommon_config_set(null(), key.as_ptr(), cstr("notanumber").as_ptr());
|
||||
assert_eq!(oakcommon_config_get_int(null(), key.as_ptr(), -1), 7);
|
||||
}
|
||||
|
||||
/// INT64 set/get round trip and fallback paths.
|
||||
#[test]
|
||||
fn config_set_get_int64_roundtrip() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("int64");
|
||||
let big: i64 = 3_000_000_000; // exceeds i32 range
|
||||
oakcommon_config_set_int64(null(), key.as_ptr(), big);
|
||||
assert_eq!(oakcommon_config_get_int64(null(), key.as_ptr(), -1), big);
|
||||
|
||||
let missing = unique_key("int64_missing");
|
||||
assert_eq!(
|
||||
oakcommon_config_get_int64(null(), missing.as_ptr(), -99),
|
||||
-99
|
||||
);
|
||||
assert_eq!(oakcommon_config_get_int64(null(), null(), -99), -99);
|
||||
}
|
||||
|
||||
// ---- double ---------------------------------------------------------------
|
||||
|
||||
/// DOUBLE set/get round trip and fallback paths.
|
||||
#[test]
|
||||
fn config_set_get_double_roundtrip() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("double");
|
||||
oakcommon_config_set_double(null(), key.as_ptr(), 3.5);
|
||||
assert_eq!(oakcommon_config_get_double(null(), key.as_ptr(), -1.0), 3.5);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), key.as_ptr()), 3);
|
||||
|
||||
let missing = unique_key("double_missing");
|
||||
let wrong = unique_key("double_wrongtype");
|
||||
oakcommon_config_set_int(null(), wrong.as_ptr(), 1);
|
||||
assert_eq!(
|
||||
oakcommon_config_get_double(null(), missing.as_ptr(), 2.5),
|
||||
2.5
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_config_get_double(null(), wrong.as_ptr(), 2.5),
|
||||
2.5
|
||||
);
|
||||
assert_eq!(oakcommon_config_get_double(null(), null(), 2.5), 2.5);
|
||||
}
|
||||
|
||||
// ---- bool -----------------------------------------------------------------
|
||||
|
||||
/// BOOL set/get round trip (0/1) and string formatting as "true"/"false".
|
||||
#[test]
|
||||
fn config_set_get_bool_roundtrip() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("bool");
|
||||
oakcommon_config_set_bool(null(), key.as_ptr(), 1);
|
||||
assert_eq!(oakcommon_config_get_bool(null(), key.as_ptr(), -1), 1);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), key.as_ptr()), 4);
|
||||
|
||||
let mut buf = [0i8; 8];
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), 8);
|
||||
assert_eq!(n, 5); // "true" + NUL
|
||||
assert_eq!(
|
||||
unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap(),
|
||||
"true"
|
||||
);
|
||||
|
||||
oakcommon_config_set_bool(null(), key.as_ptr(), 0);
|
||||
assert_eq!(oakcommon_config_get_bool(null(), key.as_ptr(), -1), 0);
|
||||
let n = oakcommon_config_get(null(), key.as_ptr(), buf.as_mut_ptr(), 8);
|
||||
assert_eq!(n, 6); // "false" + NUL
|
||||
assert_eq!(
|
||||
unsafe { CStr::from_ptr(buf.as_ptr()) }.to_str().unwrap(),
|
||||
"false"
|
||||
);
|
||||
}
|
||||
|
||||
/// BOOL getter fallback paths: absent key, wrong type, null key.
|
||||
#[test]
|
||||
fn config_get_bool_fallback_paths() {
|
||||
let _guard = lock();
|
||||
let missing = unique_key("bool_missing");
|
||||
let wrong = unique_key("bool_wrongtype");
|
||||
oakcommon_config_set_int(null(), wrong.as_ptr(), 1);
|
||||
assert_eq!(oakcommon_config_get_bool(null(), missing.as_ptr(), 1), 1);
|
||||
assert_eq!(oakcommon_config_get_bool(null(), wrong.as_ptr(), 0), 0);
|
||||
assert_eq!(oakcommon_config_get_bool(null(), null(), 1), 1);
|
||||
}
|
||||
|
||||
/// Null keys are silently ignored by every typed setter.
|
||||
#[test]
|
||||
fn config_typed_setters_null_key_are_noop() {
|
||||
let _guard = lock();
|
||||
oakcommon_config_set_int(null(), null(), 1);
|
||||
oakcommon_config_set_int64(null(), null(), 1);
|
||||
oakcommon_config_set_bool(null(), null(), 1);
|
||||
oakcommon_config_set_double(null(), null(), 1.0);
|
||||
}
|
||||
|
||||
// ---- groups ---------------------------------------------------------------
|
||||
|
||||
/// A non-empty group prefixes the stored key (`group/key`), so the flat
|
||||
/// lookup must not find it.
|
||||
#[test]
|
||||
fn config_group_prefixes_keys() {
|
||||
let _guard = lock();
|
||||
let key = unique_key("grouped");
|
||||
let group = cstr("ffi_test_group");
|
||||
oakcommon_config_set_int(group.as_ptr(), key.as_ptr(), 5);
|
||||
assert_eq!(oakcommon_config_get_int(null(), key.as_ptr(), -1), -1);
|
||||
assert_eq!(
|
||||
oakcommon_config_get_int(group.as_ptr(), key.as_ptr(), -1),
|
||||
5
|
||||
);
|
||||
assert_eq!(oakcommon_config_entry_type(group.as_ptr(), key.as_ptr()), 2);
|
||||
}
|
||||
|
||||
// ---- entry_type -----------------------------------------------------------
|
||||
|
||||
/// Entry type codes: 1 = String, 2 = Int, 3 = Double, 4 = Bool.
|
||||
#[test]
|
||||
fn config_entry_type_codes() {
|
||||
let _guard = lock();
|
||||
let s = unique_key("et_str");
|
||||
let i = unique_key("et_int");
|
||||
let d = unique_key("et_double");
|
||||
let b = unique_key("et_bool");
|
||||
oakcommon_config_set(null(), s.as_ptr(), cstr("x").as_ptr());
|
||||
oakcommon_config_set_int(null(), i.as_ptr(), 1);
|
||||
oakcommon_config_set_double(null(), d.as_ptr(), 1.0);
|
||||
oakcommon_config_set_bool(null(), b.as_ptr(), 1);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), s.as_ptr()), 1);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), i.as_ptr()), 2);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), d.as_ptr()), 3);
|
||||
assert_eq!(oakcommon_config_entry_type(null(), b.as_ptr()), 4);
|
||||
}
|
||||
|
||||
/// Missing, null, and empty keys for `entry_type`.
|
||||
#[test]
|
||||
fn config_entry_type_missing_and_invalid() {
|
||||
let _guard = lock();
|
||||
let missing = unique_key("et_missing");
|
||||
assert_eq!(
|
||||
oakcommon_config_entry_type(null(), missing.as_ptr()),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_config_entry_type(null(), null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_config_entry_type(null(), cstr("").as_ptr()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- error handler --------------------------------------------------------
|
||||
|
||||
/// Registering and clearing the error handler both succeed; the handler
|
||||
/// itself is exercised by `config_save_failure_reports_and_returns_failed`.
|
||||
#[test]
|
||||
fn config_set_error_handler_roundtrip() {
|
||||
let _guard = lock();
|
||||
let userdata = 0x1234 as *mut c_void;
|
||||
let _handler_guard = HandlerGuard;
|
||||
assert_eq!(
|
||||
oakcommon_config_set_error_handler(Some(record_handler), userdata),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_config_set_error_handler(None, std::ptr::null_mut()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
}
|
||||
@@ -1,247 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Integration tests for the C ABI surface of `oakcommon::ffi::ffmpegutils`,
|
||||
//! which implements `include/common/ffmpegutils.h` (oracle:
|
||||
//! `src/common/src/ffmpegutils.cpp`).
|
||||
//!
|
||||
//! MUST be run with `--features test-stubs`: unlike every other ffi module,
|
||||
//! `oakcommon_ffmpegutils_get_compatible_bridge_pixel_format` reaches the
|
||||
//! real `fb_find_best_pix_fmt_of_list` symbol (ffmpeg_bridge C ABI) in
|
||||
//! non-test builds, and the integration-test binary cannot link it. The
|
||||
//! `test-stubs` feature substitutes the in-crate stub (see
|
||||
//! `src/ffmpegutils.rs::find_best_pix_fmt_of_list`), mirroring the
|
||||
//! oakplugin/oaktimeline convention. `cargo test --lib` needs no flag: the
|
||||
//! stub is active under `cfg(test)` there.
|
||||
//!
|
||||
//! The pure mappings are already unit-tested in `src/ffmpegutils.rs`; these
|
||||
//! tests pin the exported wrapper contract: a value written to a non-null
|
||||
//! out-param with `OAKCOMMON_OK`, or `OAKCOMMON_E_INVALID` for a null
|
||||
//! out-param, for every export.
|
||||
|
||||
#[cfg(feature = "test-stubs")]
|
||||
mod ffi_ffmpegutils_tests {
|
||||
use std::ptr::null_mut;
|
||||
|
||||
use oakcommon::error::{OAKCOMMON_E_INVALID, OAKCOMMON_OK};
|
||||
use oakcommon::ffi::ffmpegutils::*;
|
||||
|
||||
/// Every out-param wrapper must reject a null pointer without writing.
|
||||
#[test]
|
||||
fn null_out_param_returns_invalid() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_bridge_pixel_format(26, -1, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_pixel_format(0, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_pixel_format(0, 4, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_native_sample_format(0, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_sample_format(6, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(12, null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
// The out-param is untouched on the failure path.
|
||||
assert_eq!(out, -999);
|
||||
}
|
||||
|
||||
/// `get_compatible_bridge_pixel_format` picks an exact candidate from the
|
||||
/// clamped list (values match `FB_PIX_FMT_*` / `PixelFormat`).
|
||||
#[test]
|
||||
fn compatible_bridge_pixel_format_exact_candidate() {
|
||||
let mut out: i32 = -999;
|
||||
// pix_fmt = RGBA (26), maximum = invalid (-1): no limit, RGBA is
|
||||
// first in the candidate list.
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_bridge_pixel_format(26, -1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 26);
|
||||
// pix_fmt = RGBA_F32_LE (220), maximum = F32 (4): the f32 clamp adds
|
||||
// RGBA_F32_LE, so it matches exactly.
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_bridge_pixel_format(220, 4, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 220);
|
||||
}
|
||||
|
||||
/// `get_compatible_bridge_pixel_format` falls back to the first candidate
|
||||
/// for an unknown source format (bridge loss-metric behaviour).
|
||||
#[test]
|
||||
fn compatible_bridge_pixel_format_unknown_falls_back() {
|
||||
let mut out: i32 = -999;
|
||||
// YUV420P (0) is not in the RGBA-oriented candidate list -> first
|
||||
// candidate RGBA (26).
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_bridge_pixel_format(0, -1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 26);
|
||||
}
|
||||
|
||||
/// `get_compatible_pixel_format` maps native formats to the least-lossy
|
||||
/// native format.
|
||||
#[test]
|
||||
fn compatible_pixel_format_maps_native() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_pixel_format(0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U8
|
||||
assert_eq!(out, 0);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_pixel_format(3, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // F16
|
||||
assert_eq!(out, 2); // -> U16
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_compatible_pixel_format(-1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // invalid
|
||||
assert_eq!(out, -1);
|
||||
}
|
||||
|
||||
/// `get_ffmpeg_pixel_format` maps native format + channel count to a
|
||||
/// bridge pixel format.
|
||||
#[test]
|
||||
fn ffmpeg_pixel_format_maps_native_to_bridge() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_pixel_format(0, 3, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U8 RGB
|
||||
assert_eq!(out, 2); // RGB24
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_pixel_format(0, 4, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U8 RGBA
|
||||
assert_eq!(out, 26); // RGBA
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_pixel_format(2, 4, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U16 RGBA
|
||||
assert_eq!(out, 105); // RGBA64LE
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_pixel_format(1, 3, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U10 RGB
|
||||
assert_eq!(out, -1); // no bridge format
|
||||
}
|
||||
|
||||
/// `get_native_sample_format` maps bridge sample formats to native.
|
||||
#[test]
|
||||
fn native_sample_format_maps_bridge_to_native() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_native_sample_format(0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // U8
|
||||
assert_eq!(out, 6); // SMP_FMT_U8
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_native_sample_format(1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // S16
|
||||
assert_eq!(out, 7);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_native_sample_format(8, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // FLTP
|
||||
assert_eq!(out, 4); // SMP_FMT_F32_P
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_native_sample_format(999, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, -1); // unknown -> invalid
|
||||
}
|
||||
|
||||
/// `get_ffmpeg_sample_format` maps native sample formats to bridge.
|
||||
#[test]
|
||||
fn ffmpeg_sample_format_maps_native_to_bridge() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_sample_format(6, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // SMP_FMT_U8
|
||||
assert_eq!(out, 0); // U8
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_sample_format(10, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // SMP_FMT_F32
|
||||
assert_eq!(out, 3); // FLT
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_get_ffmpeg_sample_format(-1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // invalid
|
||||
assert_eq!(out, -1);
|
||||
}
|
||||
|
||||
/// JPEG-range bridge formats convert to their regular counterparts;
|
||||
/// everything else passes through unchanged.
|
||||
#[test]
|
||||
fn jpeg_space_converts_to_regular_space() {
|
||||
let mut out: i32 = -999;
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(12, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // YUVJ420P
|
||||
assert_eq!(out, 0); // YUV420P
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(13, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // YUVJ422P
|
||||
assert_eq!(out, 4); // YUV422P
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(14, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // YUVJ444P
|
||||
assert_eq!(out, 5); // YUV444P
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(32, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // YUVJ440P
|
||||
assert_eq!(out, 31); // YUV440P
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(138, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // YUVJ411P
|
||||
assert_eq!(out, 7); // YUV411P
|
||||
// Non-JPEG formats pass through unchanged.
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(26, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // RGBA
|
||||
assert_eq!(out, 26);
|
||||
assert_eq!(
|
||||
oakcommon_ffmpegutils_convert_jpeg_space_to_regular_space(-1, &mut out),
|
||||
OAKCOMMON_OK
|
||||
); // none
|
||||
assert_eq!(out, -1);
|
||||
}
|
||||
}
|
||||
@@ -1,670 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! FFI-level integration tests for the C-ABI `misc`, `debug`, and `current`
|
||||
//! exports in `oakcommon::ffi`, asserted against the C++ oracle
|
||||
//! (`src/common/c_api/*.cpp`) and the Rust domain modules
|
||||
//! (`src/common/rust/src/{miscutils,debug}.rs`).
|
||||
//!
|
||||
//! The contract under test (each point matches the implementations):
|
||||
//! - decibel/lerp/power helpers write into a non-null out-param and reject a
|
||||
//! null one with `E_INVALID`; domain helpers never fail, so the only
|
||||
//! failure path is the null out-param;
|
||||
//! - `drop_workflow_behavior_is_valid` returns 1/0 without error codes, and
|
||||
//! `drop_workflow_behavior_name` is a non-truncating two-stage getter
|
||||
//! ("UNKNOWN" for out-of-range codes);
|
||||
//! - `debug` exports: null messages are `E_INVALID`, the level-name getter
|
||||
//! is a non-truncating two-stage getter, level codes outside 0..=4 are
|
||||
//! rejected by `log_set_level` with `E_INVALID`, and the get-level export
|
||||
//! rejects a null out-param;
|
||||
//! - `current` exports: the singleton handle is stamped and `free` is
|
||||
//! idempotent, setters/getters reject a null handle (getters also reject a
|
||||
//! null out-param), empty slots read back as null, and replacing an
|
||||
//! occupied slot runs the previous owner's destructor exactly once.
|
||||
|
||||
use std::ffi::{c_char, c_void, CString};
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use oakcommon::error::{OAKCOMMON_E_INVALID, OAKCOMMON_OK};
|
||||
use oakcommon::ffi::current::*;
|
||||
use oakcommon::ffi::debug::*;
|
||||
use oakcommon::ffi::misc::*;
|
||||
use oakcommon::handle::{CHandle, OAKCOMMON_ABI_VERSION};
|
||||
|
||||
/// Serialises tests that mutate the process-wide `Current` singleton (cargo
|
||||
/// runs tests on threads).
|
||||
static CURRENT_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// Serialises tests that mutate the process-wide logging level.
|
||||
static DEBUG_LOCK: Mutex<()> = Mutex::new(());
|
||||
|
||||
/// Destroy tally for the slot-replacement test.
|
||||
static DESTROY_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
/// A `DestroyFn` that bumps [`DESTROY_COUNT`].
|
||||
unsafe extern "C" fn count_destroy(_p: *mut c_void) {
|
||||
DESTROY_COUNT.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Convert a string slice to a NUL-terminated C string for FFI inputs.
|
||||
fn to_cstring(s: &str) -> CString {
|
||||
CString::new(s).expect("test string must not contain NUL")
|
||||
}
|
||||
|
||||
/// Cheap struct copy: `CHandle` is neither `Clone` nor `Copy`, but every
|
||||
/// getter takes it by value. Rebuilding from the same fields duplicates
|
||||
/// only the handle value — the box stays alive as long as the original
|
||||
/// handle lives, and the getters never release.
|
||||
fn dup(h: &CHandle) -> CHandle {
|
||||
CHandle {
|
||||
ctx: h.ctx,
|
||||
addref: h.addref,
|
||||
release: h.release,
|
||||
abi_version: h.abi_version,
|
||||
}
|
||||
}
|
||||
|
||||
/// Assert two f64 values agree within `tol`.
|
||||
fn assert_close(actual: f64, expected: f64, tol: f64) {
|
||||
assert!(
|
||||
(actual - expected).abs() <= tol,
|
||||
"expected {expected} ± {tol}, got {actual}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Drive a two-stage string getter against the standard `copy_string`
|
||||
/// convention: a null-buffer size query, a short buffer that must stay
|
||||
/// untouched (no truncation), an exact-fit copy with its NUL, and an
|
||||
/// oversized copy with the tail untouched.
|
||||
fn assert_two_stage_getter(getter: impl Fn(*mut c_char, i32) -> i32, expected: &str) {
|
||||
let required = (expected.len() + 1) as i32;
|
||||
|
||||
// Size query: a null buffer returns the required size, NUL included.
|
||||
assert_eq!(getter(std::ptr::null_mut(), 0), required);
|
||||
|
||||
// Short buffer: too small, so nothing is written to it.
|
||||
let short_size = (required - 1).max(0);
|
||||
let mut short = vec![0xABu8; short_size as usize];
|
||||
assert_eq!(
|
||||
getter(short.as_mut_ptr() as *mut c_char, short_size),
|
||||
required
|
||||
);
|
||||
assert!(
|
||||
short.iter().all(|&b| b == 0xAB),
|
||||
"short buffer must stay untouched"
|
||||
);
|
||||
|
||||
// Exact fit: payload followed by a NUL.
|
||||
let mut exact = vec![0xCDu8; required as usize];
|
||||
assert_eq!(
|
||||
getter(exact.as_mut_ptr() as *mut c_char, required),
|
||||
required
|
||||
);
|
||||
assert_eq!(&exact[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(exact[expected.len()], 0);
|
||||
|
||||
// Oversized: payload and NUL written, tail left as initialized.
|
||||
let mut big = vec![0u8; (required + 8) as usize];
|
||||
assert_eq!(
|
||||
getter(big.as_mut_ptr() as *mut c_char, required + 8),
|
||||
required
|
||||
);
|
||||
assert_eq!(&big[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(big[expected.len()], 0);
|
||||
assert!(big[(required + 1) as usize..].iter().all(|&b| b == 0));
|
||||
}
|
||||
|
||||
// ---- debug ----
|
||||
|
||||
/// A null message is `E_INVALID`.
|
||||
#[test]
|
||||
fn debug_log_null_msg_is_invalid() {
|
||||
assert_eq!(
|
||||
oakcommon_debug_log(0, std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// Any non-null message returns `OK`, including out-of-range level codes
|
||||
/// (`log_raw` never fails on the level itself).
|
||||
#[test]
|
||||
fn debug_log_returns_ok_for_any_level() {
|
||||
let msg = to_cstring("ffi_misc debug_log probe");
|
||||
assert_eq!(oakcommon_debug_log(0, msg.as_ptr()), OAKCOMMON_OK);
|
||||
|
||||
let raw = to_cstring("out-of-range level");
|
||||
assert_eq!(oakcommon_debug_log(99, raw.as_ptr()), OAKCOMMON_OK);
|
||||
}
|
||||
|
||||
/// Level-name getter is a non-truncating two-stage getter; out-of-range
|
||||
/// codes yield "UNKNOWN".
|
||||
#[test]
|
||||
fn debug_level_name_two_stage() {
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_debug_level_name(0, buf, size),
|
||||
"DEBUG",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_debug_level_name(2, buf, size),
|
||||
"WARNING",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_debug_level_name(4, buf, size),
|
||||
"FATAL",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_debug_level_name(5, buf, size),
|
||||
"UNKNOWN",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_debug_level_name(-1, buf, size),
|
||||
"UNKNOWN",
|
||||
);
|
||||
}
|
||||
|
||||
/// `log_set_level` accepts 0..=4, rejects everything else with `E_INVALID`
|
||||
/// without changing the stored level, and `log_get_level` reads it back.
|
||||
#[test]
|
||||
fn log_set_get_level_roundtrip_and_invalid() {
|
||||
let _guard = DEBUG_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
|
||||
assert_eq!(oakcommon_log_set_level(0), OAKCOMMON_OK);
|
||||
let mut level = -1;
|
||||
assert_eq!(oakcommon_log_get_level(&mut level), OAKCOMMON_OK);
|
||||
assert_eq!(level, 0);
|
||||
|
||||
assert_eq!(oakcommon_log_set_level(4), OAKCOMMON_OK);
|
||||
assert_eq!(oakcommon_log_get_level(&mut level), OAKCOMMON_OK);
|
||||
assert_eq!(level, 4);
|
||||
|
||||
assert_eq!(oakcommon_log_set_level(5), OAKCOMMON_E_INVALID);
|
||||
assert_eq!(oakcommon_log_set_level(-1), OAKCOMMON_E_INVALID);
|
||||
// The rejected set leaves the stored level untouched.
|
||||
assert_eq!(oakcommon_log_get_level(&mut level), OAKCOMMON_OK);
|
||||
assert_eq!(level, 4);
|
||||
|
||||
// Restore the default so other tests see a clean level.
|
||||
assert_eq!(oakcommon_log_set_level(1), OAKCOMMON_OK);
|
||||
assert_eq!(oakcommon_log_get_level(&mut level), OAKCOMMON_OK);
|
||||
assert_eq!(level, 1);
|
||||
}
|
||||
|
||||
/// The get-level export rejects a null out-param.
|
||||
#[test]
|
||||
fn log_get_level_null_out_is_invalid() {
|
||||
assert_eq!(
|
||||
oakcommon_log_get_level(std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- misc: decibel / lerp ----
|
||||
|
||||
/// Linear -> decibels for exact and mid-range inputs; zero (log10 = -inf)
|
||||
/// collapses to the decibel minimum, negative inputs yield NaN.
|
||||
#[test]
|
||||
fn decibel_from_linear_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(oakcommon_decibel_from_linear(1.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 0.0, 1e-9);
|
||||
assert_eq!(oakcommon_decibel_from_linear(10.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 20.0, 1e-9);
|
||||
assert_eq!(oakcommon_decibel_from_linear(0.5, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, -6.020599913, 1e-6);
|
||||
assert_eq!(oakcommon_decibel_from_linear(0.0, &mut out), OAKCOMMON_OK);
|
||||
assert_eq!(out, -200.0);
|
||||
assert_eq!(oakcommon_decibel_from_linear(-1.0, &mut out), OAKCOMMON_OK);
|
||||
assert!(
|
||||
out.is_nan(),
|
||||
"negative linear input must produce NaN, got {out}"
|
||||
);
|
||||
}
|
||||
|
||||
/// Decibels -> linear for exact inputs; results below 1e-6 clamp to 0.0.
|
||||
#[test]
|
||||
fn decibel_to_linear_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(oakcommon_decibel_to_linear(0.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 1.0, 1e-9);
|
||||
assert_eq!(oakcommon_decibel_to_linear(20.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 10.0, 1e-9);
|
||||
assert_eq!(oakcommon_decibel_to_linear(-100.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 1e-5, 1e-12);
|
||||
assert_eq!(oakcommon_decibel_to_linear(-200.0, &mut out), OAKCOMMON_OK);
|
||||
assert_eq!(out, 0.0);
|
||||
}
|
||||
|
||||
/// Logarithmic position -> decibels: saturates to the minimum below 0.001,
|
||||
/// to 0 dB above 0.99, and follows the linearization formula in between.
|
||||
#[test]
|
||||
fn decibel_from_logarithmic_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_logarithmic(0.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, -200.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_logarithmic(1.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 0.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_logarithmic(0.99, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, 0.0, 1e-6);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_logarithmic(0.5, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, -16.45, 0.05);
|
||||
}
|
||||
|
||||
/// Decibels -> logarithmic position; `|db| <= 1e-12` snaps to 1.0.
|
||||
#[test]
|
||||
fn decibel_to_logarithmic_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_decibel_to_logarithmic(0.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 1.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_to_logarithmic(20.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, 1.0, 1e-9);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_to_logarithmic(-120.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, 4.605e-6, 1e-9);
|
||||
}
|
||||
|
||||
/// Linear amplitude -> logarithmic position (`1 - exp(-linear * lo_g100)`).
|
||||
#[test]
|
||||
fn decibel_linear_to_logarithmic_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_decibel_linear_to_logarithmic(0.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 0.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_linear_to_logarithmic(1.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, 0.99, 1e-9);
|
||||
}
|
||||
|
||||
/// Logarithmic position -> linear amplitude; values above 0.99 snap to 1.0.
|
||||
#[test]
|
||||
fn decibel_logarithmic_to_linear_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_decibel_logarithmic_to_linear(0.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 0.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_logarithmic_to_linear(1.0, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 1.0);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_logarithmic_to_linear(0.99, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_close(out, 1.0, 1e-6);
|
||||
}
|
||||
|
||||
/// `lerp(a, b, t) = a*(1-t) + b*t` at the endpoints and midpoints.
|
||||
#[test]
|
||||
fn lerp_known_values() {
|
||||
let mut out = -1.0;
|
||||
|
||||
assert_eq!(oakcommon_lerp(0.0, 10.0, 0.5, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 5.0, 1e-9);
|
||||
assert_eq!(oakcommon_lerp(0.0, 10.0, 0.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 0.0, 1e-9);
|
||||
assert_eq!(oakcommon_lerp(0.0, 10.0, 1.0, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 10.0, 1e-9);
|
||||
assert_eq!(oakcommon_lerp(2.0, 4.0, 0.25, &mut out), OAKCOMMON_OK);
|
||||
assert_close(out, 2.5, 1e-9);
|
||||
}
|
||||
|
||||
/// Every decibel/lerp export rejects a null out-param with `E_INVALID`.
|
||||
#[test]
|
||||
fn decibel_lerp_reject_null_out() {
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_linear(1.0, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_to_linear(0.0, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_from_logarithmic(0.5, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_to_logarithmic(0.0, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_linear_to_logarithmic(0.5, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_decibel_logarithmic_to_linear(0.5, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_lerp(0.0, 1.0, 0.5, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- misc: drop-workflow behavior / power ----
|
||||
|
||||
/// Codes 0..=3 are valid; everything else returns 0.
|
||||
#[test]
|
||||
fn drop_workflow_behavior_is_valid() {
|
||||
for value in 0..=3 {
|
||||
assert_eq!(oakcommon_drop_workflow_behavior_is_valid(value), 1);
|
||||
}
|
||||
assert_eq!(oakcommon_drop_workflow_behavior_is_valid(4), 0);
|
||||
assert_eq!(oakcommon_drop_workflow_behavior_is_valid(-1), 0);
|
||||
assert_eq!(oakcommon_drop_workflow_behavior_is_valid(i32::MAX), 0);
|
||||
}
|
||||
|
||||
/// Behavior-name getter is a non-truncating two-stage getter; out-of-range
|
||||
/// codes yield "UNKNOWN".
|
||||
#[test]
|
||||
fn drop_workflow_behavior_name_two_stage() {
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(0, buf, size),
|
||||
"ASK",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(1, buf, size),
|
||||
"AUTO",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(2, buf, size),
|
||||
"MANUAL",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(3, buf, size),
|
||||
"DISABLE",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(4, buf, size),
|
||||
"UNKNOWN",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_drop_workflow_behavior_name(-1, buf, size),
|
||||
"UNKNOWN",
|
||||
);
|
||||
}
|
||||
|
||||
/// Round `value` up to a power of two (wrapping overflow -> 0); a null
|
||||
/// out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn power_ceil_to_power_of_2() {
|
||||
let mut out = 0u32;
|
||||
for (input, expected) in [
|
||||
(0u32, 0u32),
|
||||
(1, 1),
|
||||
(2, 2),
|
||||
(3, 4),
|
||||
(5, 8),
|
||||
(9, 16),
|
||||
(0x8000_0001, 0),
|
||||
] {
|
||||
assert_eq!(
|
||||
oakcommon_power_ceil_to_power_of_2(input, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, expected, "ceil({input})");
|
||||
}
|
||||
assert_eq!(
|
||||
oakcommon_power_ceil_to_power_of_2(7, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// Round `value` down to a power of two; a null out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn power_floor_to_power_of_2() {
|
||||
let mut out = 0u32;
|
||||
for (input, expected) in [
|
||||
(0u32, 0u32),
|
||||
(1, 1),
|
||||
(4, 4),
|
||||
(5, 4),
|
||||
(9, 8),
|
||||
(0x8000_0000, 0x8000_0000),
|
||||
] {
|
||||
assert_eq!(
|
||||
oakcommon_power_floor_to_power_of_2(input, &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, expected, "floor({input})");
|
||||
}
|
||||
assert_eq!(
|
||||
oakcommon_power_floor_to_power_of_2(7, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- current ----
|
||||
|
||||
/// The singleton handle is stamped; `free` nullifies it, is idempotent, and
|
||||
/// tolerates a null pointer or an explicit null handle.
|
||||
#[test]
|
||||
fn current_instance_and_free_lifecycle() {
|
||||
let mut h = oakcommon_current_instance();
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(h.addref.is_some());
|
||||
assert!(h.release.is_some());
|
||||
|
||||
oakcommon_current_free(&mut h);
|
||||
assert!(h.is_null());
|
||||
// A second free of the now-empty handle is safe.
|
||||
oakcommon_current_free(&mut h);
|
||||
assert!(h.is_null());
|
||||
// Freeing a null pointer is safe.
|
||||
oakcommon_current_free(std::ptr::null_mut());
|
||||
// Freeing an explicit null handle is safe.
|
||||
let mut null_h = CHandle::null();
|
||||
oakcommon_current_free(&mut null_h);
|
||||
assert!(null_h.is_null());
|
||||
}
|
||||
|
||||
/// All four slots round-trip set -> get; null handles and null out-params
|
||||
/// are `E_INVALID`, and clearing a slot makes it read back as null.
|
||||
#[test]
|
||||
fn current_set_get_all_slots_roundtrip() {
|
||||
let _guard = CURRENT_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let h = oakcommon_current_instance();
|
||||
|
||||
let video = 0x1000usize as *mut c_void;
|
||||
let audio = 0x2000usize as *mut c_void;
|
||||
let host = 0x3000usize as *mut c_void;
|
||||
let cache = 0x4000usize as *mut c_void;
|
||||
|
||||
// Empty slots read back as null before anything is stored.
|
||||
let mut got: *mut c_void = std::ptr::null_mut();
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert!(got.is_null());
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(dup(&h), video, None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_audio_params(dup(&h), audio, None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_plugin_host(dup(&h), host, None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_plugin_cache(dup(&h), cache, None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(got, video);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_audio_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(got, audio);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_plugin_host(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(got, host);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_plugin_cache(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(got, cache);
|
||||
|
||||
// Getters reject a null out-param and a null handle.
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(CHandle::null(), &mut got),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
// Setters reject a null handle.
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(CHandle::null(), video, None),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
|
||||
// Clear every slot so other tests see a clean singleton.
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(dup(&h), std::ptr::null_mut(), None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_audio_params(dup(&h), std::ptr::null_mut(), None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_plugin_host(dup(&h), std::ptr::null_mut(), None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_set_plugin_cache(dup(&h), std::ptr::null_mut(), None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert!(got.is_null());
|
||||
}
|
||||
|
||||
/// Replacing an occupied slot runs the previous owner's destructor exactly
|
||||
/// once; clearing a slot whose occupant has no destructor runs nothing.
|
||||
#[test]
|
||||
fn current_set_destroys_replaced_pointer() {
|
||||
let _guard = CURRENT_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let h = oakcommon_current_instance();
|
||||
let base = DESTROY_COUNT.load(Ordering::SeqCst);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(
|
||||
dup(&h),
|
||||
0xAAAAusize as *mut c_void,
|
||||
Some(count_destroy)
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(DESTROY_COUNT.load(Ordering::SeqCst), base);
|
||||
|
||||
// Replacing the slot invokes the stored destructor exactly once.
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(dup(&h), 0xBBBBusize as *mut c_void, None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(DESTROY_COUNT.load(Ordering::SeqCst), base + 1);
|
||||
|
||||
let mut got: *mut c_void = std::ptr::null_mut();
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(got, 0xBBBBusize as *mut c_void);
|
||||
|
||||
// Clearing a slot with no destructor invokes nothing.
|
||||
assert_eq!(
|
||||
oakcommon_current_set_video_params(dup(&h), std::ptr::null_mut(), None),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(DESTROY_COUNT.load(Ordering::SeqCst), base + 1);
|
||||
assert_eq!(
|
||||
oakcommon_current_get_video_params(dup(&h), &mut got),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert!(got.is_null());
|
||||
}
|
||||
|
||||
/// `is_interactive` writes 1; null handle/out-param are `E_INVALID`.
|
||||
#[test]
|
||||
fn current_is_interactive_writes_one() {
|
||||
let _guard = CURRENT_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let h = oakcommon_current_instance();
|
||||
|
||||
let mut out = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_current_is_interactive(dup(&h), &mut out),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(out, 1);
|
||||
assert_eq!(
|
||||
oakcommon_current_is_interactive(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_current_is_interactive(CHandle::null(), &mut out),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
@@ -1,721 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! FFI-level integration tests for the C-ABI `subtitleparams` exports in
|
||||
//! `oakcommon::ffi::subtitleparams`, asserted against the C++ oracle
|
||||
//! `src/common/c_api/subtitleparams.cpp` and the Rust domain
|
||||
//! `src/common/rust/src/subtitleparams.rs`.
|
||||
//!
|
||||
//! The contract under test (each point matches the C++ oracle):
|
||||
//! - exports take a `CHandle` by value and never release it, so callers
|
||||
//! keep the handle alive and `free` it exactly once;
|
||||
//! - `free` nullifies the handle, is idempotent, and tolerates a null
|
||||
//! pointer;
|
||||
//! - two-stage string getters return the required size (NUL included) and
|
||||
//! only copy when the buffer is large enough — they never truncate;
|
||||
//! - getters reject a null handle or null out-param with `E_INVALID`,
|
||||
//! setters reject a null handle, and string inputs reject null pointers;
|
||||
//! - an out-of-range subtitle index yields `E_NOT_FOUND`; malformed XML
|
||||
//! yields `E_FAILED`; the empty set is invalid with `count` 0 and
|
||||
//! `duration` 0/1.
|
||||
|
||||
use std::ffi::{c_char, CString};
|
||||
|
||||
use oakcommon::error::{
|
||||
OAKCOMMON_E_FAILED, OAKCOMMON_E_INVALID, OAKCOMMON_E_NOT_FOUND, OAKCOMMON_OK,
|
||||
};
|
||||
use oakcommon::ffi::subtitleparams::*;
|
||||
use oakcommon::handle::{CHandle, OAKCOMMON_ABI_VERSION};
|
||||
|
||||
/// Create an empty subtitle parameter set.
|
||||
fn make() -> CHandle {
|
||||
oakcommon_subtitleparams_init()
|
||||
}
|
||||
|
||||
/// A populated set: stream index 2, disabled, two subtitles.
|
||||
fn make_populated() -> CHandle {
|
||||
let h = make();
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(dup(&h), 0, 1, 25, 1, to_cstring("hello").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(dup(&h), 25, 1, 50, 1, to_cstring("world").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_stream_index(dup(&h), 2),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_enabled(dup(&h), 0),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
h
|
||||
}
|
||||
|
||||
/// Convert a string slice to a NUL-terminated C string for FFI inputs.
|
||||
fn to_cstring(s: &str) -> CString {
|
||||
CString::new(s).expect("test string must not contain NUL")
|
||||
}
|
||||
|
||||
/// Cheap struct copy: `CHandle` is neither `Clone` nor `Copy`, but every
|
||||
/// getter takes it by value. Rebuilding from the same fields duplicates
|
||||
/// only the handle value — the box stays alive as long as the original
|
||||
/// handle lives, and the getters never release.
|
||||
fn dup(h: &CHandle) -> CHandle {
|
||||
CHandle {
|
||||
ctx: h.ctx,
|
||||
addref: h.addref,
|
||||
release: h.release,
|
||||
abi_version: h.abi_version,
|
||||
}
|
||||
}
|
||||
|
||||
/// Drive a two-stage string getter against the C++ `copy_string`
|
||||
/// convention: a null-buffer size query, a short buffer that must stay
|
||||
/// untouched (no truncation), an exact-fit copy with its NUL, and an
|
||||
/// oversized copy with the tail untouched.
|
||||
fn assert_two_stage_getter(getter: impl Fn(*mut c_char, i32) -> i32, expected: &str) {
|
||||
let required = (expected.len() + 1) as i32;
|
||||
|
||||
// Size query: a null buffer returns the required size, NUL included.
|
||||
assert_eq!(getter(std::ptr::null_mut(), 0), required);
|
||||
|
||||
// Short buffer: too small, so nothing is written to it.
|
||||
let short_size = (required - 1).max(0);
|
||||
let mut short = vec![0xABu8; short_size as usize];
|
||||
assert_eq!(
|
||||
getter(short.as_mut_ptr() as *mut c_char, short_size),
|
||||
required
|
||||
);
|
||||
assert!(
|
||||
short.iter().all(|&b| b == 0xAB),
|
||||
"short buffer must stay untouched"
|
||||
);
|
||||
|
||||
// Exact fit: payload followed by a NUL.
|
||||
let mut exact = vec![0xCDu8; required as usize];
|
||||
assert_eq!(
|
||||
getter(exact.as_mut_ptr() as *mut c_char, required),
|
||||
required
|
||||
);
|
||||
assert_eq!(&exact[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(exact[expected.len()], 0);
|
||||
|
||||
// Oversized: payload and NUL written, tail left as initialized.
|
||||
let mut big = vec![0u8; (required + 8) as usize];
|
||||
assert_eq!(
|
||||
getter(big.as_mut_ptr() as *mut c_char, required + 8),
|
||||
required
|
||||
);
|
||||
assert_eq!(&big[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(big[expected.len()], 0);
|
||||
assert!(big[(required + 1) as usize..].iter().all(|&b| b == 0));
|
||||
}
|
||||
|
||||
// ---- Handle lifecycle ----
|
||||
|
||||
/// `init` yields a stamped, non-empty handle; `free` nullifies it, is
|
||||
/// idempotent, and tolerates a null pointer.
|
||||
#[test]
|
||||
fn init_free_lifecycle() {
|
||||
let h = oakcommon_subtitleparams_init();
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(h.addref.is_some());
|
||||
assert!(h.release.is_some());
|
||||
|
||||
let mut hf = make();
|
||||
assert!(!hf.is_null());
|
||||
oakcommon_subtitleparams_free(&mut hf);
|
||||
assert!(hf.is_null());
|
||||
// A second free of the now-empty handle is safe.
|
||||
oakcommon_subtitleparams_free(&mut hf);
|
||||
assert!(hf.is_null());
|
||||
// Freeing a null pointer is safe.
|
||||
oakcommon_subtitleparams_free(std::ptr::null_mut());
|
||||
// Freeing a handle struct that is already null (by value) is safe too.
|
||||
let mut empty = CHandle::null();
|
||||
oakcommon_subtitleparams_free(&mut empty);
|
||||
assert!(empty.is_null());
|
||||
}
|
||||
|
||||
// ---- Core field round-trips ----
|
||||
|
||||
/// Stream index defaults to 0 and round-trips; a null handle or null
|
||||
/// out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn stream_index_roundtrip() {
|
||||
let h = make();
|
||||
let mut si = -1i32;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(dup(&h), &mut si),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(si, 0);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_stream_index(dup(&h), 3),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(dup(&h), &mut si),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(si, 3);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_stream_index(CHandle::null(), 1),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(CHandle::null(), &mut si),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `enabled` defaults to 1 (CPP-PARITY: the C++ constructor sets
|
||||
/// `enabled_ = true`) and round-trips; any non-zero setter value enables.
|
||||
#[test]
|
||||
fn enabled_roundtrip() {
|
||||
let h = make();
|
||||
let mut en = -1i32;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(en, 1);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_enabled(dup(&h), 0),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(en, 0);
|
||||
|
||||
// A non-zero code (even 5) enables the stream.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_enabled(dup(&h), 5),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(en, 1);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_set_enabled(CHandle::null(), 1),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(CHandle::null(), &mut en),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Empty-set behavior ----
|
||||
|
||||
/// An empty set is invalid, has count 0, and duration 0/1; null handle or
|
||||
/// null out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn empty_set_defaults() {
|
||||
let h = make();
|
||||
let mut v = -1i32;
|
||||
let mut c = -1i32;
|
||||
let mut n = -1i32;
|
||||
let mut d = -1i32;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_is_valid(dup(&h), &mut v),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(v, 0);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 0);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(dup(&h), &mut n, &mut d),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (0, 1));
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_is_valid(CHandle::null(), &mut v),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(CHandle::null(), &mut c),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(CHandle::null(), &mut n, &mut d),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_is_valid(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(dup(&h), std::ptr::null_mut(), &mut d),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(dup(&h), &mut n, std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Subtitles ----
|
||||
|
||||
/// `add_subtitle` appends entries; `is_valid`/`count`/`duration`/
|
||||
/// `get_subtitle` reflect the populated set. Rationals are reduced
|
||||
/// (CPP-PARITY with the C++ `Rational` constructor).
|
||||
#[test]
|
||||
fn add_subtitle_and_query() {
|
||||
let h = make_populated();
|
||||
let mut v = -1i32;
|
||||
let mut c = -1i32;
|
||||
let mut n = -1i32;
|
||||
let mut d = -1i32;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_is_valid(dup(&h), &mut v),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(v, 1);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 2);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(dup(&h), &mut n, &mut d),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (50, 1));
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), 0, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (0, 1));
|
||||
assert_eq!((v, c), (25, 1));
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), 1, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (25, 1));
|
||||
assert_eq!((v, c), (50, 1));
|
||||
|
||||
// Reduction: Rational(2,4) -> 1/2, Rational(9,3) -> 3/1.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(dup(&h), 2, 4, 9, 3, to_cstring("t").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), 2, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (1, 2));
|
||||
assert_eq!((v, c), (3, 1));
|
||||
}
|
||||
|
||||
/// `add_subtitle` rejects a null handle or null text; an empty text string
|
||||
/// is a valid subtitle.
|
||||
#[test]
|
||||
fn add_subtitle_failures() {
|
||||
let h = make();
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(
|
||||
CHandle::null(),
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
to_cstring("x").as_ptr()
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(dup(&h), 0, 1, 1, 1, std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
|
||||
// Empty text is fine and counts as a subtitle.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_add_subtitle(dup(&h), 0, 1, 1, 1, to_cstring("").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
let mut c = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 1);
|
||||
}
|
||||
|
||||
/// `get_subtitle` on an out-of-range index is `E_NOT_FOUND`; a null handle
|
||||
/// or any null out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn get_subtitle_out_of_range() {
|
||||
let h = make_populated();
|
||||
let mut n = -1i32;
|
||||
let mut d = -1i32;
|
||||
let mut v = -1i32;
|
||||
let mut c = -1i32;
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), 2, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), -1, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
|
||||
// On an empty set even index 0 is out of range.
|
||||
let e = make();
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&e), 0, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(CHandle::null(), 0, &mut n, &mut d, &mut v, &mut c),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(
|
||||
dup(&h),
|
||||
0,
|
||||
std::ptr::null_mut(),
|
||||
&mut d,
|
||||
&mut v,
|
||||
&mut c
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(
|
||||
dup(&h),
|
||||
0,
|
||||
&mut n,
|
||||
std::ptr::null_mut(),
|
||||
&mut v,
|
||||
&mut c
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(
|
||||
dup(&h),
|
||||
0,
|
||||
&mut n,
|
||||
&mut d,
|
||||
std::ptr::null_mut(),
|
||||
&mut c
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(
|
||||
dup(&h),
|
||||
0,
|
||||
&mut n,
|
||||
&mut d,
|
||||
&mut v,
|
||||
std::ptr::null_mut()
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `clear` removes every subtitle (count 0, invalid, duration 0/1) and
|
||||
/// rejects a null handle.
|
||||
#[test]
|
||||
fn clear() {
|
||||
let h = make_populated();
|
||||
assert_eq!(oakcommon_subtitleparams_clear(dup(&h)), OAKCOMMON_OK);
|
||||
|
||||
let mut v = -1i32;
|
||||
let mut c = -1i32;
|
||||
let mut n = -1i32;
|
||||
let mut d = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 0);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_is_valid(dup(&h), &mut v),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(v, 0);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_duration(dup(&h), &mut n, &mut d),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (0, 1));
|
||||
|
||||
// Clearing again is a no-op success.
|
||||
assert_eq!(oakcommon_subtitleparams_clear(dup(&h)), OAKCOMMON_OK);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_clear(CHandle::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- String getters ----
|
||||
|
||||
/// `get_subtitle_text` is a two-stage string getter; an out-of-range index
|
||||
/// is `E_NOT_FOUND`, and a null handle or invalid out-buffer is
|
||||
/// `E_INVALID`.
|
||||
#[test]
|
||||
fn get_subtitle_text_two_stage() {
|
||||
let h = make_populated();
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_get_subtitle_text(dup(&h), 0, buf, size),
|
||||
"hello",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_get_subtitle_text(dup(&h), 1, buf, size),
|
||||
"world",
|
||||
);
|
||||
|
||||
// Out-of-range index is E_NOT_FOUND (even on a size query).
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(dup(&h), 2, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(dup(&h), -1, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(CHandle::null(), 0, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
// A null buffer with a positive size is an invalid string out-param.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(dup(&h), 0, std::ptr::null_mut(), 5),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
// A negative size is invalid too.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(dup(&h), 0, std::ptr::null_mut(), -1),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `generate_ass_header` is a static two-stage string getter (no handle).
|
||||
/// The payload matches the C++ header verbatim (CRLF line endings).
|
||||
#[test]
|
||||
fn generate_ass_header_two_stage() {
|
||||
let expected = "[Script Info]\r\n\
|
||||
; Script generated by Oak\r\n\
|
||||
ScriptType: v4.00+\r\n\
|
||||
PlayResX: 384\r\n\
|
||||
PlayResY: 288\r\n\
|
||||
ScaledBorderAndShadow: yes\r\n\
|
||||
\r\n\
|
||||
[V4+ Styles]\r\n\
|
||||
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, \
|
||||
BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, \
|
||||
BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\r\n\
|
||||
Style: Default,Arial,16,&HFFFFFF,&HFFFFFF,&H000000,&H000000,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0\r\n\
|
||||
\r\n\
|
||||
[Events]\r\n\
|
||||
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n";
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_generate_ass_header(buf, size),
|
||||
expected,
|
||||
);
|
||||
|
||||
// An invalid out-buffer (null with a positive size) is E_INVALID.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_generate_ass_header(std::ptr::null_mut(), 5),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_generate_ass_header(std::ptr::null_mut(), -1),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- XML ----
|
||||
|
||||
/// `load_xml` parses a fragment and applies stream index, enabled, and
|
||||
/// subtitles; malformed input or a missing root is `E_FAILED`; a null
|
||||
/// handle or null xml is `E_INVALID`.
|
||||
#[test]
|
||||
fn load_xml() {
|
||||
let h = make();
|
||||
|
||||
let xml = "<subtitleparams><streamindex>7</streamindex><enabled>0</enabled>\
|
||||
<subtitles><subtitle in=\"0/1\" out=\"25/1\">hello</subtitle>\
|
||||
<subtitle in=\"25/1\" out=\"50/1\">world</subtitle></subtitles></subtitleparams>";
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(dup(&h), to_cstring(xml).as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
|
||||
let mut si = -1i32;
|
||||
let mut en = -1i32;
|
||||
let mut c = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(dup(&h), &mut si),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(si, 7);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(en, 0);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 2);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle_text(dup(&h), 0, std::ptr::null_mut(), 0),
|
||||
6
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_get_subtitle_text(dup(&h), 1, buf, size),
|
||||
"world",
|
||||
);
|
||||
|
||||
// Malformed / missing-root fragments fail with E_FAILED.
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(
|
||||
dup(&h),
|
||||
to_cstring("<subtitleparams><streamindex>").as_ptr()
|
||||
),
|
||||
OAKCOMMON_E_FAILED
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(dup(&h), to_cstring("not xml").as_ptr()),
|
||||
OAKCOMMON_E_FAILED
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(dup(&h), to_cstring("").as_ptr()),
|
||||
OAKCOMMON_E_FAILED
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(CHandle::null(), to_cstring(xml).as_ptr()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(dup(&h), std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `save_xml` is a two-stage string getter; the output matches the C++
|
||||
/// `XmlStreamWriter` format exactly (no indentation, escaped text).
|
||||
#[test]
|
||||
fn save_xml_two_stage() {
|
||||
// The empty set serializes to streamindex 0 / enabled 1 / no subtitles.
|
||||
let e = make();
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_save_xml(dup(&e), buf, size),
|
||||
"<subtitleparams><streamindex>0</streamindex><enabled>1</enabled><subtitles></subtitles></subtitleparams>",
|
||||
);
|
||||
|
||||
// The populated set round-trips through load_xml.
|
||||
let h = make_populated();
|
||||
let expected = "<subtitleparams><streamindex>2</streamindex><enabled>0</enabled>\
|
||||
<subtitles><subtitle in=\"0/1\" out=\"25/1\">hello</subtitle>\
|
||||
<subtitle in=\"25/1\" out=\"50/1\">world</subtitle></subtitles></subtitleparams>";
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_save_xml(dup(&h), buf, size),
|
||||
expected,
|
||||
);
|
||||
|
||||
// A loaded fragment round-trips byte-for-byte.
|
||||
let xml = "<subtitleparams><streamindex>9</streamindex><enabled>1</enabled>\
|
||||
<subtitles><subtitle in=\"3/2\" out=\"5/1\">hi</subtitle></subtitles></subtitleparams>";
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_load_xml(dup(&h), to_cstring(xml).as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_subtitleparams_save_xml(dup(&h), buf, size),
|
||||
xml,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_save_xml(CHandle::null(), std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_save_xml(dup(&h), std::ptr::null_mut(), 5),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
|
||||
// The saved XML parses back to the same data via the C API.
|
||||
let mut si = -1i32;
|
||||
let mut en = -1i32;
|
||||
let mut c = -1i32;
|
||||
let mut n = -1i32;
|
||||
let mut d = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_stream_index(dup(&h), &mut si),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(si, 9);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_enabled(dup(&h), &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(en, 1);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_count(dup(&h), &mut c),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(c, 1);
|
||||
assert_eq!(
|
||||
oakcommon_subtitleparams_get_subtitle(dup(&h), 0, &mut n, &mut d, &mut si, &mut en),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!((n, d), (3, 2));
|
||||
assert_eq!((si, en), (5, 1));
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,761 +0,0 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! FFI-level integration tests for the C-ABI XML streaming exports in
|
||||
//! `oakcommon::ffi::xmlutils`, asserted against the C++ oracle
|
||||
//! `src/common/c_api/xmlutils.cpp`.
|
||||
//!
|
||||
//! `ffi::error` and `ffi::error_abi` are documented placeholder modules
|
||||
//! (`include/common/error.h` exposes no functions; the `OAKCOMMON_OK` /
|
||||
//! `OAKCOMMON_E_*` constants live in `crate::error`), so the only callable
|
||||
//! surface here is `xmlutils`. The error constants its exports return are
|
||||
//! pinned in `error_constants_returned_by_xmlutils_match_header`; the full
|
||||
//! set is already asserted in `contract.rs`.
|
||||
//!
|
||||
//! The contract under test (each point matches the C++ oracle):
|
||||
//! - exports take a `CHandle` by value and never release it;
|
||||
//! - two-stage string getters return the required size (NUL included) and
|
||||
//! only copy when the buffer is large enough — they never truncate;
|
||||
//! - out-of-range attribute indexes report `OAKCOMMON_E_NOT_FOUND`;
|
||||
//! - a malformed document still yields a usable handle whose `has_error`
|
||||
//! flag reads back 1 and whose navigation reports no start elements.
|
||||
|
||||
use std::ffi::{c_char, CString};
|
||||
|
||||
use oakcommon::error::{OAKCOMMON_E_INVALID, OAKCOMMON_E_NOT_FOUND, OAKCOMMON_OK};
|
||||
use oakcommon::ffi::xmlutils::*;
|
||||
use oakcommon::handle::{CHandle, OAKCOMMON_ABI_VERSION};
|
||||
|
||||
/// Shared sample document: two attributes on the root plus a nested text
|
||||
/// element. The writer test sequence reproduces this exact document.
|
||||
const DOC: &str = r#"<root a="1" b="two"><child>text here</child></root>"#;
|
||||
|
||||
/// Convert a string slice to a NUL-terminated C string for FFI inputs.
|
||||
fn to_cstring(s: &str) -> CString {
|
||||
CString::new(s).expect("test string must not contain NUL")
|
||||
}
|
||||
|
||||
/// Cheap struct copy: `CHandle` is neither `Clone` nor `Copy`, but every
|
||||
/// getter takes it by value. Rebuilding from the same fields duplicates
|
||||
/// only the handle value — the box stays alive as long as the original
|
||||
/// handle lives, and the getters never release.
|
||||
fn dup(h: &CHandle) -> CHandle {
|
||||
CHandle {
|
||||
ctx: h.ctx,
|
||||
addref: h.addref,
|
||||
release: h.release,
|
||||
abi_version: h.abi_version,
|
||||
}
|
||||
}
|
||||
|
||||
/// Drive a two-stage string getter against the C++ `copy_string`
|
||||
/// convention: a null-buffer size query, a short buffer that must stay
|
||||
/// untouched (no truncation), an exact-fit copy with its NUL, and an
|
||||
/// oversized copy with the tail untouched.
|
||||
fn assert_two_stage_getter(getter: impl Fn(*mut c_char, i32) -> i32, expected: &str) {
|
||||
let required = (expected.len() + 1) as i32;
|
||||
|
||||
// Size query: a null buffer returns the required size, NUL included.
|
||||
assert_eq!(getter(std::ptr::null_mut(), 0), required);
|
||||
|
||||
// Short buffer: too small, so nothing is written to it.
|
||||
let short_size = (required - 1).max(0);
|
||||
let mut short = vec![0xABu8; short_size as usize];
|
||||
assert_eq!(
|
||||
getter(short.as_mut_ptr() as *mut c_char, short_size),
|
||||
required
|
||||
);
|
||||
assert!(
|
||||
short.iter().all(|&b| b == 0xAB),
|
||||
"short buffer must stay untouched"
|
||||
);
|
||||
|
||||
// Exact fit: payload followed by a NUL.
|
||||
let mut exact = vec![0xCDu8; required as usize];
|
||||
assert_eq!(
|
||||
getter(exact.as_mut_ptr() as *mut c_char, required),
|
||||
required
|
||||
);
|
||||
assert_eq!(&exact[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(exact[expected.len()], 0);
|
||||
|
||||
// Oversized: payload and NUL written, tail left as initialized.
|
||||
let mut big = vec![0u8; (required + 8) as usize];
|
||||
assert_eq!(
|
||||
getter(big.as_mut_ptr() as *mut c_char, required + 8),
|
||||
required
|
||||
);
|
||||
assert_eq!(&big[..expected.len()], expected.as_bytes());
|
||||
assert_eq!(big[expected.len()], 0);
|
||||
assert!(big[(required + 1) as usize..].iter().all(|&b| b == 0));
|
||||
}
|
||||
|
||||
// ---- Reader: handle lifecycle ----
|
||||
|
||||
/// `init` with a null data pointer yields an empty handle.
|
||||
#[test]
|
||||
fn init_returns_null_handle_for_null_data() {
|
||||
let h = oakcommon_xml_reader_init(std::ptr::null());
|
||||
assert!(h.is_null());
|
||||
assert!(h.ctx.is_null());
|
||||
assert!(h.addref.is_none());
|
||||
assert!(h.release.is_none());
|
||||
}
|
||||
|
||||
/// `init` over valid data yields a stamped, non-empty handle.
|
||||
#[test]
|
||||
fn init_creates_stamped_handle() {
|
||||
let h = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
assert!(!h.is_null());
|
||||
assert_eq!(h.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(h.addref.is_some());
|
||||
assert!(h.release.is_some());
|
||||
}
|
||||
|
||||
/// `free` nullifies the handle, is idempotent, and tolerates a null
|
||||
/// pointer.
|
||||
#[test]
|
||||
fn free_nullifies_and_is_idempotent() {
|
||||
let mut h = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
assert!(!h.is_null());
|
||||
oakcommon_xml_reader_free(&mut h);
|
||||
assert!(h.is_null());
|
||||
// A second free of the now-empty handle is safe.
|
||||
oakcommon_xml_reader_free(&mut h);
|
||||
assert!(h.is_null());
|
||||
// Freeing a null pointer is safe.
|
||||
oakcommon_xml_reader_free(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
// ---- Reader: navigation ----
|
||||
|
||||
/// `read_next_start_element` writes 1 while a start element is found and
|
||||
/// 0 once the document is exhausted (CPP-PARITY: an end element and the
|
||||
/// end of the document both report 0, not an error).
|
||||
#[test]
|
||||
fn read_next_start_element_reports_found() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1); // root
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1); // child
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0); // child's end element
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0); // root's end element
|
||||
}
|
||||
|
||||
/// A null reader handle or a null `found` out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn read_next_start_element_rejects_null_args() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(CHandle::null(), &mut found),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Reader: two-stage string getters ----
|
||||
|
||||
/// `name` is a two-stage getter over the current element's name; a null
|
||||
/// handle is `E_INVALID`.
|
||||
#[test]
|
||||
fn name_two_stage_getter() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_name(dup(&r), buf, size),
|
||||
"root",
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_name(CHandle::null(), std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// Before any token is read the name is the empty string (size 1).
|
||||
#[test]
|
||||
fn name_is_empty_before_any_read() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_name(dup(&r), std::ptr::null_mut(), 0),
|
||||
1
|
||||
);
|
||||
}
|
||||
|
||||
/// `read_element_text` is a two-stage getter and caches its result, so a
|
||||
/// second read returns the same text even though the stream was consumed
|
||||
/// by the first (CPP-PARITY with the C++ `XmlReaderState::cached_text`).
|
||||
#[test]
|
||||
fn read_element_text_two_stage_with_cache() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_read_element_text(dup(&r), buf, size),
|
||||
"text here",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_read_element_text(dup(&r), buf, size),
|
||||
"text here",
|
||||
);
|
||||
}
|
||||
|
||||
/// When not on a start element the text is empty (CPP-PARITY: the C++
|
||||
/// reader returns an empty string).
|
||||
#[test]
|
||||
fn read_element_text_not_on_start_element_is_empty() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_element_text(dup(&r), std::ptr::null_mut(), 0),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_element_text(CHandle::null(), std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `skip_current_element` consumes the current element and its subtree; a
|
||||
/// null handle is `E_INVALID`.
|
||||
#[test]
|
||||
fn skip_current_element_skips_subtree() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_skip_current_element(dup(&r)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_skip_current_element(CHandle::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Reader: attributes ----
|
||||
|
||||
/// `attribute_count` reports 0 before any token and the real count on a
|
||||
/// start element; a null handle or a null count out-param is `E_INVALID`.
|
||||
#[test]
|
||||
fn attribute_count_reports_attributes() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut count = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_count(dup(&r), &mut count),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(count, 0);
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_count(dup(&r), &mut count),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(count, 2);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_count(CHandle::null(), &mut count),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_count(dup(&r), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// `attribute_name` / `attribute_value` are two-stage getters over the
|
||||
/// attributes of the current start element, in document order.
|
||||
#[test]
|
||||
fn attribute_name_and_value_two_stage() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_attribute_name(dup(&r), 0, buf, size),
|
||||
"a",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_attribute_value(dup(&r), 0, buf, size),
|
||||
"1",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_attribute_name(dup(&r), 1, buf, size),
|
||||
"b",
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_attribute_value(dup(&r), 1, buf, size),
|
||||
"two",
|
||||
);
|
||||
}
|
||||
|
||||
/// An out-of-range attribute index is `E_NOT_FOUND` (CPP-PARITY), while a
|
||||
/// null handle is `E_INVALID`.
|
||||
#[test]
|
||||
fn attribute_out_of_range_is_not_found() {
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_name(dup(&r), 2, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_name(dup(&r), -1, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_value(dup(&r), 2, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_attribute_name(CHandle::null(), 0, std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Reader: error reporting ----
|
||||
|
||||
/// `has_error` is 0 for a well-formed document and 1 for a malformed one;
|
||||
/// a malformed document still navigates safely and reports no start
|
||||
/// elements (CPP-PARITY: parse failures surface lazily through
|
||||
/// `has_error`, never at init time).
|
||||
#[test]
|
||||
fn has_error_flags_malformed_documents() {
|
||||
let mut err = -1i32;
|
||||
|
||||
let r = oakcommon_xml_reader_init(to_cstring(DOC).as_ptr());
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_has_error(dup(&r), &mut err),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(err, 0);
|
||||
|
||||
let bad = oakcommon_xml_reader_init(to_cstring("<a></b>").as_ptr());
|
||||
assert!(!bad.is_null());
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_has_error(dup(&bad), &mut err),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(err, 1);
|
||||
let mut found = -1i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&bad), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0);
|
||||
|
||||
let empty = oakcommon_xml_reader_init(to_cstring("").as_ptr());
|
||||
assert!(!empty.is_null());
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_has_error(dup(&empty), &mut err),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(err, 1);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_has_error(CHandle::null(), &mut err),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_has_error(dup(&r), std::ptr::null_mut()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Writer: handle lifecycle ----
|
||||
|
||||
/// `writer_init` yields a stamped handle; `free` nullifies it, is
|
||||
/// idempotent, and tolerates a null pointer.
|
||||
#[test]
|
||||
fn writer_init_free_and_idempotent() {
|
||||
let mut w = oakcommon_xml_writer_init();
|
||||
assert!(!w.is_null());
|
||||
assert_eq!(w.abi_version, OAKCOMMON_ABI_VERSION);
|
||||
assert!(w.addref.is_some());
|
||||
assert!(w.release.is_some());
|
||||
oakcommon_xml_writer_free(&mut w);
|
||||
assert!(w.is_null());
|
||||
oakcommon_xml_writer_free(&mut w);
|
||||
assert!(w.is_null());
|
||||
oakcommon_xml_writer_free(std::ptr::null_mut());
|
||||
}
|
||||
|
||||
// ---- Writer: operations ----
|
||||
|
||||
/// Every writer export rejects a null handle with `E_INVALID`, and the
|
||||
/// string-taking exports reject null strings.
|
||||
#[test]
|
||||
fn writer_rejects_null_arguments() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(CHandle::null(), to_cstring("a").as_ptr()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
CHandle::null(),
|
||||
to_cstring("a").as_ptr(),
|
||||
to_cstring("b").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_characters(CHandle::null(), to_cstring("x").as_ptr()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
CHandle::null(),
|
||||
to_cstring("a").as_ptr(),
|
||||
to_cstring("b").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(CHandle::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_document(CHandle::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_output(CHandle::null(), std::ptr::null_mut(), 0),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(dup(&w), std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(dup(&w), std::ptr::null(), to_cstring("b").as_ptr()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(dup(&w), to_cstring("a").as_ptr(), std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_characters(dup(&w), std::ptr::null()),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
dup(&w),
|
||||
std::ptr::null(),
|
||||
to_cstring("b").as_ptr()
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
dup(&w),
|
||||
to_cstring("a").as_ptr(),
|
||||
std::ptr::null()
|
||||
),
|
||||
OAKCOMMON_E_INVALID
|
||||
);
|
||||
}
|
||||
|
||||
/// The writer sequence builds the exact sample document, and `output` is a
|
||||
/// two-stage getter over it.
|
||||
#[test]
|
||||
fn writer_builds_document_and_output_two_stage() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(dup(&w), to_cstring("root").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("a").as_ptr(),
|
||||
to_cstring("1").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("b").as_ptr(),
|
||||
to_cstring("two").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
dup(&w),
|
||||
to_cstring("child").as_ptr(),
|
||||
to_cstring("text here").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_document(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_writer_output(dup(&w), buf, size),
|
||||
DOC,
|
||||
);
|
||||
}
|
||||
|
||||
/// The writer's output round-trips through the reader.
|
||||
#[test]
|
||||
fn writer_output_round_trips_through_reader() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(dup(&w), to_cstring("root").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("a").as_ptr(),
|
||||
to_cstring("1").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("b").as_ptr(),
|
||||
to_cstring("two").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
dup(&w),
|
||||
to_cstring("child").as_ptr(),
|
||||
to_cstring("text here").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
|
||||
let mut out = vec![0u8; 64];
|
||||
let needed = oakcommon_xml_writer_output(dup(&w), out.as_mut_ptr() as *mut c_char, 64);
|
||||
assert_eq!(needed, (DOC.len() + 1) as i32);
|
||||
assert_eq!(&out[..DOC.len()], DOC.as_bytes());
|
||||
assert_eq!(out[DOC.len()], 0);
|
||||
|
||||
let r = oakcommon_xml_reader_init(out.as_ptr() as *const c_char);
|
||||
assert!(!r.is_null());
|
||||
let mut found = 0i32;
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_name(dup(&r), buf, size),
|
||||
"root",
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 1);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_reader_read_element_text(dup(&r), buf, size),
|
||||
"text here",
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_reader_read_next_start_element(dup(&r), &mut found),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(found, 0);
|
||||
}
|
||||
|
||||
/// Documented no-op writer operations (attribute without an open start
|
||||
/// tag, end element on an empty stack, end document with nothing open)
|
||||
/// still return `OK`.
|
||||
#[test]
|
||||
fn writer_noop_operations_return_ok() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("a").as_ptr(),
|
||||
to_cstring("b").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_document(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_characters(dup(&w), to_cstring("x").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_writer_output(dup(&w), buf, size),
|
||||
"x",
|
||||
);
|
||||
}
|
||||
|
||||
/// An empty element with attributes serializes as `<a k="v"/>`.
|
||||
#[test]
|
||||
fn writer_self_closing_empty_element() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(dup(&w), to_cstring("a").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("k").as_ptr(),
|
||||
to_cstring("v").as_ptr()
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_writer_output(dup(&w), buf, size),
|
||||
r#"<a k="v"/>"#,
|
||||
);
|
||||
}
|
||||
|
||||
/// Text and attribute values are escaped for the five predefined XML
|
||||
/// entities.
|
||||
#[test]
|
||||
fn writer_escapes_text_and_attributes() {
|
||||
let w = oakcommon_xml_writer_init();
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_text_element(
|
||||
dup(&w),
|
||||
to_cstring("e").as_ptr(),
|
||||
to_cstring("hi & bye <there>").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_start_element(dup(&w), to_cstring("a").as_ptr()),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_attribute(
|
||||
dup(&w),
|
||||
to_cstring("q").as_ptr(),
|
||||
to_cstring("x\"y&z").as_ptr(),
|
||||
),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_eq!(
|
||||
oakcommon_xml_writer_write_end_element(dup(&w)),
|
||||
OAKCOMMON_OK
|
||||
);
|
||||
assert_two_stage_getter(
|
||||
|buf, size| oakcommon_xml_writer_output(dup(&w), buf, size),
|
||||
r#"<e>hi & bye <there></e><a q="x"y&z"/>"#,
|
||||
);
|
||||
}
|
||||
|
||||
// ---- error / error_abi ----
|
||||
|
||||
/// The constants the xmlutils exports return are defined in `crate::error`
|
||||
/// and match `include/common/error.h` (the `ffi::error` / `ffi::error_abi`
|
||||
/// modules only point here). The full set is pinned in `contract.rs`.
|
||||
#[test]
|
||||
fn error_constants_returned_by_xmlutils_match_header() {
|
||||
assert_eq!(OAKCOMMON_OK, 0);
|
||||
assert_eq!(OAKCOMMON_E_INVALID, -10001);
|
||||
assert_eq!(OAKCOMMON_E_NOT_FOUND, -10004);
|
||||
}
|
||||
Reference in New Issue
Block a user