diff --git a/iceoryx2-ffi/ffi/Cargo.toml b/iceoryx2-ffi/ffi/Cargo.toml index ddcb9662f..084ee5032 100644 --- a/iceoryx2-ffi/ffi/Cargo.toml +++ b/iceoryx2-ffi/ffi/Cargo.toml @@ -30,3 +30,4 @@ iceoryx2-ffi-macros = { workspace = true } [dev-dependencies] iceoryx2-bb-testing = { workspace = true } +generic-tests = { workspace = true } diff --git a/iceoryx2-ffi/ffi/src/node.rs b/iceoryx2-ffi/ffi/src/node.rs index b68540086..0d67fa3a2 100644 --- a/iceoryx2-ffi/ffi/src/node.rs +++ b/iceoryx2-ffi/ffi/src/node.rs @@ -348,150 +348,3 @@ pub unsafe extern "C" fn iox2_node_drop(node_handle: iox2_node_h) { } // END C API - -#[cfg(test)] -mod test { - use crate::*; - use iceoryx2_bb_testing::assert_that; - - use core::{slice, str}; - - fn create_sut_node() -> iox2_node_h { - unsafe { - let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut()); - - let mut node_name_handle = std::ptr::null_mut(); - let node_name = "hypnotoad"; - let ret_val = iox2_node_name_new( - std::ptr::null_mut(), - node_name.as_ptr() as *const _, - node_name.len() as _, - &mut node_name_handle, - ); - assert_that!(ret_val, eq(IOX2_OK)); - iox2_node_builder_set_name( - iox2_cast_node_builder_ref_h(node_builder_handle), - node_name_handle, - ); - - let mut node_handle: iox2_node_h = std::ptr::null_mut(); - let ret_val = iox2_node_builder_create( - node_builder_handle, - std::ptr::null_mut(), - iox2_service_type_e::IPC, - &mut node_handle as *mut iox2_node_h, - ); - - assert_that!(ret_val, eq(IOX2_OK)); - - node_handle - } - } - - #[test] - fn basic_node_api_test() { - unsafe { - let node_handle = create_sut_node(); - - assert_that!(node_handle, ne(std::ptr::null_mut())); - - iox2_node_drop(node_handle); - } - } - - #[test] - fn basic_node_config_test() { - unsafe { - let node_handle = create_sut_node(); - - let expected_config = Config::global_config(); - - let config = iox2_node_config(node_handle); - - assert_that!(*(config as *const Config), eq(*expected_config)); - - iox2_node_drop(node_handle); - } - } - - #[test] - fn basic_node_name_test() -> Result<(), Box> { - unsafe { - let node_handle = create_sut_node(); - - let node_name = iox2_node_name(node_handle); - - let mut node_name_len = 0; - let node_name_c_str = iox2_node_name_as_c_str(node_name, &mut node_name_len); - - let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _); - let node_name_str = str::from_utf8(slice)?; - - assert_that!(node_name_str, eq("hypnotoad")); - - iox2_node_drop(node_handle); - - Ok(()) - } - } - - #[derive(Default)] - struct NodeListCtx { - alive: u64, - dead: u64, - inaccessible: u64, - undefined: u64, - } - - extern "C" fn node_list_callback( - node_state: iox2_node_state_e, - _node_id_ptr: iox2_node_id_ptr, - _node_name_ptr: iox2_node_name_ptr, - _config_ptr: iox2_config_ptr, - ctx: iox2_node_list_callback_context, - ) -> iox2_callback_progression_e { - let ctx = unsafe { &mut *(ctx as *mut NodeListCtx) }; - - match node_state { - iox2_node_state_e::ALIVE => { - ctx.alive += 1; - } - iox2_node_state_e::DEAD => { - ctx.dead += 1; - } - iox2_node_state_e::INACCESSIBLE => { - ctx.inaccessible += 1; - } - iox2_node_state_e::UNDEFINED => { - ctx.undefined += 1; - } - } - - iox2_callback_progression_e::CONTINUE - } - - #[test] - fn basic_node_list_test() { - unsafe { - let mut ctx = NodeListCtx::default(); - let node_handle = create_sut_node(); - let config = iox2_node_config(node_handle); - - let ret_val = iox2_node_list( - iox2_service_type_e::IPC, - config, - node_list_callback, - &mut ctx as *mut _ as *mut _, - ); - - iox2_node_drop(node_handle); - - assert_that!(ret_val, eq(IOX2_OK)); - - assert_that!(ctx.alive, eq(1)); - assert_that!(ctx.dead, eq(0)); - assert_that!(ctx.inaccessible, eq(0)); - assert_that!(ctx.undefined, eq(0)); - } - } -} diff --git a/iceoryx2-ffi/ffi/src/node_builder.rs b/iceoryx2-ffi/ffi/src/node_builder.rs index d7d3340b3..3b5af15cd 100644 --- a/iceoryx2-ffi/ffi/src/node_builder.rs +++ b/iceoryx2-ffi/ffi/src/node_builder.rs @@ -253,28 +253,3 @@ pub unsafe extern "C" fn iox2_node_builder_create( } // END C API - -#[cfg(test)] -mod test { - use crate::*; - use iceoryx2_bb_testing::assert_that; - - #[test] - fn basic_node_builder_api_test() { - unsafe { - let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut()); - let mut node_handle: iox2_node_h = std::ptr::null_mut(); - let ret_val = iox2_node_builder_create( - node_builder_handle, - std::ptr::null_mut(), - iox2_service_type_e::LOCAL, - &mut node_handle as *mut iox2_node_h, - ); - - assert_that!(ret_val, eq(IOX2_OK)); - assert_that!(node_handle, ne(std::ptr::null_mut())); - - iox2_node_drop(node_handle); - } - } -} diff --git a/iceoryx2-ffi/ffi/src/node_name.rs b/iceoryx2-ffi/ffi/src/node_name.rs index 88de26b9e..9bd3252ea 100644 --- a/iceoryx2-ffi/ffi/src/node_name.rs +++ b/iceoryx2-ffi/ffi/src/node_name.rs @@ -210,41 +210,3 @@ pub unsafe extern "C" fn iox2_node_name_drop(node_name_handle: iox2_node_name_h) } // END C API - -#[cfg(test)] -mod test { - use super::*; - - use iceoryx2_bb_testing::assert_that; - - #[test] - fn basic_node_name_test() -> Result<(), Box> { - unsafe { - let expected_node_name = NodeName::new("hypnotaod")?; - - let mut node_name_handle: iox2_node_name_h = std::ptr::null_mut(); - let ret_val = iox2_node_name_new( - std::ptr::null_mut(), - expected_node_name.as_str().as_ptr() as *const _, - expected_node_name.len() as _, - &mut node_name_handle, - ); - assert_that!(ret_val, eq(IOX2_OK)); - - let mut node_name_len = 0; - let node_name_c_str = iox2_node_name_as_c_str( - iox2_cast_node_name_ptr(node_name_handle), - &mut node_name_len, - ); - - let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _); - let node_name = str::from_utf8(slice)?; - - assert_that!(node_name, eq(expected_node_name.as_str())); - - iox2_node_name_drop(node_name_handle); - - Ok(()) - } - } -} diff --git a/iceoryx2-ffi/ffi/tests/common.rs b/iceoryx2-ffi/ffi/tests/common.rs new file mode 100644 index 000000000..e20232da8 --- /dev/null +++ b/iceoryx2-ffi/ffi/tests/common.rs @@ -0,0 +1,65 @@ +// Copyright (c) 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +#![allow(dead_code)] + +use iceoryx2::prelude::*; +use iceoryx2_bb_testing::assert_that; +use iceoryx2_ffi::*; + +pub(crate) trait ServiceTypeMapping { + fn service_type() -> iox2_service_type_e; +} + +impl ServiceTypeMapping for iceoryx2::service::zero_copy::Service { + fn service_type() -> iox2_service_type_e { + iox2_service_type_e::IPC + } +} + +impl ServiceTypeMapping for iceoryx2::service::process_local::Service { + fn service_type() -> iox2_service_type_e { + iox2_service_type_e::LOCAL + } +} + +#[cfg(test)] +pub(crate) fn create_node(node_name: &str) -> iox2_node_h { + unsafe { + let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut()); + + let mut node_name_handle = std::ptr::null_mut(); + let ret_val = iox2_node_name_new( + std::ptr::null_mut(), + node_name.as_ptr() as *const _, + node_name.len() as _, + &mut node_name_handle, + ); + assert_that!(ret_val, eq(IOX2_OK)); + iox2_node_builder_set_name( + iox2_cast_node_builder_ref_h(node_builder_handle), + node_name_handle, + ); + + let mut node_handle: iox2_node_h = std::ptr::null_mut(); + let ret_val = iox2_node_builder_create( + node_builder_handle, + std::ptr::null_mut(), + S::service_type(), + &mut node_handle as *mut iox2_node_h, + ); + + assert_that!(ret_val, eq(IOX2_OK)); + + node_handle + } +} diff --git a/iceoryx2-ffi/ffi/tests/node_builder_tests.rs b/iceoryx2-ffi/ffi/tests/node_builder_tests.rs new file mode 100644 index 000000000..5497c725f --- /dev/null +++ b/iceoryx2-ffi/ffi/tests/node_builder_tests.rs @@ -0,0 +1,47 @@ +// Copyright (c) 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +mod common; + +#[generic_tests::define] +mod node_builder { + + use crate::common::*; + use iceoryx2::prelude::*; + use iceoryx2_bb_testing::assert_that; + use iceoryx2_ffi::*; + + #[test] + fn basic_node_builder_api_test() { + unsafe { + let node_builder_handle = iox2_node_builder_new(std::ptr::null_mut()); + let mut node_handle: iox2_node_h = std::ptr::null_mut(); + let ret_val = iox2_node_builder_create( + node_builder_handle, + std::ptr::null_mut(), + S::service_type(), + &mut node_handle as *mut iox2_node_h, + ); + + assert_that!(ret_val, eq(IOX2_OK)); + assert_that!(node_handle, ne(std::ptr::null_mut())); + + iox2_node_drop(node_handle); + } + } + + #[instantiate_tests()] + mod ipc {} + + #[instantiate_tests()] + mod local {} +} diff --git a/iceoryx2-ffi/ffi/tests/node_name_tests.rs b/iceoryx2-ffi/ffi/tests/node_name_tests.rs new file mode 100644 index 000000000..dfb49308a --- /dev/null +++ b/iceoryx2-ffi/ffi/tests/node_name_tests.rs @@ -0,0 +1,48 @@ +// Copyright (c) 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use iceoryx2::prelude::*; +use iceoryx2_bb_testing::assert_that; +use iceoryx2_ffi::*; + +use core::{slice, str}; + +#[test] +fn basic_node_name_test() -> Result<(), Box> { + unsafe { + let expected_node_name = NodeName::new("hypnotaod")?; + + let mut node_name_handle: iox2_node_name_h = std::ptr::null_mut(); + let ret_val = iox2_node_name_new( + std::ptr::null_mut(), + expected_node_name.as_str().as_ptr() as *const _, + expected_node_name.len() as _, + &mut node_name_handle, + ); + assert_that!(ret_val, eq(IOX2_OK)); + + let mut node_name_len = 0; + let node_name_c_str = iox2_node_name_as_c_str( + iox2_cast_node_name_ptr(node_name_handle), + &mut node_name_len, + ); + + let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _); + let node_name = str::from_utf8(slice)?; + + assert_that!(node_name, eq(expected_node_name.as_str())); + + iox2_node_name_drop(node_name_handle); + + Ok(()) + } +} diff --git a/iceoryx2-ffi/ffi/tests/node_tests.rs b/iceoryx2-ffi/ffi/tests/node_tests.rs new file mode 100644 index 000000000..8a90f46c8 --- /dev/null +++ b/iceoryx2-ffi/ffi/tests/node_tests.rs @@ -0,0 +1,139 @@ +// Copyright (c) 2024 Contributors to the Eclipse Foundation +// +// See the NOTICE file(s) distributed with this work for additional +// information regarding copyright ownership. +// +// This program and the accompanying materials are made available under the +// terms of the Apache Software License 2.0 which is available at +// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license +// which is available at https://opensource.org/licenses/MIT. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +mod common; + +#[generic_tests::define] +mod node { + + use crate::common::*; + + use iceoryx2::prelude::*; + use iceoryx2_bb_testing::assert_that; + use iceoryx2_ffi::*; + + use core::{slice, str}; + + #[test] + fn basic_node_api_test() { + unsafe { + let node_handle = create_node::(""); + + assert_that!(node_handle, ne(std::ptr::null_mut())); + + iox2_node_drop(node_handle); + } + } + + #[test] + fn basic_node_config_test() { + unsafe { + let node_handle = create_node::(""); + + let expected_config = Config::global_config(); + + let config = iox2_node_config(node_handle); + + assert_that!(*(config as *const Config), eq(*expected_config)); + + iox2_node_drop(node_handle); + } + } + + #[test] + fn basic_node_name_test( + ) -> Result<(), Box> { + unsafe { + let node_handle = create_node::("hypnotoad"); + + let node_name = iox2_node_name(node_handle); + + let mut node_name_len = 0; + let node_name_c_str = iox2_node_name_as_c_str(node_name, &mut node_name_len); + + let slice = slice::from_raw_parts(node_name_c_str as *const _, node_name_len as _); + let node_name_str = str::from_utf8(slice)?; + + assert_that!(node_name_str, eq("hypnotoad")); + + iox2_node_drop(node_handle); + + Ok(()) + } + } + + #[derive(Default)] + struct NodeListCtx { + alive: u64, + dead: u64, + inaccessible: u64, + undefined: u64, + } + + extern "C" fn node_list_callback( + node_state: iox2_node_state_e, + _node_id_ptr: iox2_node_id_ptr, + _node_name_ptr: iox2_node_name_ptr, + _config_ptr: iox2_config_ptr, + ctx: iox2_node_list_callback_context, + ) -> iox2_callback_progression_e { + let ctx = unsafe { &mut *(ctx as *mut NodeListCtx) }; + + match node_state { + iox2_node_state_e::ALIVE => { + ctx.alive += 1; + } + iox2_node_state_e::DEAD => { + ctx.dead += 1; + } + iox2_node_state_e::INACCESSIBLE => { + ctx.inaccessible += 1; + } + iox2_node_state_e::UNDEFINED => { + ctx.undefined += 1; + } + } + + iox2_callback_progression_e::CONTINUE + } + + #[test] + fn basic_node_list_test() { + unsafe { + let mut ctx = NodeListCtx::default(); + let node_handle = create_node::(""); + let config = iox2_node_config(node_handle); + + let ret_val = iox2_node_list( + S::service_type(), + config, + node_list_callback, + &mut ctx as *mut _ as *mut _, + ); + + iox2_node_drop(node_handle); + + assert_that!(ret_val, eq(IOX2_OK)); + + assert_that!(ctx.alive, eq(1)); + assert_that!(ctx.dead, eq(0)); + assert_that!(ctx.inaccessible, eq(0)); + assert_that!(ctx.undefined, eq(0)); + } + } + + #[instantiate_tests()] + mod ipc {} + + #[instantiate_tests()] + mod local {} +}