oxedyne/fe2o3/fe2o3_o3db_sync/tests/dist_codec.rs
22.6 KiB, 47 runs
created by r1870400018:67484, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #![cfg(feature = "dist")] |
| 2 | //! Integration tests for the distributed-Ozone wire codec. |
| 3 | //! |
| 4 | //! The layout tests state the wire form independently, as `Dat` trees and as bytes built by hand, |
| 5 | //! so a codec that agrees only with itself cannot pass them. |
| 6 | //! |
| 7 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 8 | //! Anthropic Claude |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | use oxedyne_fe2o3_jdat::prelude::*; |
| 12 | use oxedyne_fe2o3_o3db_sync::{ |
| 13 | dist::{ |
| 14 | codec::{ |
| 15 | MAX_ENVELOPE_BYTES, |
| 16 | MAX_ENVELOPE_DEPTH, |
| 17 | MAX_ENVELOPE_VALUES, |
| 18 | WIRE_VERSION, |
| 19 | }, |
| 20 | hotstuff::types::{ |
| 21 | NewView, |
| 22 | Phase, |
| 23 | Proposal, |
| 24 | Qc, |
| 25 | Vote, |
| 26 | }, |
| 27 | record::{ |
| 28 | Record, |
| 29 | RecordId, |
| 30 | }, |
| 31 | transport::{ |
| 32 | Envelope, |
| 33 | MsgKind, |
| 34 | }, |
| 35 | }, |
| 36 | kademlia::id::NodeId, |
| 37 | }; |
| 38 | |
| 39 | use std::collections::BTreeSet; |
| 40 | |
| 41 | |
| 42 | // Distinct, non-uniform bytes, so a swapped or shifted field cannot hide. |
| 43 | fn bytes32(n: u8) -> [u8; 32] { |
| 44 | let mut b = [0u8; 32]; |
| 45 | for (i, x) in b.iter_mut().enumerate() { |
| 46 | *x = n.wrapping_mul(31).wrapping_add(i as u8); |
| 47 | } |
| 48 | b |
| 49 | } |
| 50 | |
| 51 | fn rid(n: u8) -> RecordId { |
| 52 | RecordId::from_bytes(bytes32(n)) |
| 53 | } |
| 54 | |
| 55 | fn nid(n: u8) -> NodeId { |
| 56 | NodeId::from_bytes(bytes32(n)) |
| 57 | } |
| 58 | |
| 59 | fn payload(n: u8, len: usize) -> Vec<u8> { |
| 60 | (0..len).map(|i| (i as u8).wrapping_mul(7).wrapping_add(n)).collect() |
| 61 | } |
| 62 | |
| 63 | fn rec(n: u8, table: &str, len: usize) -> Record { |
| 64 | Record::new(rid(n), table, payload(n, len)) |
| 65 | } |
| 66 | |
| 67 | fn env(body: MsgKind) -> Envelope { |
| 68 | Envelope::new(nid(1), nid(2), body) |
| 69 | } |
| 70 | |
| 71 | fn qc(view: u64, phase: Phase, voters: &[u16]) -> Qc { |
| 72 | Qc { |
| 73 | view, |
| 74 | phase, |
| 75 | block_hash: bytes32(9), |
| 76 | signatures: voters.iter().map(|v| (*v, payload(*v as u8, 64))).collect(), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const PHASES: [Phase; 4] = [Phase::Prepare, Phase::PreCommit, Phase::Commit, Phase::Decide]; |
| 81 | |
| 82 | // The nine-deep shape: the greatest the encoder writes. |
| 83 | fn deepest() -> Envelope { |
| 84 | env(MsgKind::CohortNewView { |
| 85 | table: "t".to_string(), |
| 86 | id: rid(3), |
| 87 | new_view: NewView { view: 2, sender: 1, prepare_qc: Some(qc(1, Phase::Prepare, &[0, 1])) }, |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | // One of every kind, with the variations that change the bytes. |
| 92 | fn samples() -> Vec<Envelope> { |
| 93 | let mut v = Vec::new(); |
| 94 | // ReplicatePut: a value past the 255 bytes a Dat::BU8 can count, a short one, an empty one. |
| 95 | v.push(env(MsgKind::ReplicatePut { record: rec(1, "ledger", 300) })); |
| 96 | v.push(env(MsgKind::ReplicatePut { record: rec(2, "", 3) })); |
| 97 | v.push(env(MsgKind::ReplicatePut { record: rec(3, "ledger", 0) })); |
| 98 | v.push(env(MsgKind::GetRequest { request_id: u64::MAX, table: "ledger".to_string(), id: rid(4) })); |
| 99 | v.push(env(MsgKind::GetResponse { request_id: 0, record: None })); |
| 100 | v.push(env(MsgKind::GetResponse { request_id: 7, record: Some(rec(5, "ledger", 40)) })); |
| 101 | v.push(env(MsgKind::AntiEntropyDigest { table: "ledger".to_string(), sketch: payload(6, 5000), after: None })); |
| 102 | v.push(env(MsgKind::AntiEntropyDigest { table: "ledger".to_string(), sketch: Vec::new(), after: Some(rid(14)) })); |
| 103 | // AntiEntropyReply: both bulk settings, empty and full vectors, and every cursor shape. |
| 104 | for bulk in [false, true] { |
| 105 | for (after, next) in [(None, None), (Some(rid(15)), None), (None, Some(rid(16))), (Some(rid(15)), Some(rid(16)))] { |
| 106 | v.push(env(MsgKind::AntiEntropyReply { |
| 107 | table: "ledger".to_string(), |
| 108 | records: Vec::new(), |
| 109 | requested_ids: Vec::new(), |
| 110 | bulk, |
| 111 | after, |
| 112 | next, |
| 113 | })); |
| 114 | v.push(env(MsgKind::AntiEntropyReply { |
| 115 | table: "ledger".to_string(), |
| 116 | records: vec![rec(7, "ledger", 10), rec(8, "ledger", 0)], |
| 117 | requested_ids: vec![rid(9), rid(10), rid(11)], |
| 118 | bulk, |
| 119 | after, |
| 120 | next, |
| 121 | })); |
| 122 | } |
| 123 | } |
| 124 | v.push(env(MsgKind::AntiEntropyPush { table: "ledger".to_string(), records: Vec::new() })); |
| 125 | v.push(env(MsgKind::AntiEntropyPush { table: "ledger".to_string(), records: vec![rec(12, "ledger", 300)] })); |
| 126 | v.push(env(MsgKind::CohortSubmit { record: rec(13, "names", 21) })); |
| 127 | // CohortPropose: every phase, with and without the block and the justifying QC. |
| 128 | for phase in PHASES { |
| 129 | for block in [None, Some(payload(2, 300))] { |
| 130 | for justify in [None, Some(qc(5, Phase::Prepare, &[]))] { |
| 131 | v.push(env(MsgKind::CohortPropose { |
| 132 | table: "names".to_string(), |
| 133 | id: rid(14), |
| 134 | proposal: Proposal { |
| 135 | view: u64::MAX, |
| 136 | phase, |
| 137 | block_hash: bytes32(15), |
| 138 | block: block.clone(), |
| 139 | justify, |
| 140 | }, |
| 141 | })); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | v.push(env(MsgKind::CohortPropose { |
| 146 | table: "names".to_string(), |
| 147 | id: rid(14), |
| 148 | proposal: Proposal { |
| 149 | view: 3, |
| 150 | phase: Phase::Commit, |
| 151 | block_hash: bytes32(15), |
| 152 | block: Some(Vec::new()), |
| 153 | justify: Some(qc(3, Phase::PreCommit, &[0, 2, 3, u16::MAX])), |
| 154 | }, |
| 155 | })); |
| 156 | for phase in PHASES { |
| 157 | v.push(env(MsgKind::CohortVote { |
| 158 | table: "names".to_string(), |
| 159 | id: rid(16), |
| 160 | vote: Vote { view: 4, phase, block_hash: bytes32(17), voter: u16::MAX, signature: payload(1, 64) }, |
| 161 | })); |
| 162 | } |
| 163 | v.push(env(MsgKind::CohortNewView { |
| 164 | table: "names".to_string(), |
| 165 | id: rid(18), |
| 166 | new_view: NewView { view: 5, sender: 0, prepare_qc: None }, |
| 167 | })); |
| 168 | v.push(deepest()); |
| 169 | v |
| 170 | } |
| 171 | |
| 172 | fn tags_of(e: &Error<ErrTag>) -> Vec<ErrTag> { |
| 173 | e.tags() |
| 174 | } |
| 175 | |
| 176 | // A refusal of malformed input names Decode and Input. |
| 177 | fn assert_refused(what: &str, bytes: &[u8]) { |
| 178 | match Envelope::decode(bytes) { |
| 179 | Ok(e) => panic!("{}: decode accepted {} bytes as {:?}.", what, bytes.len(), e.body.label()), |
| 180 | Err(e) => { |
| 181 | let tags = tags_of(&e); |
| 182 | assert!( |
| 183 | tags.contains(&ErrTag::Decode) && tags.contains(&ErrTag::Input), |
| 184 | "{}: the refusal carries {:?}, not Decode and Input: {}", what, tags, e, |
| 185 | ); |
| 186 | }, |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | fn bdat(dat: &Dat) -> Vec<u8> { |
| 191 | match dat.to_bytes(Vec::new()) { |
| 192 | Ok(b) => b, |
| 193 | Err(e) => panic!("The hand-built Dat did not encode: {}", e), |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fn b32(n: u8) -> Dat { |
| 198 | Dat::B32(B32(bytes32(n))) |
| 199 | } |
| 200 | |
| 201 | fn s(x: &str) -> Dat { |
| 202 | Dat::Str(x.to_string()) |
| 203 | } |
| 204 | |
| 205 | fn list(v: Vec<Dat>) -> Dat { |
| 206 | Dat::List(v) |
| 207 | } |
| 208 | |
| 209 | fn opt(d: Option<Dat>) -> Dat { |
| 210 | Dat::Opt(Box::new(d)) |
| 211 | } |
| 212 | |
| 213 | // The wire form of a record, built by hand: [B32 id, Str table, BU64 value]. |
| 214 | fn record_dat(n: u8, table: &str, len: usize) -> Dat { |
| 215 | list(vec![b32(n), s(table), Dat::BU64(payload(n, len))]) |
| 216 | } |
| 217 | |
| 218 | // A ReplicatePut envelope, built by hand from the layout and not from the codec. |
| 219 | fn put_dat(version: u8, kind: u8) -> Dat { |
| 220 | list(vec![ |
| 221 | Dat::U8(version), |
| 222 | b32(1), |
| 223 | b32(2), |
| 224 | list(vec![Dat::U8(kind), record_dat(5, "ledger", 6)]), |
| 225 | ]) |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn every_kind_round_trips() -> Outcome<()> { |
| 230 | let all = samples(); |
| 231 | let labels: BTreeSet<&str> = all.iter().map(|e| e.body.label()).collect(); |
| 232 | assert_eq!(labels.len(), 10, "The samples cover only {:?}.", labels); |
| 233 | for e in &all { |
| 234 | let bytes = res!(e.encode()); |
| 235 | let back = res!(Envelope::decode(&bytes)); |
| 236 | assert_eq!(&back, e, "{} did not survive its own encoding.", e.body.label()); |
| 237 | assert_eq!(res!(back.encode()), bytes, "{} re-encodes to other bytes.", e.body.label()); |
| 238 | } |
| 239 | Ok(()) |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn wire_layout_is_pinned() -> Outcome<()> { |
| 244 | let qc_dat = |view: u64, phase: u8, sigs: Vec<(u16, Vec<u8>)>| list(vec![ |
| 245 | Dat::U64(view), |
| 246 | Dat::U8(phase), |
| 247 | b32(9), |
| 248 | list(sigs.into_iter().map(|(v, g)| list(vec![Dat::U16(v), Dat::BU64(g)])).collect()), |
| 249 | ]); |
| 250 | let cases: Vec<(Envelope, Dat)> = vec![ |
| 251 | ( |
| 252 | env(MsgKind::ReplicatePut { record: rec(5, "ledger", 6) }), |
| 253 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![Dat::U8(1), record_dat(5, "ledger", 6)])]), |
| 254 | ), |
| 255 | ( |
| 256 | env(MsgKind::GetRequest { request_id: 77, table: "t".to_string(), id: rid(4) }), |
| 257 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 258 | Dat::U8(2), |
| 259 | list(vec![Dat::U64(77), s("t"), b32(4)]), |
| 260 | ])]), |
| 261 | ), |
| 262 | ( |
| 263 | env(MsgKind::GetResponse { request_id: 8, record: Some(rec(5, "ledger", 2)) }), |
| 264 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 265 | Dat::U8(3), |
| 266 | list(vec![Dat::U64(8), opt(Some(record_dat(5, "ledger", 2)))]), |
| 267 | ])]), |
| 268 | ), |
| 269 | ( |
| 270 | env(MsgKind::GetResponse { request_id: 8, record: None }), |
| 271 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 272 | Dat::U8(3), |
| 273 | list(vec![Dat::U64(8), opt(None)]), |
| 274 | ])]), |
| 275 | ), |
| 276 | ( |
| 277 | env(MsgKind::AntiEntropyDigest { table: "t".to_string(), sketch: payload(1, 4), after: Some(rid(8)) }), |
| 278 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 279 | Dat::U8(4), |
| 280 | list(vec![s("t"), Dat::BU64(payload(1, 4)), opt(Some(b32(8)))]), |
| 281 | ])]), |
| 282 | ), |
| 283 | ( |
| 284 | env(MsgKind::AntiEntropyReply { |
| 285 | table: "t".to_string(), |
| 286 | records: vec![rec(5, "t", 1)], |
| 287 | requested_ids: vec![rid(6), rid(7)], |
| 288 | bulk: true, |
| 289 | after: None, |
| 290 | next: Some(rid(9)), |
| 291 | }), |
| 292 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 293 | Dat::U8(5), |
| 294 | list(vec![ |
| 295 | s("t"), list(vec![record_dat(5, "t", 1)]), list(vec![b32(6), b32(7)]), Dat::Bool(true), |
| 296 | opt(None), opt(Some(b32(9))), |
| 297 | ]), |
| 298 | ])]), |
| 299 | ), |
| 300 | ( |
| 301 | env(MsgKind::AntiEntropyPush { table: "t".to_string(), records: vec![rec(5, "t", 1)] }), |
| 302 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 303 | Dat::U8(6), |
| 304 | list(vec![s("t"), list(vec![record_dat(5, "t", 1)])]), |
| 305 | ])]), |
| 306 | ), |
| 307 | ( |
| 308 | env(MsgKind::CohortSubmit { record: rec(5, "ledger", 6) }), |
| 309 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![Dat::U8(7), record_dat(5, "ledger", 6)])]), |
| 310 | ), |
| 311 | ( |
| 312 | env(MsgKind::CohortPropose { |
| 313 | table: "t".to_string(), |
| 314 | id: rid(3), |
| 315 | proposal: Proposal { |
| 316 | view: 9, |
| 317 | phase: Phase::PreCommit, |
| 318 | block_hash: bytes32(9), |
| 319 | block: Some(payload(2, 3)), |
| 320 | justify: Some(qc(8, Phase::Prepare, &[4])), |
| 321 | }, |
| 322 | }), |
| 323 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 324 | Dat::U8(8), |
| 325 | list(vec![s("t"), b32(3), list(vec![ |
| 326 | Dat::U64(9), |
| 327 | Dat::U8(1), |
| 328 | b32(9), |
| 329 | opt(Some(Dat::BU64(payload(2, 3)))), |
| 330 | opt(Some(qc_dat(8, 0, vec![(4, payload(4, 64))]))), |
| 331 | ])]), |
| 332 | ])]), |
| 333 | ), |
| 334 | ( |
| 335 | env(MsgKind::CohortVote { |
| 336 | table: "t".to_string(), |
| 337 | id: rid(3), |
| 338 | vote: Vote { view: 6, phase: Phase::Commit, block_hash: bytes32(9), voter: 3, signature: payload(1, 5) }, |
| 339 | }), |
| 340 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 341 | Dat::U8(9), |
| 342 | list(vec![s("t"), b32(3), list(vec![ |
| 343 | Dat::U64(6), |
| 344 | Dat::U8(2), |
| 345 | b32(9), |
| 346 | Dat::U16(3), |
| 347 | Dat::BU64(payload(1, 5)), |
| 348 | ])]), |
| 349 | ])]), |
| 350 | ), |
| 351 | ( |
| 352 | env(MsgKind::CohortNewView { |
| 353 | table: "t".to_string(), |
| 354 | id: rid(3), |
| 355 | new_view: NewView { view: 7, sender: 2, prepare_qc: Some(qc(6, Phase::Decide, &[0, 2])) }, |
| 356 | }), |
| 357 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 358 | Dat::U8(10), |
| 359 | list(vec![s("t"), b32(3), list(vec![ |
| 360 | Dat::U64(7), |
| 361 | Dat::U16(2), |
| 362 | opt(Some(qc_dat(6, 3, vec![(0, payload(0, 64)), (2, payload(2, 64))]))), |
| 363 | ])]), |
| 364 | ])]), |
| 365 | ), |
| 366 | ( |
| 367 | env(MsgKind::CohortNewView { |
| 368 | table: "t".to_string(), |
| 369 | id: rid(3), |
| 370 | new_view: NewView { view: 7, sender: 2, prepare_qc: None }, |
| 371 | }), |
| 372 | list(vec![Dat::U8(1), b32(1), b32(2), list(vec![ |
| 373 | Dat::U8(10), |
| 374 | list(vec![s("t"), b32(3), list(vec![Dat::U64(7), Dat::U16(2), opt(None)])]), |
| 375 | ])]), |
| 376 | ), |
| 377 | ]; |
| 378 | assert_eq!(WIRE_VERSION, 1); |
| 379 | let mut kinds = BTreeSet::new(); |
| 380 | for (e, want) in &cases { |
| 381 | let got = res!(e.to_dat()); |
| 382 | assert_eq!(&got, want, "The {} layout moved.", e.body.label()); |
| 383 | assert_eq!(res!(e.encode()), bdat(want), "The {} bytes moved.", e.body.label()); |
| 384 | kinds.insert(e.body.label()); |
| 385 | } |
| 386 | assert_eq!(kinds.len(), 10, "The pinned layouts cover only {:?}.", kinds); |
| 387 | Ok(()) |
| 388 | } |
| 389 | |
| 390 | #[test] |
| 391 | fn decode_refuses_malformed() -> Outcome<()> { |
| 392 | // The baseline is good, so a refusal below is the fault planted and not a broken fixture. |
| 393 | let good = bdat(&put_dat(1, 1)); |
| 394 | let want = env(MsgKind::ReplicatePut { record: rec(5, "ledger", 6) }); |
| 395 | assert_eq!(res!(Envelope::decode(&good)), want); |
| 396 | |
| 397 | assert_refused("version 0", &bdat(&put_dat(0, 1))); |
| 398 | assert_refused("version 2", &bdat(&put_dat(2, 1))); |
| 399 | assert_refused("kind 0", &bdat(&put_dat(1, 0))); |
| 400 | assert_refused("kind 11", &bdat(&put_dat(1, 11))); |
| 401 | assert_refused("kind 255", &bdat(&put_dat(1, 255))); |
| 402 | |
| 403 | // Trailing bytes, one and a whole second envelope. |
| 404 | let mut one = good.clone(); |
| 405 | one.push(0); |
| 406 | assert_refused("one trailing byte", &one); |
| 407 | let mut two = good.clone(); |
| 408 | two.extend_from_slice(&good); |
| 409 | assert_refused("two envelopes", &two); |
| 410 | |
| 411 | // Nothing, and every proper prefix of a valid envelope. |
| 412 | assert_refused("empty", &[]); |
| 413 | let rich = res!(deepest().encode()); |
| 414 | for n in 0..rich.len() { |
| 415 | assert_refused(&fmt!("a prefix of {} bytes", n), &rich[..n]); |
| 416 | } |
| 417 | |
| 418 | // A 31-byte id in each place an id sits, and ids in other widths. |
| 419 | let short = |d: Dat| bdat(&list(vec![ |
| 420 | Dat::U8(1), |
| 421 | b32(1), |
| 422 | b32(2), |
| 423 | list(vec![Dat::U8(1), list(vec![d, s("ledger"), Dat::BU64(payload(5, 6))])]), |
| 424 | ])); |
| 425 | assert_refused("a 31-byte record id as BU8", &short(Dat::BU8(bytes32(5)[..31].to_vec()))); |
| 426 | assert_refused("a 32-byte record id as BU8", &short(Dat::BU8(bytes32(5).to_vec()))); |
| 427 | assert_refused("a 16-byte record id", &short(Dat::B16([5u8; 16]))); |
| 428 | let short_from = bdat(&list(vec![ |
| 429 | Dat::U8(1), |
| 430 | Dat::BU8(bytes32(1)[..31].to_vec()), |
| 431 | b32(2), |
| 432 | list(vec![Dat::U8(1), record_dat(5, "ledger", 6)]), |
| 433 | ])); |
| 434 | assert_refused("a 31-byte sender", &short_from); |
| 435 | |
| 436 | // A field missing, and a field too many, in the envelope, the body and the record. |
| 437 | let body = list(vec![Dat::U8(1), record_dat(5, "ledger", 6)]); |
| 438 | assert_refused("an envelope without a body", &bdat(&list(vec![Dat::U8(1), b32(1), b32(2)]))); |
| 439 | assert_refused("an envelope with a fifth field", &bdat(&list(vec![ |
| 440 | Dat::U8(1), b32(1), b32(2), body.clone(), Dat::U8(0), |
| 441 | ]))); |
| 442 | assert_refused("a body without its fields", &bdat(&list(vec![ |
| 443 | Dat::U8(1), b32(1), b32(2), list(vec![Dat::U8(1)]), |
| 444 | ]))); |
| 445 | assert_refused("a record without a value", &bdat(&list(vec![ |
| 446 | Dat::U8(1), b32(1), b32(2), list(vec![Dat::U8(1), list(vec![b32(5), s("ledger")])]), |
| 447 | ]))); |
| 448 | assert_refused("a record with a fourth field", &bdat(&list(vec![ |
| 449 | Dat::U8(1), b32(1), b32(2), |
| 450 | list(vec![Dat::U8(1), list(vec![b32(5), s("ledger"), Dat::BU64(Vec::new()), Dat::U8(0)])]), |
| 451 | ]))); |
| 452 | |
| 453 | // Shapes the encoder never writes: a narrower integer, a short byte string, a flag as a number. |
| 454 | assert_refused("a version as U16", &bdat(&list(vec![Dat::U16(1), b32(1), b32(2), body.clone()]))); |
| 455 | assert_refused("a value as BU8", &bdat(&list(vec![ |
| 456 | Dat::U8(1), b32(1), b32(2), |
| 457 | list(vec![Dat::U8(1), list(vec![b32(5), s("ledger"), Dat::BU8(vec![1, 2, 3])])]), |
| 458 | ]))); |
| 459 | assert_refused("an envelope that is no list", &bdat(&Dat::U8(1))); |
| 460 | let vote = |phase: Dat, voter: Dat| bdat(&list(vec![ |
| 461 | Dat::U8(1), b32(1), b32(2), |
| 462 | list(vec![Dat::U8(9), list(vec![s("t"), b32(3), list(vec![ |
| 463 | Dat::U64(6), phase, b32(9), voter, Dat::BU64(Vec::new()), |
| 464 | ])])]), |
| 465 | ])); |
| 466 | let ok = vote(Dat::U8(2), Dat::U16(3)); |
| 467 | assert!(Envelope::decode(&ok).is_ok(), "The baseline vote must decode."); |
| 468 | assert_refused("phase 4", &vote(Dat::U8(4), Dat::U16(3))); |
| 469 | assert_refused("phase 255", &vote(Dat::U8(255), Dat::U16(3))); |
| 470 | assert_refused("a phase as U16", &vote(Dat::U16(2), Dat::U16(3))); |
| 471 | assert_refused("a voter as U8", &vote(Dat::U8(2), Dat::U8(3))); |
| 472 | let reply_with = |bulk: Dat, after: Dat, next: Dat| bdat(&list(vec![ |
| 473 | Dat::U8(1), b32(1), b32(2), |
| 474 | list(vec![Dat::U8(5), list(vec![s("t"), list(vec![]), list(vec![]), bulk, after, next])]), |
| 475 | ])); |
| 476 | let reply = |bulk: Dat| reply_with(bulk, opt(None), opt(Some(b32(4)))); |
| 477 | assert!(Envelope::decode(&reply(Dat::Bool(true))).is_ok(), "The baseline reply must decode."); |
| 478 | assert_refused("bulk as a number", &reply(Dat::U8(1))); |
| 479 | assert_refused("a cursor without its option", &reply_with(Dat::Bool(true), b32(4), opt(None))); |
| 480 | assert_refused("a cursor option holding a number", &reply_with(Dat::Bool(true), opt(None), opt(Some(Dat::U8(4))))); |
| 481 | assert_refused("a cursor of the wrong length", &reply_with(Dat::Bool(true), opt(Some(Dat::BU64(vec![1, 2]))), opt(None))); |
| 482 | let get = |rec: Dat| bdat(&list(vec![ |
| 483 | Dat::U8(1), b32(1), b32(2), |
| 484 | list(vec![Dat::U8(3), list(vec![Dat::U64(1), rec])]), |
| 485 | ])); |
| 486 | assert!(Envelope::decode(&get(opt(None))).is_ok(), "The baseline response must decode."); |
| 487 | assert_refused("a record without its option", &get(record_dat(5, "t", 1))); |
| 488 | assert_refused("a record option holding a number", &get(opt(Some(Dat::U8(1))))); |
| 489 | Ok(()) |
| 490 | } |
| 491 | |
| 492 | // Every one-byte change to a valid envelope is either refused or read as an envelope the encoder |
| 493 | // would write back as those same bytes, so decode accepts no shape encode cannot produce. |
| 494 | #[test] |
| 495 | fn decode_accepts_only_what_encode_writes() -> Outcome<()> { |
| 496 | let mut seeds = vec![ |
| 497 | env(MsgKind::GetResponse { request_id: 300, record: Some(rec(5, "ledger", 4)) }), |
| 498 | env(MsgKind::AntiEntropyReply { |
| 499 | table: "t".to_string(), |
| 500 | records: vec![rec(5, "t", 2)], |
| 501 | requested_ids: vec![rid(6)], |
| 502 | bulk: true, |
| 503 | after: Some(rid(2)), |
| 504 | next: None, |
| 505 | }), |
| 506 | ]; |
| 507 | seeds.push(env(MsgKind::CohortNewView { |
| 508 | table: "t".to_string(), |
| 509 | id: rid(3), |
| 510 | new_view: NewView { view: 7, sender: 2, prepare_qc: Some(qc(6, Phase::Decide, &[0])) }, |
| 511 | })); |
| 512 | let mut accepted = 0usize; |
| 513 | for seed in &seeds { |
| 514 | let bytes = res!(seed.encode()); |
| 515 | for i in 0..bytes.len() { |
| 516 | for x in 0..=255u8 { |
| 517 | if x == bytes[i] { |
| 518 | continue; |
| 519 | } |
| 520 | let mut b = bytes.clone(); |
| 521 | b[i] = x; |
| 522 | if let Ok(e) = Envelope::decode(&b) { |
| 523 | accepted += 1; |
| 524 | let again = res!(e.encode()); |
| 525 | let at = again.iter().zip(b.iter()).position(|(p, q)| p != q); |
| 526 | assert_eq!( |
| 527 | again, b, |
| 528 | "Byte {} set to {} was read as {} but encodes to other bytes, first \ |
| 529 | differing at {:?} ({:?} against {:?}).", |
| 530 | i, x, e.body.label(), at, |
| 531 | at.map(|k| again[k]), at.map(|k| b[k]), |
| 532 | |
| 533 | ); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | // Some changes are legitimate: a different id, value or number is another valid envelope. |
| 539 | assert!(accepted > 0, "No one-byte change was accepted, so the test checks nothing."); |
| 540 | |
| 541 | // Arbitrary bytes never panic. |
| 542 | let mut state: u64 = 0x2545_F491_4F6C_DD1D; |
| 543 | for round in 0..3000usize { |
| 544 | let len = round % 97; |
| 545 | let mut b = Vec::with_capacity(len); |
| 546 | for _ in 0..len { |
| 547 | state ^= state << 13; |
| 548 | state ^= state >> 7; |
| 549 | state ^= state << 17; |
| 550 | b.push((state >> 32) as u8); |
| 551 | } |
| 552 | let _ = Envelope::decode(&b); |
| 553 | } |
| 554 | Ok(()) |
| 555 | } |
| 556 | |
| 557 | // A list of lists `levels` deep, built as bytes: a recursive `Dat` of that depth would overflow |
| 558 | // the stack of this test before it reached the decoder. |
| 559 | fn nested_list_bytes(levels: usize) -> Vec<u8> { |
| 560 | fn c64(n: usize) -> Vec<u8> { |
| 561 | let be = (n as u64).to_be_bytes(); |
| 562 | let skip = be.iter().take_while(|b| **b == 0).count(); |
| 563 | let mut v = vec![Dat::C64_CODE_START + (8 - skip) as u8]; |
| 564 | v.extend_from_slice(&be[skip..]); |
| 565 | v |
| 566 | } |
| 567 | let mut inner = vec![Dat::LIST_CODE]; |
| 568 | inner.extend(c64(0)); |
| 569 | for _ in 1..levels { |
| 570 | let mut outer = vec![Dat::LIST_CODE]; |
| 571 | outer.extend(c64(inner.len())); |
| 572 | outer.extend(inner); |
| 573 | inner = outer; |
| 574 | } |
| 575 | inner |
| 576 | } |
| 577 | |
| 578 | #[test] |
| 579 | fn decode_is_bounded() -> Outcome<()> { |
| 580 | assert_eq!(MAX_ENVELOPE_BYTES, 16 * 1024 * 1024); |
| 581 | assert!(MAX_ENVELOPE_DEPTH >= 9, "The limit must admit the deepest shape written."); |
| 582 | // The deepest shape written is admitted. |
| 583 | let deep = deepest(); |
| 584 | assert_eq!(res!(Envelope::decode(&res!(deep.encode()))), deep); |
| 585 | |
| 586 | // A list 10,000 deep is refused as too deep, before the decoder recurses to it. |
| 587 | let lists = nested_list_bytes(10_000); |
| 588 | match Envelope::decode(&lists) { |
| 589 | Ok(_) => panic!("A list 10,000 deep decoded as an envelope."), |
| 590 | Err(e) => assert!( |
| 591 | tags_of(&e).contains(&ErrTag::Excessive), |
| 592 | "A list 10,000 deep was not refused as Excessive: {}", e, |
| 593 | ), |
| 594 | } |
| 595 | // A depth the decoder could reach, but the limit forbids: the limit and not the shape check |
| 596 | // is what refuses it. |
| 597 | let limit = MAX_ENVELOPE_DEPTH + 1; |
| 598 | match Envelope::decode(&nested_list_bytes(limit)) { |
| 599 | Ok(_) => panic!("A list {} deep decoded as an envelope.", limit), |
| 600 | Err(e) => assert!( |
| 601 | tags_of(&e).contains(&ErrTag::Excessive), |
| 602 | "A list {} deep, one past the limit, was not refused as Excessive: {}", limit, e, |
| 603 | ), |
| 604 | } |
| 605 | match Envelope::decode(&nested_list_bytes(MAX_ENVELOPE_DEPTH)) { |
| 606 | Ok(_) => panic!("A list of lists decoded as an envelope."), |
| 607 | Err(e) => assert!( |
| 608 | !tags_of(&e).contains(&ErrTag::Excessive), |
| 609 | "A list exactly at the depth limit was refused as Excessive: {}", e, |
| 610 | ), |
| 611 | } |
| 612 | |
| 613 | // A buffer one byte over the size limit is refused for its size, one at the limit is not. |
| 614 | let over = vec![0u8; MAX_ENVELOPE_BYTES + 1]; |
| 615 | match Envelope::decode(&over) { |
| 616 | Ok(_) => panic!("A buffer over the size limit decoded."), |
| 617 | Err(e) => assert!( |
| 618 | tags_of(&e).contains(&ErrTag::Excessive), |
| 619 | "A buffer over the size limit was not refused as Excessive: {}", e, |
| 620 | ), |
| 621 | } |
| 622 | let at = vec![0u8; MAX_ENVELOPE_BYTES]; |
| 623 | match Envelope::decode(&at) { |
| 624 | Ok(_) => panic!("A buffer of zeros decoded."), |
| 625 | Err(e) => assert!( |
| 626 | !tags_of(&e).contains(&ErrTag::Excessive), |
| 627 | "A buffer exactly at the size limit was refused as Excessive: {}", e, |
| 628 | ), |
| 629 | } |
| 630 | |
| 631 | // The encoder refuses what no peer would read, so an envelope that encodes also decodes. |
| 632 | let huge = env(MsgKind::ReplicatePut { record: Record::new(rid(1), "t", vec![7u8; MAX_ENVELOPE_BYTES]) }); |
| 633 | match huge.encode() { |
| 634 | Ok(b) => panic!("An envelope of {} bytes encoded past the limit.", b.len()), |
| 635 | Err(e) => assert!( |
| 636 | tags_of(&e).contains(&ErrTag::Excessive), |
| 637 | "An oversize envelope was not refused as Excessive: {}", e, |
| 638 | ), |
| 639 | } |
| 640 | Ok(()) |
| 641 | } |
| 642 | |
| 643 | |
| 644 | // The value cap. A buffer of one-byte values must not become a gibibyte of `Dat`, and no envelope |
| 645 | // the encoder writes may be refused by the cap. |
| 646 | |
| 647 | #[test] |
| 648 | fn a_16_mib_page_of_small_records_encodes_and_decodes_under_the_value_cap() -> Outcome<()> { |
| 649 | let small = |n: u32| { |
| 650 | let mut id = [0u8; 32]; |
| 651 | id[..4].copy_from_slice(&n.to_be_bytes()); |
| 652 | Record { id: RecordId::from_bytes(id), table: String::new(), value: Vec::new() } |
| 653 | }; |
| 654 | let push = |records: Vec<Record>| env(MsgKind::AntiEntropyPush { table: String::new(), records }); |
| 655 | // The size of one record, from the difference between two envelopes. |
| 656 | let one = res!(push(vec![small(0)]).encode()).len(); |
| 657 | let two = res!(push(vec![small(0), small(1)]).encode()).len(); |
| 658 | let each = two - one; |
| 659 | let n = (MAX_ENVELOPE_BYTES - one) / each + 1; |
| 660 | let records: Vec<Record> = (0..n as u32).map(small).collect(); |
| 661 | let bytes = res!(push(records).encode()); |
| 662 | assert!(bytes.len() > MAX_ENVELOPE_BYTES - each, "The page is {} of {} bytes.", bytes.len(), MAX_ENVELOPE_BYTES); |
| 663 | // Four values a record: its list, id, table and value. |
| 664 | let values = 4 * n; |
| 665 | assert!(values < MAX_ENVELOPE_VALUES, "{} values in a full page, over the cap of {}.", values, MAX_ENVELOPE_VALUES); |
| 666 | match res!(Envelope::decode(&bytes)).body { |
| 667 | MsgKind::AntiEntropyPush { records, .. } => assert_eq!(records.len(), n), |
| 668 | other => return Err(err!("Decoded {:?}.", other; Test, Mismatch)), |
| 669 | } |
| 670 | Ok(()) |
| 671 | } |
| 672 | |
| 673 | #[test] |
| 674 | fn a_16_mib_list_of_one_byte_values_is_refused_at_the_value_cap() -> Outcome<()> { |
| 675 | let n = MAX_ENVELOPE_BYTES - 8; |
| 676 | // Under 2^24, so the minimal c64 is three bytes. |
| 677 | let mut b = vec![Dat::LIST_CODE, Dat::C64_CODE_START + 3]; |
| 678 | b.extend_from_slice(&(n as u32).to_be_bytes()[1..]); |
| 679 | b.resize(b.len() + n, Dat::EMPTY_CODE); |
| 680 | assert!(b.len() <= MAX_ENVELOPE_BYTES); |
| 681 | let msg = match Envelope::decode(&b) { |
| 682 | Ok(e) => return Err(err!("A buffer of one-byte values decoded to {:?}.", e.body.label(); Test, Invalid)), |
| 683 | Err(e) => fmt!("{}", e), |
| 684 | }; |
| 685 | assert!(msg.contains(&fmt!("maximum of {} values", MAX_ENVELOPE_VALUES)), "The refusal does not name the cap: {}", msg); |
| 686 | assert!(msg.contains(&fmt!("value number {}", MAX_ENVELOPE_VALUES + 1)), "The refusal did not stop at the cap: {}", msg); |
| 687 | Ok(()) |
| 688 | } |