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