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.
1415#![doc = include_str!("../README.md")]
16#![cfg_attr(docsrs, feature(doc_auto_cfg))]
17#![warn(missing_debug_implementations, missing_docs)]
1819mod error;
20mod types;
21mod utils;
2223pub use error::{DecodingError, EncodingError};
24pub use qrcode;
25pub use types::{
26 QrVerificationData, SelfVerificationData, SelfVerificationNoMasterKey, VerificationData,
27};
2829#[cfg(test)]
30mod tests {
31use crate::{DecodingError, QrVerificationData};
3233#[test]
34fn decode_invalid_header() {
35let data = b"NonMatrixCode";
36let result = QrVerificationData::from_bytes(data);
37assert!(matches!(result, Err(DecodingError::Header)))
38 }
3940#[test]
41fn decode_invalid_mode() {
42let data = b"MATRIX\x02\x03";
43let result = QrVerificationData::from_bytes(data);
44assert!(matches!(result, Err(DecodingError::Mode(3))))
45 }
4647#[test]
48fn decode_invalid_version() {
49let data = b"MATRIX\x01\x03";
50let result = QrVerificationData::from_bytes(data);
51assert!(matches!(result, Err(DecodingError::Version(1))))
52 }
5354#[test]
55fn decode_missing_data() {
56let data = b"MATRIX\x02\x02";
57let result = QrVerificationData::from_bytes(data);
58assert!(matches!(result, Err(DecodingError::Read(_))))
59 }
6061#[test]
62fn decode_short_secret() {
63let data = b"MATRIX\
64 \x02\x02\x00\x07\
65 FLOW_ID\
66 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
67 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\
68 SECRET";
6970let result = QrVerificationData::from_bytes(data);
71assert!(matches!(result, Err(DecodingError::SharedSecret(_))))
72 }
7374#[test]
75fn decode_invalid_keys() {
76let data = b"MATRIX\
77 \x02\x00\x00\x0f\
78 !test:localhost\
79 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
80 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\
81 SECRETISLONGENOUGH";
82let result = QrVerificationData::from_bytes(data);
83assert!(matches!(result, Err(DecodingError::Keys(_))))
84 }
85}