| 1 | use super::*; |
| 2 | |
| 3 | fn field(tag: u16, value: &[u8]) -> Vec<u8> { |
| 4 | [ |
| 5 | tag.to_le_bytes().as_slice(), |
| 6 | &(value.len() as u16).to_le_bytes(), |
| 7 | value, |
| 8 | ] |
| 9 | .concat() |
| 10 | } |
| 11 | |
| 12 | #[test] |
| 13 | fn truncated_headers_and_values_are_rejected_by_every_policy() { |
| 14 | for policy in [Policy::Streaming, Policy::Recovery, Policy::Names] { |
| 15 | for bytes in [vec![0], vec![0, 0], vec![0, 0, 0], vec![2, 0, 1, 0]] { |
| 16 | assert!(matches!(parse(&bytes, policy), Err(ErrorCode::Integrity))); |
| 17 | } |
| 18 | assert!(parse(&field(2, &[]), policy).is_ok()); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | #[test] |
| 23 | fn zip64_is_borrowed_only_for_normal_streaming() { |
| 24 | let bytes = field(1, &[0; 16]); |
| 25 | assert_eq!( |
| 26 | parse(&bytes, Policy::Streaming).unwrap().zip64, |
| 27 | Some(&bytes[4..]) |
| 28 | ); |
| 29 | assert!(matches!( |
| 30 | parse(&bytes, Policy::Recovery), |
| 31 | Err(ErrorCode::UnsupportedFeature) |
| 32 | )); |
| 33 | assert!(matches!( |
| 34 | parse( |
| 35 | &[bytes.as_slice(), bytes.as_slice()].concat(), |
| 36 | Policy::Streaming |
| 37 | ), |
| 38 | Err(ErrorCode::Integrity) |
| 39 | )); |
| 40 | for tag in [1, 0x0017, 0x0018] { |
| 41 | assert!(matches!( |
| 42 | parse(&field(tag, &[]), Policy::Recovery), |
| 43 | Err(ErrorCode::UnsupportedFeature) |
| 44 | )); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn aes_policy_keeps_support_and_duplicate_checks() { |
| 50 | let value = [2, 0, b'A', b'E', 3, 8, 0]; |
| 51 | let bytes = field(0x9901, &value); |
| 52 | let policy = Policy::Streaming; |
| 53 | assert_eq!( |
| 54 | parse(&bytes, policy).unwrap().aes, |
| 55 | Some(AesExtra { |
| 56 | version: 2, |
| 57 | method: 8 |
| 58 | }) |
| 59 | ); |
| 60 | assert!(matches!( |
| 61 | parse(&[bytes.as_slice(), bytes.as_slice()].concat(), policy), |
| 62 | Err(ErrorCode::Integrity) |
| 63 | )); |
| 64 | assert!(matches!( |
| 65 | parse(&field(0x9901, &value[..6]), policy), |
| 66 | Err(ErrorCode::Integrity) |
| 67 | )); |
| 68 | let mut unsupported = value; |
| 69 | unsupported[4] = 2; |
| 70 | assert!(matches!( |
| 71 | parse(&field(0x9901, &unsupported), policy), |
| 72 | Err(ErrorCode::UnsupportedFeature) |
| 73 | )); |
| 74 | assert!(parse(&bytes, Policy::Recovery).unwrap().aes.is_none()); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn unicode_and_aes_fields_can_share_the_same_extra() { |
| 79 | let bytes = [ |
| 80 | field(0x7075, &[1, 0, 0, 0, 0]), |
| 81 | field(0x9901, &[2, 0, b'A', b'E', 3, 0, 0]), |
| 82 | ] |
| 83 | .concat(); |
| 84 | assert_eq!( |
| 85 | parse(&bytes, Policy::Names).unwrap().unicode, |
| 86 | Some(&bytes[4..9]) |
| 87 | ); |
| 88 | assert!(parse(&bytes, Policy::Streaming).unwrap().aes.is_some()); |
| 89 | assert!(matches!( |
| 90 | parse(&field(0x7075, &[0; 4]), Policy::Names), |
| 91 | Err(ErrorCode::Integrity) |
| 92 | )); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn mixed_invalid_fields_keep_error_precedence() { |
| 97 | let unsupported_aes = field(0x9901, &[3, 0, b'A', b'E', 3, 0, 0]); |
| 98 | let duplicate_zip64 = [field(1, &[]), field(1, &[])].concat(); |
| 99 | let extra = [unsupported_aes.as_slice(), duplicate_zip64.as_slice()].concat(); |
| 100 | assert!(matches!( |
| 101 | parse(&extra, Policy::Streaming), |
| 102 | Err(ErrorCode::Integrity) |
| 103 | )); |
| 104 | let truncated = [unsupported_aes.as_slice(), &[2, 0, 1, 0]].concat(); |
| 105 | assert!(matches!( |
| 106 | parse(&truncated, Policy::Streaming), |
| 107 | Err(ErrorCode::Integrity) |
| 108 | )); |
| 109 | assert!(matches!( |
| 110 | parse(&[1, 0, 8, 0], Policy::Recovery), |
| 111 | Err(ErrorCode::UnsupportedFeature) |
| 112 | )); |
| 113 | } |