quic_tests.rs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. use ring::{aead::quic, test, test_file};
  15. #[test]
  16. fn quic_aes_128() {
  17. test_quic(&quic::AES_128, test_file!("quic_aes_128_tests.txt"));
  18. }
  19. #[test]
  20. fn quic_aes_256() {
  21. test_quic(&quic::AES_256, test_file!("quic_aes_256_tests.txt"));
  22. }
  23. #[test]
  24. fn quic_chacha20() {
  25. test_quic(&quic::CHACHA20, test_file!("quic_chacha20_tests.txt"));
  26. }
  27. fn test_quic(alg: &'static quic::Algorithm, test_file: test::File) {
  28. test_sample_len(alg);
  29. test::run(test_file, |section, test_case| {
  30. assert_eq!(section, "");
  31. let key_bytes = test_case.consume_bytes("KEY");
  32. let sample = test_case.consume_bytes("SAMPLE");
  33. let mask = test_case.consume_bytes("MASK");
  34. let key = quic::HeaderProtectionKey::new(alg, &key_bytes)?;
  35. assert_eq!(mask.as_ref(), key.new_mask(&sample)?);
  36. Ok(())
  37. });
  38. }
  39. #[allow(clippy::range_plus_one)]
  40. fn test_sample_len(alg: &'static quic::Algorithm) {
  41. let key_len = alg.key_len();
  42. let key_data = vec![0u8; key_len];
  43. let key = quic::HeaderProtectionKey::new(alg, &key_data).unwrap();
  44. let sample_len = 16;
  45. let sample_data = vec![0u8; sample_len + 2];
  46. // Sample is the right size.
  47. assert!(key.new_mask(&sample_data[..sample_len]).is_ok());
  48. // Sample is one byte too small.
  49. assert!(key.new_mask(&sample_data[..(sample_len - 1)]).is_err());
  50. // Sample is one byte too big.
  51. assert!(key.new_mask(&sample_data[..(sample_len + 1)]).is_err());
  52. // Sample is empty.
  53. assert!(key.new_mask(&[]).is_err());
  54. }