matter_kit/bdx/transfer.rs
1//! Negotiating a BDX session and keeping it honest (§11.22.5, §11.22.6.1).
2//!
3//! Nothing here encodes or decodes. [`message`](super::message) does that; this is the part
4//! that says whether a message was *allowed* — the block counters are in order, the blocks are
5//! within the negotiated size, the promised number of bytes actually arrived — and it says so
6//! with the [`StatusCode`] the peer has to be told, because that is the whole content of a
7//! BDX failure.
8
9use super::message::{Init, MessageType, ReceiveAccept, SendAccept, TransferControl, VERSION};
10use super::{Rejected, StatusCode};
11
12/// Where §11.22.6.1's counters start.
13///
14/// §11.22.6.2 says the block counter "SHOULD start at 0 at the start of the transfer", and
15/// §11.22.6.5 states it as fact for the trivial case: "In that trivial case, the Block Counter
16/// would be 0 in the BlockEOF." Both halves here require it rather than adopting whatever the
17/// first message carried — a Sender and a Receiver that each picked their own starting point
18/// would disagree on every counter after it, and §11.22.6.1's only tool for noticing is the
19/// comparison that would then always fail.
20const FIRST_COUNTER: u32 = 0;
21
22/// A block size that fits a Matter message over any transport.
23///
24/// 1024 octets leaves comfortable room under §4.4.4's 1280-octet datagram limit for the
25/// message header, the block counter and the AEAD tag. A transfer over TCP can negotiate far
26/// more; this is the figure that always works.
27pub const DEFAULT_MAX_BLOCK_SIZE: u16 = 1024;
28
29/// Which Init started the transfer, and so which end sends the data.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum Direction {
32 /// `SendInit`: the Initiator is the Sender (§11.22.2.3).
33 Upload,
34 /// `ReceiveInit`: the Initiator is the Receiver — an OTA image, a diagnostic log.
35 Download,
36}
37
38impl Direction {
39 /// The opcode that opens the transfer.
40 #[must_use]
41 pub const fn init(self) -> MessageType {
42 match self {
43 Self::Upload => MessageType::SendInit,
44 Self::Download => MessageType::ReceiveInit,
45 }
46 }
47
48 /// The opcode that accepts it.
49 #[must_use]
50 pub const fn accept(self) -> MessageType {
51 match self {
52 Self::Upload => MessageType::SendAccept,
53 Self::Download => MessageType::ReceiveAccept,
54 }
55 }
56}
57
58/// What a Responder will agree to.
59///
60/// Every field is a reason §11.22.5.1 gives for rejecting an Init, turned into a number the
61/// application sets once instead of a check it has to remember to write.
62#[derive(Debug, Clone, Copy)]
63pub struct Limits {
64 /// The largest block this node will handle. The negotiated size is the smaller of this and
65 /// the Initiator's proposal.
66 pub max_block_size: u16,
67 /// The largest definite length to accept — `LENGTH_TOO_LARGE` past it.
68 pub max_length: Option<u64>,
69 /// The smallest definite length that makes sense here — `LENGTH_TOO_SHORT` under it.
70 pub min_length: Option<u64>,
71 /// Whether a transfer may proceed without a definite length — `LENGTH_REQUIRED` if not.
72 pub require_definite_length: bool,
73 /// Whether a non-zero start offset is supported — `START_OFFSET_NOT_SUPPORTED` if not.
74 ///
75 /// §11.22.5.1.3: "Receivers are not required to accept non-zero start offset transfers.
76 /// Devices SHOULD make every attempt to support non-zero start offset."
77 pub allow_start_offset: bool,
78 /// How much data this node actually has past the start offset, for a
79 /// [`Download`](Direction::Download).
80 ///
81 /// §11.22.5.4.4 is the reason this exists: the accepted length is the proposed one only
82 /// when the file is that big, and "smaller than the proposed definite length, if the
83 /// remaining data in the file beyond the Start Offset is smaller than the proposed length".
84 pub available: Option<u64>,
85 /// Whether to turn the transfer away with `RESPONDER_BUSY`. §11.22.5.1: an Initiator
86 /// "SHOULD wait at least 60 seconds" before trying again.
87 pub busy: bool,
88}
89
90impl Default for Limits {
91 fn default() -> Self {
92 Self {
93 max_block_size: DEFAULT_MAX_BLOCK_SIZE,
94 max_length: None,
95 min_length: None,
96 require_definite_length: false,
97 allow_start_offset: true,
98 available: None,
99 busy: false,
100 }
101 }
102}
103
104/// The parameters both ends settled on: what the Accept message says, in one value.
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub struct Parameters {
107 /// The chosen version and the one chosen drive mode.
108 pub control: TransferControl,
109 /// The Max Block Size in force. A block may be shorter; none may be longer.
110 pub max_block_size: u16,
111 /// Where in the file the transfer starts.
112 pub start_offset: u64,
113 /// The number of octets that will be transferred, when it is known in advance. `None` is
114 /// §11.22.5.1.5's indefinite length, and turns off the end-of-transfer length check.
115 pub length: Option<u64>,
116}
117
118impl Parameters {
119 /// Whether the Sender paces the transfer (`TC[SENDER_DRIVE]`).
120 #[must_use]
121 pub const fn sender_drives(&self) -> bool {
122 self.control.sender_drive
123 }
124
125 /// Whether the Receiver paces it with BlockQuery messages (`TC[RECEIVER_DRIVE]`).
126 #[must_use]
127 pub const fn receiver_drives(&self) -> bool {
128 self.control.receiver_drive
129 }
130
131 /// The `SendAccept` that carries these parameters (§11.22.5.2).
132 #[must_use]
133 pub const fn send_accept<'a>(&self, metadata: &'a [u8]) -> SendAccept<'a> {
134 SendAccept {
135 control: self.control,
136 max_block_size: self.max_block_size,
137 metadata,
138 }
139 }
140
141 /// The `ReceiveAccept` that carries these parameters (§11.22.5.3).
142 #[must_use]
143 pub const fn receive_accept<'a>(&self, metadata: &'a [u8]) -> ReceiveAccept<'a> {
144 ReceiveAccept {
145 control: self.control,
146 max_block_size: self.max_block_size,
147 length: self.length,
148 metadata,
149 }
150 }
151
152 /// Checks a Responder's `SendAccept` against what was proposed, for an upload.
153 ///
154 /// §11.22.5.1: "The parameters in the SendAccept/ReceiveAccept message SHALL be used in the
155 /// transfer. If those parameters are unacceptable to the Initiator, it SHALL abort the
156 /// transfer with an appropriate error."
157 pub fn from_send_accept(proposed: &Init<'_>, accept: &SendAccept<'_>) -> Rejected<Self> {
158 let control = check_accepted(proposed.control, accept.control)?;
159 Ok(Self {
160 control,
161 max_block_size: check_block_size(proposed.max_block_size, accept.max_block_size)?,
162 start_offset: proposed.start_offset.unwrap_or(0),
163 // A SendAccept has no length field: the Sender said how much it would send, and a
164 // Responder that could not take that much had to reject the SendInit instead.
165 length: proposed.definite_length,
166 })
167 }
168
169 /// Checks a Responder's `ReceiveAccept` against what was proposed, for a download.
170 pub fn from_receive_accept(proposed: &Init<'_>, accept: &ReceiveAccept<'_>) -> Rejected<Self> {
171 let control = check_accepted(proposed.control, accept.control)?;
172 if let (Some(asked), Some(offered)) = (proposed.definite_length, accept.length) {
173 // §11.22.5.4.4: the accepted length is the proposed one, or smaller when the file
174 // is. Larger is the Responder answering a question that was not asked.
175 if offered > asked {
176 return Err(StatusCode::LengthTooLarge);
177 }
178 }
179 Ok(Self {
180 control,
181 max_block_size: check_block_size(proposed.max_block_size, accept.max_block_size)?,
182 start_offset: proposed.start_offset.unwrap_or(0),
183 length: accept.length,
184 })
185 }
186}
187
188/// The Responder's half of §11.22.5.1: accept an Init, or say why not.
189///
190/// The returned parameters go straight into [`Parameters::send_accept`] or
191/// [`Parameters::receive_accept`] and into a [`Sender`] or [`Receiver`]; the error goes into a
192/// [`StatusReport`](crate::sc::StatusReport) via [`report`](super::report).
193pub fn negotiate(direction: Direction, init: &Init<'_>, limits: &Limits) -> Rejected<Parameters> {
194 if limits.busy {
195 return Err(StatusCode::ResponderBusy);
196 }
197 if init.max_block_size == 0 {
198 return Err(StatusCode::BadMessageContents);
199 }
200 // §11.22.5.1.1: "At least one of the PTC[RECEIVER_DRIVE] or PTC[SENDER_DRIVE] field bits
201 // SHALL be set in order for the Responder to set the final transfer control."
202 if !init.control.sender_drive && !init.control.receiver_drive {
203 return Err(StatusCode::TransferMethodNotSupported);
204 }
205 let start_offset = match init.start_offset {
206 // §11.22.5.1.3 speaks of the *presence* of the field, not of a non-zero value.
207 Some(_) if !limits.allow_start_offset => return Err(StatusCode::StartOffsetNotSupported),
208 Some(offset) => offset,
209 None => 0,
210 };
211 if let Some(length) = init.definite_length {
212 if limits.max_length.is_some_and(|max| length > max) {
213 return Err(StatusCode::LengthTooLarge);
214 }
215 if limits.min_length.is_some_and(|min| length < min) {
216 return Err(StatusCode::LengthTooShort);
217 }
218 } else if limits.require_definite_length {
219 return Err(StatusCode::LengthRequired);
220 }
221 let length = match direction {
222 // The Initiator is the Sender and committed to a size; there is no field to answer it
223 // with, so the only choices were to take it or to have rejected it above.
224 Direction::Upload => init.definite_length,
225 // §11.22.5.4.4: the Responder is the Sender and knows what the file actually holds.
226 Direction::Download => match (init.definite_length, limits.available) {
227 (Some(asked), Some(have)) => Some(asked.min(have)),
228 (Some(asked), None) => Some(asked),
229 (None, have) => have,
230 },
231 };
232 Ok(Parameters {
233 control: TransferControl {
234 // §11.22.5.4.1: "the newest version that is supported by the Responder and is not
235 // newer than the proposed version". This crate speaks only version 0, the one BDX
236 // has had since Matter 1.0, and 0 is never newer than anything — so there is no
237 // version a proposal could carry that VERSION_NOT_SUPPORTED would answer.
238 version: VERSION,
239 // "If the Initiator proposed both … the Responder SHALL default to
240 // TC[SENDER_DRIVE]", which keeps the request/response shape.
241 sender_drive: init.control.sender_drive,
242 receiver_drive: init.control.receiver_drive && !init.control.sender_drive,
243 // "Support for the asynchronous mode is provisional and SHALL not be chosen by the
244 // Responder."
245 asynchronous: false,
246 },
247 max_block_size: init.max_block_size.min(limits.max_block_size),
248 start_offset,
249 length,
250 })
251}
252
253/// Checks the Transfer Control an Accept came back with (§11.22.5.4.1).
254fn check_accepted(
255 proposed: TransferControl,
256 accepted: TransferControl,
257) -> Rejected<TransferControl> {
258 if accepted.version > proposed.version {
259 return Err(StatusCode::VersionNotSupported);
260 }
261 // Provisional, and a Responder is told not to choose it — so a transfer that comes back
262 // asynchronous is one this crate will not drive.
263 if accepted.asynchronous || !accepted.is_decided() {
264 return Err(StatusCode::TransferMethodNotSupported);
265 }
266 let offered = (accepted.sender_drive && proposed.sender_drive)
267 || (accepted.receiver_drive && proposed.receiver_drive);
268 if !offered {
269 return Err(StatusCode::TransferMethodNotSupported);
270 }
271 Ok(accepted)
272}
273
274/// §11.22.5.4.3: the accepted Max Block Size "SHALL be less than or equal to the proposed max
275/// block size".
276fn check_block_size(proposed: u16, accepted: u16) -> Rejected<u16> {
277 if accepted == 0 || accepted > proposed {
278 return Err(StatusCode::BadMessageContents);
279 }
280 Ok(accepted)
281}
282
283/// The data-carrying half of a transfer: what a Sender may send next, and when.
284///
285/// Whether this node is the Initiator or the Responder does not change any rule here — only
286/// [`Parameters::control`] does, by saying which end drives.
287#[derive(Debug)]
288pub struct Sender {
289 params: Parameters,
290 /// Where in the file the next block is read from. Moves with the data sent *and* with a
291 /// `BlockQueryWithSkip`, which is the only reason it is not `start_offset + sent`.
292 cursor: u64,
293 sent: u64,
294 /// The counter of the last block sent, once one has been.
295 counter: Option<u32>,
296 /// A `BlockQuery` is outstanding, so a driving Receiver is waiting for a block.
297 queried: bool,
298 /// The last block sent was a `BlockEOF`.
299 finished: bool,
300 /// A `BlockAckEOF` closed the session (§11.22.2.8).
301 complete: bool,
302}
303
304impl Sender {
305 /// A Sender that has agreed `params` and sent nothing.
306 #[must_use]
307 pub const fn new(params: Parameters) -> Self {
308 Self {
309 cursor: params.start_offset,
310 params,
311 sent: 0,
312 counter: None,
313 queried: false,
314 finished: false,
315 complete: false,
316 }
317 }
318
319 /// The negotiated parameters.
320 #[must_use]
321 pub const fn parameters(&self) -> &Parameters {
322 &self.params
323 }
324
325 /// The offset in the file the next block should be read from.
326 #[must_use]
327 pub const fn cursor(&self) -> u64 {
328 self.cursor
329 }
330
331 /// How many octets have been put into blocks so far.
332 #[must_use]
333 pub const fn sent(&self) -> u64 {
334 self.sent
335 }
336
337 /// Whether the Receiver has acknowledged the `BlockEOF`, ending the session.
338 #[must_use]
339 pub const fn is_complete(&self) -> bool {
340 self.complete
341 }
342
343 /// Whether a block may be sent right now.
344 ///
345 /// In Sender drive, always, until the end. In Receiver drive, only while a `BlockQuery` is
346 /// outstanding — which is the whole point of the mode: a sleepy Receiver is not woken by
347 /// blocks it did not ask for.
348 #[must_use]
349 pub const fn may_send(&self) -> bool {
350 if self.finished || self.complete {
351 return false;
352 }
353 self.params.control.sender_drive || self.queried
354 }
355
356 /// The largest block that may be sent next: the negotiated size, or what is left of a
357 /// definite length if that is smaller.
358 #[must_use]
359 pub fn room(&self) -> usize {
360 let block = usize::from(self.params.max_block_size);
361 match self.params.length {
362 Some(length) => usize::try_from(length.saturating_sub(self.sent))
363 .unwrap_or(usize::MAX)
364 .min(block),
365 None => block,
366 }
367 }
368
369 /// Takes a `BlockQuery` from a driving Receiver (§11.22.6.2).
370 pub fn on_query(&mut self, counter: u32) -> Rejected<()> {
371 self.on_query_with_skip(counter, 0).map(|_| ())
372 }
373
374 /// Takes a `BlockQueryWithSkip` (§11.22.6.3) and returns the cursor it moved to.
375 ///
376 /// Skipping past the end of the file is explicitly not an error: "there SHALL be no error
377 /// indicated when receiving a request to skip past the end of the transferable data", and
378 /// the answer is an empty `BlockEOF`.
379 pub fn on_query_with_skip(&mut self, counter: u32, bytes_to_skip: u64) -> Rejected<u64> {
380 if self.finished || self.complete {
381 return Err(StatusCode::UnexpectedMessage);
382 }
383 // A query is what a *driving Receiver* sends. In Sender drive nobody may ask.
384 if !self.params.control.receiver_drive {
385 return Err(StatusCode::UnexpectedMessage);
386 }
387 if self.queried {
388 return Err(StatusCode::UnexpectedMessage);
389 }
390 if counter != self.next_counter() {
391 return Err(StatusCode::BadBlockCounter);
392 }
393 self.queried = true;
394 self.cursor = self.cursor.saturating_add(bytes_to_skip);
395 Ok(self.cursor)
396 }
397
398 /// Registers a block of `len` octets, and returns the Block Counter to put in it.
399 ///
400 /// `eof` chooses between a `Block` and a `BlockEOF`, and the difference is not only the
401 /// opcode: §11.22.6.4 requires a `Block` to carry at least one octet, while §11.22.6.5
402 /// allows an empty `BlockEOF` "to indicate an empty file".
403 pub fn block(&mut self, len: usize, eof: bool) -> Rejected<u32> {
404 if !self.may_send() {
405 return Err(StatusCode::UnexpectedMessage);
406 }
407 if len > usize::from(self.params.max_block_size) || (len == 0 && !eof) {
408 return Err(StatusCode::BadMessageContents);
409 }
410 let len64 = u64::try_from(len).unwrap_or(u64::MAX);
411 let total = self
412 .sent
413 .checked_add(len64)
414 .ok_or(StatusCode::LengthTooLarge)?;
415 if let Some(length) = self.params.length {
416 // The Sender committed to exactly this many octets: overrunning it, or stopping
417 // early, is the discrepancy §11.22.6.5 has the Receiver fail the transfer over.
418 if total > length || (eof && total != length) {
419 return Err(StatusCode::LengthMismatch);
420 }
421 }
422 let counter = self.next_counter();
423 self.counter = Some(counter);
424 self.sent = total;
425 self.cursor = self.cursor.saturating_add(len64);
426 self.queried = false;
427 self.finished = eof;
428 Ok(counter)
429 }
430
431 /// Takes a `BlockAck` or `BlockAckEOF` (§11.22.6.6, §11.22.6.7).
432 pub fn on_ack(&mut self, counter: u32, eof: bool) -> Rejected<()> {
433 let Some(last) = self.counter else {
434 return Err(StatusCode::UnexpectedMessage);
435 };
436 if self.complete {
437 return Err(StatusCode::UnexpectedMessage);
438 }
439 // §11.22.6.7: a BlockAckEOF answers a BlockEOF and nothing else, and a plain BlockAck
440 // does not end a session.
441 if eof != self.finished {
442 return Err(StatusCode::UnexpectedMessage);
443 }
444 if counter != last {
445 return Err(StatusCode::BadBlockCounter);
446 }
447 self.complete = eof;
448 Ok(())
449 }
450
451 /// §11.22.6.1: ascending, sequential, "modulo 2^32 integer arithmetic".
452 const fn next_counter(&self) -> u32 {
453 match self.counter {
454 Some(last) => last.wrapping_add(1),
455 None => FIRST_COUNTER,
456 }
457 }
458}
459
460/// The receiving half: what a Receiver may accept next, and what it owes the Sender.
461#[derive(Debug)]
462pub struct Receiver {
463 params: Parameters,
464 received: u64,
465 /// The counter of the last block taken, once one has been.
466 counter: Option<u32>,
467 /// The counter of the next query to send, in Receiver drive.
468 query: u32,
469 /// A block has arrived and has not been acknowledged.
470 pending: bool,
471 /// The last block taken was a `BlockEOF`.
472 finished: bool,
473 /// A `BlockAckEOF` has been sent, ending the session.
474 complete: bool,
475}
476
477impl Receiver {
478 /// A Receiver that has agreed `params` and taken nothing.
479 #[must_use]
480 pub const fn new(params: Parameters) -> Self {
481 Self {
482 params,
483 received: 0,
484 counter: None,
485 query: FIRST_COUNTER,
486 pending: false,
487 finished: false,
488 complete: false,
489 }
490 }
491
492 /// The negotiated parameters.
493 #[must_use]
494 pub const fn parameters(&self) -> &Parameters {
495 &self.params
496 }
497
498 /// How many octets have arrived.
499 #[must_use]
500 pub const fn received(&self) -> u64 {
501 self.received
502 }
503
504 /// Whether the session has ended with a `BlockAckEOF` (§11.22.2.8).
505 #[must_use]
506 pub const fn is_complete(&self) -> bool {
507 self.complete
508 }
509
510 /// The Block Counter for the next `BlockQuery` or `BlockQueryWithSkip` (§11.22.6.2).
511 ///
512 /// A query "implies a BlockAck of the previous block if no BlockAck was explicitly sent",
513 /// so asking for the next block also clears the outstanding acknowledgement.
514 pub fn query(&mut self) -> Rejected<u32> {
515 if !self.params.control.receiver_drive || self.finished || self.complete {
516 return Err(StatusCode::UnexpectedMessage);
517 }
518 // One query at a time: the transfer "consists of a series of request/responses, each
519 // one initiated by the Driver" (§11.22.2.6), and a second query before the first is
520 // answered is two outstanding requests.
521 if self.awaiting() {
522 return Err(StatusCode::UnexpectedMessage);
523 }
524 self.pending = false;
525 let counter = self.query;
526 self.query = self.query.wrapping_add(1);
527 Ok(counter)
528 }
529
530 /// Takes a `Block` or `BlockEOF` (§11.22.6.4, §11.22.6.5).
531 ///
532 /// `len` is the data length, which the caller has in hand either way; the data itself is
533 /// the application's to store.
534 pub fn on_block(&mut self, counter: u32, len: usize, eof: bool) -> Rejected<()> {
535 if self.finished || self.complete {
536 return Err(StatusCode::UnexpectedMessage);
537 }
538 // In Receiver drive a block answers a query; one that answers nothing is a Sender that
539 // did not wait.
540 if self.params.control.receiver_drive && !self.awaiting() {
541 return Err(StatusCode::UnexpectedMessage);
542 }
543 if counter != self.next_counter() {
544 return Err(StatusCode::BadBlockCounter);
545 }
546 if len > usize::from(self.params.max_block_size) || (len == 0 && !eof) {
547 return Err(StatusCode::BadMessageContents);
548 }
549 let len64 = u64::try_from(len).unwrap_or(u64::MAX);
550 let total = self
551 .received
552 .checked_add(len64)
553 .ok_or(StatusCode::LengthTooLarge)?;
554 if let Some(length) = self.params.length {
555 // §11.22.6.5: "the recipient SHALL verify that the pre-negotiated file size was
556 // transferred". Too much is as wrong as too little, and is caught a block earlier.
557 if total > length || (eof && total != length) {
558 return Err(StatusCode::LengthMismatch);
559 }
560 }
561 self.received = total;
562 self.counter = Some(counter);
563 self.pending = true;
564 self.finished = eof;
565 Ok(())
566 }
567
568 /// Acknowledges the block just taken: the opcode to send and the counter to put in it.
569 ///
570 /// §11.22.6.6: the Block Counter "SHALL correspond to the Block Counter which was embedded
571 /// in the Block being acknowledged", so it is never the caller's to choose.
572 pub fn ack(&mut self) -> Rejected<(MessageType, u32)> {
573 let (Some(counter), true) = (self.counter, self.pending) else {
574 return Err(StatusCode::UnexpectedMessage);
575 };
576 self.pending = false;
577 self.complete = self.finished;
578 Ok((
579 if self.finished {
580 MessageType::BlockAckEof
581 } else {
582 MessageType::BlockAck
583 },
584 counter,
585 ))
586 }
587
588 /// Whether a query has gone out that no block has answered.
589 const fn awaiting(&self) -> bool {
590 // `query` counts queries sent; `counter` the blocks taken. They are equal exactly when
591 // every query has been answered.
592 match self.counter {
593 Some(last) => self.query != last.wrapping_add(1),
594 None => self.query != FIRST_COUNTER,
595 }
596 }
597
598 /// §11.22.6.1: ascending, sequential, modulo 2^32.
599 const fn next_counter(&self) -> u32 {
600 match self.counter {
601 Some(last) => last.wrapping_add(1),
602 None => FIRST_COUNTER,
603 }
604 }
605}
606
607#[cfg(test)]
608mod tests {
609 use super::*;
610
611 fn agreed(sender_drive: bool) -> Parameters {
612 Parameters {
613 control: TransferControl {
614 version: VERSION,
615 sender_drive,
616 receiver_drive: !sender_drive,
617 asynchronous: false,
618 },
619 max_block_size: 16,
620 start_offset: 0,
621 length: None,
622 }
623 }
624
625 /// §11.22.6.1: "if the last Block Counter was 0xFFFF_FFFF, the next expected Block Counter
626 /// would be 0x0000_0000". Reaching that by counting would take four billion blocks, so the
627 /// counter is set directly — this is the one rule an integration test cannot get at.
628 #[test]
629 fn block_counters_wrap_at_2_to_the_32() {
630 let mut sender = Sender::new(agreed(true));
631 sender.counter = Some(u32::MAX);
632 assert_eq!(sender.block(4, false), Ok(0));
633
634 let mut receiver = Receiver::new(agreed(true));
635 receiver.counter = Some(u32::MAX);
636 assert_eq!(receiver.on_block(0, 4, false), Ok(()));
637 // And one that did not wrap is still out of order.
638 let mut receiver = Receiver::new(agreed(true));
639 receiver.counter = Some(u32::MAX);
640 assert_eq!(
641 receiver.on_block(1, 4, false),
642 Err(StatusCode::BadBlockCounter)
643 );
644 }
645
646 #[test]
647 fn a_query_wraps_the_same_way() {
648 let mut sender = Sender::new(agreed(false));
649 sender.counter = Some(u32::MAX);
650 assert_eq!(sender.on_query(0), Ok(()));
651 }
652}