snapper-box 0.0.4

Cryptographic storage for snapper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
#![doc = include_str!("../README.md")]
#![warn(
    clippy::all,
    clippy::pedantic,
    rust_2018_idioms,
    missing_docs,
    clippy::missing_docs_in_private_items
)]
#![allow(
    clippy::option_if_let_else,
    clippy::module_name_repetitions,
    clippy::shadow_unrelated,
    clippy::must_use_candidate,
    clippy::implicit_hasher
)]
#![doc(
    html_logo_url = "https://gitlab.com/rust-community-matrix/snapper/-/raw/trunk/static/snapper.png"
)]

use std::{
    collections::HashMap,
    fs::{create_dir_all, File},
    io::Write,
    path::{Path, PathBuf},
    sync::Arc,
};

use serde::{de::DeserializeOwned, Serialize};
use snafu::{ensure, OptionExt, ResultExt};
use tracing::{info, instrument};

use crate::{
    crypto::{DerivedKey, EncryptedRootKey, RootKey},
    entries::{Control, Namespace, Namespaces, Settings},
    error::{
        CryptoBoxError, DirectoryAlreadyExists, DirectoryDoesNotExist, FailedCreatingDirectory,
        Fetch, MissingConfiguration, MissingNamespaceDirectory, NamespaceOpen, RootKeyDecryption,
        RootKeyEncryption, RootKeyIO, RootKeySerial, RootNamespaceInit, RootNamespaceOpen, Store,
    },
    file::LsmFile,
};

#[cfg(feature = "experimental-async")]
pub mod async_wrapper;
pub mod crypto;
mod entries;
pub mod error;
pub mod file;

/// The `CryptoBox` encrypted, namespaced document store
///
/// See module level documentation for more information.
pub struct CryptoBox {
    /// The path for this CryptoBox
    path: PathBuf,
    /// The root key for this `CryptoBox`
    root_key: Arc<RootKey>,
    /// The root namespace for this `CryptoBox`
    root_namespace: LsmFile<File, RootKey>,
    /// Compression level for namespaces in this `CryptoBox`
    compression: Option<i32>,
    /// Maximum cache entries per namespace
    max_cache_entries: Option<usize>,
    /// Namespaces in this CryptoBox
    namespaces: HashMap<String, (Namespace, LsmFile<File, DerivedKey>)>,
}

