1 // Copyright 2018 Brian Smith.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 
15 #![forbid(
16     anonymous_parameters,
17     box_pointers,
18     missing_copy_implementations,
19     missing_debug_implementations,
20     missing_docs,
21     trivial_casts,
22     trivial_numeric_casts,
23     unsafe_code,
24     unstable_features,
25     unused_extern_crates,
26     unused_import_braces,
27     unused_qualifications,
28     unused_results,
29     variant_size_differences,
30     warnings
31 )]
32 
33 use ring::{aead::quic, test, test_file};
34 
35 #[test]
quic_aes_128()36 fn quic_aes_128() {
37     test_quic(&quic::AES_128, test_file!("quic_aes_128_tests.txt"));
38 }
39 
40 #[test]
quic_aes_256()41 fn quic_aes_256() {
42     test_quic(&quic::AES_256, test_file!("quic_aes_256_tests.txt"));
43 }
44 
45 #[test]
quic_chacha20()46 fn quic_chacha20() {
47     test_quic(&quic::CHACHA20, test_file!("quic_chacha20_tests.txt"));
48 }
49 
test_quic(alg: &'static quic::Algorithm, test_file: test::File)50 fn test_quic(alg: &'static quic::Algorithm, test_file: test::File) {
51     test_sample_len(alg);
52 
53     test::run(test_file, |section, test_case| {
54         assert_eq!(section, "");
55         let key_bytes = test_case.consume_bytes("KEY");
56         let sample = test_case.consume_bytes("SAMPLE");
57         let mask = test_case.consume_bytes("MASK");
58 
59         let key = quic::HeaderProtectionKey::new(alg, &key_bytes)?;
60 
61         assert_eq!(mask.as_ref(), key.new_mask(&sample)?);
62 
63         Ok(())
64     });
65 }
66 
67 #[allow(clippy::range_plus_one)]
test_sample_len(alg: &'static quic::Algorithm)68 fn test_sample_len(alg: &'static quic::Algorithm) {
69     let key_len = alg.key_len();
70     let key_data = vec![0u8; key_len];
71 
72     let key = quic::HeaderProtectionKey::new(alg, &key_data).unwrap();
73 
74     let sample_len = 16;
75     let sample_data = vec![0u8; sample_len + 2];
76 
77     // Sample is the right size.
78     assert!(key.new_mask(&sample_data[..sample_len]).is_ok());
79 
80     // Sample is one byte too small.
81     assert!(key.new_mask(&sample_data[..(sample_len - 1)]).is_err());
82 
83     // Sample is one byte too big.
84     assert!(key.new_mask(&sample_data[..(sample_len + 1)]).is_err());
85 
86     // Sample is empty.
87     assert!(key.new_mask(&[]).is_err());
88 }
89