From: Elias Rohrer Date: Fri, 4 Aug 2023 08:09:55 +0000 (+0200) Subject: Add `test_utils` X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=0c3d56c575e1a5f82fabc13291bf4e9c59e5171f;p=rust-lightning Add `test_utils` We add a utility function needed by upcoming `KVStore` implementation tests. --- diff --git a/lightning-persister/src/lib.rs b/lightning-persister/src/lib.rs index dc205feba..0a667e421 100644 --- a/lightning-persister/src/lib.rs +++ b/lightning-persister/src/lib.rs @@ -10,6 +10,9 @@ #[cfg(ldk_bench)] extern crate criterion; +#[cfg(test)] +mod test_utils; + mod util; extern crate lightning; diff --git a/lightning-persister/src/test_utils.rs b/lightning-persister/src/test_utils.rs new file mode 100644 index 000000000..daf6f5ee6 --- /dev/null +++ b/lightning-persister/src/test_utils.rs @@ -0,0 +1,30 @@ +use lightning::util::persist::KVStore; + +pub(crate) fn do_read_write_remove_list_persist(kv_store: &K) { + use lightning::util::ser::Readable; + + let data = [42u8; 32]; + + let namespace = "testspace"; + let key = "testkey"; + + // Test the basic KVStore operations. + kv_store.write(namespace, key, &data).unwrap(); + + // Test empty namespace is allowed, but not empty key. + kv_store.write("", key, &data).unwrap(); + assert!(kv_store.write(namespace, "", &data).is_err()); + + let listed_keys = kv_store.list(namespace).unwrap(); + assert_eq!(listed_keys.len(), 1); + assert_eq!(listed_keys[0], key); + + let mut reader = kv_store.read(namespace, key).unwrap(); + let read_data: [u8; 32] = Readable::read(&mut reader).unwrap(); + assert_eq!(data, read_data); + + kv_store.remove(namespace, key).unwrap(); + + let listed_keys = kv_store.list(namespace).unwrap(); + assert_eq!(listed_keys.len(), 0); +}