From: Elias Rohrer Date: Fri, 4 Aug 2023 08:09:55 +0000 (+0200) Subject: Add `test_utils` X-Git-Tag: v0.0.117-alpha1~24^2~6 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=c18f43fb07e7af7c0b8d73f92eef259f3b06f7e0;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..357a8b2ee --- /dev/null +++ b/lightning-persister/src/test_utils.rs @@ -0,0 +1,50 @@ +use lightning::util::persist::{KVStore, KVSTORE_NAMESPACE_KEY_MAX_LEN}; + +use std::panic::RefUnwindSafe; + +pub(crate) fn do_read_write_remove_list_persist(kv_store: &K) { + let data = [42u8; 32]; + + let namespace = "testspace"; + let sub_namespace = "testsubspace"; + let key = "testkey"; + + // Test the basic KVStore operations. + kv_store.write(namespace, sub_namespace, key, &data).unwrap(); + + // Test empty namespace/sub_namespace is allowed, but not empty namespace and non-empty + // sub-namespace, and not empty key. + kv_store.write("", "", key, &data).unwrap(); + let res = std::panic::catch_unwind(|| kv_store.write("", sub_namespace, key, &data)); + assert!(res.is_err()); + let res = std::panic::catch_unwind(|| kv_store.write(namespace, sub_namespace, "", &data)); + assert!(res.is_err()); + + let listed_keys = kv_store.list(namespace, sub_namespace).unwrap(); + assert_eq!(listed_keys.len(), 1); + assert_eq!(listed_keys[0], key); + + let read_data = kv_store.read(namespace, sub_namespace, key).unwrap(); + assert_eq!(data, &*read_data); + + kv_store.remove(namespace, sub_namespace, key, false).unwrap(); + + let listed_keys = kv_store.list(namespace, sub_namespace).unwrap(); + assert_eq!(listed_keys.len(), 0); + + // Ensure we have no issue operating with namespace/sub_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN + let max_chars: String = std::iter::repeat('A').take(KVSTORE_NAMESPACE_KEY_MAX_LEN).collect(); + kv_store.write(&max_chars, &max_chars, &max_chars, &data).unwrap(); + + let listed_keys = kv_store.list(&max_chars, &max_chars).unwrap(); + assert_eq!(listed_keys.len(), 1); + assert_eq!(listed_keys[0], max_chars); + + let read_data = kv_store.read(&max_chars, &max_chars, &max_chars).unwrap(); + assert_eq!(data, &*read_data); + + kv_store.remove(&max_chars, &max_chars, &max_chars, false).unwrap(); + + let listed_keys = kv_store.list(&max_chars, &max_chars).unwrap(); + assert_eq!(listed_keys.len(), 0); +}