| 1 | // Copyright 2021 The Matrix.org Foundation C.I.C. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #![doc = include_str!("../README.md")] |
| 16 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 17 | #![warn(missing_debug_implementations, missing_docs)] |
| 18 | |
| 19 | mod error; |
| 20 | mod types; |
| 21 | mod utils; |
| 22 | |
| 23 | pub use error::{DecodingError, EncodingError}; |
| 24 | pub use qrcode; |
| 25 | pub use types::{ |
| 26 | QrVerificationData, SelfVerificationData, SelfVerificationNoMasterKey, VerificationData, |
| 27 | }; |
| 28 | |
| 29 | #[cfg(test)] |
| 30 | mod tests { |
| 31 | use crate::{DecodingError, QrVerificationData}; |
| 32 | |
| 33 | #[test] |
| 34 | fn decode_invalid_header() { |
| 35 | let data = b"NonMatrixCode"; |
| 36 | let result = QrVerificationData::from_bytes(data); |
| 37 | assert!(matches!(result, Err(DecodingError::Header))) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn decode_invalid_mode() { |
| 42 | let data = b"MATRIX\x02\x03"; |
| 43 | let result = QrVerificationData::from_bytes(data); |
| 44 | assert!(matches!(result, Err(DecodingError::Mode(3)))) |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn decode_invalid_version() { |
| 49 | let data = b"MATRIX\x01\x03"; |
| 50 | let result = QrVerificationData::from_bytes(data); |
| 51 | assert!(matches!(result, Err(DecodingError::Version(1)))) |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn decode_missing_data() { |
| 56 | let data = b"MATRIX\x02\x02"; |
| 57 | let result = QrVerificationData::from_bytes(data); |
| 58 | assert!(matches!(result, Err(DecodingError::Read(_)))) |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn decode_short_secret() { |
| 63 | let data = b"MATRIX\ |
| 64 | \x02\x02\x00\x07\ |
| 65 | FLOW_ID\ |
| 66 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\ |
| 67 | BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\ |
| 68 | SECRET"; |
| 69 | |
| 70 | let result = QrVerificationData::from_bytes(data); |
| 71 | assert!(matches!(result, Err(DecodingError::SharedSecret(_)))) |
| 72 | } |
| 73 | |
| 74 | #[test] |
| 75 | fn decode_invalid_keys() { |
| 76 | let data = b"MATRIX\ |
| 77 | \x02\x00\x00\x0f\ |
| 78 | !test:localhost\ |
| 79 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\ |
| 80 | BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\ |
| 81 | SECRETISLONGENOUGH"; |
| 82 | let result = QrVerificationData::from_bytes(data); |
| 83 | assert!(matches!(result, Err(DecodingError::Keys(_)))) |
| 84 | } |
| 85 | } |