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