impl CryptoBox {
    /// Initialize a new `CryptoBox`
    ///
    /// # Arguments
    ///
    ///   * `path`
    ///
    ///     The desired path to the `CryptoBox` directory. This must be a directory that the user
    ///     has write permission to, and _does not_ already exist.
    ///   * `compression`
    ///
    ///     The desired zstd compression level for this `CryptoBox`. Defaults to no compression.
    ///   * `max_cache_entries`
    ///
    ///     The desired maximum number of write-cache entries before a flush automatically occurs.
    ///     Defaults to 100.
    ///   * `password`
    ///
    ///     The password to encrypt the root string with. This is specified as a byte slice for
    ///     flexibility.
    ///
    ///
    ///
    /// # Errors
    ///   * [`CryptoBoxError::DirectoryAlreadyExists`] if the desired path already exists
    ///   * [`CryptoBoxError::FailedCreatingDirectory`], [`CryptoBoxError::RootKeyIO`], or
    ///     [`CryptoBoxError::RootNamespaceInit`] if an IO error creating the `CryptoBox` occurs
    #[instrument(skip(path, password), err)]
    pub fn init(
        path: impl AsRef<Path>,
        compression: Option<i32>,
        max_cache_entries: Option<usize>,
        password: impl AsRef<[u8]>,
    ) -> Result<Self, CryptoBoxError> {
        let path = path.as_ref();
        info!(?path, "Creating CryptoBox");
        let password = password.as_ref();
        // Bailout if the directory already exists
        ensure!(
            !path.exists(),
            DirectoryAlreadyExists {
                directory: format!("{:?}", path)
            }
        );
        // Create the directory
        create_dir_all(path).context(FailedCreatingDirectory {
            directory: format!("{:?}", path),
        })?;
        // Create the namespaces directory
        let namespaces_path = path.join("namespaces");
        create_dir_all(&namespaces_path).context(FailedCreatingDirectory {
            directory: format!("{:?}", namespaces_path),
        })?;
        // Generate the root key
        let root_key = Arc::new(RootKey::random());
        // Encrypt and write the key
        let encrypted_root_key = root_key.encrypt(password).context(RootKeyEncryption)?;
        let root_key_path = path.join("KEY");
        let mut key_file = File::create(&root_key_path).context(RootKeyIO {
            path: format!("{:?}", root_key_path),
        })?;
        serde_cbor::to_writer(&mut key_file, &encrypted_root_key).context(RootKeySerial)?;
        key_file.flush().context(RootKeyIO {
            path: format!("{:?}", root_key_path),
        })?;
        std::mem::drop(key_file);
        // Create root namespace
        let root_namespace_path = path.join("root");
        let mut root_namespace =
            LsmFile::create(&root_namespace_path, None, root_key.clone(), Some(0)).context(
                RootNamespaceInit {
                    path: format!("{:?}", root_namespace_path),
                },
            )?;
        // Write the init
        root_namespace
            .insert(
                &"",
                &Control::Settings(Settings {
                    compression,
                    max_cache_entries,
                }),
            )
            .context(RootNamespaceInit {
                path: format!("{:?}", root_namespace_path),
            })?;
        // Write the namespaces record
        root_namespace
            .insert(
                &"namespaces",
                &Control::Namespaces(Namespaces { namespaces: vec![] }),
            )
            .context(RootNamespaceInit {
                path: format!("{:?}", root_namespace_path),
            })?;
        root_namespace.flush().context(RootNamespaceInit {
            path: format!("{:?}", root_namespace_path),
        })?;

        Ok(CryptoBox {
            path: path.to_path_buf(),
            root_key,
            root_namespace,
            compression,
            namespaces: HashMap::new(),
            max_cache_entries,
        })
    }

