Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / rust / tests / coverage_signal_binary.rs
coverage_signal_binary.rs224 lines · 9.0 KiBRaw
| 1 | //! Coverage tests: sealed-sender stub, Kyber replay guard, WA binary nodes. |
| 2 | //! Pure/deterministic, synthetic inputs only. |
| 3 | |
| 4 | use communicate_signal::signal::{ |
| 5 | mark_kyber_pre_key_used, sealed_sender_decrypt, sealed_sender_decrypt_any, |
| 6 | sealed_sender_encrypt, |
| 7 | }; |
| 8 | use communicate_signal::wa_binary::{decode_node, encode_node, Node}; |
| 9 | |
| 10 | fn round_trip(node: &Node) -> Node { |
| 11 | let encoded = encode_node(node); |
| 12 | assert_eq!(encoded[0], 0); |
| 13 | decode_node(&encoded[1..]).expect("decode") |
| 14 | } |
| 15 | |
| 16 | // ---------------- sealed sender stub ---------------- |
| 17 | |
| 18 | #[test] |
| 19 | fn sealed_sender_round_trip_with_hint() { |
| 20 | for aci in ["alice", "[email protected]", ""] { |
| 21 | for plain in [b"".as_slice(), b"x".as_slice(), b"a longer sealed payload 123".as_slice()] { |
| 22 | let ct = sealed_sender_encrypt(plain, aci); |
| 23 | assert_eq!(ct[0], 1, "version byte"); |
| 24 | assert_eq!(sealed_sender_decrypt(&ct, Some(aci)).unwrap(), plain); |
| 25 | } |
| 26 | } |
| 27 | // Empty-ACI ciphertext is what decrypt_any handles. |
| 28 | let ct = sealed_sender_encrypt(b"anon", ""); |
| 29 | assert_eq!(sealed_sender_decrypt_any(&ct).unwrap(), b"anon"); |
| 30 | assert_eq!(sealed_sender_decrypt(&ct, Some("")).unwrap(), b"anon"); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn sealed_sender_wrong_recipient_and_tamper() { |
| 35 | let ct = sealed_sender_encrypt(b"for alice only", "alice"); |
| 36 | assert!(sealed_sender_decrypt(&ct, Some("bob")).is_err(), "wrong key/AAD"); |
| 37 | assert!(sealed_sender_decrypt_any(&ct).is_err(), "anon key cannot open alice mail"); |
| 38 | // Two encryptions differ (random IV) yet both decrypt. |
| 39 | let ct2 = sealed_sender_encrypt(b"for alice only", "alice"); |
| 40 | assert_ne!(ct, ct2); |
| 41 | assert_eq!(sealed_sender_decrypt(&ct2, Some("alice")).unwrap(), b"for alice only"); |
| 42 | // Tamper: version, IV, ciphertext and tag flips all fail. |
| 43 | for idx in [0usize, 3, 15, 20] { |
| 44 | let mut bad = ct.clone(); |
| 45 | if idx < bad.len() { |
| 46 | bad[idx] ^= 1; |
| 47 | assert!(sealed_sender_decrypt(&bad, Some("alice")).is_err(), "flip at {idx}"); |
| 48 | } |
| 49 | } |
| 50 | let n = ct.len(); |
| 51 | let mut bad_tag = ct.clone(); |
| 52 | bad_tag[n - 1] ^= 1; |
| 53 | assert!(sealed_sender_decrypt(&bad_tag, Some("alice")).is_err()); |
| 54 | // Every strict prefix is an error, never a panic. |
| 55 | for cut in 0..ct.len() { |
| 56 | assert!(sealed_sender_decrypt(&ct[..cut], Some("alice")).is_err(), "cut {cut}"); |
| 57 | } |
| 58 | let mut bad_version = ct.clone(); |
| 59 | bad_version[0] = 2; |
| 60 | assert!(sealed_sender_decrypt(&bad_version, Some("alice")).is_err()); |
| 61 | } |
| 62 | |
| 63 | // ---------------- Kyber replay guard ---------------- |
| 64 | |
| 65 | #[test] |
| 66 | fn kyber_replay_guard_keys_and_pairs() { |
| 67 | // Unique ids per assertion group: the guard is process-global state. |
| 68 | assert!(mark_kyber_pre_key_used(91_001, 7, b"base-a")); |
| 69 | assert!(mark_kyber_pre_key_used(91_001, 7, b"base-b"), "new base key ok"); |
| 70 | assert!(!mark_kyber_pre_key_used(91_001, 7, b"base-a"), "replay rejected"); |
| 71 | // Same base key under a different (kyber, signed-ec) pair is independent. |
| 72 | assert!(mark_kyber_pre_key_used(91_002, 7, b"base-a")); |
| 73 | assert!(mark_kyber_pre_key_used(91_001, 8, b"base-a")); |
| 74 | // Empty base key is tracked like any other. |
| 75 | assert!(mark_kyber_pre_key_used(91_003, 1, b"")); |
| 76 | assert!(!mark_kyber_pre_key_used(91_003, 1, b"")); |
| 77 | } |
| 78 | |
| 79 | // ---------------- WA binary node codec ---------------- |
| 80 | |
| 81 | #[test] |
| 82 | fn node_many_children_and_deep_nesting() { |
| 83 | // >255 children forces the LIST_16 list header. |
| 84 | let mut parent = Node::new("message"); |
| 85 | for i in 0..300 { |
| 86 | parent.content.push(Node::new("enc").attr("v", &i.to_string())); |
| 87 | } |
| 88 | assert_eq!(round_trip(&parent), parent); |
| 89 | // Deep nesting round-trips. |
| 90 | let mut deep = Node::new("leaf").attr("depth", "end"); |
| 91 | for i in 0..40 { |
| 92 | let mut wrap = Node::new("iq").attr("level", &i.to_string()); |
| 93 | wrap.content.push(deep); |
| 94 | deep = wrap; |
| 95 | } |
| 96 | assert_eq!(round_trip(&deep), deep); |
| 97 | // Many attributes (>127 pairs stays LIST_8-sized list; order deterministic). |
| 98 | let mut wide = Node::new("iq"); |
| 99 | for i in 0..60 { |
| 100 | wide.attrs.insert(format!("k{i:02}"), format!("value-{i}")); |
| 101 | } |
| 102 | assert_eq!(round_trip(&wide), wide); |
| 103 | assert_eq!(encode_node(&wide), encode_node(&wide.clone()), "encoding is deterministic"); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn node_payload_length_classes() { |
| 108 | // 256..=1MiB-1 payloads use BINARY_20; spot-check around the boundary. |
| 109 | for size in [255usize, 256, 257, 65_535, 65_536] { |
| 110 | let mut node = Node::new("enc"); |
| 111 | node.data = Some((0..size).map(|i| (i % 253) as u8).collect()); |
| 112 | let back = round_trip(&node); |
| 113 | assert_eq!(back.data.as_ref().map(Vec::len), Some(size), "size {size}"); |
| 114 | assert_eq!(back.data, node.data); |
| 115 | } |
| 116 | // Empty payload is preserved as Some(empty), distinct from no payload. |
| 117 | let mut empty_data = Node::new("enc"); |
| 118 | empty_data.data = Some(vec![]); |
| 119 | let back = round_trip(&empty_data); |
| 120 | assert_eq!(back.data, Some(vec![])); |
| 121 | // Text content in the payload slot decodes as bytes. |
| 122 | let mut texty = Node::new("body"); |
| 123 | texty.data = Some(b"plain text body".to_vec()); |
| 124 | assert_eq!(round_trip(&texty), texty); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn node_packed_string_boundaries() { |
| 129 | // 127 chars is the longest packable value; it must survive packed. |
| 130 | let at_max = "7".repeat(127); |
| 131 | let node = Node::new("iq").attr("zzz_raw_key", &at_max); |
| 132 | assert_eq!(round_trip(&node).attrs["zzz_raw_key"], at_max); |
| 133 | let over = "7".repeat(128); |
| 134 | let node = Node::new("iq").attr("zzz_raw_key", &over); |
| 135 | assert_eq!(round_trip(&node).attrs["zzz_raw_key"], over); |
| 136 | // Mixed nibble alphabet, even/odd lengths, and hex boundaries. |
| 137 | for v in ["0", "00", "1-2", "12.34", "-", ".", "ABCDEF0", "F", "FF", "0123456789ABCDEF"] { |
| 138 | let node = Node::new("iq").attr("zzz_raw_key", v); |
| 139 | assert_eq!(round_trip(&node).attrs["zzz_raw_key"], v, "value {v}"); |
| 140 | } |
| 141 | // Strings that merely look numeric with other chars fall back to raw. |
| 142 | for v in ["12a", "1 2", "+123", "12,3", "abc-def"] { |
| 143 | let node = Node::new("iq").attr("zzz_raw_key", v); |
| 144 | assert_eq!(round_trip(&node).attrs["zzz_raw_key"], v, "value {v}"); |
| 145 | } |
| 146 | // Non-ASCII text round-trips as raw bytes. |
| 147 | let node = Node::new("iq").attr("zzz_raw_key", "héllo wörld ✓"); |
| 148 | assert_eq!(round_trip(&node).attrs["zzz_raw_key"], "héllo wörld ✓"); |
| 149 | } |
| 150 | |
| 151 | #[test] |
| 152 | fn node_jid_variants() { |
| 153 | for jid in [ |
| 154 | "[email protected]", |
| 155 | "[email protected]", |
| 156 | "[email protected]", |
| 157 | "@s.whatsapp.net", |
| 158 | "user@lid", |
| 159 | "plainstring-no-at", |
| 160 | ] { |
| 161 | let node = Node::new("iq").attr("to", jid); |
| 162 | assert_eq!(round_trip(&node).attrs["to"], jid, "jid {jid}"); |
| 163 | } |
| 164 | // A dotted non-numeric user part must round-trip losslessly: only the |
| 165 | // canonical numeric `<user>.<agent>:<device>` form uses AD_JID. |
| 166 | for jid in [ |
| 167 | "[email protected]", |
| 168 | "[email protected]", |
| 169 | "[email protected]", |
| 170 | "[email protected]", |
| 171 | "user:[email protected]", |
| 172 | "user.0:[email protected]", |
| 173 | "user.256:[email protected]", |
| 174 | "user:[email protected]", |
| 175 | "15551234567.1:[email protected]", |
| 176 | "15551234567.0:[email protected]", |
| 177 | ] { |
| 178 | let node = Node::new("iq").attr("to", jid); |
| 179 | assert_eq!(round_trip(&node).attrs["to"], jid, "jid {jid}"); |
| 180 | } |
| 181 | // Attribute keys are never JID-encoded even when the value is. |
| 182 | let node = Node::new("iq").attr("jid", "[email protected]"); |
| 183 | assert_eq!(round_trip(&node), node); |
| 184 | // Tag "0" is the wire empty-node marker: LIST_8 followed by LIST_EMPTY, |
| 185 | // and it must decode back to the same node. |
| 186 | let encoded = encode_node(&Node::new("0")); |
| 187 | assert_eq!(encoded, vec![0u8, 248, 0]); |
| 188 | assert_eq!(round_trip(&Node::new("0")), Node::new("0")); |
| 189 | // An empty node nested as a child also round-trips. |
| 190 | let mut parent = Node::new("iq"); |
| 191 | parent.content.push(Node::new("0")); |
| 192 | parent.content.push(Node::new("enc")); |
| 193 | assert_eq!(round_trip(&parent), parent); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn node_decoder_rejects_garbage_robustly() { |
| 198 | assert!(decode_node(&[]).is_err()); |
| 199 | assert!(decode_node(&[0]).is_err(), "list tag 0 unknown at node level"); |
| 200 | assert!(decode_node(&[248]).is_err(), "LIST_8 without size"); |
| 201 | assert!(decode_node(&[248, 1]).is_err(), "LIST_8 size without tag"); |
| 202 | assert!(decode_node(&[249, 0]).is_err(), "truncated LIST_16"); |
| 203 | // Every prefix of a valid encoding: Ok or Err, never panic/hang. |
| 204 | let node = |
| 205 | Node::new("message").attr("to", "15551234567.1:[email protected]").attr("id", "ABC123"); |
| 206 | let mut with_child = node.clone(); |
| 207 | let mut child = Node::new("enc"); |
| 208 | child.data = Some(vec![1, 2, 3, 4, 5]); |
| 209 | with_child.content.push(child); |
| 210 | let encoded = encode_node(&with_child); |
| 211 | for cut in 1..encoded.len() { |
| 212 | let _ = decode_node(&encoded[1..cut]); |
| 213 | } |
| 214 | // Random-ish garbage sweeps: never panic. |
| 215 | let mut state = 0x1234_5678u32; |
| 216 | for _ in 0..200 { |
| 217 | let mut buf = Vec::new(); |
| 218 | for _ in 0..24 { |
| 219 | state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); |
| 220 | buf.push((state >> 24) as u8); |
| 221 | } |
| 222 | let _ = decode_node(&buf); |
| 223 | } |
| 224 | } |