From: Tage Johansson <frans.tage(a)gmail.com>
---
rust/Cargo.toml | 3 +
rust/Makefile.am | 1 +
rust/run-tests.sh | 6 +-
rust/tests/test_100_handle.rs | 25 +++++++
rust/tests/test_110_defaults.rs | 33 ++++++++++
rust/tests/test_120_set_non_defaults.rs | 56 ++++++++++++++++
rust/tests/test_130_private_data.rs | 28 ++++++++
rust/tests/test_140_explicit_close.rs | 31 +++++++++
rust/tests/test_200_connect_command.rs | 33 ++++++++++
rust/tests/test_210_opt_abort.rs | 39 +++++++++++
rust/tests/test_220_opt_list.rs | 85 ++++++++++++++++++++++++
rust/tests/test_log/mod.rs | 86 +++++++++++++++++++++++++
12 files changed, 424 insertions(+), 2 deletions(-)
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 0a934e7..2ce7e06 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -46,3 +46,6 @@ log = { version = "0.4.19", optional = true }
[features]
default = ["log"]
+
+[dev-dependencies]
+byte-strings = "0.3.1"
diff --git a/rust/Makefile.am b/rust/Makefile.am
index cb8d7c9..9ff0779 100644
--- a/rust/Makefile.am
+++ b/rust/Makefile.am
@@ -49,6 +49,7 @@ TESTS_ENVIRONMENT = \
LIBNBD_DEBUG=1 \
$(MALLOC_CHECKS) \
abs_top_srcdir=$(abs_top_srcdir) \
+ CARGO=$(CARGO) \
$(NULL)
LOG_COMPILER = $(top_builddir)/run
TESTS = run-tests.sh
diff --git a/rust/run-tests.sh b/rust/run-tests.sh
index 7a0bc85..005000e 100755
--- a/rust/run-tests.sh
+++ b/rust/run-tests.sh
@@ -1,6 +1,6 @@
#!/bin/bash -
# nbd client library in userspace
-# Copyright Red Hat
+# Copyright Tage Johansson
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -21,4 +21,6 @@
set -e
set -x
-cargo test
+requires nbdkit --version
+
+$CARGO test -- --nocapture
diff --git a/rust/tests/test_100_handle.rs b/rust/tests/test_100_handle.rs
new file mode 100644
index 0000000..3a281ca
--- /dev/null
+++ b/rust/tests/test_100_handle.rs
@@ -0,0 +1,25 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+//! Just check that we can link with libnbd and create a handle.
+
+#![deny(warnings)]
+
+#[test]
+fn test_nbd_handle_new() {
+ let _ = libnbd::NbdHandle::new().unwrap();
+}
diff --git a/rust/tests/test_110_defaults.rs b/rust/tests/test_110_defaults.rs
new file mode 100644
index 0000000..8960ef9
--- /dev/null
+++ b/rust/tests/test_110_defaults.rs
@@ -0,0 +1,33 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+#[test]
+fn test_defaults() {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+ assert!(nbd.get_export_name().unwrap().to_str().unwrap().is_empty());
+ assert!(!nbd.get_full_info().unwrap());
+ assert_eq!(nbd.get_tls(), libnbd::Tls::Disable);
+ assert!(nbd.get_request_structured_replies());
+ assert!(nbd.get_request_meta_context().unwrap());
+ assert!(nbd.get_request_block_size().unwrap());
+ assert!(nbd.get_pread_initialize());
+ assert!(nbd.get_handshake_flags().is_all());
+ assert!(!nbd.get_opt_mode());
+}
diff --git a/rust/tests/test_120_set_non_defaults.rs
b/rust/tests/test_120_set_non_defaults.rs
new file mode 100644
index 0000000..88dd2c8
--- /dev/null
+++ b/rust/tests/test_120_set_non_defaults.rs
@@ -0,0 +1,56 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+use std::ffi::CString;
+
+#[test]
+fn test_set_non_defaults() {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+ let name = CString::new("name").unwrap();
+ nbd.set_export_name(&name).unwrap();
+ assert_eq!(nbd.get_export_name().unwrap(), name);
+
+ nbd.set_full_info(true).unwrap();
+ assert!(nbd.get_full_info().unwrap());
+
+ if nbd.supports_tls() {
+ nbd.set_tls(libnbd::Tls::Allow).unwrap();
+ assert_eq!(nbd.get_tls(), libnbd::Tls::Allow);
+ }
+
+ nbd.set_request_structured_replies(false).unwrap();
+ assert!(!nbd.get_request_structured_replies());
+
+ nbd.set_request_meta_context(false).unwrap();
+ assert!(!nbd.get_request_meta_context().unwrap());
+
+ nbd.set_request_block_size(false).unwrap();
+ assert!(!nbd.get_request_block_size().unwrap());
+
+ nbd.set_pread_initialize(false).unwrap();
+ assert!(!nbd.get_pread_initialize());
+
+ nbd.set_handshake_flags(libnbd::HandshakeFlag::empty())
+ .unwrap();
+ assert!(nbd.get_handshake_flags().is_empty());
+
+ nbd.set_opt_mode(true).unwrap();
+ assert!(nbd.get_opt_mode());
+}
diff --git a/rust/tests/test_130_private_data.rs b/rust/tests/test_130_private_data.rs
new file mode 100644
index 0000000..62db160
--- /dev/null
+++ b/rust/tests/test_130_private_data.rs
@@ -0,0 +1,28 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+#[test]
+fn test_private_data() {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+
+ assert_eq!(nbd.get_private_data(), 0);
+ assert_eq!(nbd.set_private_data(42), 0);
+ assert_eq!(nbd.set_private_data(314), 42);
+ assert_eq!(nbd.get_private_data(), 314);
+}
diff --git a/rust/tests/test_140_explicit_close.rs
b/rust/tests/test_140_explicit_close.rs
new file mode 100644
index 0000000..19f4f85
--- /dev/null
+++ b/rust/tests/test_140_explicit_close.rs
@@ -0,0 +1,31 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+mod test_log;
+
+use test_log::DEBUG_LOGGER;
+
+#[test]
+fn test_private_data() {
+ DEBUG_LOGGER.init();
+
+ let nbd = libnbd::NbdHandle::new().unwrap();
+ drop(nbd);
+ assert!(DEBUG_LOGGER.contains("closing handle"));
+}
diff --git a/rust/tests/test_200_connect_command.rs
b/rust/tests/test_200_connect_command.rs
new file mode 100644
index 0000000..ad23d39
--- /dev/null
+++ b/rust/tests/test_200_connect_command.rs
@@ -0,0 +1,33 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+use byte_strings::c_str;
+
+#[test]
+fn test_connect_command() {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+ nbd.connect_command(&[
+ c_str!("nbdkit"),
+ c_str!("-s"),
+ c_str!("--exit-with-parent"),
+ c_str!("-v"),
+ c_str!("null"),
+ ])
+ .unwrap();
+}
diff --git a/rust/tests/test_210_opt_abort.rs b/rust/tests/test_210_opt_abort.rs
new file mode 100644
index 0000000..c5109d5
--- /dev/null
+++ b/rust/tests/test_210_opt_abort.rs
@@ -0,0 +1,39 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+use byte_strings::c_str;
+
+#[test]
+fn test_opt_abort() {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+ nbd.set_opt_mode(true).unwrap();
+ nbd.connect_command(&[
+ c_str!("nbdkit"),
+ c_str!("-s"),
+ c_str!("--exit-with-parent"),
+ c_str!("-v"),
+ c_str!("null"),
+ ])
+ .unwrap();
+ assert_eq!(nbd.get_protocol().unwrap(), c_str!("newstyle-fixed"));
+ assert!(nbd.get_structured_replies_negotiated().unwrap());
+
+ nbd.opt_abort().unwrap();
+ assert!(nbd.aio_is_closed());
+}
diff --git a/rust/tests/test_220_opt_list.rs b/rust/tests/test_220_opt_list.rs
new file mode 100644
index 0000000..d2357de
--- /dev/null
+++ b/rust/tests/test_220_opt_list.rs
@@ -0,0 +1,85 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#![deny(warnings)]
+
+use byte_strings::c_str;
+use std::cell::RefCell;
+use std::env;
+use std::ffi::{CStr, CString};
+use std::os::unix::ffi::OsStringExt as _;
+use std::path::Path;
+use std::rc::Rc;
+
+/// Test different types of connections.
+struct ConnTester {
+ script_path: CString,
+}
+
+impl ConnTester {
+ fn new() -> Self {
+ let srcdir = env::var("srcdir").unwrap();
+ let srcdir = Path::new(&srcdir);
+ let script_path = srcdir.join("../tests/opt-list.sh");
+ let script_path =
+ CString::new(script_path.into_os_string().into_vec()).unwrap();
+ Self { script_path }
+ }
+
+ fn connect(
+ &self,
+ mode: u8,
+ expected_exports: &[&CStr],
+ ) -> libnbd::Result<()> {
+ let mut nbd = libnbd::NbdHandle::new().unwrap();
+ nbd.set_opt_mode(true).unwrap();
+ nbd.connect_command(&[
+ c_str!("nbdkit"),
+ c_str!("-s"),
+ c_str!("--exit-with-parent"),
+ c_str!("-v"),
+ c_str!("sh"),
+ &self.script_path,
+ &CString::new(format!("mode={mode}")).unwrap(),
+ ])
+ .unwrap();
+
+ // Collect all exports in this list.
+ let exports = Rc::new(RefCell::new(Vec::new()));
+ let exports_clone = exports.clone();
+ let count = nbd.opt_list(move |name, _| {
+ exports_clone.borrow_mut().push(name.to_owned());
+ Ok(())
+ })?;
+ let exports = exports.borrow();
+ assert_eq!(exports.len(), count as usize);
+ assert_eq!(exports.len(), expected_exports.len());
+ for (export, &expected) in exports.iter().zip(expected_exports) {
+ assert_eq!(export.as_c_str(), expected);
+ }
+ Ok(())
+ }
+}
+
+#[test]
+fn test_opt_list() {
+ let conn_tester = ConnTester::new();
+ assert!(conn_tester.connect(0, &[]).is_err());
+ assert!(conn_tester.connect(1, &[c_str!("a"),
c_str!("b")]).is_ok());
+ assert!(conn_tester.connect(2, &[]).is_ok());
+ assert!(conn_tester.connect(3, &[c_str!("a")]).is_ok());
+}
diff --git a/rust/tests/test_log/mod.rs b/rust/tests/test_log/mod.rs
new file mode 100644
index 0000000..8dbcd79
--- /dev/null
+++ b/rust/tests/test_log/mod.rs
@@ -0,0 +1,86 @@
+// libnbd Rust test case
+// Copyright Tage Johansson
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library 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
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+//! This module provides facilities for capturing log output and asserting that
+//! it does or does not contain certain messages. The primary use of this module
+//! is to assert that certain libnbd operations are or are not performed.
+
+#![allow(unused)]
+
+use std::sync::Mutex;
+
+/// Logger that stores all debug messages in a list.
+pub struct DebugLogger {
+ /// All targets and messages logged. Wrapped in a mutex so that it can be
+ /// updated with an imutable reference to self.
+ entries: Mutex<Vec<(String, String)>>,
+ is_initialized: Mutex<bool>,
+}
+
+impl DebugLogger {
+ const fn new() -> Self {
+ Self {
+ entries: Mutex::new(Vec::new()),
+ is_initialized: Mutex::new(false),
+ }
+ }
+
+ /// Set this logger as the global logger.
+ pub fn init(&'static self) {
+ let mut is_initialized = self.is_initialized.lock().unwrap();
+ if !*is_initialized {
+ log::set_logger(self).unwrap();
+ log::set_max_level(log::LevelFilter::Debug);
+ *is_initialized = true;
+ }
+ }
+
+ /// Check wether a specific message has been logged.
+ pub fn contains(&self, msg: &str) -> bool {
+ self.entries.lock().unwrap().iter().any(|(_, x)| x == msg)
+ }
+
+ /// Print all logged messages, in no particular order.
+ ///
+ /// Only for debug purposes. Remember to run cargo test with the `--
+ /// --nocapture` arguments. That is, from the rust directory run:
+ /// `./../run cargo test -- --nocapture`
+ pub fn print_messages(&self) {
+ for (target, msg) in self.entries.lock().unwrap().iter() {
+ eprintln!("{target}: {msg}");
+ }
+ }
+}
+
+/// A static global `DebugLogger`. Just call `.init()` on this to set it as the
+/// global logger.
+pub static DEBUG_LOGGER: DebugLogger = DebugLogger::new();
+
+impl log::Log for DebugLogger {
+ fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
+ metadata.level() == log::Level::Debug
+ }
+
+ fn log(&self, record: &log::Record<'_>) {
+ self.entries
+ .lock()
+ .unwrap()
+ .push((record.target().to_string(), record.args().to_string()));
+ }
+
+ fn flush(&self) {}
+}
--
2.41.0