    /// Opens an existing `CryptoBox`
    ///
    /// Will read the default compression and cache settings from the root namespace.
    ///
    /// # Arguments
    ///
    ///   * `path`
    ///
    ///     The path to the `CryptoBox` directory.
    ///   * `password`
    ///
    ///     The password to encrypt the root string with. This is specified as a byte slice for
    ///     flexibility.
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::DirectoryDoesNotExist`] if the directory does not exist
    ///   * [`CryptoBoxError::RootKeyDecryption`] if the root key fails to decrypt
    ///   * [`CryptoBoxError::MissingConfiguration`] if the configuration control structure is missing
    ///   * [`CryptoBoxError::RootKeyIO`], [`CryptoBoxError::MissingNamespaceDirectory`], or
    ///     [`CryptoBoxError::RootNamespaceOpen`] if something goes wrong with IO while opening
    #[instrument(skip(path, password), err)]
    pub fn open(
        path: impl AsRef<Path>,
        password: impl AsRef<[u8]>,
    ) -> Result<Self, CryptoBoxError> {
        let path = path.as_ref().to_path_buf();
        let password = password.as_ref();
        info!(?path, "Opening CryptoBox");
        // Make sure the directory exists
        ensure!(
            path.exists() && path.is_dir(),
            DirectoryDoesNotExist {
                path: format!("{:?}", path)
            }
        );
        // Open up the root key and decrypt it
        let root_key_path = path.join("KEY");
        let mut root_key_file = File::open(&root_key_path).context(RootKeyIO {
            path: format!("{:?}", root_key_path),
        })?;
        let enc_root_key: EncryptedRootKey =
            serde_cbor::from_reader(&mut root_key_file).context(RootKeySerial)?;
        let root_key = Arc::new(enc_root_key.decrypt(password).context(RootKeyDecryption)?);
        // Close the file
        std::mem::drop(root_key_file);
        // Open the root namespace
        let root_namespace_path = path.join("root");
        let mut root_namespace =
            LsmFile::open(&root_namespace_path, None, root_key.clone(), Some(0)).context(
                RootNamespaceOpen {
                    path: format!("{:?}", root_namespace_path),
                },
            )?;
        // ensure the namespaces directory exists
        ensure!(path.join("namespaces").exists(), MissingNamespaceDirectory);
        // Get the configuration
        if let Control::Settings(settings) = root_namespace
            .get(&"")
            .ok()
            .context(MissingConfiguration)?
            .context(MissingConfiguration)?
        {
            // Get the namespaces
            if let Control::Namespaces(namespaces_raw) = root_namespace
                .get(&"namespaces")
                .ok()
                .context(MissingConfiguration)?
                .context(MissingConfiguration)?
            {
                let mut namespaces = HashMap::new();
                for namespace in namespaces_raw.namespaces {
                    let name = namespace.name.clone();
                    let path = path.join("namespaces").join(namespace.uuid.to_string());
                    let lsm_file = LsmFile::open(
                        &path,
                        settings.compression,
                        namespace.key.clone(),
                        settings.max_cache_entries,
                    )
                    .context(NamespaceOpen { name: name.clone() })?;
                    namespaces.insert(name, (namespace, lsm_file));
                }
                Ok(CryptoBox {
                    path,
                    root_key,
                    root_namespace,
                    compression: settings.compression,
                    namespaces,
                    max_cache_entries: settings.max_cache_entries,
                })
            } else {
                Err(CryptoBoxError::MissingConfiguration)
            }
        } else {
            Err(CryptoBoxError::MissingConfiguration)
        }
    }

    /// Check to see if a namespace exists
    pub fn namespace_exists(&self, name: &str) -> bool {
        self.namespaces.contains_key(name)
    }

    /// Get the list of namespaces
    pub fn namespaces(&self) -> Vec<String> {
        self.namespaces.keys().cloned().collect()
    }

    /// Create a namespace
    ///
    /// This method will create the namespace with the given name. In the event that the namespace already
    /// exists, this method will sort circuit with `Ok()`.
    ///
    /// Generates a [`Uuid`](uuid::Uuid) and a derived key for the namespace automatically.
    ///
    /// # Errors
    ///
    /// Will return [`CryptoBoxError::NamespaceOpen`] if any underlying errors happen while initializing
    /// the new name space.
    #[instrument(skip(self), err)]
    pub fn create_namespace(&mut self, name: String) -> Result<(), CryptoBoxError> {
        if self.namespace_exists(&name) {
            Ok(())
        } else {
            info!("Creating namespace");
            let derived_key = Arc::new(self.root_key.derive(&name));
            let uuid = uuid::Uuid::new_v4();
            let path = self.path.join("namespaces").join(uuid.to_string());
            // Init lsm file
            let lsm_file = LsmFile::create(
                &path,
                self.compression,
                derived_key.clone(),
                self.max_cache_entries,
            )
            .context(NamespaceOpen { name: name.clone() })?;
            // Build the namespace
            let namespace = Namespace {
                name: name.clone(),
                key: derived_key,
                uuid,
            };
            // Add it to our local store
            self.namespaces.insert(name.clone(), (namespace, lsm_file));
            // Update the namespaces entry
            let namespaces: Vec<_> = self.namespaces.values().map(|(x, _)| x.clone()).collect();
            let namespaces = Control::Namespaces(Namespaces { namespaces });
            self.root_namespace
                .insert(&"namespaces", &namespaces)
                .context(NamespaceOpen { name: name.clone() })?;
            self.root_namespace
                .flush()
                .context(NamespaceOpen { name })?;
            Ok(())
        }
    }

    /// Gets the specified item from the specified namespace, if it exists
    ///
    /// # Arguments
    ///
    ///   * `key` - Key for the key/value pair to be retrieved
    ///   * `namespace` - The namespace the item was from
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::NoSuchNamespace`] if the namespace does not exist
    ///   * [`CryptoBoxError::Fetch`] if an underlying error occurs
    #[instrument(skip(self, key, namespace), err)]
    pub fn get<K, V>(&mut self, key: &K, namespace: &str) -> Result<Option<V>, CryptoBoxError>
    where
        K: Serialize,
        V: DeserializeOwned,
    {
        if let Some((_, lsm)) = self.namespaces.get_mut(namespace) {
            // Try to get the item from the lsm
            lsm.get(key).context(Fetch)
        } else {
            Err(CryptoBoxError::NoSuchNamespace {
                name: namespace.to_string(),
            })
        }
    }

    /// Gets the specified item from the root namespace, if it exists
    ///
    /// # Arguments
    ///
    ///   * `key` - Key for the key/value pair to be retrieved
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::Fetch`] if an underlying error occurs
    #[instrument(skip(self, key), err)]
    pub fn get_root<K, V>(&mut self, key: &K) -> Result<Option<V>, CryptoBoxError>
    where
        K: Serialize,
        V: DeserializeOwned,
    {
        self.root_namespace.get(key).context(Fetch)
    }

    /// Stores the specified item from the specified namespace
    ///
    /// # Arguments
    ///
    ///   * `key` - Key for the key/value pair to be stored
    ///   * `value` - Value for the key/value pair to be stored
    ///   * `namespace` - The namespace the item will be stored in
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::NoSuchNamespace`] if the namespace does not exist
    ///   * [`CryptoBoxError::Store`] if an underlying error occurs
    #[instrument(skip(self, key, value, namespace), err)]
    pub fn insert<K, V>(
        &mut self,
        key: &K,
        value: &V,
        namespace: &str,
    ) -> Result<(), CryptoBoxError>
    where
        K: Serialize,
        V: Serialize,
    {
        if let Some((_, lsm)) = self.namespaces.get_mut(namespace) {
            // Store the key in the lsmfile
            lsm.insert(key, value).context(Store)
        } else {
            Err(CryptoBoxError::NoSuchNamespace {
                name: namespace.to_string(),
            })
        }
    }

    /// Stores the specified item in the root namespace
    ///
    /// # Arguments
    ///
    ///   * `key` - Key for the key/value pair to be stored
    ///   * `value` - Value for the key/value pair to be stored
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::Store`] if an underlying error occurs
    #[instrument(skip(self, key, value), err)]
    pub fn insert_root<K, V>(&mut self, key: &K, value: &V) -> Result<(), CryptoBoxError>
    where
        K: Serialize,
        V: Serialize,
    {
        self.root_namespace.insert(key, value).context(Store)
    }

    /// Returns true if the specified namespace contains the provided key
    ///
    /// # Arguments
    ///
    ///   * `key` - Key for the key/value pair to be retrieved
    ///   * `namespace` - The namespace the item was from
    ///
    /// # Errors
    ///
    ///   * [`CryptoBoxError::NoSuchNamespace`] if the namespace does not exist
    ///   * [`CryptoBoxError::Fetch`] if an underlying error occurs
    #[instrument(skip(self, key, namespace), err)]
    pub fn contains_key<K, V>(&mut self, key: &K, namespace: &str) -> Result<bool, CryptoBoxError>
    where
        K: Serialize,
        V: DeserializeOwned,
    {
        let res = self.get::<K, V>(key, namespace)?;
        Ok(res.is_some())
    }

    /// Will flush all of the namespaces in the `CryptoBox`, including the root namespace.
    ///
    /// # Errors
    ///
    /// Will return [`CryptoBoxError::Flush`] if any underlying errors occur.
    #[instrument(skip(self))]
    pub fn flush(&mut self) -> Result<(), CryptoBoxError> {
        let mut errors = vec![];
        // First try the root namespace
        let res = self.root_namespace.flush();
        if let Err(e) = res {
            errors.push((None, e));
        }
        // Now try all the namespaces
        for (name, (_, lsm)) in &mut self.namespaces {
            if let Err(e) = lsm.flush() {
                errors.push((Some(name.clone()), e));
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(CryptoBoxError::Flush { sources: errors })
        }
    }

    /// Gets all the key/value pairs in a namespace as [`HashMap`]
    ///
    /// # Errors
    ///
    /// Will return [`CryptoBoxError::Fetch`] if there are any underlying errors.
    pub fn to_hashmap<K, V>(&mut self, namespace: &str) -> Result<HashMap<K, V>, CryptoBoxError>
    where
        K: DeserializeOwned + Serialize + std::hash::Hash + Eq,
        V: DeserializeOwned,
    {
        if let Some((_, lsm)) = self.namespaces.get_mut(namespace) {
            // Store the key in the lsmfile
            lsm.to_hashmap().context(Fetch)
        } else {
            Err(CryptoBoxError::NoSuchNamespace {
                name: namespace.to_string(),
            })
        }
    }

    /// Gets all the key/value pairs in a namespace as [`Vec`] of pairs
    ///
    /// # Errors
    ///
    /// Will return [`CryptoBoxError::Fetch`] if there are any underlying errors.
    pub fn to_pairs<K, V>(&mut self, namespace: &str) -> Result<Vec<(K, V)>, CryptoBoxError>
    where
        K: DeserializeOwned + Serialize + Eq + Clone,
        V: DeserializeOwned + Clone,
    {
        if let Some((_, lsm)) = self.namespaces.get_mut(namespace) {
            // Store the key in the lsmfile
            lsm.to_pairs().context(Fetch)
        } else {
            Err(CryptoBoxError::NoSuchNamespace {
                name: namespace.to_string(),
            })
        }
    }

    /// Gets all the key/value pairs in the root namespace as a [`Vec`] of pairs
    ///
    /// # Errors
    ///
    /// Will return [`CryptoBoxError::Fetch`] if there are any underlying errors.
    pub fn root_to_pairs<K, V>(&mut self) -> Result<Vec<(K, V)>, CryptoBoxError>
    where
        K: DeserializeOwned + Serialize + Eq + Clone,
        V: DeserializeOwned + Clone,
    {
        // Store the key in the lsmfile
        self.root_namespace.to_pairs().context(Fetch)
    }
}

/// Unit tests for `CryptoBox`
#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;
    use tempfile::tempdir;
    /// initialization
    mod init {
        use super::*;
        /// Make sure it produces the correct files and directories
        #[test]
        fn directory_layout() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            let _crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            assert!(path.join("KEY").exists());
            assert!(path.join("root").exists());
            assert!(path.join("namespaces").exists());
            Ok(())
        }
        /// Create a box and make sure we can open it back up
        #[test]
        fn init_open() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            // Initialize the box
            let crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Drop it
            std::mem::drop(crypto_box);
            // Open it back up
            let _crypto_box = CryptoBox::open(&path, "testing")?;
            Ok(())
        }
        /// Create a box, add some namespaces, close it, open it, and verify the name spaces
        #[test]
        fn namespaces() -> Result<(), CryptoBoxError> {
            let namespace_names = ["one", "two", "three"]
                .into_iter()
                .map(std::string::ToString::to_string)
                .collect::<HashSet<_>>();
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            // Initialize the box
            let mut crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Add the namespaces
            for namespace in &namespace_names {
                crypto_box.create_namespace(namespace.to_string())?;
            }
            // Drop it
            std::mem::drop(crypto_box);
            // Open it back up
            let crypto_box = CryptoBox::open(&path, "testing")?;
            // Make sure the namespaces match
            assert_eq!(
                namespace_names,
                crypto_box.namespaces().into_iter().collect::<HashSet<_>>()
            );

            Ok(())
        }
    }
    /// Basic functionality aka smoke testing
    mod box_smoke {
        use super::*;
        /// Test basic insertions
        ///
        /// A handful of pre baked insertions into a single namespace, without flushing or reloading
        #[test]
        fn basic_insertions() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            let pairs = [(1, 2), (3, 4), (5, 6)];
            // Initialize the box
            let mut crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Add the empty string namespace
            crypto_box.create_namespace("".to_string())?;
            // do the insertions
            let namespace = "";
            for (key, value) in &pairs {
                crypto_box.insert(key, value, namespace)?;
            }
            // Do the comparison
            for (key, value) in &pairs {
                let res = crypto_box.get(key, namespace)?;
                if Some(*value) != res {
                    panic!("Unable to retrieve pair k: {} v: {}", key, value);
                }
            }
            Ok(())
        }
        /// Test basic insertions with flush
        ///
        /// A handful of pre baked insertions into a single namespace, with a flush and reload
        #[test]
        fn basic_insertions_flush() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            let pairs = [(1, 2), (3, 4), (5, 6)];
            // Initialize the box
            let mut crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Add the empty string namespace
            crypto_box.create_namespace("".to_string())?;
            // do the insertions
            let namespace = "";
            for (key, value) in &pairs {
                crypto_box.insert(key, value, namespace)?;
            }
            // Flush and drop the box
            crypto_box.flush()?;
            std::mem::drop(crypto_box);
            // Reopen the box
            let mut crypto_box = CryptoBox::open(&path, "testing")?;
            // Do the comparison
            for (key, value) in &pairs {
                let res = crypto_box.get(key, namespace)?;
                if Some(*value) != res {
                    panic!("Unable to retrieve pair k: {} v: {}", key, value);
                }
            }
            Ok(())
        }
        /// Test basic insertions with `HashMap` export
        #[test]
        fn basic_insertions_hashmap() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            let pairs = [(1, 2), (3, 4), (5, 6)];
            // Initialize the box
            let mut crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Add the empty string namespace
            crypto_box.create_namespace("".to_string())?;
            // do the insertions
            let namespace = "";
            for (key, value) in &pairs {
                crypto_box.insert(key, value, namespace)?;
            }
            assert_eq!(
                crypto_box.to_hashmap("")?,
                pairs.into_iter().collect::<HashMap<_, _>>()
            );
            Ok(())
        }
        /// Test basic insertions with `pairs` export
        #[test]
        fn basic_insertions_pairs() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            let pairs = [(1, 2), (3, 4), (5, 6)];
            // Initialize the box
            let mut crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Add the empty string namespace
            crypto_box.create_namespace("".to_string())?;
            // do the insertions
            let namespace = "";
            for (key, value) in &pairs {
                crypto_box.insert(key, value, namespace)?;
            }
            let comparison = pairs.into_iter().collect::<HashMap<i32, i32>>();
            assert_eq!(crypto_box.to_hashmap("")?, comparison,);
            assert_eq!(
                crypto_box
                    .to_pairs::<i32, i32>("")?
                    .into_iter()
                    .collect::<HashMap<_, _>>(),
                comparison
            );
            Ok(())
        }
    }
    /// Test failure modes of the box
    mod failures {
        use super::*;
        #[test]
        fn bad_password() -> Result<(), CryptoBoxError> {
            let tempdir = tempdir().context(FailedCreatingDirectory {
                directory: "tempdir".to_string(),
            })?;
            let path = tempdir.path().join("box");
            // Initialize the box
            let crypto_box = CryptoBox::init(&path, None, None, "testing")?;
            // Drop it
            std::mem::drop(crypto_box);
            // Open it back up with the wrong password
            let crypto_box = CryptoBox::open(&path, "testing 2");
            assert!(matches!(
                crypto_box,
                Err(CryptoBoxError::RootKeyDecryption { .. })
            ));
            Ok(())
        }
    }
}