From c269f9324f42f57e5b3798dd9544bb8d93716a82 Mon Sep 17 00:00:00 2001 From: Miroslav Kovar Date: Thu, 12 Jan 2023 14:05:35 +0100 Subject: [PATCH] Make workspace tests pass Signed-off-by: Miroslav Kovar --- .github/workflows/main.yml | 2 +- .../src/messages/update_com_method.rs | 1 + .../indy-wallet/src/cache/wallet_cache.rs | 1095 ----- libvdrtools/indy-wallet/src/export_import.rs | 624 --- libvdrtools/indy-wallet/src/lib.rs | 4324 ----------------- .../indy-wallet/src/storage/default/mod.rs | 1042 +--- .../indy-wallet/src/storage/default/query.rs | 34 - .../indy-wallet/src/storage/mysql/query.rs | 36 - libvdrtools/indy-wallet/src/wallet.rs | 3511 ------------- shared_vcx/src/validation/did.rs | 1 + shared_vcx/src/validation/verkey.rs | 1 + 11 files changed, 5 insertions(+), 10666 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e9ebc301aa..dd90234642 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -340,7 +340,7 @@ jobs: with: skip-docker-setup: true - name: "Run workspace tests: general_test" - run: RUST_TEST_THREADS=1 cargo test --workspace --exclude aries-vcx-agent -F "general_test" + run: RUST_TEST_THREADS=1 cargo test --workspace --exclude aries-vcx-agent --exclude libvdrtools -F general_test test-integration-aries-vcx: needs: workflow-setup diff --git a/agency_client/src/messages/update_com_method.rs b/agency_client/src/messages/update_com_method.rs index 18c662c749..cbc228e2f7 100644 --- a/agency_client/src/messages/update_com_method.rs +++ b/agency_client/src/messages/update_com_method.rs @@ -64,6 +64,7 @@ impl UpdateComMethod { #[cfg(test)] mod tests { + use super::*; #[test] #[cfg(feature = "general_test")] diff --git a/libvdrtools/indy-wallet/src/cache/wallet_cache.rs b/libvdrtools/indy-wallet/src/cache/wallet_cache.rs index b2ef5b1b0f..b29c23747c 100644 --- a/libvdrtools/indy-wallet/src/cache/wallet_cache.rs +++ b/libvdrtools/indy-wallet/src/cache/wallet_cache.rs @@ -329,1098 +329,3 @@ impl WalletCacheHitMetrics { self.data.read().unwrap().clone() } } - -#[cfg(test)] -mod tests { - extern crate rand; - - use super::*; - use crate::storage::{Tag, TagName}; - use futures::Future; - use indy_api_types::domain::wallet::DEFAULT_CACHE_SIZE; - use rand::{distributions::Alphanumeric, distributions::Uniform, Rng}; - use std::time::Duration; - - const TYPE_A: &str = "TypeA"; - const TYPE_B: &str = "TypeB"; - const TYPE_NON_CACHED: &str = "TypeNonCached"; - - const ETYPE1: &[u8] = &[1, 2, 3, 1]; - const EID1: &[u8] = &[2, 3, 4, 1]; - const EID2: &[u8] = &[2, 3, 4, 2]; - - const FULL_OPTIONS: RecordOptions = RecordOptions { - retrieve_type: true, - retrieve_value: true, - retrieve_tags: true, - }; - - fn _rand_vec(size: usize) -> Vec { - rand::thread_rng() - .sample_iter(&Uniform::new(0, 255)) - .take(size) - .collect() - } - - fn _rand_str(size: usize) -> String { - rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(size) - .map(char::from) - .collect() - } - - fn _enc_value() -> EncryptedValue { - EncryptedValue { - data: _rand_vec(200), - key: _rand_vec(20), - } - } - - fn _enc_tag() -> Tag { - if rand::thread_rng().gen::() % 2 == 1 { - Tag::Encrypted(_rand_vec(20), _rand_vec(100)) - } else { - Tag::PlainText(_rand_vec(20), _rand_str(100)) - } - } - - fn _cache() -> WalletCache { - let config = CacheConfig { - size: 10, - entities: vec![TYPE_A.to_string(), TYPE_B.to_string()], - algorithm: CachingAlgorithm::LRU, - }; - WalletCache::new(Some(config)) - } - - fn _no_cache() -> WalletCache { - let config = CacheConfig { - size: 10, - entities: vec![], - algorithm: CachingAlgorithm::LRU, - }; - WalletCache::new(Some(config)) - } - - fn _vec_to_hash_set(items: &[&str]) -> HashSet { - HashSet::from_iter(items.into_iter().map(|el| el.to_string())) - } - - fn _tag_names(tags: &[Tag]) -> Vec { - tags.into_iter() - .map(|el| match el { - Encrypted(key, _) => TagName::OfEncrypted(key.to_owned()), - PlainText(key, _) => TagName::OfPlain(key.to_owned()), - }) - .collect() - } - - #[test] - fn new_with_no_config_works() { - let cache = WalletCache::new(None); - assert!(cache.cache.is_none()); - assert_eq!(cache.cache_entities.len(), 0); - } - - #[test] - fn new_with_default_config_works() { - let config = CacheConfig { - size: DEFAULT_CACHE_SIZE, - entities: vec![], - algorithm: CachingAlgorithm::LRU, - }; - let cache = WalletCache::new(Some(config)); - assert!(cache.cache.is_none()); - assert_eq!(cache.cache_entities.len(), 0); - } - - #[test] - fn new_with_size_but_no_entities_in_config_works() { - let config = CacheConfig { - size: 20, - entities: vec![], - algorithm: CachingAlgorithm::LRU, - }; - let cache = WalletCache::new(Some(config)); - assert!(cache.cache.is_none()); - assert_eq!(cache.cache_entities.len(), 0); - } - - #[test] - fn new_with_default_size_in_config_works() { - let config_str = - json!({ "entities": vec![TYPE_A.to_string(), TYPE_B.to_string()] }).to_string(); - let config: CacheConfig = serde_json::from_str(&config_str).unwrap(); - let wallet_cache = WalletCache::new(Some(config)); - assert!(wallet_cache.cache.is_some()); - let mut cache = wallet_cache.cache.unwrap(); - assert_eq!(cache.get_mut().cap(), DEFAULT_CACHE_SIZE); - assert_eq!(cache.get_mut().len(), 0); - assert_eq!(wallet_cache.cache_entities.len(), 2); - assert_eq!( - wallet_cache.cache_entities, - _vec_to_hash_set(&[TYPE_A, TYPE_B]) - ); - } - - #[test] - fn new_with_size_in_config_works() { - let config = CacheConfig { - size: 20, - entities: vec![TYPE_A.to_string(), TYPE_B.to_string()], - algorithm: CachingAlgorithm::LRU, - }; - let wallet_cache = WalletCache::new(Some(config)); - assert!(wallet_cache.cache.is_some()); - let mut cache = wallet_cache.cache.unwrap(); - assert_eq!(cache.get_mut().cap(), 20); - assert_eq!(cache.get_mut().len(), 0); - assert_eq!(wallet_cache.cache_entities.len(), 2); - assert_eq!( - wallet_cache.cache_entities, - _vec_to_hash_set(&[TYPE_A, TYPE_B]) - ); - } - - #[test] - fn is_type_cacheable_works() { - let cache = _cache(); - let result = cache.is_type_cacheable(TYPE_A); - assert_eq!(result, true); - } - - #[test] - fn is_type_cacheable_for_noncacheable_type_works() { - let cache = _cache(); - let result = cache.is_type_cacheable(TYPE_NON_CACHED); - assert_eq!(result, false); - } - - #[test] - fn is_type_cacheable_for_no_cache_enabled_works() { - let cache = _no_cache(); - let result = cache.is_type_cacheable(TYPE_A); - assert_eq!(result, false); - } - - #[async_std::test] - async fn add_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - } - - #[async_std::test] - async fn add_without_tags_works() { - let value = _enc_value(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![]); - } - - #[async_std::test] - async fn add_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn add_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn add_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.add_tags(TYPE_A, ETYPE1, EID1, &[tag3.clone()]).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2, tag3]); - } - - #[async_std::test] - async fn add_tags_on_item_without_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - cache - .add_tags(TYPE_A, ETYPE1, EID1, &[tag1.clone(), tag2.clone()]) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - } - - #[async_std::test] - async fn add_tags_on_non_cached_item_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.add_tags(TYPE_A, ETYPE1, EID2, &[tag3]).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - - let key2 = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID2.to_vec(), - }; - - assert!(lru.peek(&key2).is_none()); - } - - #[async_std::test] - async fn add_tags_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.add_tags(TYPE_NON_CACHED, ETYPE1, EID1, &[tag3]).await; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn add_tags_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.add_tags(TYPE_A, ETYPE1, EID1, &[tag3]).await; - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn update_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache - .update_tags(TYPE_A, ETYPE1, EID1, &[tag3.clone()]) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag3]); - } - - #[async_std::test] - async fn update_tags_on_item_without_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - cache - .update_tags(TYPE_A, ETYPE1, EID1, &[tag1.clone()]) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1]); - } - - #[async_std::test] - async fn update_tags_on_non_cached_item_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - let tag4 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.update_tags(TYPE_A, ETYPE1, EID2, &[tag3, tag4]).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - - let key2 = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID2.to_vec(), - }; - - assert!(lru.peek(&key2).is_none()); - } - - #[async_std::test] - async fn update_tags_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - cache - .update_tags(TYPE_NON_CACHED, ETYPE1, EID1, &[tag3]) - .await; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn update_tags_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.update_tags(TYPE_A, ETYPE1, EID1, &[tag3]).await; - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn delete_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - let tag3 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache - .delete_tags(TYPE_A, ETYPE1, EID1, &_tag_names(&[tag1, tag3])) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag2]); - } - - #[async_std::test] - async fn delete_tags_on_item_without_tags_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - cache - .delete_tags(TYPE_A, ETYPE1, EID1, &_tag_names(&[tag1])) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![]); - } - - #[async_std::test] - async fn delete_tags_on_non_cached_item_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache - .delete_tags(TYPE_A, ETYPE1, EID2, &_tag_names(&[tag1.clone()])) - .await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - - let key2 = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID2.to_vec(), - }; - - assert!(lru.peek(&key2).is_none()); - } - - #[async_std::test] - async fn delete_tags_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add( - TYPE_NON_CACHED, - ETYPE1, - EID1, - &value, - &[tag1.clone(), tag2.clone()], - ); - cache - .delete_tags(TYPE_NON_CACHED, ETYPE1, EID1, &_tag_names(&[tag1.clone()])) - .await; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn delete_tags_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache - .delete_tags(TYPE_A, ETYPE1, EID1, &_tag_names(&[tag1])) - .await; - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn update_works() { - let value = _enc_value(); - let value2 = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.update(TYPE_A, ETYPE1, EID1, &value2).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value2); - assert_eq!(cached.tags, vec![tag1, tag2]); - } - - #[async_std::test] - async fn update_on_item_without_tags_works() { - let value = _enc_value(); - let value2 = _enc_value(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - cache.update(TYPE_A, ETYPE1, EID1, &value2).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value2); - assert_eq!(cached.tags, vec![]); - } - - #[async_std::test] - async fn update_on_non_cached_item_works() { - let value = _enc_value(); - let value2 = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.update(TYPE_A, ETYPE1, EID2, &value2).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - - let key2 = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID2.to_vec(), - }; - - assert!(lru.peek(&key2).is_none()); - } - - #[async_std::test] - async fn update_for_non_cacheable_type_works() { - let value = _enc_value(); - let value2 = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.update(TYPE_NON_CACHED, ETYPE1, EID1, &value2).await; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn update_for_no_cache_enabled_works() { - let value = _enc_value(); - let value2 = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.update(TYPE_A, ETYPE1, EID1, &value2).await; - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn delete_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.delete(TYPE_A, ETYPE1, EID1).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - assert!(lru.peek(&key).is_none()); - } - - #[async_std::test] - async fn delete_on_item_without_tags_works() { - let value = _enc_value(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - cache.delete(TYPE_A, ETYPE1, EID1).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - assert!(lru.peek(&key).is_none()); - } - - #[async_std::test] - async fn delete_on_non_cached_item_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - cache.delete(TYPE_A, ETYPE1, EID2).await; - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - - let key2 = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID2.to_vec(), - }; - - assert!(lru.peek(&key2).is_none()); - } - - #[async_std::test] - async fn delete_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.delete(TYPE_NON_CACHED, ETYPE1, EID1).await; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn delete_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - cache.delete(TYPE_A, ETYPE1, EID1).await; - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn get_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - let result = cache - .get(TYPE_A, ETYPE1, EID1, &FULL_OPTIONS) - .await - .unwrap(); - - assert_eq!(result.id, EID1); - assert_eq!(result.type_, Some(ETYPE1.to_owned())); - assert_eq!(result.value, Some(value.clone())); - assert_eq!(result.tags, Some(vec![tag1.clone(), tag2.clone()])); - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - } - - #[async_std::test] - async fn get_for_item_without_tags_works() { - let value = _enc_value(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[]); - let result = cache - .get(TYPE_A, ETYPE1, EID1, &FULL_OPTIONS) - .await - .unwrap(); - - assert_eq!(result.id, EID1); - assert_eq!(result.type_, Some(ETYPE1.to_owned())); - assert_eq!(result.value, Some(value.clone())); - assert_eq!(result.tags, Some(vec![])); - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![]); - } - - #[async_std::test] - async fn get_for_non_cached_item_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1.clone(), tag2.clone()]); - let result = cache.get(TYPE_A, ETYPE1, EID2, &FULL_OPTIONS).await; - - assert!(result.is_none()); - - let key = WalletCacheKey { - type_: ETYPE1.to_vec(), - id: EID1.to_vec(), - }; - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 1); - let cached = lru.peek(&key).unwrap(); - assert_eq!(cached.value, value); - assert_eq!(cached.tags, vec![tag1, tag2]); - } - - #[async_std::test] - async fn get_for_non_cacheable_type_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _cache(); - - cache.add(TYPE_NON_CACHED, ETYPE1, EID1, &value, &[tag1, tag2]); - let result = cache.get(TYPE_A, ETYPE1, EID1, &FULL_OPTIONS).await; - - assert!(result.is_none()); - - let mut internal_cache = cache.cache.unwrap(); - let lru = internal_cache.get_mut(); - assert_eq!(lru.len(), 0); - } - - #[async_std::test] - async fn get_for_no_cache_enabled_works() { - let value = _enc_value(); - let tag1 = _enc_tag(); - let tag2 = _enc_tag(); - - let cache = _no_cache(); - - cache.add(TYPE_A, ETYPE1, EID1, &value, &[tag1, tag2]); - let result = cache.get(TYPE_A, ETYPE1, EID1, &FULL_OPTIONS).await; - - assert!(result.is_none()); - - assert!(cache.cache.is_none()); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_new_works() { - let mut metrics = WalletCacheHitMetrics::new(); - - assert!(metrics.data.get_mut().is_empty()); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_inc_cache_hit_works() { - let metrics = WalletCacheHitMetrics::new(); - - metrics.inc_cache_hit(TYPE_A).await; - - let type_data = metrics.get_data_for_type(TYPE_A).await.unwrap(); - assert_eq!(type_data.get_hit(), 1); - assert_eq!(type_data.get_miss(), 0); - assert_eq!(type_data.get_not_cached(), 0); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_inc_cache_miss_works() { - let metrics = WalletCacheHitMetrics::new(); - - metrics.inc_cache_miss(TYPE_A).await; - - let type_data = metrics.get_data_for_type(TYPE_A).await.unwrap(); - assert_eq!(type_data.get_hit(), 0); - assert_eq!(type_data.get_miss(), 1); - assert_eq!(type_data.get_not_cached(), 0); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_inc_not_cached_works() { - let metrics = WalletCacheHitMetrics::new(); - - metrics.inc_not_cached(TYPE_A).await; - - let type_data = metrics.get_data_for_type(TYPE_A).await.unwrap(); - assert_eq!(type_data.get_hit(), 0); - assert_eq!(type_data.get_miss(), 0); - assert_eq!(type_data.get_not_cached(), 1); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_get_data_works() { - let metrics = WalletCacheHitMetrics::new(); - - let fut1 = metrics.inc_cache_hit(TYPE_A); - let fut2 = metrics.inc_cache_miss(TYPE_A); - let fut3 = metrics.inc_cache_miss(TYPE_B); - let fut4 = metrics.inc_not_cached(TYPE_NON_CACHED); - - let result = futures::future::join4(fut1, fut2, fut3, fut4).await; - assert_eq!(result, (0, 0, 0, 0)); - - let data = metrics.get_data(); - - assert_eq!(data.len(), 3); - assert_eq!(data.get(TYPE_A).unwrap().get_hit(), 1); - assert_eq!(data.get(TYPE_A).unwrap().get_miss(), 1); - assert_eq!(data.get(TYPE_A).unwrap().get_not_cached(), 0); - assert_eq!(data.get(TYPE_B).unwrap().get_hit(), 0); - assert_eq!(data.get(TYPE_B).unwrap().get_miss(), 1); - assert_eq!(data.get(TYPE_B).unwrap().get_not_cached(), 0); - assert_eq!(data.get(TYPE_NON_CACHED).unwrap().get_hit(), 0); - assert_eq!(data.get(TYPE_NON_CACHED).unwrap().get_miss(), 0); - assert_eq!(data.get(TYPE_NON_CACHED).unwrap().get_not_cached(), 1); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_get_data_for_type_works() { - let metrics = WalletCacheHitMetrics::new(); - - let fut1 = metrics.inc_cache_hit(TYPE_A); - let fut2 = metrics.inc_cache_miss(TYPE_A); - let fut3 = metrics.inc_cache_miss(TYPE_B); - let fut4 = metrics.inc_not_cached(TYPE_NON_CACHED); - - let result = futures::future::join4(fut1, fut2, fut3, fut4).await; - assert_eq!(result, (0, 0, 0, 0)); - - let data_a = metrics.get_data_for_type(TYPE_A).await.unwrap(); - let data_b = metrics.get_data_for_type(TYPE_B).await.unwrap(); - let data_nc = metrics.get_data_for_type(TYPE_NON_CACHED).await.unwrap(); - - assert_eq!(data_a.get_hit(), 1); - assert_eq!(data_a.get_miss(), 1); - assert_eq!(data_a.get_not_cached(), 0); - assert_eq!(data_b.get_hit(), 0); - assert_eq!(data_b.get_miss(), 1); - assert_eq!(data_b.get_not_cached(), 0); - assert_eq!(data_nc.get_hit(), 0); - assert_eq!(data_nc.get_miss(), 0); - assert_eq!(data_nc.get_not_cached(), 1); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_get_data_works_with_empty() { - let metrics = WalletCacheHitMetrics::new(); - - assert!(metrics.get_data().is_empty()); - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_get_data_for_type_works_with_empty() { - let metrics = WalletCacheHitMetrics::new(); - - assert!(metrics.get_data_for_type(TYPE_A).await.is_none()); - } - - async fn _execute_with_random_delay(future: F) -> usize - where - F: Future, - { - async_std::task::sleep(Duration::from_millis(rand::thread_rng().gen_range(0, 1000))).await; - future.await + 0 - } - - #[async_std::test] - async fn wallet_cache_hit_metrics_work_correctly_under_concurrent_load() { - let metrics = WalletCacheHitMetrics::new(); - let mut futures1 = vec![]; - let mut futures2 = vec![]; - let mut futures3 = vec![]; - - for _ in 0..1000 { - futures1.push(_execute_with_random_delay(metrics.inc_cache_hit(TYPE_A))); - futures2.push(_execute_with_random_delay(metrics.inc_cache_miss(TYPE_A))); - futures3.push(_execute_with_random_delay( - metrics.inc_not_cached(TYPE_NON_CACHED), - )); - } - - let result = futures::future::join3( - futures::future::join_all(futures1), - futures::future::join_all(futures2), - futures::future::join_all(futures3), - ) - .await; - println!("result: {:?}", result); - - let type_a_data = metrics.get_data_for_type(TYPE_A).await.unwrap(); - assert!(metrics.get_data_for_type(TYPE_B).await.is_none()); - let type_b_data = metrics.get_data_for_type(TYPE_NON_CACHED).await.unwrap(); - - assert_eq!(type_a_data.get_hit(), 1000); - assert_eq!(type_a_data.get_miss(), 1000); - assert_eq!(type_a_data.get_not_cached(), 0); - assert_eq!(type_b_data.get_hit(), 0); - assert_eq!(type_b_data.get_miss(), 0); - assert_eq!(type_b_data.get_not_cached(), 1000); - } -} diff --git a/libvdrtools/indy-wallet/src/export_import.rs b/libvdrtools/indy-wallet/src/export_import.rs index e6b9302295..eeafe1f701 100644 --- a/libvdrtools/indy-wallet/src/export_import.rs +++ b/libvdrtools/indy-wallet/src/export_import.rs @@ -336,627 +336,3 @@ fn _map_io_err(e: io::Error) -> IndyError { e => e.to_indy(IndyErrorKind::IOError, "Can't read export file"), } } - -#[cfg(test)] -mod tests { - use std::collections::HashMap; - use std::sync::Arc; - - use serde_json; - - use crate::encryption; - use crate::storage::default::SQLiteStorageType; - use crate::storage::WalletStorageType; - use crate::wallet::{Keys, Wallet}; - use crate::{Metadata, MetadataArgon}; - use indy_utils::crypto::pwhash_argon2i13; - use indy_utils::test; - - use super::*; - use crate::cache::wallet_cache::{WalletCache, WalletCacheHitMetrics}; - - async fn export( - wallet: Wallet, - writer: &mut (dyn Write + Send + Sync), - passphrase: &str, - version: u32, - key_derivation_method: &KeyDerivationMethod, - ) -> IndyResult<()> { - if version != 0 { - Err(err_msg(IndyErrorKind::InvalidState, "Unsupported version"))?; - } - - let key_data = - KeyDerivationData::from_passphrase_with_new_salt(passphrase, key_derivation_method); - let key = key_data.calc_master_key()?; - - export_continue(Arc::new(wallet), writer, version, key, &key_data).await - } - - #[async_std::test] - async fn export_import_works_for_empty_wallet() { - _cleanup("export_import_works_for_empty_wallet1"); - _cleanup("export_import_works_for_empty_wallet2"); - - { - let mut output: Vec = Vec::new(); - { - let wallet1 = _wallet("export_import_works_for_empty_wallet1").await; - - export( - wallet1, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - } - - test::cleanup_wallet("export_import_works_for_empty_wallet1"); - - let wallet = _wallet("export_import_works_for_empty_wallet2").await; - _assert_is_empty(&wallet).await; - - import(&wallet, &mut output.as_slice(), _passphrase()) - .await - .unwrap(); - - _assert_is_empty(&wallet).await; - - std::mem::drop(wallet); - } - - test::cleanup_wallet("export_import_works_for_empty_wallet2"); - } - - #[async_std::test] - async fn export_import_works_for_2_items() { - _cleanup("export_import_works_for_2_items1"); - _cleanup("export_import_works_for_2_items2"); - - { - let mut output: Vec = Vec::new(); - - export( - _add_2_records(_wallet("export_import_works_for_2_items1").await).await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - let wallet = _wallet("export_import_works_for_2_items2").await; - _assert_is_empty(&wallet).await; - - import(&wallet, &mut output.as_slice(), _passphrase()) - .await - .unwrap(); - - _assert_has_2_records(&wallet).await; - } - _cleanup("export_import_works_for_2_items1"); - _cleanup("export_import_works_for_2_items2"); - } - - #[async_std::test] - async fn export_import_works_for_2_items_and_interactive_method() { - _cleanup("export_import_works_for_2_items_and_interactive_method1"); - _cleanup("export_import_works_for_2_items_and_interactive_method2"); - - { - let mut output: Vec = Vec::new(); - { - export( - _add_2_records( - _wallet("export_import_works_for_2_items_and_interactive_method1").await, - ) - .await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_INT, - ) - .await - .unwrap(); - } - - _cleanup("export_import_works_for_2_items_and_interactive_method1"); - - let wallet = _wallet("export_import_works_for_2_items_and_interactive_method2").await; - _assert_is_empty(&wallet).await; - - import(&wallet, &mut output.as_slice(), _passphrase()) - .await - .unwrap(); - - _assert_has_2_records(&wallet).await; - } - - _cleanup("export_import_works_for_2_items_and_interactive_method2"); - } - - #[async_std::test] - async fn export_import_works_for_multiple_items() { - _cleanup("export_import_works_for_multiple_items1"); - _cleanup("export_import_works_for_multiple_items2"); - - { - let mut output: Vec = Vec::new(); - - export( - _add_300_records(_wallet("export_import_works_for_multiple_items1").await).await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - let wallet = _wallet("export_import_works_for_multiple_items2").await; - _assert_is_empty(&wallet).await; - - import(&wallet, &mut output.as_slice(), _passphrase()) - .await - .unwrap(); - - _assert_has_300_records(&wallet).await; - } - - _cleanup("export_import_works_for_multiple_items1"); - _cleanup("export_import_works_for_multiple_items2"); - } - - #[async_std::test] - async fn import_works_for_empty() { - _cleanup("import_works_for_empty"); - - let res = import( - &_wallet("import_works_for_empty").await, - &mut "".as_bytes(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_empty"); - } - - #[async_std::test] - async fn import_works_for_cut_header_length() { - _cleanup("import_works_for_cut_header_length"); - - let res = import( - &_wallet("import_works_for_cut_header_length").await, - &mut "\x00".as_bytes(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_cut_header_length"); - } - - #[async_std::test] - async fn import_works_for_cut_header_body() { - _cleanup("import_works_for_cut_header_body"); - - let res = import( - &_wallet("import_works_for_cut_header_body").await, - &mut "\x00\x20small".as_bytes(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_cut_header_body"); - } - - #[async_std::test] - async fn import_works_for_invalid_header_body() { - _cleanup("import_works_for_invalid_header_body"); - - let output = { - let invalid_header = "invalid_header".as_bytes(); - let mut output: Vec = Vec::new(); - output - .write_u32::(invalid_header.len() as u32) - .unwrap(); - output.write_all(invalid_header).unwrap(); - output.write_all(&hash(invalid_header).unwrap()).unwrap(); - output - }; - - let res = import( - &_wallet("import_works_for_invalid_header_body").await, - &mut output.as_slice(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_invalid_header_body"); - } - - #[async_std::test] - async fn import_works_for_invalid_header_hash() { - _cleanup("import_works_for_invalid_header_hash1"); - _cleanup("import_works_for_invalid_header_hash2"); - - let mut output: Vec = Vec::new(); - export( - _wallet("import_works_for_invalid_header_hash1").await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - // Modifying one of the bytes in the header hash - let pos = (&mut output.as_slice()).read_u32::().unwrap() as usize + 2; - _change_byte(&mut output, pos); - - let res = import( - &mut _wallet("import_works_for_invalid_header_hash2").await, - &mut output.as_slice(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_invalid_header_hash1"); - _cleanup("import_works_for_invalid_header_hash2"); - } - - #[async_std::test] - async fn export_import_works_for_changed_record() { - _cleanup("export_import_works_for_changed_record1"); - _cleanup("export_import_works_for_changed_record2"); - - let mut output: Vec = Vec::new(); - - export( - _add_300_records(_wallet("export_import_works_for_changed_record1").await).await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - // Modifying one byte in the middle of encrypted part - let pos = output.len() / 2; - _change_byte(&mut output, pos); - - let res = import( - &mut _wallet("export_import_works_for_changed_record2").await, - &mut output.as_slice(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - - _cleanup("export_import_works_for_changed_record1"); - _cleanup("export_import_works_for_changed_record2"); - } - - #[async_std::test] - async fn import_works_for_data_cut() { - _cleanup("import_works_for_data_cut1"); - _cleanup("import_works_for_data_cut2"); - - let mut output: Vec = Vec::new(); - - export( - _add_2_records(_wallet("import_works_for_data_cut1").await).await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - output.pop().unwrap(); - - let res = import( - &mut _wallet("import_works_for_data_cut2").await, - &mut output.as_slice(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_data_cut1"); - _cleanup("import_works_for_data_cut2"); - } - - #[async_std::test] - async fn import_works_for_data_extended() { - _cleanup("import_works_for_data_extended1"); - _cleanup("import_works_for_data_extended2"); - - let mut output: Vec = Vec::new(); - - export( - _add_2_records(_wallet("import_works_for_data_extended1").await).await, - &mut output, - _passphrase(), - _version1(), - &KeyDerivationMethod::ARGON2I_MOD, - ) - .await - .unwrap(); - - output.push(10); - - let res = import( - &mut _wallet("import_works_for_data_extended2").await, - &mut output.as_slice(), - _passphrase(), - ) - .await; - - assert_eq!(IndyErrorKind::InvalidStructure, res.unwrap_err().kind()); - _cleanup("import_works_for_data_extended1"); - _cleanup("import_works_for_data_extended2"); - } - - fn _cleanup(name: &str) { - test::cleanup_storage(name) - } - - async fn _cleanup_wallet(wallet: Wallet, name: &str) { - std::mem::drop(wallet); - test::cleanup_wallet(name); - } - - async fn _wallet(id: &str) -> Wallet { - let storage_type = SQLiteStorageType::new(); - let master_key = _master_key(); - let keys = Keys::new(); - - let metadata = { - let master_key_salt = encryption::gen_master_key_salt().unwrap(); - - let metadata = Metadata::MetadataArgon(MetadataArgon { - master_key_salt: master_key_salt[..].to_vec(), - keys: keys.serialize_encrypted(&master_key).unwrap(), - }); - - serde_json::to_vec(&metadata) - .to_indy( - IndyErrorKind::InvalidState, - "Cannot serialize wallet metadata", - ) - .unwrap() - }; - - storage_type - .create_storage(id, None, None, &metadata) - .await - .unwrap(); - - let storage = storage_type.open_storage(id, None, None).await.unwrap(); - - Wallet::new( - id.to_string(), - storage, - Arc::new(keys), - WalletCache::new(None), - ) - } - - async fn _assert_is_empty(wallet: &Wallet) { - assert!(wallet - .get_all() - .await - .unwrap() - .next() - .await - .unwrap() - .is_none()); - } - - async fn _add_2_records(wallet: Wallet) -> Wallet { - wallet - .add(&_type1(), &_id1(), &_value1(), &_tags1()) - .await - .unwrap(); - wallet - .add(&_type2(), &_id2(), &_value2(), &_tags2()) - .await - .unwrap(); - wallet - } - - async fn _assert_has_2_records(wallet: &Wallet) { - let metrics = WalletCacheHitMetrics::new(); - - let record = wallet - .get(&_type1(), &_id1(), _options(), &metrics) - .await - .unwrap(); - assert_eq!(record.type_.unwrap(), _type1()); - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.tags.unwrap(), _tags1()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - let record = wallet - .get(&_type2(), &_id2(), _options(), &metrics) - .await - .unwrap(); - assert_eq!(record.type_.unwrap(), _type2()); - assert_eq!(record.id, _id2()); - assert_eq!(record.value.unwrap(), _value2()); - assert_eq!(record.tags.unwrap(), _tags2()); - assert_eq!( - metrics - .get_data_for_type(&_type2()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - } - - async fn _add_300_records(wallet: Wallet) -> Wallet { - for i in 0..300 { - wallet - .add(&_type(i % 3), &_id(i), &_value(i), &_tags(i)) - .await - .unwrap(); - } - - wallet - } - - async fn _assert_has_300_records(wallet: &Wallet) { - let metrics = WalletCacheHitMetrics::new(); - - for i in 0..300 { - let record = wallet - .get(&_type(i % 3), &_id(i), _options(), &metrics) - .await - .unwrap(); - - assert_eq!(record.type_.unwrap(), _type(i % 3)); - assert_eq!(record.id, _id(i)); - assert_eq!(record.value.unwrap(), _value(i)); - assert_eq!(record.tags.unwrap(), _tags(i)); - } - assert_eq!( - metrics - .get_data_for_type(&_type(0)) - .await - .unwrap() - .get_not_cached(), - 100 - ); - assert_eq!( - metrics - .get_data_for_type(&_type(1)) - .await - .unwrap() - .get_not_cached(), - 100 - ); - assert_eq!( - metrics - .get_data_for_type(&_type(2)) - .await - .unwrap() - .get_not_cached(), - 100 - ); - } - - fn _master_key() -> chacha20poly1305_ietf::Key { - chacha20poly1305_ietf::gen_key() - } - - fn _nonce() -> chacha20poly1305_ietf::Nonce { - chacha20poly1305_ietf::gen_nonce() - } - - fn _change_byte(data: &mut [u8], pos: usize) { - let value = data[pos]; - data[pos] = if value < 255 { value + 1 } else { 0 }; - } - - fn _options() -> &'static str { - r##"{"retrieveType": true, "retrieveValue": true, "retrieveTags": true}"## - } - - fn _salt() -> pwhash_argon2i13::Salt { - pwhash_argon2i13::Salt::new([ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, - 5, 6, 7, - ]) - } - - fn _version1() -> u32 { - 0 - } - - fn _id(suffix: usize) -> String { - format!("id_{}", suffix) - } - - fn _id1() -> String { - _id(1) - } - - fn _id2() -> String { - _id(2) - } - - fn _value(suffix: usize) -> String { - format!("id_{}", suffix) - } - - fn _value1() -> String { - _value(1) - } - - fn _value2() -> String { - _value(2) - } - - fn _type(suffix: usize) -> String { - format!("type_{}", suffix) - } - - fn _type1() -> String { - _type(1) - } - - fn _type2() -> String { - _type(2) - } - - fn _tags(suffix: usize) -> HashMap { - let mut tags = HashMap::new(); - tags.insert( - format!("tag_id_{}_1", suffix), - format!("tag_value_{}_1", suffix), - ); - tags.insert( - format!("tag_id_{}_2", suffix), - format!("tag_value_{}_2", suffix), - ); - tags.insert( - format!("~tag_id_{}_3", suffix), - format!("tag_value_{}_3", suffix), - ); - tags - } - - fn _tags1() -> HashMap { - _tags(1) - } - - fn _tags2() -> HashMap { - _tags(2) - } - - fn _passphrase() -> &'static str { - "key" - } -} diff --git a/libvdrtools/indy-wallet/src/lib.rs b/libvdrtools/indy-wallet/src/lib.rs index f449b41af7..705c427b8d 100644 --- a/libvdrtools/indy-wallet/src/lib.rs +++ b/libvdrtools/indy-wallet/src/lib.rs @@ -1243,4327 +1243,3 @@ fn short_type_name() -> &'static str { let type_name = std::any::type_name::(); type_name.rsplit("::").next().unwrap_or(type_name) } - -#[cfg(test)] -mod tests { - use std::{collections::HashMap, fs, path::Path}; - - use indy_api_types::{ - domain::wallet::{CachingAlgorithm, KeyDerivationMethod}, - INVALID_WALLET_HANDLE, - }; - use indy_utils::{ - assert_kind, assert_match, environment, inmem_wallet::InmemWallet, next_wallet_handle, test, - }; - use serde_json::json; - - use lazy_static::lazy_static; - - use super::*; - use indy_api_types::domain::wallet::CacheConfig; - - impl WalletService { - async fn open_wallet( - &self, - config: &Config, - credentials: &Credentials, - ) -> IndyResult { - self._is_id_from_config_not_used(config)?; - - let (storage, metadata, key_derivation_data) = self - ._open_storage_and_fetch_metadata(config, credentials) - .await?; - - let wallet_handle = next_wallet_handle(); - - let rekey_data: Option = - credentials.rekey.as_ref().map(|ref rekey| { - KeyDerivationData::from_passphrase_with_new_salt( - rekey, - &credentials.rekey_derivation_method, - ) - }); - - self.pending_for_open.lock().await.insert( - wallet_handle, - ( - WalletService::_get_wallet_id(config), - storage, - metadata, - rekey_data.clone(), - ), - ); - - let key = key_derivation_data.calc_master_key()?; - - let rekey = match rekey_data { - Some(rekey_data) => { - let rekey_result = rekey_data.calc_master_key()?; - Some(rekey_result) - } - None => None, - }; - - self.open_wallet_continue(wallet_handle, (&key, rekey.as_ref()), config.cache.clone()) - .await - } - - pub async fn import_wallet( - &self, - config: &Config, - credentials: &Credentials, - export_config: &ExportConfig, - ) -> IndyResult<()> { - trace!( - "import_wallet_prepare >>> config: {:?}, credentials: {:?}, export_config: {:?}", - config, - secret!(export_config), - secret!(export_config) - ); - - let exported_file_to_import = fs::OpenOptions::new() - .read(true) - .open(&export_config.path)?; - - let (reader, import_key_derivation_data, nonce, chunk_size, header_bytes) = - preparse_file_to_import(exported_file_to_import, &export_config.key)?; - let key_data = KeyDerivationData::from_passphrase_with_new_salt( - &credentials.key, - &credentials.key_derivation_method, - ); - - let wallet_handle = next_wallet_handle(); - - let import_key = import_key_derivation_data.calc_master_key()?; - let master_key = key_data.calc_master_key()?; - - self.pending_for_import.lock().await.insert( - wallet_handle, - (reader, nonce, chunk_size, header_bytes, key_data), - ); - - self.import_wallet_continue( - wallet_handle, - config, - credentials, - (import_key, master_key), - ) - .await - } - - pub async fn delete_wallet( - &self, - config: &Config, - credentials: &Credentials, - ) -> IndyResult<()> { - if self - .wallets - .lock() - .await - .values() - .any(|ref wallet| wallet.get_id() == WalletService::_get_wallet_id(config)) - { - return Err(err_msg( - IndyErrorKind::InvalidState, - format!( - "Wallet has to be closed before deleting: {:?}", - WalletService::_get_wallet_id(config) - ), - ))?; - } - - let (_, metadata, key_derivation_data) = self - ._open_storage_and_fetch_metadata(config, credentials) - .await?; - - let master_key = key_derivation_data.calc_master_key()?; - - self.delete_wallet_continue(config, credentials, &metadata, &master_key) - .await - } - } - - #[test] - fn wallet_service_new_works() { - WalletService::new(); - } - - #[test] - fn wallet_service_register_type_works() { - _cleanup("wallet_service_register_type_works"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - _cleanup("wallet_service_register_type_works"); - } - - #[async_std::test] - async fn wallet_service_create_wallet_works() { - test::cleanup_wallet("wallet_service_create_wallet_works"); - - { - WalletService::new() - .create_wallet( - &_config_default("wallet_service_create_wallet_works"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_create_wallet_works"); - } - - #[async_std::test] - async fn wallet_service_create_wallet_works_for_interactive_key_derivation() { - test::cleanup_wallet("wallet_service_create_wallet_works_for_interactive_key_derivation"); - - { - WalletService::new() - .create_wallet( - &_config_default( - "wallet_service_create_wallet_works_for_interactive_key_derivation", - ), - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_create_wallet_works_for_interactive_key_derivation"); - } - - #[async_std::test] - async fn wallet_service_create_wallet_works_for_moderate_key_derivation() { - test::cleanup_wallet("wallet_service_create_wallet_works_for_moderate_key_derivation"); - - { - WalletService::new() - .create_wallet( - &_config_default( - "wallet_service_create_wallet_works_for_moderate_key_derivation", - ), - &ARGON_MOD_CREDENTIAL, - (&MODERATE_KDD, &MODERATE_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_create_wallet_works_for_moderate_key_derivation"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_create_wallet_works_for_comparision_time_of_different_key_types() { - use std::time::Instant; - test::cleanup_wallet( - "wallet_service_create_wallet_works_for_comparision_time_of_different_key_types", - ); - { - let wallet_service = WalletService::new(); - - let config = _config_default( - "wallet_service_create_wallet_works_for_comparision_time_of_different_key_types", - ); - - let time = Instant::now(); - - wallet_service - .create_wallet( - &config, - &ARGON_MOD_CREDENTIAL, - (&MODERATE_KDD, &MODERATE_MASTER_KEY), - ) - .await - .unwrap(); - - let time_diff_moderate_key = time.elapsed(); - - wallet_service - .delete_wallet(&config, &ARGON_MOD_CREDENTIAL) - .await - .unwrap(); - - _cleanup( - "wallet_service_create_wallet_works_for_comparision_time_of_different_key_types", - ); - - let time = Instant::now(); - - wallet_service - .create_wallet( - &config, - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - - let time_diff_interactive_key = time.elapsed(); - - wallet_service - .delete_wallet(&config, &ARGON_INT_CREDENTIAL) - .await - .unwrap(); - - assert!(time_diff_interactive_key < time_diff_moderate_key); - } - - test::cleanup_wallet( - "wallet_service_create_wallet_works_for_comparision_time_of_different_key_types", - ); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_create_works_for_plugged() { - _cleanup("wallet_service_create_works_for_plugged"); - - { - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - } - - _cleanup("wallet_service_create_works_for_plugged"); - } - - #[async_std::test] - async fn wallet_service_create_wallet_works_for_none_type() { - test::cleanup_wallet("wallet_service_create_wallet_works_for_none_type"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_create_wallet_works_for_none_type"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_create_wallet_works_for_none_type"); - } - - #[async_std::test] - async fn wallet_service_create_wallet_works_for_unknown_type() { - test::cleanup_wallet("wallet_service_create_wallet_works_for_unknown_type"); - - { - let wallet_service = WalletService::new(); - - let res = wallet_service - .create_wallet( - &_config_unknown("wallet_service_create_wallet_works_for_unknown_type"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await; - - assert_kind!(IndyErrorKind::UnknownWalletStorageType, res); - } - } - - #[async_std::test] - async fn wallet_service_create_wallet_works_for_twice() { - test::cleanup_wallet("wallet_service_create_wallet_works_for_twice"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_create_wallet_works_for_twice"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let res = wallet_service - .create_wallet( - &_config("wallet_service_create_wallet_works_for_twice"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await; - - assert_kind!(IndyErrorKind::WalletAlreadyExists, res); - } - - test::cleanup_wallet("wallet_service_create_wallet_works_for_twice"); - } - - // FIXME: !!! - // - // #[async_std::test] - // async fn wallet_service_create_wallet_works_for_invalid_raw_key() { - // _cleanup("wallet_service_create_wallet_works_for_invalid_raw_key"); - - // let wallet_service = WalletService::new(); - // wallet_service - // .create_wallet( - // &_config("wallet_service_create_wallet_works_for_invalid_raw_key"), - // &_credentials(), - // ) - // .await.unwrap(); - // let res = wallet_service.create_wallet( - // &_config("wallet_service_create_wallet_works_for_invalid_raw_key"), - // &_credentials_invalid_raw(), - // ); - // assert_match!( - // Err(IndyError::CommonError(CommonError::InvalidStructure(_))), - // res - // ); - // } - - #[async_std::test] - async fn wallet_service_delete_wallet_works() { - test::cleanup_wallet("wallet_service_delete_wallet_works"); - - { - let config: &Config = &_config("wallet_service_delete_wallet_works"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - wallet_service - .delete_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_delete_wallet_works"); - } - - #[async_std::test] - async fn wallet_service_delete_wallet_works_for_interactive_key_derivation() { - test::cleanup_wallet("wallet_service_delete_wallet_works_for_interactive_key_derivation"); - - { - let config: &Config = - &_config("wallet_service_delete_wallet_works_for_interactive_key_derivation"); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - config, - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - - wallet_service - .delete_wallet(config, &ARGON_INT_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .create_wallet( - config, - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_delete_wallet_works_for_interactive_key_derivation"); - } - - #[async_std::test] - async fn wallet_service_delete_wallet_works_for_moderate_key_derivation() { - test::cleanup_wallet("wallet_service_delete_wallet_works_for_moderate_key_derivation"); - - { - let config: &Config = - &_config("wallet_service_delete_wallet_works_for_moderate_key_derivation"); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - config, - &ARGON_MOD_CREDENTIAL, - (&MODERATE_KDD, &MODERATE_MASTER_KEY), - ) - .await - .unwrap(); - - wallet_service - .delete_wallet(config, &ARGON_MOD_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .create_wallet( - config, - &ARGON_MOD_CREDENTIAL, - (&MODERATE_KDD, &MODERATE_MASTER_KEY), - ) - .await - .unwrap(); - } - - test::cleanup_wallet("wallet_service_delete_wallet_works_for_moderate_key_derivation"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_delete_works_for_plugged() { - test::cleanup_wallet("wallet_service_delete_works_for_plugged"); - - let wallet_service = WalletService::new(); - - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - wallet_service - .delete_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - } - - #[async_std::test] - async fn wallet_service_delete_wallet_returns_error_if_wallet_opened() { - test::cleanup_wallet("wallet_service_delete_wallet_returns_error_if_wallet_opened"); - - { - let config: &Config = - &_config("wallet_service_delete_wallet_returns_error_if_wallet_opened"); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - let res = wallet_service.delete_wallet(config, &RAW_CREDENTIAL).await; - assert_eq!(IndyErrorKind::InvalidState, res.unwrap_err().kind()); - } - - test::cleanup_wallet("wallet_service_delete_wallet_returns_error_if_wallet_opened"); - } - - #[async_std::test] - async fn wallet_service_delete_wallet_returns_error_if_passed_different_value_for_interactive_method( - ) { - test::cleanup_wallet("wallet_service_delete_wallet_returns_error_if_passed_different_value_for_interactive_method"); - - { - let config: &Config = &_config("wallet_service_delete_wallet_returns_error_if_passed_different_value_for_interactive_method"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let res = wallet_service - .delete_wallet(config, &ARGON_INT_CREDENTIAL) - .await; - - assert_eq!(IndyErrorKind::WalletAccessFailed, res.unwrap_err().kind()); - } - - test::cleanup_wallet("wallet_service_delete_wallet_returns_error_if_passed_different_value_for_interactive_method"); - } - - #[async_std::test] - async fn wallet_service_delete_wallet_returns_error_for_nonexistant_wallet() { - test::cleanup_wallet("wallet_service_delete_wallet_returns_error_for_nonexistant_wallet"); - - let wallet_service = WalletService::new(); - - let res = wallet_service - .delete_wallet( - &_config("wallet_service_delete_wallet_returns_error_for_nonexistant_wallet"), - &RAW_CREDENTIAL, - ) - .await; - - assert_eq!(IndyErrorKind::WalletNotFound, res.unwrap_err().kind()); - } - - #[async_std::test] - async fn wallet_service_open_wallet_works() { - test::cleanup_wallet("wallet_service_open_wallet_works"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_open_wallet_works"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let handle = wallet_service - .open_wallet( - &_config("wallet_service_open_wallet_works"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - // cleanup - wallet_service.close_wallet(handle).await.unwrap(); - } - - test::cleanup_wallet("wallet_service_open_wallet_works"); - } - - #[async_std::test] - async fn wallet_service_open_wallet_works_for_interactive_key_derivation() { - test::cleanup_wallet("wallet_service_open_wallet_works_for_interactive_key_derivation"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_open_wallet_works_for_interactive_key_derivation"), - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - - let handle = wallet_service - .open_wallet( - &_config("wallet_service_open_wallet_works_for_interactive_key_derivation"), - &ARGON_INT_CREDENTIAL, - ) - .await - .unwrap(); - - // cleanup - wallet_service.close_wallet(handle).await.unwrap(); - } - - test::cleanup_wallet("wallet_service_open_wallet_works_for_interactive_key_derivation"); - } - - #[async_std::test] - async fn wallet_service_open_wallet_works_for_moderate_key_derivation() { - test::cleanup_wallet("wallet_service_open_wallet_works_for_moderate_key_derivation"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_open_wallet_works_for_moderate_key_derivation"), - &ARGON_MOD_CREDENTIAL, - (&MODERATE_KDD, &MODERATE_MASTER_KEY), - ) - .await - .unwrap(); - - let handle = wallet_service - .open_wallet( - &_config("wallet_service_open_wallet_works_for_moderate_key_derivation"), - &ARGON_MOD_CREDENTIAL, - ) - .await - .unwrap(); - - // cleanup - wallet_service.close_wallet(handle).await.unwrap(); - } - - test::cleanup_wallet("wallet_service_open_wallet_works_for_moderate_key_derivation"); - } - - #[async_std::test] - async fn wallet_service_open_wallet_works_for_two_wallets_with_same_ids_but_different_paths() { - _cleanup( - "wallet_service_open_wallet_works_for_two_wallets_with_same_ids_but_different_paths", - ); - - let wallet_service = WalletService::new(); - - let config_1 = Config { - id: String::from("same_id"), - storage_type: None, - storage_config: None, - cache: None, - }; - - wallet_service - .create_wallet(&config_1, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let handle_1 = wallet_service - .open_wallet(&config_1, &RAW_CREDENTIAL) - .await - .unwrap(); - - let config_2 = Config { - id: String::from("same_id"), - storage_type: None, - storage_config: Some(json!({ - "path": _custom_path("wallet_service_open_wallet_works_for_two_wallets_with_same_ids_but_different_paths") - })), - cache: None, - }; - - wallet_service - .create_wallet(&config_2, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let handle_2 = wallet_service - .open_wallet(&config_2, &RAW_CREDENTIAL) - .await - .unwrap(); - - // cleanup - wallet_service.close_wallet(handle_1).await.unwrap(); - wallet_service.close_wallet(handle_2).await.unwrap(); - - wallet_service - .delete_wallet(&config_1, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .delete_wallet(&config_2, &RAW_CREDENTIAL) - .await - .unwrap(); - - _cleanup( - "wallet_service_open_wallet_works_for_two_wallets_with_same_ids_but_different_paths", - ); - } - - #[async_std::test] - async fn wallet_service_open_unknown_wallet() { - test::cleanup_wallet("wallet_service_open_unknown_wallet"); - - let wallet_service = WalletService::new(); - - let res = wallet_service - .open_wallet( - &_config("wallet_service_open_unknown_wallet"), - &RAW_CREDENTIAL, - ) - .await; - - assert_eq!(IndyErrorKind::WalletNotFound, res.unwrap_err().kind()); - } - - #[async_std::test] - async fn wallet_service_open_wallet_returns_appropriate_error_if_already_opened() { - test::cleanup_wallet( - "wallet_service_open_wallet_returns_appropriate_error_if_already_opened", - ); - - { - let config: &Config = - &_config("wallet_service_open_wallet_returns_appropriate_error_if_already_opened"); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - - assert_eq!(IndyErrorKind::WalletAlreadyOpened, res.unwrap_err().kind()); - } - - test::cleanup_wallet( - "wallet_service_open_wallet_returns_appropriate_error_if_already_opened", - ); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_open_works_for_plugged() { - _cleanup("wallet_service_open_works_for_plugged"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - } - - #[async_std::test] - async fn wallet_service_open_wallet_returns_error_if_used_different_methods_for_creating_and_opening( - ) { - test::cleanup_wallet("wallet_service_open_wallet_returns_error_if_used_different_methods_for_creating_and_opening"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_open_wallet_returns_error_if_used_different_methods_for_creating_and_opening"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY)) - .await.unwrap(); - - let res = wallet_service - .open_wallet( - &_config("wallet_service_open_wallet_returns_error_if_used_different_methods_for_creating_and_opening"), - &ARGON_INT_CREDENTIAL - ) - .await; - - assert_kind!(IndyErrorKind::WalletAccessFailed, res); - } - - test::cleanup_wallet("wallet_service_open_wallet_returns_error_if_used_different_methods_for_creating_and_opening"); - } - - #[async_std::test] - async fn wallet_service_close_wallet_works() { - test::cleanup_wallet("wallet_service_close_wallet_works"); - - { - let config: &Config = &_config("wallet_service_close_wallet_works"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - } - - test::cleanup_wallet("wallet_service_close_wallet_works"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_close_works_for_plugged() { - _cleanup("wallet_service_close_works_for_plugged"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - } - - #[async_std::test] - async fn wallet_service_close_wallet_returns_appropriate_error_if_wrong_handle() { - test::cleanup_wallet( - "wallet_service_close_wallet_returns_appropriate_error_if_wrong_handle", - ); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config( - "wallet_service_close_wallet_returns_appropriate_error_if_wrong_handle", - ), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config( - "wallet_service_close_wallet_returns_appropriate_error_if_wrong_handle", - ), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let res = wallet_service.close_wallet(INVALID_WALLET_HANDLE).await; - assert_kind!(IndyErrorKind::InvalidWalletHandle, res); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - } - - test::cleanup_wallet( - "wallet_service_close_wallet_returns_appropriate_error_if_wrong_handle", - ); - } - - #[async_std::test] - async fn wallet_service_add_record_works() { - test::cleanup_wallet("wallet_service_add_record_works"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_record_works"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_add_record_works"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - test::cleanup_wallet( - "wallet_service_ - cord_works", - ); - } - - #[async_std::test] - async fn wallet_service_add_record_works_for_cached_wallet() { - test::cleanup_wallet("wallet_service_add_record_works_for_cached_wallet"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_record_works_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_add_record_works_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_add_record_works_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_add_record_works_for_plugged() { - _cleanup("wallet_service_add_record_works_for_plugged"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_id_only() { - test::cleanup_wallet("wallet_service_get_record_works_for_id_only"); - - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_id_only"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_get_record_works_for_id_only"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, false, false), - ) - .await - .unwrap(); - - assert!(record.get_value().is_none()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_id_only"); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_id_only_for_cached_wallet() { - test::cleanup_wallet("wallet_service_get_record_works_for_id_only_for_cached_wallet"); - - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_id_only_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached( - "wallet_service_get_record_works_for_id_only_for_cached_wallet", - ), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, false, false), - ) - .await - .unwrap(); - - assert!(record.get_value().is_none()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_id_only_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_get_record_works_for_plugged_for_id_only() { - test::cleanup_indy_home("wallet_service_get_record_works_for_plugged_for_id_only"); - InmemWallet::cleanup(); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, false, false), - ) - .await - .unwrap(); - - assert!(record.get_value().is_none()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_id_value() { - test::cleanup_wallet("wallet_service_get_record_works_for_id_value"); - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_id_value"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_get_record_works_for_id_value"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!("value1", record.get_value().unwrap()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_id_value"); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_id_value_for_cached_wallet() { - test::cleanup_wallet("wallet_service_get_record_works_for_id_value_for_cached_wallet"); - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_id_value_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached( - "wallet_service_get_record_works_for_id_value_for_cached_wallet", - ), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!("value1", record.get_value().unwrap()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_id_value_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_get_record_works_for_plugged_for_id_value() { - _cleanup("wallet_service_get_record_works_for_plugged_for_id_value"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!("value1", record.get_value().unwrap()); - assert!(record.get_type().is_none()); - assert!(record.get_tags().is_none()); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_all_fields() { - test::cleanup_wallet("wallet_service_get_record_works_for_all_fields"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_all_fields"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_get_record_works_for_all_fields"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let mut tags = HashMap::new(); - tags.insert(String::from("1"), String::from("some")); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &tags) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - assert_eq!(&tags, record.get_tags().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_all_fields"); - } - - #[async_std::test] - async fn wallet_service_get_record_works_for_all_fields_for_cached_wallet() { - test::cleanup_wallet("wallet_service_get_record_works_for_all_fields_for_cached_wallet"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_get_record_works_for_all_fields_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached( - "wallet_service_get_record_works_for_all_fields_for_cached_wallet", - ), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let mut tags = HashMap::new(); - tags.insert(String::from("1"), String::from("some")); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &tags) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - assert_eq!(&tags, record.get_tags().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_get_record_works_for_all_fields_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_get_record_works_for_plugged_for_for_all_fields() { - _cleanup("wallet_service_get_record_works_for_plugged_for_for_all_fields"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - let tags = serde_json::from_str(r#"{"1":"some"}"#).unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &tags) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - assert_eq!(tags, record.get_tags().unwrap().clone()); - } - - #[async_std::test] - async fn wallet_service_add_get_works_for_reopen() { - test::cleanup_wallet("wallet_service_add_get_works_for_reopen"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_get_works_for_reopen"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_add_get_works_for_reopen"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_add_get_works_for_reopen"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!("value1", record.get_value().unwrap()); - } - - test::cleanup_wallet("wallet_service_add_get_works_for_reopen"); - } - - #[async_std::test] - async fn wallet_service_add_get_works_for_reopen_for_cached_wallet() { - test::cleanup_wallet("wallet_service_add_get_works_for_reopen_for_cached_wallet"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_get_works_for_reopen_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_add_get_works_for_reopen_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_add_get_works_for_reopen_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!("value1", record.get_value().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_miss(), 1); - } - - test::cleanup_wallet("wallet_service_add_get_works_for_reopen_for_cached_wallet"); - } - - #[async_std::test] - async fn wallet_service_get_works_for_unknown() { - test::cleanup_wallet("wallet_service_get_works_for_unknown"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_get_works_for_unknown"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_get_works_for_unknown"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_get_works_for_unknown"); - } - - #[async_std::test] - async fn wallet_service_get_works_for_unknown_for_cached_wallet() { - test::cleanup_wallet("wallet_service_get_works_for_unknown_for_cached_wallet"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_get_works_for_unknown_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_get_works_for_unknown_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_miss(), 1); - } - - test::cleanup_wallet("wallet_service_get_works_for_unknown_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_get_works_for_plugged_and_unknown() { - _cleanup("wallet_service_get_works_for_plugged_and_unknown"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - /** - * Update tests - */ - #[async_std::test] - async fn wallet_service_update() { - test::cleanup_wallet("wallet_service_update"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let new_value = "new_value"; - - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_update"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_update"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .update_record_value(wallet_handle, type_, name, new_value) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(new_value, record.get_value().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_not_cached(), 2); - } - - test::cleanup_wallet("wallet_service_update"); - } - - #[async_std::test] - async fn wallet_service_update_for_cached_wallet() { - test::cleanup_wallet("wallet_service_update_for_cached_wallet"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let new_value = "new_value"; - - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_update_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_update_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .update_record_value(wallet_handle, type_, name, new_value) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(new_value, record.get_value().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_hit(), 2); - } - - test::cleanup_wallet("wallet_service_update_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_update_for_plugged() { - _cleanup("wallet_service_update_for_plugged"); - - let type_ = "type"; - let name = "name"; - let value = "value"; - let new_value = "new_value"; - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .update_record_value(wallet_handle, type_, name, new_value) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(new_value, record.get_value().unwrap()); - } - - /** - * Delete tests - */ - #[async_std::test] - async fn wallet_service_delete_record() { - test::cleanup_wallet("wallet_service_delete_record"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_delete_record"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_delete_record"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .delete_record(wallet_handle, type_, name) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_not_cached(), 2); - } - - test::cleanup_wallet("wallet_service_delete_record"); - } - - #[async_std::test] - async fn wallet_service_delete_record_for_cached_wallet() { - test::cleanup_wallet("wallet_service_delete_record_for_cached_wallet"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_delete_record_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_delete_record_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .delete_record(wallet_handle, type_, name) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_hit(), 1); - assert_eq!(metrics_data.get(type_).unwrap().get_miss(), 1); // after delete - } - - test::cleanup_wallet("wallet_service_delete_record_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_delete_record_for_plugged() { - _cleanup("wallet_service_delete_record_for_plugged"); - - let type_ = "type"; - let name = "name"; - let value = "value"; - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await - .unwrap(); - - assert_eq!(value, record.get_value().unwrap()); - - wallet_service - .delete_record(wallet_handle, type_, name) - .await - .unwrap(); - - let res = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(false, true, false), - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - /** - * Add tags tests - */ - #[async_std::test] - async fn wallet_service_add_tags() { - test::cleanup_wallet("wallet_service_add_tags"); - - { - let type_ = "type"; - let name = "name"; - let value = "value"; - - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1"}"#).unwrap(); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_tags"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_add_tags"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = serde_json::from_str( - r#"{"tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#, - ) - .unwrap(); - - wallet_service - .add_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#).unwrap(); - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_add_tags"); - } - - #[async_std::test] - async fn wallet_service_add_tags_for_cached_wallet() { - test::cleanup_wallet("wallet_service_add_tags_for_cached_wallet"); - - { - let type_ = "type"; - let name = "name"; - let value = "value"; - - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1"}"#).unwrap(); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_add_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_add_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = serde_json::from_str( - r#"{"tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#, - ) - .unwrap(); - - wallet_service - .add_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#).unwrap(); - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_add_tags_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_add_tags_for_plugged() { - _cleanup("wallet_service_add_tags_for_plugged"); - - let type_ = "type"; - let name = "name"; - let value = "value"; - - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1"}"#).unwrap(); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = - serde_json::from_str(r#"{"tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#) - .unwrap(); - - wallet_service - .add_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#) - .unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - } - - /** - * Update tags tests - */ - #[async_std::test] - async fn wallet_service_update_tags() { - test::cleanup_wallet("wallet_service_update_tags"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#).unwrap(); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_update_tags"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_update_tags"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - wallet_service - .update_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(new_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_update_tags"); - } - - #[async_std::test] - async fn wallet_service_update_tags_for_cached_wallet() { - test::cleanup_wallet("wallet_service_update_tags_for_cached_wallet"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#).unwrap(); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_update_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_update_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - wallet_service - .update_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(new_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_update_tags_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_update_tags_for_plugged() { - _cleanup("wallet_service_update_tags_for_plugged"); - - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"tag_value_2", "~tag_name_3":"tag_value_3"}"#).unwrap(); - let wallet_service = WalletService::new(); - - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let new_tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - wallet_service - .update_record_tags(wallet_handle, type_, name, &new_tags) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(new_tags, retrieved_tags); - } - - _cleanup("wallet_service_update_tags_for_plugged"); - } - - /** - * Delete tags tests - */ - #[async_std::test] - async fn wallet_service_delete_tags() { - test::cleanup_wallet("wallet_service_delete_tags"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_delete_tags"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config("wallet_service_delete_tags"), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let tag_names = vec!["tag_name_1", "~tag_name_3"]; - - wallet_service - .delete_record_tags(wallet_handle, type_, name, &tag_names) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = - serde_json::from_str(r#"{"tag_name_2":"new_tag_value_2"}"#).unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_not_cached(), 1); - } - - test::cleanup_wallet("wallet_service_delete_tags"); - } - - #[async_std::test] - async fn wallet_service_delete_tags_for_cached_wallet() { - test::cleanup_wallet("wallet_service_delete_tags_for_cached_wallet"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_delete_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config_cached("wallet_service_delete_tags_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let tag_names = vec!["tag_name_1", "~tag_name_3"]; - - wallet_service - .delete_record_tags(wallet_handle, type_, name, &tag_names) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = - serde_json::from_str(r#"{"tag_name_2":"new_tag_value_2"}"#).unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get(type_).unwrap().get_hit(), 1); - } - - test::cleanup_wallet("wallet_service_delete_tags_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_delete_tags_for_plugged() { - _cleanup("wallet_service_delete_tags_for_plugged"); - { - let type_ = "type"; - let name = "name"; - let value = "value"; - let tags = serde_json::from_str(r#"{"tag_name_1":"tag_value_1", "tag_name_2":"new_tag_value_2", "~tag_name_3":"new_tag_value_3"}"#).unwrap(); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, type_, name, value, &tags) - .await - .unwrap(); - - let tag_names = vec!["tag_name_1", "~tag_name_3"]; - - wallet_service - .delete_record_tags(wallet_handle, type_, name, &tag_names) - .await - .unwrap(); - - let item = wallet_service - .get_record( - wallet_handle, - type_, - name, - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let expected_tags: Tags = - serde_json::from_str(r#"{"tag_name_2":"new_tag_value_2"}"#).unwrap(); - - let retrieved_tags = item.tags.unwrap(); - assert_eq!(expected_tags, retrieved_tags); - } - - _cleanup("wallet_service_delete_tags_for_plugged"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_search_records_works() { - test::cleanup_wallet("wallet_service_search_records_works"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_search_records_works"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_search_records_works"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key2", "value2", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type3", "key3", "value3", &HashMap::new()) - .await - .unwrap(); - - let mut search = wallet_service - .search_records( - wallet_handle, - "type3", - "{}", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let record = search.fetch_next_record().await.unwrap().unwrap(); - assert_eq!("value3", record.get_value().unwrap()); - assert_eq!(HashMap::new(), record.get_tags().unwrap().clone()); - - assert!(search.fetch_next_record().await.unwrap().is_none()); - - assert_eq!( - wallet_service - .get_wallet_cache_hit_metrics_data() - .await - .len(), - 0 - ); // only get is using cache - } - - test::cleanup_wallet("wallet_service_search_records_works"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_search_records_works_for_cached_wallet() { - test::cleanup_wallet("wallet_service_search_records_works_for_cached_wallet"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_search_records_works_for_cached_wallet"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_search_records_works_for_cached_wallet"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key2", "value2", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type3", "key3", "value3", &HashMap::new()) - .await - .unwrap(); - - let mut search = wallet_service - .search_records( - wallet_handle, - "type3", - "{}", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let record = search.fetch_next_record().await.unwrap().unwrap(); - assert_eq!("value3", record.get_value().unwrap()); - assert_eq!(HashMap::new(), record.get_tags().unwrap().clone()); - - assert!(search.fetch_next_record().await.unwrap().is_none()); - - assert_eq!( - wallet_service - .get_wallet_cache_hit_metrics_data() - .await - .len(), - 0 - ); // only get() is using cache - } - - test::cleanup_wallet("wallet_service_search_records_works_for_cached_wallet"); - } - - #[async_std::test] - #[ignore] - async fn wallet_service_search_records_works_for_plugged_wallet() { - _cleanup("wallet_service_search_records_works_for_plugged_wallet"); - - let wallet_service = WalletService::new(); - _register_inmem_wallet(&wallet_service); - - wallet_service - .create_wallet( - &_config_inmem(), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(&_config_inmem(), &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key2", "value2", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type3", "key3", "value3", &HashMap::new()) - .await - .unwrap(); - - let mut search = wallet_service - .search_records( - wallet_handle, - "type3", - "{}", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - let record = search.fetch_next_record().await.unwrap().unwrap(); - assert_eq!("value3", record.get_value().unwrap()); - assert_eq!(HashMap::new(), record.get_tags().unwrap().clone()); - - assert!(search.fetch_next_record().await.unwrap().is_none()); - } - - /** - Key rotation test - */ - #[async_std::test] - async fn wallet_service_key_rotation() { - test::cleanup_wallet("wallet_service_key_rotation"); - { - let config: &Config = &_config("wallet_service_key_rotation"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &_rekey_credentials_moderate()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - // Access failed for old key - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - assert_kind!(IndyErrorKind::WalletAccessFailed, res); - - // Works ok with new key when reopening - let wallet_handle = wallet_service - .open_wallet(config, &_credentials_for_new_key_moderate()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - assert_eq!(metrics_data.get("type").unwrap().get_not_cached(), 3); - } - - test::cleanup_wallet("wallet_service_key_rotation"); - } - - #[async_std::test] - async fn wallet_service_key_rotation_for_cached_wallet() { - test::cleanup_wallet("wallet_service_key_rotation_for_cached_wallet"); - { - let config: &Config = &_config_cached("wallet_service_key_rotation_for_cached_wallet"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); // cache hit - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &_rekey_credentials_moderate()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); // cache miss (wallet just opened) - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - // Access failed for old key - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - assert_kind!(IndyErrorKind::WalletAccessFailed, res); - - // Works ok with new key when reopening - let wallet_handle = wallet_service - .open_wallet(config, &_credentials_for_new_key_moderate()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); // cache miss (wallet just opened) - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - // check again, now it should be in cache. - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); // cache hit - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - let metrics_data = wallet_service.get_wallet_cache_hit_metrics_data().await; - println!("metrics: {:?}", metrics_data); - assert_eq!(metrics_data.get("type").unwrap().get_hit(), 2); - assert_eq!(metrics_data.get("type").unwrap().get_miss(), 2); - } - - test::cleanup_wallet("wallet_service_key_rotation_for_cached_wallet"); - } - - #[async_std::test] - async fn wallet_service_key_rotation_for_rekey_interactive_method() { - test::cleanup_wallet("wallet_service_key_rotation_for_rekey_interactive_method"); - { - let config: &Config = - &_config("wallet_service_key_rotation_for_rekey_interactive_method"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &_rekey_credentials_interactive()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - // Access failed for old key - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - assert_kind!(IndyErrorKind::WalletAccessFailed, res); - - // Works ok with new key when reopening - let wallet_handle = wallet_service - .open_wallet(config, &_credentials_for_new_key_interactive()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - } - - test::cleanup_wallet("wallet_service_key_rotation_for_rekey_interactive_method"); - } - - #[async_std::test] - async fn wallet_service_key_rotation_for_rekey_raw_method() { - test::cleanup_wallet("wallet_service_key_rotation_for_rekey_raw_method"); - - { - let config: &Config = &_config("wallet_service_key_rotation_for_rekey_raw_method"); - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &_rekey_credentials_raw()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - // Access failed for old key - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - assert_kind!(IndyErrorKind::WalletAccessFailed, res); - - // Works ok with new key when reopening - let wallet_handle = wallet_service - .open_wallet(config, &_credentials_for_new_key_raw()) - .await - .unwrap(); - - let record = wallet_service - .get_record( - wallet_handle, - "type", - "key1", - &_fetch_options(true, true, true), - ) - .await - .unwrap(); - - assert_eq!("type", record.get_type().unwrap()); - assert_eq!("value1", record.get_value().unwrap()); - } - - test::cleanup_wallet("wallet_service_key_rotation_for_rekey_raw_method"); - } - - fn remove_exported_wallet(export_config: &ExportConfig) -> &Path { - let export_path = Path::new(&export_config.path); - - if export_path.exists() { - fs::remove_file(export_path).unwrap(); - } - - export_path - } - - #[async_std::test] - async fn wallet_service_export_wallet_when_empty() { - test::cleanup_wallet("wallet_service_export_wallet_when_empty"); - let export_config = _export_config_raw("export_wallet_service_export_wallet_when_empty"); - { - let wallet_service = WalletService::new(); - let wallet_config = _config("wallet_service_export_wallet_when_empty"); - wallet_service - .create_wallet(&wallet_config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_when_empty"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let export_path = remove_exported_wallet(&export_config); - let (kdd, master_key) = _export_key_raw("key_wallet_service_export_wallet_when_empty"); - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - } - remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_wallet_when_empty"); - } - - #[async_std::test] - async fn wallet_service_export_wallet_1_item() { - test::cleanup_wallet("wallet_service_export_wallet_1_item"); - let export_config = _export_config_raw("export_config_wallet_service_export_wallet_1_item"); - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_export_wallet_1_item"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_1_item"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let export_path = remove_exported_wallet(&export_config); - let (kdd, master_key) = _export_key_raw("key_wallet_service_export_wallet_1_item"); - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - assert!(export_path.exists()); - } - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_wallet_1_item"); - } - - #[async_std::test] - async fn wallet_service_export_wallet_1_item_interactive_method() { - test::cleanup_wallet("wallet_service_export_wallet_1_item_interactive_method"); - let export_config = - _export_config_interactive("wallet_service_export_wallet_1_item_interactive_method"); - { - let wallet_service = WalletService::new(); - wallet_service - .create_wallet( - &_config("wallet_service_export_wallet_1_item_interactive_method"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_1_item_interactive_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let export_path = remove_exported_wallet(&export_config); - let (kdd, master_key) = - _export_key_interactive("wallet_service_export_wallet_1_item_interactive_method"); - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - assert!(export_path.exists()); - } - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_wallet_1_item_interactive_method"); - } - - #[async_std::test] - async fn wallet_service_export_wallet_1_item_raw_method() { - test::cleanup_wallet("wallet_service_export_wallet_1_item_raw_method"); - let export_config = _export_config_raw("wallet_service_export_wallet_1_item_raw_method"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_wallet_1_item_raw_method"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_1_item_raw_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let export_path = remove_exported_wallet(&export_config); - let (kdd, master_key) = _export_key("wallet_service_export_wallet_1_item_raw_method"); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(&export_path.exists()); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_wallet_1_item_raw_method"); - } - - #[async_std::test] - async fn wallet_service_export_wallet_returns_error_if_file_exists() { - test::cleanup_wallet("wallet_service_export_wallet_returns_error_if_file_exists"); - - { - fs::create_dir_all( - _export_file_path("wallet_service_export_wallet_returns_error_if_file_exists") - .parent() - .unwrap(), - ) - .unwrap(); - - fs::File::create(_export_file_path( - "wallet_service_export_wallet_returns_error_if_file_exists", - )) - .unwrap(); - } - - assert!( - _export_file_path("wallet_service_export_wallet_returns_error_if_file_exists").exists() - ); - - let export_config = - _export_config_raw("wallet_service_export_wallet_returns_error_if_file_exists"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_wallet_returns_error_if_file_exists"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_returns_error_if_file_exists"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let (kdd, master_key) = - _export_key_raw("key_wallet_service_export_wallet_returns_error_if_file_exists"); - - let res = wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await; - - assert_eq!(IndyErrorKind::IOError, res.unwrap_err().kind()); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_wallet_returns_error_if_file_exists"); - } - - #[async_std::test] - async fn wallet_service_export_wallet_returns_error_if_wrong_handle() { - test::cleanup_wallet("wallet_service_export_wallet_returns_error_if_wrong_handle"); - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_wallet_returns_error_if_wrong_handle"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let _wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_wallet_returns_error_if_wrong_handle"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let (kdd, master_key) = - _export_key_raw("key_wallet_service_export_wallet_returns_error_if_wrong_handle"); - - let export_config = - _export_config_raw("wallet_service_export_wallet_returns_error_if_wrong_handle"); - - let export_path = remove_exported_wallet(&export_config); - - let res = wallet_service - .export_wallet( - INVALID_WALLET_HANDLE, - &export_config, - 0, - (&kdd, &master_key), - ) - .await; - - assert_kind!(IndyErrorKind::InvalidWalletHandle, res); - assert!(!export_path.exists()); - } - - test::cleanup_wallet("wallet_service_export_wallet_returns_error_if_wrong_handle"); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_1_item() { - test::cleanup_wallet("wallet_service_export_import_wallet_1_item"); - let export_config = _export_config_raw("wallet_service_export_import_wallet_1_item"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_import_wallet_1_item"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let (kdd, master_key) = - _export_key_raw("key_wallet_service_export_import_wallet_1_item"); - - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet( - &_config("wallet_service_export_import_wallet_1_item"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - let export_config = _export_config_raw("wallet_service_export_import_wallet_1_item"); - - wallet_service - .import_wallet( - &_config("wallet_service_export_import_wallet_1_item"), - &RAW_CREDENTIAL, - &export_config, - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_import_wallet_1_item"); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_1_item_for_interactive_method() { - test::cleanup_wallet("wallet_service_export_import_wallet_1_item_for_interactive_method"); - - let export_config = _export_config_interactive( - "wallet_service_export_import_wallet_1_item_for_interactive_method", - ); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_interactive_method"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_interactive_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let (kdd, master_key) = _export_key_interactive( - "wallet_service_export_import_wallet_1_item_for_interactive_method", - ); - - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_interactive_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .import_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_interactive_method"), - &RAW_CREDENTIAL, - &_export_config_interactive( - "wallet_service_export_import_wallet_1_item_for_interactive_method", - ), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_interactive_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_import_wallet_1_item_for_interactive_method"); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_1_item_for_moderate_method() { - test::cleanup_wallet("wallet_service_export_import_wallet_1_item_for_moderate_method"); - - let export_config = - _export_config_raw("wallet_service_export_import_wallet_1_item_for_moderate_method"); - - { - let wallet_service = WalletService::new(); - - wallet_service - .create_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_moderate_method"), - &RAW_CREDENTIAL, - (&RAW_KDD, &RAW_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_moderate_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let (kdd, master_key) = _export_key_raw( - "key_wallet_service_export_import_wallet_1_item_for_moderate_method", - ); - - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_moderate_method"), - &RAW_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .import_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_moderate_method"), - &ARGON_MOD_CREDENTIAL, - &export_config, - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet( - &_config("wallet_service_export_import_wallet_1_item_for_moderate_method"), - &ARGON_MOD_CREDENTIAL, - ) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_import_wallet_1_item_for_moderate_method"); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw() { - test::cleanup_wallet( - "wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw", - ); - - let export_config = _export_config_raw( - "wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw", - ); - - { - let wallet_service = WalletService::new(); - - let config: &Config = &_config( - "wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw", - ); - - wallet_service - .create_wallet( - config, - &ARGON_INT_CREDENTIAL, - (&INTERACTIVE_KDD, &INTERACTIVE_MASTER_KEY), - ) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &ARGON_INT_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let (kdd, master_key) = _export_key_interactive( - "wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw", - ); - - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet(config, &ARGON_INT_CREDENTIAL) - .await - .unwrap(); - - wallet_service.import_wallet(config, &ARGON_MOD_CREDENTIAL, &_export_config_moderate("wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw")).await.unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &ARGON_MOD_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - - test::cleanup_wallet( - "wallet_service_export_import_wallet_1_item_for_export_interactive_import_as_raw", - ); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive() { - test::cleanup_wallet( - "wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive", - ); - - let export_config = _export_config_interactive( - "wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive", - ); - - { - let wallet_service = WalletService::new(); - - let config: &Config = &_config( - "wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive", - ); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .add_record(wallet_handle, "type", "key1", "value1", &HashMap::new()) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - - let (kdd, master_key) = _export_key_interactive( - "wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive", - ); - - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .import_wallet(config, &ARGON_INT_CREDENTIAL, &export_config) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &ARGON_INT_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .get_record(wallet_handle, "type", "key1", "{}") - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - - test::cleanup_wallet( - "wallet_service_export_import_wallet_1_item_for_export_raw_import_as_interactive", - ); - } - - #[async_std::test] - async fn wallet_service_export_import_wallet_if_empty() { - test::cleanup_wallet("wallet_service_export_import_wallet_if_empty"); - - let export_config = _export_config_raw("wallet_service_export_import_wallet_if_empty"); - - { - let wallet_service = WalletService::new(); - let config: &Config = &_config("wallet_service_export_import_wallet_if_empty"); - - wallet_service - .create_wallet(config, &RAW_CREDENTIAL, (&RAW_KDD, &RAW_MASTER_KEY)) - .await - .unwrap(); - - let wallet_handle = wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - let (kdd, master_key) = _export_key("wallet_service_export_import_wallet_if_empty"); - let export_path = remove_exported_wallet(&export_config); - - wallet_service - .export_wallet(wallet_handle, &export_config, 0, (&kdd, &master_key)) - .await - .unwrap(); - - assert!(export_path.exists()); - - wallet_service.close_wallet(wallet_handle).await.unwrap(); - - wallet_service - .delete_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - - wallet_service - .import_wallet(config, &RAW_CREDENTIAL, &export_config) - .await - .unwrap(); - - wallet_service - .open_wallet(config, &RAW_CREDENTIAL) - .await - .unwrap(); - } - - let _export_path = remove_exported_wallet(&export_config); - test::cleanup_wallet("wallet_service_export_import_wallet_if_empty"); - } - - #[async_std::test] - async fn wallet_service_export_import_returns_error_if_path_missing() { - _cleanup("wallet_service_export_import_returns_error_if_path_missing"); - - let wallet_service = WalletService::new(); - - let config: &Config = - &_config("wallet_service_export_import_returns_error_if_path_missing"); - - let export_config = - _export_config_raw("wallet_service_export_import_returns_error_if_path_missing"); - - let res = wallet_service - .import_wallet(config, &RAW_CREDENTIAL, &export_config) - .await; - assert_eq!(IndyErrorKind::IOError, res.unwrap_err().kind()); - - let res = wallet_service.open_wallet(config, &RAW_CREDENTIAL).await; - assert_match!(Err(_), res); - - _cleanup("wallet_service_export_import_returns_error_if_path_missing"); - } - - fn _fetch_options(type_: bool, value: bool, tags: bool) -> String { - json!({ - "retrieveType": type_, - "retrieveValue": value, - "retrieveTags": tags, - }) - .to_string() - } - - fn _config(name: &str) -> Config { - Config { - id: name.to_string(), - storage_type: None, - storage_config: None, - cache: None, - } - } - - fn _config_cached(name: &str) -> Config { - Config { - id: name.to_string(), - storage_type: None, - storage_config: None, - cache: Some(CacheConfig { - size: 10, - entities: vec![ - "did".to_string(), - "their_did".to_string(), - "type".to_string(), - ], - algorithm: CachingAlgorithm::LRU, - }), - } - } - - fn _config_default(name: &str) -> Config { - Config { - id: name.to_string(), - storage_type: Some("default".to_string()), - storage_config: None, - cache: None, - } - } - - fn _config_default_cached(name: &str) -> Config { - Config { - id: name.to_string(), - storage_type: Some("default".to_string()), - storage_config: None, - cache: Some(CacheConfig { - size: 10, - entities: vec![ - "did".to_string(), - "their_did".to_string(), - "type".to_string(), - ], - algorithm: CachingAlgorithm::LRU, - }), - } - } - - fn _config_inmem() -> Config { - Config { - id: "w1".to_string(), - storage_type: Some("inmem".to_string()), - storage_config: None, - cache: None, - } - } - - fn _config_unknown(name: &str) -> Config { - Config { - id: name.to_string(), - storage_type: Some("unknown".to_string()), - storage_config: None, - cache: None, - } - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref ARGON_MOD_CREDENTIAL: Credentials = Credentials { - key: "my_key".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - }; - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref ARGON_INT_CREDENTIAL: Credentials = Credentials { - key: "my_key".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::ARGON2I_INT, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_INT, - }; - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref RAW_CREDENTIAL: Credentials = Credentials { - key: "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::RAW, - }; - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref MODERATE_KDD: KeyDerivationData = - KeyDerivationData::from_passphrase_with_new_salt( - "my_key", - &KeyDerivationMethod::ARGON2I_MOD - ); - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref MODERATE_MASTER_KEY: MasterKey = MODERATE_KDD.calc_master_key().unwrap(); - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref INTERACTIVE_KDD: KeyDerivationData = - KeyDerivationData::from_passphrase_with_new_salt( - "my_key", - &KeyDerivationMethod::ARGON2I_INT - ); - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref INTERACTIVE_MASTER_KEY: MasterKey = INTERACTIVE_KDD.calc_master_key().unwrap(); - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref RAW_KDD: KeyDerivationData = KeyDerivationData::from_passphrase_with_new_salt( - "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw", - &KeyDerivationMethod::RAW - ); - } - - #[allow(non_upper_case_globals)] - lazy_static! { - static ref RAW_MASTER_KEY: MasterKey = RAW_KDD.calc_master_key().unwrap(); - } - - fn _credentials_invalid_raw() -> Credentials { - Credentials { - key: "key".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::RAW, - } - } - - fn _rekey_credentials_moderate() -> Credentials { - Credentials { - key: "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - rekey: Some("my_new_key".to_string()), - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - } - } - - fn _rekey_credentials_interactive() -> Credentials { - Credentials { - key: "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - rekey: Some("my_new_key".to_string()), - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_INT, - } - } - - fn _rekey_credentials_raw() -> Credentials { - Credentials { - key: "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - rekey: Some("7nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string()), - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::RAW, - } - } - - fn _credentials_for_new_key_moderate() -> Credentials { - Credentials { - key: "my_new_key".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - } - } - - fn _credentials_for_new_key_interactive() -> Credentials { - Credentials { - key: "my_new_key".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::ARGON2I_INT, - rekey_derivation_method: KeyDerivationMethod::ARGON2I_INT, - } - } - - fn _credentials_for_new_key_raw() -> Credentials { - Credentials { - key: "7nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - rekey: None, - storage_credentials: None, - key_derivation_method: KeyDerivationMethod::RAW, - rekey_derivation_method: KeyDerivationMethod::RAW, - } - } - - fn _export_file_path(name: &str) -> PathBuf { - let mut path = environment::tmp_path(); - path.push(name); - path - } - - fn _export_config_moderate(name: &str) -> ExportConfig { - ExportConfig { - key: "export_key".to_string(), - path: _export_file_path(name).to_str().unwrap().to_string(), - key_derivation_method: KeyDerivationMethod::ARGON2I_MOD, - } - } - - fn _calc_key(export_config: &ExportConfig) -> (KeyDerivationData, MasterKey) { - let kdd = KeyDerivationData::from_passphrase_with_new_salt( - &export_config.key, - &export_config.key_derivation_method, - ); - let master_key = kdd.calc_master_key().unwrap(); - (kdd, master_key) - } - - fn _export_key(name: &str) -> (KeyDerivationData, MasterKey) { - _calc_key(&_export_config_raw(name)) - } - - fn _export_config_interactive(name: &str) -> ExportConfig { - ExportConfig { - key: "export_key".to_string(), - path: _export_file_path(name).to_str().unwrap().to_string(), - key_derivation_method: KeyDerivationMethod::ARGON2I_INT, - } - } - - fn _export_key_interactive(name: &str) -> (KeyDerivationData, MasterKey) { - _calc_key(&_export_config_interactive(name)) - } - - fn _export_config_raw(name: &str) -> ExportConfig { - ExportConfig { - key: "6nxtSiXFvBd593Y2DCed2dYvRY1PGK9WMtxCBjLzKgbw".to_string(), - path: _export_file_path(name).to_str().unwrap().to_string(), - key_derivation_method: KeyDerivationMethod::RAW, - } - } - - fn _export_key_raw(name: &str) -> (KeyDerivationData, MasterKey) { - _calc_key(&_export_config_raw(name)) - } - - fn _cleanup(name: &str) { - test::cleanup_storage(name); - InmemWallet::cleanup(); - } - - fn _register_inmem_wallet(wallet_service: &WalletService) { - wallet_service - .register_wallet_storage( - "inmem", - InmemWallet::create, - InmemWallet::open, - InmemWallet::close, - InmemWallet::delete, - InmemWallet::add_record, - InmemWallet::update_record_value, - InmemWallet::update_record_tags, - InmemWallet::add_record_tags, - InmemWallet::delete_record_tags, - InmemWallet::delete_record, - InmemWallet::get_record, - InmemWallet::get_record_id, - InmemWallet::get_record_type, - InmemWallet::get_record_value, - InmemWallet::get_record_tags, - InmemWallet::free_record, - InmemWallet::get_storage_metadata, - InmemWallet::set_storage_metadata, - InmemWallet::free_storage_metadata, - InmemWallet::search_records, - InmemWallet::search_all_records, - InmemWallet::get_search_total_count, - InmemWallet::fetch_search_next_record, - InmemWallet::free_search, - ) - .unwrap(); - } - - fn _custom_path(name: &str) -> String { - let mut path = environment::tmp_path(); - path.push(name); - path.to_str().unwrap().to_owned() - } - - #[test] - fn short_type_name_works() { - assert_eq!("WalletRecord", short_type_name::()); - } - - // #[test] - // fn short_type_name_works2() { - // assert_eq!("WalletRecord2", short_type_name::()); - // } -} diff --git a/libvdrtools/indy-wallet/src/storage/default/mod.rs b/libvdrtools/indy-wallet/src/storage/default/mod.rs index 494eb57523..cc5632c00d 100644 --- a/libvdrtools/indy-wallet/src/storage/default/mod.rs +++ b/libvdrtools/indy-wallet/src/storage/default/mod.rs @@ -859,7 +859,7 @@ impl WalletStorageType for SQLiteStorageType { CREATE INDEX ix_tags_plaintext_name ON tags_plaintext(name); CREATE INDEX ix_tags_plaintext_value ON tags_plaintext(value); CREATE INDEX ix_tags_plaintext_item_id ON tags_plaintext(item_id); - + /*** Insert metadata ***/ INSERT INTO metadata(value) VALUES (?1); @@ -942,1043 +942,3 @@ impl WalletStorageType for SQLiteStorageType { })) } } - -#[cfg(test)] -mod tests { - use std::path::Path; - - use indy_utils::{assert_kind, test}; - use serde_json::json; - - use super::super::Tag; - use super::*; - - #[async_std::test] - async fn sqlite_storage_type_create_works() { - _cleanup("sqlite_storage_type_create_works"); - - let storage_type = SQLiteStorageType::new(); - - storage_type - .create_storage("sqlite_storage_type_create_works", None, None, &_metadata()) - .await - .unwrap(); - - _cleanup("sqlite_storage_type_create_works"); - } - - #[async_std::test] - async fn sqlite_storage_type_create_works_for_custom_path() { - _cleanup("sqlite_storage_type_create_works_for_custom_path"); - - let config = json!({ - "path": _custom_path("sqlite_storage_type_create_works_for_custom_path") - }) - .to_string(); - - _cleanup_custom_path("sqlite_storage_type_create_works_for_custom_path"); - let storage_type = SQLiteStorageType::new(); - - storage_type - .create_storage( - "sqlite_storage_type_create_works_for_custom_path", - Some(&config), - None, - &_metadata(), - ) - .await - .unwrap(); - - storage_type - .delete_storage( - "sqlite_storage_type_create_works_for_custom_path", - Some(&config), - None, - ) - .await - .unwrap(); - - _cleanup_custom_path("sqlite_storage_type_create_works_for_custom_path"); - _cleanup("sqlite_storage_type_create_works_for_custom_path"); - } - - fn _cleanup_custom_path(custom_path: &str) { - let my_path = _custom_path(custom_path); - let path = Path::new(&my_path); - if path.exists() { - fs::remove_dir_all(path).unwrap(); - } - } - - #[async_std::test] - async fn sqlite_storage_type_create_works_for_twice() { - _cleanup("sqlite_storage_type_create_works_for_twice"); - - let storage_type = SQLiteStorageType::new(); - storage_type - .create_storage( - "sqlite_storage_type_create_works_for_twice", - None, - None, - &_metadata(), - ) - .await - .unwrap(); - - let res = storage_type - .create_storage( - "sqlite_storage_type_create_works_for_twice", - None, - None, - &_metadata(), - ) - .await; - - assert_kind!(IndyErrorKind::WalletAlreadyExists, res); - - storage_type - .delete_storage("sqlite_storage_type_create_works_for_twice", None, None) - .await - .unwrap(); - } - - #[async_std::test] - async fn sqlite_storage_get_storage_metadata_works() { - _cleanup("sqlite_storage_get_storage_metadata_works"); - - { - let storage = _storage("sqlite_storage_get_storage_metadata_works").await; - let metadata = storage.get_storage_metadata().await.unwrap(); - - assert_eq!(metadata, _metadata()); - } - - _cleanup("sqlite_storage_get_storage_metadata_works"); - } - - #[async_std::test] - async fn sqlite_storage_type_delete_works() { - _cleanup("sqlite_storage_type_delete_works"); - - let storage_type = SQLiteStorageType::new(); - storage_type - .create_storage("sqlite_storage_type_delete_works", None, None, &_metadata()) - .await - .unwrap(); - - storage_type - .delete_storage("sqlite_storage_type_delete_works", None, None) - .await - .unwrap(); - } - - #[async_std::test] - async fn sqlite_storage_type_delete_works_for_non_existing() { - _cleanup("sqlite_storage_type_delete_works_for_non_existing"); - - let storage_type = SQLiteStorageType::new(); - - storage_type - .create_storage( - "sqlite_storage_type_delete_works_for_non_existing", - None, - None, - &_metadata(), - ) - .await - .unwrap(); - - let res = storage_type.delete_storage("unknown", None, None).await; - assert_kind!(IndyErrorKind::WalletNotFound, res); - - storage_type - .delete_storage( - "sqlite_storage_type_delete_works_for_non_existing", - None, - None, - ) - .await - .unwrap(); - } - - #[async_std::test] - async fn sqlite_storage_type_open_works() { - _cleanup("sqlite_storage_type_open_works"); - _storage("sqlite_storage_type_open_works").await; - _cleanup("sqlite_storage_type_open_works"); - } - - #[async_std::test] - async fn sqlite_storage_type_open_works_for_custom() { - _cleanup("sqlite_storage_type_open_works_for_custom"); - - let my_path = _custom_path("sqlite_storage_type_open_works_for_custom"); - let path = Path::new(&my_path); - - if path.exists() && path.is_dir() { - fs::remove_dir_all(path).unwrap(); - } - - _storage_custom("sqlite_storage_type_open_works_for_custom").await; - - fs::remove_dir_all(path).unwrap(); - } - - #[async_std::test] - async fn sqlite_storage_type_open_works_for_not_created() { - _cleanup("sqlite_storage_type_open_works_for_not_created"); - - let storage_type = SQLiteStorageType::new(); - - let res = storage_type - .open_storage("unknown", Some("{}"), Some("{}")) - .await; - - assert_kind!(IndyErrorKind::WalletNotFound, res); - } - - #[async_std::test] - async fn sqlite_storage_add_works_for_is_802() { - _cleanup("sqlite_storage_add_works_for_is_802"); - - { - let storage = _storage("sqlite_storage_add_works_for_is_802").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.add(&_type1(), &_id1(), &_value1(), &_tags()).await; - assert_kind!(IndyErrorKind::WalletItemAlreadyExists, res); - - let res = storage.add(&_type1(), &_id1(), &_value1(), &_tags()).await; - assert_kind!(IndyErrorKind::WalletItemAlreadyExists, res); - } - - _cleanup("sqlite_storage_add_works_for_is_802"); - } - - #[async_std::test] - async fn sqlite_storage_set_get_works() { - _cleanup("sqlite_storage_set_get_works"); - - { - let storage = _storage("sqlite_storage_set_get_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - } - - _cleanup("sqlite_storage_set_get_works"); - } - - #[async_std::test] - async fn sqlite_storage_set_get_works_for_custom() { - _cleanup("sqlite_storage_set_get_works_for_custom"); - - let path = _custom_path("sqlite_storage_set_get_works_for_custom"); - let path = Path::new(&path); - - { - let storage = _storage_custom("sqlite_storage_set_get_works_for_custom").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.type_, None); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - } - - fs::remove_dir_all(path).unwrap(); - } - - #[async_std::test] - async fn sqlite_storage_set_get_works_for_twice() { - _cleanup("sqlite_storage_set_get_works_for_twice"); - - { - let storage = _storage("sqlite_storage_set_get_works_for_twice").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.add(&_type1(), &_id1(), &_value2(), &_tags()).await; - assert_kind!(IndyErrorKind::WalletItemAlreadyExists, res); - } - - _cleanup("sqlite_storage_set_get_works_for_twice"); - } - - #[async_std::test] - async fn sqlite_storage_set_get_works_for_reopen() { - _cleanup("sqlite_storage_set_get_works_for_reopen"); - - _storage("sqlite_storage_set_get_works_for_reopen") - .await - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = SQLiteStorageType::new() - .open_storage( - "sqlite_storage_set_get_works_for_reopen", - Some("{}"), - Some("{}"), - ) - .await - .unwrap() - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - - _cleanup("sqlite_storage_set_get_works_for_reopen"); - } - - #[async_std::test] - async fn sqlite_storage_get_works_for_wrong_key() { - _cleanup("sqlite_storage_get_works_for_wrong_key"); - - { - let storage = _storage("sqlite_storage_get_works_for_wrong_key").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage - .get( - &_type1(), - &_id2(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_get_works_for_wrong_key"); - } - - #[async_std::test] - async fn sqlite_storage_delete_works() { - _cleanup("sqlite_storage_delete_works"); - - { - let storage = _storage("sqlite_storage_delete_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - - storage.delete(&_type1(), &_id1()).await.unwrap(); - - let res = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_delete_works"); - } - - #[async_std::test] - async fn sqlite_storage_delete_works_for_non_existing() { - _cleanup("sqlite_storage_delete_works_for_non_existing"); - - { - let storage = _storage("sqlite_storage_delete_works_for_non_existing").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.delete(&_type1(), &_id2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_delete_works_for_non_existing"); - } - - #[async_std::test] - async fn sqlite_storage_delete_returns_error_item_not_found_if_no_such_type() { - _cleanup("sqlite_storage_delete_returns_error_item_not_found_if_no_such_type"); - - { - let storage = - _storage("sqlite_storage_delete_returns_error_item_not_found_if_no_such_type") - .await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.delete(&_type2(), &_id2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_delete_returns_error_item_not_found_if_no_such_type"); - } - - #[async_std::test] - async fn sqlite_storage_get_all_works() { - _cleanup("sqlite_storage_get_all_works"); - - { - let storage = _storage("sqlite_storage_get_all_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - storage - .add(&_type2(), &_id2(), &_value2(), &_tags()) - .await - .unwrap(); - - let mut storage_iterator = storage.get_all().await.unwrap(); - - let record = storage_iterator.next().await.unwrap().unwrap(); - assert_eq!(record.type_.unwrap(), _type1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - - let record = storage_iterator.next().await.unwrap().unwrap(); - assert_eq!(record.type_.unwrap(), _type2()); - assert_eq!(record.value.unwrap(), _value2()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_tags())); - - let record = storage_iterator.next().await.unwrap(); - assert!(record.is_none()); - } - - _cleanup("sqlite_storage_get_all_works"); - } - - #[async_std::test] - async fn sqlite_storage_get_all_works_for_empty() { - _cleanup("sqlite_storage_get_all_works_for_empty"); - - { - let storage = _storage("sqlite_storage_get_all_works_for_empty").await; - let mut storage_iterator = storage.get_all().await.unwrap(); - - let record = storage_iterator.next().await.unwrap(); - assert!(record.is_none()); - } - - _cleanup("sqlite_storage_get_all_works_for_empty"); - } - - #[async_std::test] - async fn sqlite_storage_update_works() { - _cleanup("sqlite_storage_update_works"); - - { - let storage = _storage("sqlite_storage_update_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - storage - .update(&_type1(), &_id1(), &_value2()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value2()); - } - - _cleanup("sqlite_storage_update_works"); - } - - #[async_std::test] - async fn sqlite_storage_update_works_for_non_existing_id() { - _cleanup("sqlite_storage_update_works_for_non_existing_id"); - - { - let storage = _storage("sqlite_storage_update_works_for_non_existing_id").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - let res = storage.update(&_type1(), &_id2(), &_value2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_update_works_for_non_existing_id"); - } - - #[async_std::test] - async fn sqlite_storage_update_works_for_non_existing_type() { - _cleanup("sqlite_storage_update_works_for_non_existing_type"); - - { - let storage = _storage("sqlite_storage_update_works_for_non_existing_type").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - let res = storage.update(&_type2(), &_id1(), &_value2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_update_works_for_non_existing_type"); - } - - #[async_std::test] - async fn sqlite_storage_add_tags_works() { - _cleanup("sqlite_storage_add_tags_works"); - - { - let storage = _storage("sqlite_storage_add_tags_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - storage - .add_tags(&_type1(), &_id1(), &_new_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - let expected_tags = { - let mut tags = _tags(); - tags.extend(_new_tags()); - _sort(tags) - }; - - assert_eq!(_sort(record.tags.unwrap()), expected_tags); - } - - _cleanup("sqlite_storage_add_tags_works"); - } - - #[async_std::test] - async fn sqlite_storage_add_tags_works_for_non_existing_id() { - _cleanup("sqlite_storage_add_tags_works_for_non_existing_id"); - - { - let storage = _storage("sqlite_storage_add_tags_works_for_non_existing_id").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.add_tags(&_type1(), &_id2(), &_new_tags()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_add_tags_works_for_non_existing_id"); - } - - #[async_std::test] - async fn sqlite_storage_add_tags_works_for_non_existing_type() { - _cleanup("sqlite_storage_add_tags_works_for_non_existing_type"); - - { - let storage = _storage("sqlite_storage_add_tags_works_for_non_existing_type").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.add_tags(&_type2(), &_id1(), &_new_tags()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_add_tags_works_for_non_existing_type"); - } - - #[async_std::test] - async fn sqlite_storage_add_tags_works_for_already_existing() { - _cleanup("sqlite_storage_add_tags_works_for_already_existing"); - - { - let storage = _storage("sqlite_storage_add_tags_works_for_already_existing").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let tags_with_existing = { - let mut tags = _tags(); - tags.extend(_new_tags()); - tags - }; - - storage - .add_tags(&_type1(), &_id1(), &tags_with_existing) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - let expected_tags = { - let mut tags = _tags(); - tags.extend(_new_tags()); - _sort(tags) - }; - - assert_eq!(_sort(record.tags.unwrap()), expected_tags); - } - - _cleanup("sqlite_storage_add_tags_works_for_already_existing"); - } - - #[async_std::test] - async fn sqlite_storage_update_tags_works() { - _cleanup("sqlite_storage_update_tags_works"); - - { - let storage = _storage("sqlite_storage_update_tags_works").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - storage - .update_tags(&_type1(), &_id1(), &_new_tags()) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(_sort(record.tags.unwrap()), _sort(_new_tags())); - } - - _cleanup("sqlite_storage_update_tags_works"); - } - - #[async_std::test] - async fn sqlite_storage_update_tags_works_for_non_existing_id() { - _cleanup("sqlite_storage_update_tags_works_for_non_existing_id"); - - { - let storage = _storage("sqlite_storage_update_tags_works_for_non_existing_id").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.update_tags(&_type1(), &_id2(), &_new_tags()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_update_tags_works_for_non_existing_id"); - } - - #[async_std::test] - async fn sqlite_storage_update_tags_works_for_non_existing_type() { - _cleanup("sqlite_storage_update_tags_works_for_non_existing_type"); - - { - let storage = _storage("sqlite_storage_update_tags_works_for_non_existing_type").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let res = storage.update_tags(&_type1(), &_id2(), &_new_tags()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_update_tags_works_for_non_existing_type"); - } - - #[async_std::test] - async fn sqlite_storage_update_tags_works_for_already_existing() { - _cleanup("sqlite_storage_update_tags_works_for_already_existing"); - { - let storage = _storage("sqlite_storage_update_tags_works_for_already_existing").await; - - storage - .add(&_type1(), &_id1(), &_value1(), &_tags()) - .await - .unwrap(); - - let tags_with_existing = { - let mut tags = _tags(); - tags.extend(_new_tags()); - tags - }; - - storage - .update_tags(&_type1(), &_id1(), &tags_with_existing) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.value.unwrap(), _value1()); - - let expected_tags = { - let mut tags = _tags(); - tags.extend(_new_tags()); - _sort(tags) - }; - - assert_eq!(_sort(record.tags.unwrap()), expected_tags); - } - _cleanup("sqlite_storage_update_tags_works_for_already_existing"); - } - - #[async_std::test] - async fn sqlite_storage_delete_tags_works() { - _cleanup("sqlite_storage_delete_tags_works"); - - { - let storage = _storage("sqlite_storage_delete_tags_works").await; - - let tag_name1 = vec![0, 0, 0]; - let tag_name2 = vec![1, 1, 1]; - let tag_name3 = vec![2, 2, 2]; - let tag1 = Tag::Encrypted(tag_name1.clone(), vec![0, 0, 0]); - let tag2 = Tag::PlainText(tag_name2.clone(), "tag_value_2".to_string()); - let tag3 = Tag::Encrypted(tag_name3.clone(), vec![2, 2, 2]); - let tags = vec![tag1.clone(), tag2.clone(), tag3.clone()]; - - storage - .add(&_type1(), &_id1(), &_value1(), &tags) - .await - .unwrap(); - - let tag_names = vec![ - TagName::OfEncrypted(tag_name1.clone()), - TagName::OfPlain(tag_name2.clone()), - ]; - - storage - .delete_tags(&_type1(), &_id1(), &tag_names) - .await - .unwrap(); - - let record = storage - .get( - &_type1(), - &_id1(), - r##"{"retrieveType": false, "retrieveValue": true, "retrieveTags": true}"##, - ) - .await - .unwrap(); - - assert_eq!(record.tags.unwrap(), vec![tag3]); - } - - _cleanup("sqlite_storage_delete_tags_works"); - } - - #[async_std::test] - async fn sqlite_storage_delete_tags_works_for_non_existing_type() { - _cleanup("sqlite_storage_delete_tags_works_for_non_existing_type"); - - { - let storage = _storage("sqlite_storage_delete_tags_works_for_non_existing_type").await; - - let tag_name1 = vec![0, 0, 0]; - let tag_name2 = vec![1, 1, 1]; - let tag_name3 = vec![2, 2, 2]; - let tag1 = Tag::Encrypted(tag_name1.clone(), vec![0, 0, 0]); - let tag2 = Tag::PlainText(tag_name2.clone(), "tag_value_2".to_string()); - let tag3 = Tag::Encrypted(tag_name3.clone(), vec![2, 2, 2]); - let tags = vec![tag1.clone(), tag2.clone(), tag3.clone()]; - - storage - .add(&_type1(), &_id1(), &_value1(), &tags) - .await - .unwrap(); - - let tag_names = vec![ - TagName::OfEncrypted(tag_name1.clone()), - TagName::OfPlain(tag_name2.clone()), - ]; - - let res = storage.delete_tags(&_type2(), &_id1(), &tag_names).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_delete_tags_works_for_non_existing_type"); - } - - #[async_std::test] - async fn sqlite_storage_delete_tags_works_for_non_existing_id() { - _cleanup("sqlite_storage_delete_tags_works_for_non_existing_id"); - - { - let storage = _storage("sqlite_storage_delete_tags_works_for_non_existing_id").await; - - let tag_name1 = vec![0, 0, 0]; - let tag_name2 = vec![1, 1, 1]; - let tag_name3 = vec![2, 2, 2]; - let tag1 = Tag::Encrypted(tag_name1.clone(), vec![0, 0, 0]); - let tag2 = Tag::PlainText(tag_name2.clone(), "tag_value_2".to_string()); - let tag3 = Tag::Encrypted(tag_name3.clone(), vec![2, 2, 2]); - let tags = vec![tag1.clone(), tag2.clone(), tag3.clone()]; - - storage - .add(&_type1(), &_id1(), &_value1(), &tags) - .await - .unwrap(); - - let tag_names = vec![ - TagName::OfEncrypted(tag_name1.clone()), - TagName::OfPlain(tag_name2.clone()), - ]; - - let res = storage.delete_tags(&_type1(), &_id2(), &tag_names).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - } - - _cleanup("sqlite_storage_delete_tags_works_for_non_existing_id"); - } - - fn _cleanup(name: &str) { - test::cleanup_storage(name) - } - - async fn _storage(name: &str) -> Box { - let storage_type = SQLiteStorageType::new(); - - storage_type - .create_storage(name, None, None, &_metadata()) - .await - .unwrap(); - - storage_type.open_storage(name, None, None).await.unwrap() - } - - async fn _storage_custom(name: &str) -> Box { - let storage_type = SQLiteStorageType::new(); - - let config = json!({ "path": _custom_path(name) }).to_string(); - - storage_type - .create_storage(name, Some(&config), None, &_metadata()) - .await - .unwrap(); - - storage_type - .open_storage(name, Some(&config), None) - .await - .unwrap() - } - - fn _metadata() -> Vec { - return vec![ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, - 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, - 3, 4, 5, 6, 7, 8, - ]; - } - - fn _type(i: u8) -> Vec { - vec![i, 1 + i, 2 + i] - } - - fn _type1() -> Vec { - _type(1) - } - - fn _type2() -> Vec { - _type(2) - } - - fn _id(i: u8) -> Vec { - vec![3 + i, 4 + i, 5 + i] - } - - fn _id1() -> Vec { - _id(1) - } - - fn _id2() -> Vec { - _id(2) - } - - fn _value(i: u8) -> EncryptedValue { - EncryptedValue { - data: vec![6 + i, 7 + i, 8 + i], - key: vec![9 + i, 10 + i, 11 + i], - } - } - - fn _value1() -> EncryptedValue { - _value(1) - } - - fn _value2() -> EncryptedValue { - _value(2) - } - - fn _tags() -> Vec { - let mut tags: Vec = Vec::new(); - tags.push(Tag::Encrypted(vec![1, 5, 8], vec![3, 5, 6])); - tags.push(Tag::PlainText(vec![1, 5, 8, 1], "Plain value".to_string())); - tags - } - - fn _new_tags() -> Vec { - vec![ - Tag::Encrypted(vec![1, 1, 1], vec![2, 2, 2]), - Tag::PlainText(vec![1, 1, 1], String::from("tag_value_3")), - ] - } - - fn _sort(mut v: Vec) -> Vec { - v.sort(); - v - } - - fn _custom_path(name: &str) -> String { - let mut path = environment::tmp_path(); - path.push(name); - path.to_str().unwrap().to_owned() - } -} diff --git a/libvdrtools/indy-wallet/src/storage/default/query.rs b/libvdrtools/indy-wallet/src/storage/default/query.rs index e085b5bd9b..e2ad6f5b79 100644 --- a/libvdrtools/indy-wallet/src/storage/default/query.rs +++ b/libvdrtools/indy-wallet/src/storage/default/query.rs @@ -395,37 +395,3 @@ fn join_operators<'a>( } Ok(s) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn simple_and() { - let condition_1 = Operator::And(vec![ - Operator::Eq( - TagName::EncryptedTagName(vec![1, 2, 3]), - TargetValue::Encrypted(vec![4, 5, 6]), - ), - Operator::Eq( - TagName::PlainTagName(vec![7, 8, 9]), - TargetValue::Unencrypted("spam".to_string()), - ), - ]); - - let condition_2 = Operator::And(vec![ - Operator::Eq( - TagName::EncryptedTagName(vec![10, 11, 12]), - TargetValue::Encrypted(vec![13, 14, 15]), - ), - Operator::Not(Box::new(Operator::Eq( - TagName::PlainTagName(vec![16, 17, 18]), - TargetValue::Unencrypted("eggs".to_string()), - ))), - ]); - - let query = Operator::Or(vec![condition_1, condition_2]); - let class = vec![100, 100, 100]; - let (_query, _arguments) = wql_to_sql(&class, &query, None).unwrap(); - } -} diff --git a/libvdrtools/indy-wallet/src/storage/mysql/query.rs b/libvdrtools/indy-wallet/src/storage/mysql/query.rs index 34ab6c358a..de8c2b21df 100644 --- a/libvdrtools/indy-wallet/src/storage/mysql/query.rs +++ b/libvdrtools/indy-wallet/src/storage/mysql/query.rs @@ -317,39 +317,3 @@ impl ToPlain for TargetValue { } } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn simple_and() { - let condition_1 = Operator::And(vec![ - Operator::Eq( - TagName::EncryptedTagName(vec![1, 2, 3]), - TargetValue::Encrypted(vec![4, 5, 6]), - ), - Operator::Eq( - TagName::PlainTagName(vec![7, 8, 9]), - TargetValue::Unencrypted("spam".to_string()), - ), - ]); - - let condition_2 = Operator::And(vec![ - Operator::Eq( - TagName::EncryptedTagName(vec![10, 11, 12]), - TargetValue::Encrypted(vec![13, 14, 15]), - ), - Operator::Not(Box::new(Operator::Eq( - TagName::PlainTagName(vec![16, 17, 18]), - TargetValue::Unencrypted("eggs".to_string()), - ))), - ]); - - let query = Operator::Or(vec![condition_1, condition_2]); - let class = [100, 100, 100]; - let options = SearchOptions::default(); - - let (_query, _arguments) = wql_to_sql(1_i64, &class, &query, &options).unwrap(); - } -} diff --git a/libvdrtools/indy-wallet/src/wallet.rs b/libvdrtools/indy-wallet/src/wallet.rs index ba603ebf92..0629ae8f03 100644 --- a/libvdrtools/indy-wallet/src/wallet.rs +++ b/libvdrtools/indy-wallet/src/wallet.rs @@ -471,3514 +471,3 @@ impl Drop for Wallet { self.close().unwrap(); //FIXME pass the error to the API cb } } - -#[cfg(test)] -mod tests { - use super::*; - - use std::collections::HashMap; - - use indy_utils::{assert_kind, assert_match, test}; - use serde_json::json; - - use crate::{ - encryption, - language::*, - storage::{default::SQLiteStorageType, WalletStorageType}, - wallet::Wallet, - Metadata, MetadataArgon, - }; - use storage::mysql::MySqlStorageType; - - macro_rules! jsonstr { - ($($x:tt)+) => { - json!($($x)+).to_string() - } - } - - macro_rules! jsonmap { - ($($x:tt)+) => { - { - let map: ::std::collections::HashMap = serde_json::from_value(json!($($x)+)).unwrap(); - map - } - } - } - - #[async_std::test] - async fn wallet_get_id_works() { - test::cleanup_wallet("wallet_get_id_works"); - - { - let mut wallet = _wallet("wallet_get_id_works").await; - assert_eq!(wallet.get_id(), "wallet_get_id_works"); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_get_id_works"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_get_id_works_for_mysql() { - _mysql_cleanup_wallet("wallet_get_id_works_for_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_get_id_works_for_mysql").await; - assert_eq!(wallet.get_id(), "wallet_get_id_works_for_mysql"); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_get_id_works_for_mysql").await; - } - - #[async_std::test] - async fn wallet_add_get_works() { - test::cleanup_wallet("wallet_add_get_works"); - - { - let mut wallet = _wallet("wallet_add_get_works").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.tags.unwrap(), _tags()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_add_get_works"); - } - - #[async_std::test] - async fn wallet_add_get_works_for_reopen() { - test::cleanup_wallet("wallet_add_get_works_for_reopen"); - - { - let mut wallet = _wallet("wallet_add_get_works_for_reopen").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet.close().unwrap(); - - let mut wallet = _exists_wallet("wallet_add_get_works_for_reopen").await; - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.tags.unwrap(), _tags()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_add_get_works_for_reopen"); - } - - #[async_std::test] - async fn wallet_get_works_for_non_existing() { - test::cleanup_wallet("wallet_get_works_for_non_existing"); - - { - let mut wallet = _wallet("wallet_get_works_for_non_existing").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet - .get( - _type1(), - _id2(), - &_fetch_options(false, true, true), - &metrics, - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_get_works_for_non_existing"); - } - - #[async_std::test] - async fn wallet_add_works_for_already_existing() { - test::cleanup_wallet("wallet_add_works_for_already_existing"); - - { - let mut wallet = _wallet("wallet_add_works_for_already_existing").await; - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet.add(_type1(), _id1(), _value2(), &_tags()).await; - assert_kind!(IndyErrorKind::WalletItemAlreadyExists, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_add_works_for_already_existing"); - } - - #[async_std::test] - async fn wallet_update_works() { - test::cleanup_wallet("wallet_update_works"); - { - let mut wallet = _wallet("wallet_update_works").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.tags.unwrap(), _tags()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.update(_type1(), _id1(), _value2()).await.unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value2()); - assert_eq!(record.tags.unwrap(), _tags()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 2 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_update_works"); - } - - #[async_std::test] - async fn wallet_update_works_for_non_existing_id() { - test::cleanup_wallet("wallet_update_works_for_non_existing_id"); - - { - let mut wallet = _wallet("wallet_update_works_for_non_existing_id").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet.update(_type1(), _id2(), _value2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_update_works_for_non_existing_id"); - } - - #[async_std::test] - async fn wallet_update_works_for_non_existing_type() { - test::cleanup_wallet("wallet_update_works_for_non_existing_type"); - - { - let mut wallet = _wallet("wallet_update_works_for_non_existing_type").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet.update(_type2(), _id1(), _value2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_update_works_for_non_existing_type"); - } - - /** - * Add tags tests - */ - - #[async_std::test] - async fn wallet_add_tags_works() { - test::cleanup_wallet("wallet_add_tags_works"); - - { - let tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - }); - - let mut wallet = _wallet("wallet_add_tags_works").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let new_tags = jsonmap!({ - "tag_name_2": "tag_value_2", - "~tag_name_3": "~tag_value_3", - }); - - wallet.add_tags(_type1(), _id1(), &new_tags).await.unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - let expected_tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_3": "~tag_value_3", - }); - - assert_eq!(record.tags.unwrap(), expected_tags); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_add_tags_works"); - } - - #[async_std::test] - async fn wallet_update_tags_works() { - test::cleanup_wallet("wallet_update_tags_works"); - - { - let tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - }); - - let mut wallet = _wallet("wallet_update_tags_works").await; - let metrics = WalletCacheHitMetrics::new(); - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let new_tags = jsonmap!({ - "tag_name_2": "tag_value_2", - "~tag_name_3": "~tag_value_3", - }); - - wallet - .update_tags(_type1(), _id1(), &new_tags) - .await - .unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.tags.unwrap(), new_tags); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_update_tags_works"); - } - - #[async_std::test] - async fn wallet_delete_tags_works() { - test::cleanup_wallet("wallet_delete_tags_works"); - - { - let tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_3": "~tag_value_3", - "~tag_name_4": "~tag_value_4", - }); - - let mut wallet = _wallet("wallet_delete_tags_works").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - wallet - .delete_tags( - _type1(), - _id1(), - &vec!["tag_name_1", "~tag_name_3", "tag_name_5", "~tag_name_6"], - ) - .await - .unwrap(); - - let expected_tags = jsonmap!({ - "tag_name_2": "tag_value_2", - "~tag_name_4": "~tag_value_4", - }); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.tags.unwrap(), expected_tags); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_delete_tags_works"); - } - - #[async_std::test] - async fn wallet_delete_works() { - test::cleanup_wallet("wallet_delete_works"); - - { - let mut wallet = _wallet("wallet_delete_works").await; - let metrics = WalletCacheHitMetrics::new(); - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let record = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await - .unwrap(); - - assert_eq!(record.id, _id1()); - assert_eq!(record.value.unwrap(), _value1()); - assert_eq!(record.tags.unwrap(), _tags()); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 1 - ); - - wallet.delete(_type1(), _id1()).await.unwrap(); - - let res = wallet - .get( - _type1(), - _id1(), - &_fetch_options(false, true, true), - &metrics, - ) - .await; - - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - assert_eq!( - metrics - .get_data_for_type(&_type1()) - .await - .unwrap() - .get_not_cached(), - 2 - ); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_delete_works"); - } - - #[async_std::test] - async fn wallet_delete_works_for_non_existing_id() { - test::cleanup_wallet("wallet_delete_works_for_non_existing_id"); - - { - let mut wallet = _wallet("wallet_delete_works_for_non_existing_id").await; - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet.delete(_type1(), _id2()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_delete_works_for_non_existing_id"); - } - - #[async_std::test] - async fn wallet_delete_works_for_non_existing_type() { - test::cleanup_wallet("wallet_delete_works_for_non_existing_type"); - - { - let mut wallet = _wallet("wallet_delete_works_for_non_existing_type").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let res = wallet.delete(_type2(), _id1()).await; - assert_kind!(IndyErrorKind::WalletItemNotFound, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_delete_works_for_non_existing_type"); - } - - #[test] - fn language_parse_from_json_ecrypt_query_works() { - test::cleanup_wallet("language_parse_from_json_ecrypt_query_works"); - - { - let query = jsonstr!({ - "k1": "v1", - "$or": [ - { - "~k2": {"$like": "like_target"}, - "~k3": {"$gte": "100"}, - "$not": { - "k4": "v4", - "~k5": { - "$like": "like_string" - }, - }, - }, - { - "k6": {"$in": ["in_string_1", "in_string_2"]}, - } - ], - "$not": { - "$not": { - "$not": { - "$not": { - "k7": "v7" - } - } - } - }, - "$not": { - "k8": "v8" - } - }); - - let query = serde_json::from_str(&query).unwrap(); - let encrypted_query = encrypt_query(query, &Keys::new()).unwrap(); - - assert_match!(Operator::And(_), encrypted_query); - } - - test::cleanup_wallet("language_parse_from_json_ecrypt_query_works"); - } - - #[async_std::test] - async fn wallet_search_works_for_empty_query() { - test::cleanup_wallet("wallet_search_works_for_empty_query"); - - { - let mut wallet = _wallet("wallet_search_works_for_empty_query").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &_tags()) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - "{}", - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - type_: None, - }, - WalletRecord { - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - type_: None, - }, - ]); - - let records = _fetch_all(&mut iterator).await; - assert_eq!(records, expected_records); - - let total_count = iterator.get_total_count().unwrap(); - assert!(total_count.is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_empty_query"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_empty_query_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_empty_query_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_empty_query_mysql").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &_tags()) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - "{}", - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - type_: None, - }, - WalletRecord { - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - type_: None, - }, - ]); - - let records = _fetch_all(&mut iterator).await; - assert_eq!(records, expected_records); - - let total_count = iterator.get_total_count().unwrap(); - assert!(total_count.is_none()); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_empty_query_mysql").await; - } - - #[async_std::test] - async fn wallet_search_works_for_empty_query_with_count() { - test::cleanup_wallet("wallet_search_works_for_empty_query_with_count"); - - { - let mut wallet = _wallet("wallet_search_works_for_empty_query_with_count").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &_tags()) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - "{}", - Some(&_search_options(true, true, true, true, true)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: Some(_tags()), - type_: Some(_type1().to_string()), - }, - WalletRecord { - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: Some(_tags()), - type_: Some(_type1().to_string()), - }, - ]); - - let records = _fetch_all(&mut iterator).await; - assert_eq!(records, expected_records); - - let total_count = iterator.get_total_count().unwrap().unwrap(); - assert_eq!(total_count, 2); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_empty_query_with_count"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_empty_query_with_count_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_empty_query_with_count_mysql").await; - - { - let mut wallet = - _mysql_wallet("wallet_search_works_for_empty_query_with_count_mysql").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &_tags()) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - "{}", - Some(&_search_options(true, true, true, true, true)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: Some(_tags()), - type_: Some(_type1().to_string()), - }, - WalletRecord { - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: Some(_tags()), - type_: Some(_type1().to_string()), - }, - ]); - - let records = _fetch_all(&mut iterator).await; - assert_eq!(records, expected_records); - - let total_count = iterator.get_total_count().unwrap().unwrap(); - assert_eq!(total_count, 2); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_empty_query_with_count_mysql").await; - } - - #[async_std::test] - async fn wallet_search_works_for_empty_query_with_only_count() { - test::cleanup_wallet("wallet_search_works_for_empty_query_with_only_count"); - { - let mut wallet = _wallet("wallet_search_works_for_empty_query_with_only_count").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &_tags()) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - "{}", - Some(&_search_options(false, true, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert_eq!(iterator.get_total_count().unwrap().unwrap(), 2); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_empty_query_with_only_count"); - } - - #[async_std::test] - async fn wallet_search_works_for_eq_encrypted() { - test::cleanup_wallet("wallet_search_works_for_eq_encrypted"); - { - let mut wallet = _wallet("wallet_search_works_for_eq_encrypted").await; - - let tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_2", - }); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with different tag name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_3": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with different tag value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": "tag_value_2"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); // FIXIME: Don't use complex calls inside macro. Use variable before. - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search equal name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_eq_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_empty_tag_plain() { - test::cleanup_wallet("wallet_search_works_for_empty_tag_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_empty_tag_plain").await; - - wallet - .add(_type1(), _id1(), _type1(), &_tags()) - .await - .unwrap(); - - let res = wallet - .search( - _type1(), - &jsonstr!({ - "tag1": "tag2", - "~": "tag3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_empty_tag_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_empty_tag_encrypted() { - test::cleanup_wallet("wallet_search_works_for_empty_tag_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_empty_tag_encrypted").await; - - wallet - .add(_type1(), _id1(), _type1(), &_tags()) - .await - .unwrap(); - - let res = wallet - .search( - _type1(), - &jsonstr!({ - "tag1": "tag2", - "": "tag3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_empty_tag_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_eq_plan() { - test::cleanup_wallet("wallet_search_works_for_eq_plan"); - { - let mut wallet = _wallet("wallet_search_works_for_eq_plan").await; - - let tags = jsonmap!({ - "~tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - }); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with different tag name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_3": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with different tag value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": "tag_value_2"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search equal name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": "tag_value_1"}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_eq_plan"); - } - - // neq tests // - - #[async_std::test] - async fn wallet_search_works_for_neq_encrypted() { - test::cleanup_wallet("wallet_search_works_for_neq_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_neq_encrypted").await; - - let tags = jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_2", - }); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with matching value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": {"$neq": "tag_value_1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with neq value and neq name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_different": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search with different type - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_neq_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_neq_plain() { - test::cleanup_wallet("wallet_search_works_for_neq_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_neq_plain").await; - - let tags = jsonmap!({ - "~tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - }); - - wallet - .add(_type1(), _id1(), _value1(), &tags) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with matching value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_1": {"$neq": "tag_value_1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with neq value and neq name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search with different type - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_1": {"$neq": "tag_value_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_neq_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_gt_plain() { - test::cleanup_wallet("wallet_search_works_for_gt_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_gt_plain").await; - - wallet - .add(_type1(), _id1(), _value1(), &jsonmap!({"~tag_name":"1"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &jsonmap!({"~tag_name":"2"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id3(), _value3(), &jsonmap!({"~tag_name":"3"})) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$gt": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$gt": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$gt": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$gt": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_gt_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_gt_encrypted() { - test::cleanup_wallet("wallet_search_works_for_gt_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_gt_encrypted").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$gt": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_gt_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_gte_plain() { - test::cleanup_wallet("wallet_search_works_for_gte_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_gte_plain").await; - - wallet - .add(_type1(), _id1(), _value1(), &jsonmap!({"~tag_name":"1"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &jsonmap!({"~tag_name":"2"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id3(), _value3(), &jsonmap!({"~tag_name":"3"})) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$gte": "2"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$gte": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$gte": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$gte": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_gte_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_gte_encrypted() { - test::cleanup_wallet("wallet_search_works_for_gte_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_gte_encrypted").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$gte": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_gte_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_lt_plain() { - test::cleanup_wallet("wallet_search_works_for_lt_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_lt_plain").await; - - wallet - .add(_type1(), _id1(), _value1(), &jsonmap!({"~tag_name":"2"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &jsonmap!({"~tag_name":"3"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id3(), _value3(), &jsonmap!({"~tag_name":"4"})) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lt": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lt": "2"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$lt": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$lt": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_lt_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_lt_encrypted() { - test::cleanup_wallet("wallet_search_works_for_lt_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_lt_encrypted").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$lt": "4"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_lt_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_lte_plain() { - test::cleanup_wallet("wallet_search_works_for_lte_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_lte_plain").await; - - wallet - .add(_type1(), _id1(), _value1(), &jsonmap!({"~tag_name":"2"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &jsonmap!({"~tag_name":"3"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id3(), _value3(), &jsonmap!({"~tag_name":"4"})) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lte": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_lte_plain"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_lte_plain_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_lte_plain_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_lte_plain_mysql").await; - - wallet - .add(_type1(), _id1(), _value1(), &jsonmap!({"~tag_name":"2"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id2(), _value2(), &jsonmap!({"~tag_name":"3"})) - .await - .unwrap(); - - wallet - .add(_type1(), _id3(), _value3(), &jsonmap!({"~tag_name":"4"})) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$lte": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with different type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_lte_plain_mysql").await; - } - - #[async_std::test] - async fn wallet_search_works_for_lte_encrypted() { - test::cleanup_wallet("wallet_search_works_for_lte_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_lte_encrypted").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_lte_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_like_plain() { - test::cleanup_wallet("wallet_search_works_for_like_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_like_plain").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({"~tag_name": "tag_value_1"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({"~tag_name": "tag_value_2"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({"~tag_name": "not_matching"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$like": "tag_value_no_match%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_like_plain"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_lte_encrypted_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_lte_encrypted_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_lte_encrypted_mysql").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$lte": "3"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_lte_encrypted_mysql").await; - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_like_plain_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_like_plain_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_like_plain_mysql").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({"~tag_name": "tag_value_1"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({"~tag_name": "tag_value_2"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({"~tag_name": "not_matching"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$like": "tag_value_no_match%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$like": "tag_value_%"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_like_plain_mysql").await; - } - - #[async_std::test] - async fn wallet_search_works_for_like_encrypted() { - test::cleanup_wallet("wallet_search_works_for_like_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_like_encrypted").await; - - let res = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$like": "1"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await; - - assert_kind!(IndyErrorKind::WalletQueryError, res); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_like_encrypted"); - } - - #[async_std::test] - async fn wallet_search_works_for_in_plain() { - test::cleanup_wallet("wallet_search_works_for_in_plain"); - - { - let mut wallet = _wallet("wallet_search_works_for_in_plain").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({"~tag_name": "tag_value_1"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({"~tag_name": "tag_value_2"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({"~tag_name": "tag_value_3"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$in": ["tag_value_4", "tag_value_5"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting tag - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name_different": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful encrypted search - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"~tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_in_plain"); - } - - #[async_std::test] - async fn wallet_search_works_for_in_encrypted() { - test::cleanup_wallet("wallet_search_works_for_in_encrypted"); - - { - let mut wallet = _wallet("wallet_search_works_for_in_encrypted").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({"tag_name": "tag_value_1"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({"tag_name": "tag_value_2"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({"tag_name": "tag_value_3"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$in": ["tag_value_4", "tag_value_5"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting tag - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_different": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_in_encrypted") - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_in_encrypted_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_in_encrypted_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_in_encrypted_mysql").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({"tag_name": "tag_value_1"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({"tag_name": "tag_value_2"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({"tag_name": "tag_value_3"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name": {"$in": ["tag_value_4", "tag_value_5"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search with nonexisting tag - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"tag_name_different": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful plain search - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"~tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // unsuccessful search wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({"tag_name": {"$in": ["tag_value_1", "tag_value_3"]}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_in_encrypted_mysql").await - } - - #[async_std::test] - async fn wallet_search_works_for_and() { - test::cleanup_wallet("wallet_search_works_for_and"); - { - let mut wallet = _wallet("wallet_search_works_for_and").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_2", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_3", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_2", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_3": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_3": "tag_value_3", - "tag_name_4": "tag_value_4", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong type - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_2", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong tag name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "tag_name_3": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong tag value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_0", - "~tag_name_2": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_and"); - } - - #[async_std::test] - #[cfg(feature = "mysql")] - async fn wallet_search_works_for_and_mysql() { - _mysql_cleanup_wallet("wallet_search_works_for_and_mysql").await; - - { - let mut wallet = _mysql_wallet("wallet_search_works_for_and_mysql").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_2", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_3", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "tag_name_2": "tag_value_2", - "~tag_name_2": "tag_value_2", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_3": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // no matches - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_3": "tag_value_3", - "tag_name_4": "tag_value_4", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong type - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_2", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong tag name - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_1", - "tag_name_3": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // wrong tag value - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_0", - "~tag_name_2": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - _mysql_cleanup_wallet("wallet_search_works_for_and_mysql").await; - } - - #[async_std::test] - async fn wallet_search_works_for_or() { - test::cleanup_wallet("wallet_search_works_for_or"); - - { - let mut wallet = _wallet("wallet_search_works_for_or").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_21", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_22", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_3": "tag_value_3", - "~tag_name_4": "tag_value_4"}), - ) - .await - .unwrap(); - - // All 3 - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "$or": [ - {"tag_name_1": "tag_value_1"}, - {"~tag_name_2": "tag_value_22"}, - {"~tag_name_4": "tag_value_4"} - ] - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // 1 and 3 matching - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "$or": [ - {"~tag_name_2": "tag_value_21"}, - {"~tag_name_4": "tag_value_4"} - ] - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // 3 matching, 1 not because wrong tag type - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "$or": [ - {"tag_name_2": "tag_value_21"}, - {"~tag_name_4": "tag_value_4"} - ] - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - // no matching - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "tag_name_1": "tag_value_0", - "~tag_name_2": "tag_value_3", - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - // no matching - wrong type_ - let mut iterator = wallet - .search( - _type2(), - &jsonstr!({ - "$or": [ - {"tag_name_1": "tag_value_1"}, - {"~tag_name_2": "tag_value_22"}, - {"~tag_name_4": "tag_value_4"} - ] - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_or"); - } - - #[async_std::test] - async fn wallet_search_works_for_not() { - test::cleanup_wallet("wallet_search_works_for_not"); - { - let mut wallet = _wallet("wallet_search_works_for_not").await; - - wallet - .add( - _type1(), - _id1(), - _value1(), - &jsonmap!({ - "tag_name_1": "tag_value_1", - "~tag_name_2": "tag_value_21", - "~tag_name_3": "tag_value_3"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id2(), - _value2(), - &jsonmap!({ - "tag_name_12": "tag_value_12", - "~tag_name_2": "tag_value_22"}), - ) - .await - .unwrap(); - - wallet - .add( - _type1(), - _id3(), - _value3(), - &jsonmap!({ - "tag_name_13": "tag_value_13", - "~tag_name_4": "tag_value_4"}), - ) - .await - .unwrap(); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"$not": {"tag_name_1": "tag_value_1_different"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id2().to_string(), - value: Some(_value2().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({"$not": {"~tag_name_2": "tag_value_22"}}), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = _sort(vec![ - WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }, - WalletRecord { - type_: None, - id: _id3().to_string(), - value: Some(_value3().to_string()), - tags: None, - }, - ]); - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - let mut iterator = wallet - .search( - _type1(), - &jsonstr!({ - "$not": { - "$or": [ - {"tag_name_1": "tag_value_1"}, - {"~tag_name_2": "tag_value_22"}, - {"~tag_name_4": "tag_value_4"}, - ] - } - }), - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - assert!(iterator.next().await.unwrap().is_none()); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - test::cleanup_wallet("wallet_search_works_for_not"); - } - - #[async_std::test] - async fn wallet_search_works_for_nested() { - test::cleanup_wallet("wallet_search_works_for_nested"); - { - let mut wallet = _wallet("wallet_search_works_for_nested").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let query = jsonstr!({ - "$or": [ - {"foo": "bar"}, - {"$not": { - "$not": { - "$not": { - "$not": { - "k7": "v7" - } - } - } - }, - "$not": { - "k8": "v8" - } - } - ] - }); - - let mut iterator = wallet - .search( - _type1(), - &query, - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_nested"); - } - - #[async_std::test] - async fn wallet_search_works_for_nested_empty() { - test::cleanup_wallet("wallet_search_works_for_nested_empty"); - - { - let mut wallet = _wallet("wallet_search_works_for_nested_empty").await; - - wallet - .add(_type1(), _id1(), _value1(), &_tags()) - .await - .unwrap(); - - let query = json!({ - "$and": [ - { - "$or": [] - }, - { - "$or": [] - } - ] - }) - .to_string(); - - let mut iterator = wallet - .search( - _type1(), - &query, - Some(&_search_options(true, false, false, true, false)), - ) - .await - .unwrap(); - - let expected_records = vec![WalletRecord { - type_: None, - id: _id1().to_string(), - value: Some(_value1().to_string()), - tags: None, - }]; - - assert_eq!(_fetch_all(&mut iterator).await, expected_records); - assert!(iterator.get_total_count().unwrap().is_none()); - - wallet.close().unwrap(); - } - - test::cleanup_wallet("wallet_search_works_for_nested_empty"); - } - - fn _type1() -> &'static str { - "type1" - } - - fn _type2() -> &'static str { - "type2" - } - - fn _id1() -> &'static str { - "id1" - } - - fn _id2() -> &'static str { - "id2" - } - - fn _id3() -> &'static str { - "id3" - } - - fn _value1() -> &'static str { - "value1" - } - - fn _value2() -> &'static str { - "value2" - } - - fn _value3() -> &'static str { - "value3" - } - - fn _tags() -> HashMap { - jsonmap!({"tag1": "tag_value_1"}) - } - - fn _wallet_id() -> &'static str { - "w1" - } - - async fn _wallet(name: &str) -> Wallet { - let storage_type = SQLiteStorageType::new(); - let master_key = _master_key(); - - let keys = Keys::new(); - - let metadata = { - let master_key_salt = encryption::gen_master_key_salt().unwrap(); - - let metadata = Metadata::MetadataArgon(MetadataArgon { - master_key_salt: master_key_salt[..].to_vec(), - keys: keys.serialize_encrypted(&master_key).unwrap(), - }); - - serde_json::to_vec(&metadata).unwrap() - }; - - storage_type - .create_storage(name, None, None, &metadata) - .await - .unwrap(); - - let storage = storage_type.open_storage(name, None, None).await.unwrap(); - - Wallet::new( - name.to_string(), - storage, - Arc::new(keys), - WalletCache::new(None), - ) - } - - async fn _mysql_wallet(name: &str) -> Wallet { - let storage_type = MySqlStorageType::new(); - let master_key = _master_key(); - - let keys = Keys::new(); - - let metadata = { - let master_key_salt = encryption::gen_master_key_salt().unwrap(); - - let metadata = Metadata::MetadataArgon(MetadataArgon { - master_key_salt: master_key_salt[..].to_vec(), - keys: keys.serialize_encrypted(&master_key).unwrap(), - }); - - serde_json::to_vec(&metadata).unwrap() - }; - - storage_type - .create_storage(name, _mysql_config(), _mysql_credentials(), &metadata) - .await - .unwrap(); - - let storage = storage_type - .open_storage(name, _mysql_config(), _mysql_credentials()) - .await - .unwrap(); - - Wallet::new( - name.to_string(), - storage, - Arc::new(keys), - WalletCache::new(None), - ) - } - - async fn _exists_wallet(name: &str) -> Wallet { - let storage_type = SQLiteStorageType::new(); - let storage = storage_type.open_storage(name, None, None).await.unwrap(); - - let metadata: MetadataArgon = { - let metadata = storage.get_storage_metadata().await.unwrap(); - serde_json::from_slice::(&metadata).unwrap() - }; - - let master_key = _master_key(); - let keys = Keys::deserialize_encrypted(&metadata.keys, &master_key).unwrap(); - - Wallet::new( - name.to_string(), - storage, - Arc::new(keys), - WalletCache::new(None), - ) - } - - fn _master_key() -> chacha20poly1305_ietf::Key { - chacha20poly1305_ietf::Key::new([ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, - 6, 7, 8, - ]) - } - - fn _new_master_key() -> chacha20poly1305_ietf::Key { - chacha20poly1305_ietf::Key::new([ - 2, 2, 3, 4, 5, 6, 7, 8, 2, 2, 3, 4, 5, 6, 7, 8, 2, 2, 3, 4, 5, 6, 7, 8, 2, 2, 3, 4, 5, - 6, 7, 8, - ]) - } - - fn _fetch_options(type_: bool, value: bool, tags: bool) -> String { - json!({ - "retrieveType": type_, - "retrieveValue": value, - "retrieveTags": tags, - }) - .to_string() - } - - fn _search_options( - records: bool, - total_count: bool, - type_: bool, - value: bool, - tags: bool, - ) -> String { - json!({ - "retrieveRecords": records, - "retrieveTotalCount": total_count, - "retrieveType": type_, - "retrieveValue": value, - "retrieveTags": tags, - }) - .to_string() - } - - async fn _fetch_all<'a>(iterator: &mut WalletIterator) -> Vec { - let mut v = Vec::new(); - - loop { - if let Some(record) = iterator.next().await.unwrap() { - v.push(record); - } else { - break; - } - } - - _sort(v) - } - - fn _sort(mut v: Vec) -> Vec { - v.sort(); - v - } - - fn _mysql_config() -> Option<&'static str> { - Some( - r#" - { - "read_host": "127.0.0.1", - "write_host": "127.0.0.1", - "port": 3306, - "db_name": "wallet" - } - "#, - ) - } - - fn _mysql_credentials() -> Option<&'static str> { - Some( - r#" - { - "user": "root", - "pass": "root" - } - "#, - ) - } - - async fn _mysql_cleanup_wallet(name: &str) { - MySqlStorageType::new() - .delete_storage(name, _mysql_config(), _mysql_credentials()) - .await - .ok(); - } -} diff --git a/shared_vcx/src/validation/did.rs b/shared_vcx/src/validation/did.rs index e23c5e8c81..711487b3a6 100644 --- a/shared_vcx/src/validation/did.rs +++ b/shared_vcx/src/validation/did.rs @@ -33,6 +33,7 @@ pub fn validate_did(did: &str) -> SharedVcxResult { #[cfg(test)] mod tests { + use super::*; #[test] #[cfg(feature = "general_test")] diff --git a/shared_vcx/src/validation/verkey.rs b/shared_vcx/src/validation/verkey.rs index 8d358290f1..9005682949 100644 --- a/shared_vcx/src/validation/verkey.rs +++ b/shared_vcx/src/validation/verkey.rs @@ -18,6 +18,7 @@ pub fn validate_verkey(verkey: &str) -> SharedVcxResult { #[cfg(test)] mod tests { + use super::*; #[test] #[cfg(feature = "general_test")]