mega/
lib.rs

1mod client;
2#[cfg(feature = "easy")]
3mod easy;
4mod file_validator;
5mod parsed_mega_url;
6mod types;
7
8pub use self::client::Client;
9#[cfg(feature = "easy")]
10pub use self::easy::Client as EasyClient;
11#[cfg(feature = "easy")]
12pub use self::easy::FileDownloadReader as EasyFileDownloadReader;
13#[cfg(feature = "easy")]
14pub use self::easy::GetAttributesBuilder as EasyGetAttributesBuilder;
15pub use self::file_validator::FileValidationError;
16pub use self::file_validator::FileValidator;
17pub use self::parsed_mega_url::ParseMegaUrlError;
18pub use self::parsed_mega_url::ParsedMegaFileUrl;
19pub use self::parsed_mega_url::ParsedMegaFolderUrl;
20pub use self::parsed_mega_url::ParsedMegaUrl;
21pub use self::types::Command;
22pub use self::types::DecodeAttributesError;
23pub use self::types::ErrorCode;
24pub use self::types::FetchNodesNodeKind;
25pub use self::types::FetchNodesResponse;
26pub use self::types::FileAttributes;
27pub use self::types::FileKey;
28pub use self::types::FileKeyParseError;
29pub use self::types::FileOrFolderKey;
30pub use self::types::FolderKey;
31pub use self::types::FolderKeyParseError;
32pub use self::types::GetAttributesResponse;
33pub use self::types::Response;
34pub use self::types::ResponseData;
35pub use url::Url;
36
37/// The library error type
38#[derive(Debug, thiserror::Error)]
39pub enum Error {
40    /// A reqwest Error
41    #[error("http error")]
42    Reqwest(#[from] reqwest::Error),
43
44    /// A Url Error
45    #[error("url error")]
46    Url(#[from] url::ParseError),
47
48    /// The returned number of responses did not match what was expected
49    #[error("expected \"{expected}\" responses, but got \"{actual}\"")]
50    ResponseLengthMismatch { expected: usize, actual: usize },
51
52    /// There was an api error
53    #[error("api error")]
54    ApiError(#[from] ErrorCode),
55
56    /// Failed to decode attributes
57    #[error("failed to decode attributes")]
58    DecodeAttributes(#[from] DecodeAttributesError),
59
60    #[cfg(feature = "easy")]
61    #[error("channel closed without response")]
62    NoResponse,
63
64    #[cfg(feature = "easy")]
65    #[error("error occured as part of a batched send")]
66    BatchSend(self::easy::ArcError<Self>),
67
68    #[cfg(feature = "easy")]
69    #[error("unexpected response data type")]
70    UnexpectedResponseDataType,
71}
72
73#[cfg(test)]
74mod test {
75    use super::*;
76    use cbc::cipher::KeyIvInit;
77    use cbc::cipher::StreamCipher;
78
79    type Aes128Ctr128BE = ctr::Ctr128BE<aes::Aes128>;
80
81    pub const TEST_FILE: &str =
82        "https://mega.nz/file/7glwEQBT#Fy9cwPpCmuaVdEkW19qwBLaiMeyufB1kseqisOAxfi8";
83    pub const TEST_FILE_KEY: &str = "Fy9cwPpCmuaVdEkW19qwBLaiMeyufB1kseqisOAxfi8";
84    pub const TEST_FILE_ID: &str = "7glwEQBT";
85
86    pub const TEST_FOLDER: &str = "https://mega.nz/folder/MWsm3aBL#xsXXTpoYEFDRQdeHPDrv7A";
87    pub const TEST_FOLDER_KEY: &str = "xsXXTpoYEFDRQdeHPDrv7A";
88    pub const TEST_FOLDER_ID: &str = "MWsm3aBL";
89
90    pub const TEST_FOLDER_NESTED: &str =
91        "https://mega.nz/folder/MWsm3aBL#xsXXTpoYEFDRQdeHPDrv7A/folder/IGlBlD6K";
92
93    pub const TEST_FILE_KEY_KEY_DECODED: u128 = u128::from_be_bytes([
94        161, 141, 109, 44, 84, 62, 135, 130, 36, 158, 235, 166, 55, 235, 206, 43,
95    ]);
96    pub const TEST_FILE_KEY_IV_DECODED: u128 =
97        u128::from_be_bytes([182, 162, 49, 236, 174, 124, 29, 100, 0, 0, 0, 0, 0, 0, 0, 0]);
98    pub const TEST_FILE_META_MAC_DECODED: u64 =
99        u64::from_be_bytes([177, 234, 162, 176, 224, 49, 126, 47]);
100    pub const TEST_FOLDER_KEY_DECODED: u128 = u128::from_be_bytes([
101        198, 197, 215, 78, 154, 24, 16, 80, 209, 65, 215, 135, 60, 58, 239, 236,
102    ]);
103
104    pub const TEST_FILE_BYTES: &[u8] = include_bytes!("../test_data/Doxygen_docs.zip");
105
106    #[test]
107    fn parse_file_key() {
108        let file_key: FileKey = TEST_FILE_KEY.parse().expect("failed to parse file key");
109        assert!(file_key.key == TEST_FILE_KEY_KEY_DECODED);
110        assert!(file_key.iv == TEST_FILE_KEY_IV_DECODED);
111        assert!(file_key.meta_mac == TEST_FILE_META_MAC_DECODED);
112    }
113
114    #[test]
115    fn parse_folder_key() {
116        let folder_key: FolderKey = TEST_FOLDER_KEY.parse().expect("failed to parse folder key");
117        assert!(folder_key.0 == TEST_FOLDER_KEY_DECODED);
118    }
119
120    #[tokio::test]
121    async fn download_file() {
122        let file_key = FileKey {
123            key: TEST_FILE_KEY_KEY_DECODED,
124            iv: TEST_FILE_KEY_IV_DECODED,
125            meta_mac: TEST_FILE_META_MAC_DECODED,
126        };
127
128        let client = Client::new();
129        let commands = vec![Command::GetAttributes {
130            public_node_id: Some(TEST_FILE_ID.into()),
131            node_id: None,
132            include_download_url: Some(1),
133        }];
134        let mut response = client
135            .execute_commands(&commands, None)
136            .await
137            .expect("failed to execute commands");
138        assert!(response.len() == 1);
139        let response = response.swap_remove(0);
140        let response = response.into_result().expect("response was an error");
141        let response = match response {
142            ResponseData::GetAttributes(response) => response,
143            _ => panic!("unexpected response"),
144        };
145        let download_url = response
146            .download_url
147            .as_ref()
148            .expect("missing download url");
149
150        let mut response = client
151            .client
152            .get(download_url.as_str())
153            .send()
154            .await
155            .expect("failed to send")
156            .error_for_status()
157            .expect("invalid status");
158        let mut cipher = Aes128Ctr128BE::new(
159            &file_key.key.to_be_bytes().into(),
160            &file_key.iv.to_be_bytes().into(),
161        );
162
163        let mut output = Vec::new();
164        let mut validator = FileValidator::new(file_key.clone());
165        while let Some(chunk) = response.chunk().await.expect("failed to get chunk") {
166            let old_len = output.len();
167            output.extend(&chunk);
168            cipher.apply_keystream(&mut output[old_len..]);
169        }
170        assert!(output == TEST_FILE_BYTES);
171
172        validator.feed(&output);
173        validator.finish().expect("validation failed");
174    }
175}