Skip to main content

smb_server/encrypted/
error.rs

1//! The error of building an encrypted backend.
2
3use thiserror::Error;
4
5use crate::error::SmbError;
6
7/// Why [`EncryptedBackendBuilder::build`](crate::EncryptedBackendBuilder::build)
8/// failed.
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum EncryptionError {
12    /// A setting is outside its range, or cannot go with another setting.
13    /// `setting` names the builder method.
14    #[error("invalid setting `{setting}`")]
15    InvalidSetting { setting: &'static str },
16    /// A setting the builder was given differs from the share's. `setting`
17    /// names the builder method.
18    #[error("setting `{setting}` differs from the share's")]
19    SettingMismatch { setting: &'static str },
20    /// The key is not the share's: the marker does not verify under it, or
21    /// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
22    /// found nothing in the share that the key authenticates. A marker with a
23    /// changed byte after its version byte also gives this error.
24    #[error("the key does not match the share")]
25    KeyMismatch,
26    /// The root of the inner backend holds entries and no marker.
27    /// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
28    /// writes a new one.
29    #[error("the share has entries and no marker")]
30    MarkerMissing,
31    /// The marker is not a file of the right size, does not start with the
32    /// marker magic, or holds field values outside their ranges. A crash
33    /// during the first build can leave a short marker. Deleting a damaged
34    /// marker lets
35    /// [`restore_marker`](crate::EncryptedBackendBuilder::restore_marker)
36    /// write a new one.
37    #[error("the marker is not valid")]
38    MarkerInvalid,
39    /// The marker has a format version this release does not read.
40    #[error("format version {version} is not supported")]
41    UnsupportedFormat { version: u8 },
42    /// The inner backend failed.
43    #[error("the inner backend failed")]
44    Backend(#[source] SmbError),
45    /// The operating system's random generator failed.
46    #[error("the random generator failed")]
47    Random(#[source] getrandom::Error),
48}
49
50#[cfg(test)]
51mod tests {
52    use std::error::Error;
53
54    use super::*;
55
56    #[test]
57    fn display_texts() {
58        assert_eq!(
59            EncryptionError::InvalidSetting {
60                setting: "block_size"
61            }
62            .to_string(),
63            "invalid setting `block_size`"
64        );
65        assert_eq!(
66            EncryptionError::SettingMismatch {
67                setting: "encrypt_names"
68            }
69            .to_string(),
70            "setting `encrypt_names` differs from the share's"
71        );
72        assert_eq!(
73            EncryptionError::UnsupportedFormat { version: 7 }.to_string(),
74            "format version 7 is not supported"
75        );
76        assert_eq!(
77            EncryptionError::Backend(SmbError::DiskFull).to_string(),
78            "the inner backend failed"
79        );
80        assert_eq!(
81            EncryptionError::Random(getrandom::Error::UNEXPECTED).to_string(),
82            "the random generator failed"
83        );
84    }
85
86    #[test]
87    fn backend_and_random_errors_are_sources() {
88        let backend = EncryptionError::Backend(SmbError::DiskFull);
89        assert_eq!(backend.source().unwrap().to_string(), "disk full");
90
91        let random = EncryptionError::Random(getrandom::Error::UNEXPECTED);
92        assert!(random.source().is_some(), "the random error has a source");
93    }
94}