Skip to main content

ctrader_connector/fix/
codec.rs

1//! Tag-value encoding of FIX 4.4 messages and their framing on a byte stream.
2//!
3//! A message on the wire is a run of `tag=value` fields, each closed by the delimiter byte
4//! [`SOH`]. It opens with the begin string (8) and the body length (9), and closes with the
5//! checksum (10): the sum of every byte before the checksum field, modulo 256, as three digits.
6//! The body length counts the bytes after the body length field up to the checksum field.
7//!
8//! [`encode`] writes the header in the order 8, 9, 35, 49, 56, 57, 50, 34, 52. [`decode`]
9//! takes one whole message off the front of a buffer and accepts the header fields in any
10//! order after the message type, as the server does not send them in the order it asks for.
11//!
12//! ```
13//! use ctrader_connector::common::time::Timestamp;
14//! use ctrader_connector::fix::codec::{Field, Frame, Header, decode, encode};
15//!
16//! let header = Header::new("0", "demo.broker.1", "CSERVER", 7, Timestamp::EPOCH);
17//! let frame = Frame::new(header, vec![Field::new(112, "ping")]);
18//! let mut bytes = encode(&frame)?;
19//! assert_eq!(decode(&mut bytes)?, Some(frame));
20//! assert!(bytes.is_empty());
21//! # Ok::<(), ctrader_connector::common::errors::FixError>(())
22//! ```
23
24use std::fmt;
25use std::fmt::Write as _;
26
27use crate::common::errors::FixError;
28use crate::common::time::Timestamp;
29
30/// Byte that closes every field.
31pub const SOH: u8 = 1;
32
33/// Begin string of every message.
34pub const BEGIN_STRING: &str = "FIX.4.4";
35
36/// Longest body accepted or produced, in bytes.
37pub const MAX_BODY_LENGTH: usize = 4 * 1024 * 1024;
38
39const PREFIX: &[u8] = b"8=FIX.4.4\x019=";
40const LENGTH_DIGITS: usize = 7;
41const TRAILER_LENGTH: usize = 7;
42const MILLIS_PER_DAY: i64 = 86_400_000;
43
44const TAG_BEGIN_STRING: u32 = 8;
45const TAG_BODY_LENGTH: u32 = 9;
46const TAG_CHECKSUM: u32 = 10;
47const TAG_MSG_TYPE: u32 = 35;
48const TAG_SENDER_COMP_ID: u32 = 49;
49const TAG_TARGET_COMP_ID: u32 = 56;
50const TAG_TARGET_SUB_ID: u32 = 57;
51const TAG_SENDER_SUB_ID: u32 = 50;
52const TAG_SEQ: u32 = 34;
53const TAG_SENDING_TIME: u32 = 52;
54const TAG_POSS_DUP: u32 = 43;
55const TAG_ORIG_SENDING_TIME: u32 = 122;
56const TAG_PASSWORD: u32 = 554;
57
58const ENVELOPE: [u32; 9] = [
59    TAG_MSG_TYPE,
60    TAG_SENDER_COMP_ID,
61    TAG_TARGET_COMP_ID,
62    TAG_TARGET_SUB_ID,
63    TAG_SENDER_SUB_ID,
64    TAG_SEQ,
65    TAG_SENDING_TIME,
66    TAG_POSS_DUP,
67    TAG_ORIG_SENDING_TIME,
68];
69
70/// One `tag=value` field.
71#[derive(Clone, PartialEq, Eq, Hash)]
72pub struct Field {
73    /// Number of the tag.
74    pub tag: u32,
75    /// Value as the wire carries it.
76    pub value: String,
77}
78
79impl Field {
80    /// Builds a field.
81    pub fn new(tag: u32, value: impl Into<String>) -> Self {
82        Self {
83            tag,
84            value: value.into(),
85        }
86    }
87}
88
89impl fmt::Debug for Field {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        if self.tag == TAG_PASSWORD {
92            write!(f, "{}=***", self.tag)
93        } else {
94            write!(f, "{}={}", self.tag, self.value)
95        }
96    }
97}
98
99/// Standard header of a message, without the begin string and the body length.
100#[derive(Debug, Clone, PartialEq, Eq, Hash)]
101pub struct Header {
102    /// Type of the message (35).
103    pub msg_type: String,
104    /// Identifier of the sender (49).
105    pub sender_comp_id: String,
106    /// Identifier of the receiver (56).
107    pub target_comp_id: String,
108    /// Session qualifier of the receiver (57).
109    pub target_sub_id: Option<String>,
110    /// Session qualifier of the sender (50).
111    pub sender_sub_id: Option<String>,
112    /// Sequence number of the message (34).
113    pub seq: u64,
114    /// Time the message was sent (52).
115    pub sending_time: Timestamp,
116    /// Tells whether the message repeats one sent before under the same sequence number (43).
117    pub poss_dup: bool,
118    /// Time the repeated message was sent first (122).
119    pub orig_sending_time: Option<Timestamp>,
120}
121
122impl Header {
123    /// Builds a header without session qualifiers that marks no repetition.
124    pub fn new(
125        msg_type: impl Into<String>,
126        sender_comp_id: impl Into<String>,
127        target_comp_id: impl Into<String>,
128        seq: u64,
129        sending_time: Timestamp,
130    ) -> Self {
131        Self {
132            msg_type: msg_type.into(),
133            sender_comp_id: sender_comp_id.into(),
134            target_comp_id: target_comp_id.into(),
135            target_sub_id: None,
136            sender_sub_id: None,
137            seq,
138            sending_time,
139            poss_dup: false,
140            orig_sending_time: None,
141        }
142    }
143
144    /// Sets both session qualifiers.
145    #[must_use]
146    pub fn qualified(mut self, target_sub_id: &str, sender_sub_id: &str) -> Self {
147        self.target_sub_id = Some(target_sub_id.to_owned());
148        self.sender_sub_id = Some(sender_sub_id.to_owned());
149        self
150    }
151}
152
153/// One message: its header and the fields of its body in the order of the wire.
154#[derive(Debug, Clone, PartialEq, Eq, Hash)]
155pub struct Frame {
156    /// Standard header.
157    pub header: Header,
158    /// Fields of the body.
159    pub body: Vec<Field>,
160}
161
162impl Frame {
163    /// Builds a message.
164    #[must_use]
165    pub const fn new(header: Header, body: Vec<Field>) -> Self {
166        Self { header, body }
167    }
168}
169
170/// Computes the checksum of the bytes: their sum modulo 256.
171#[must_use]
172pub fn checksum(bytes: &[u8]) -> u8 {
173    bytes.iter().fold(0_u8, |sum, byte| sum.wrapping_add(*byte))
174}
175
176fn put(out: &mut String, msg_type: &str, tag: u32, value: &str) -> Result<(), FixError> {
177    let plain = !value.is_empty() && !value.bytes().any(|byte| byte == SOH);
178    let framing = matches!(tag, 0 | TAG_BEGIN_STRING | TAG_BODY_LENGTH | TAG_CHECKSUM);
179    if !plain || framing {
180        return Err(FixError::InvalidField {
181            msg_type: msg_type.to_owned(),
182            tag,
183        });
184    }
185    let _ = write!(out, "{tag}={value}\u{1}");
186    Ok(())
187}
188
189/// Encodes a message as the bytes of the wire.
190///
191/// # Errors
192///
193/// Returns [`FixError::InvalidField`] when a value is empty or holds the delimiter, or a body
194/// field carries a tag of the envelope: of the framing (8, 9, 10) or of the standard header
195/// (34, 35, 43, 49, 50, 52, 56, 57, 122), which [`Header`] alone sets, and
196/// [`FixError::TooLong`] when the body exceeds [`MAX_BODY_LENGTH`].
197pub fn encode(frame: &Frame) -> Result<Vec<u8>, FixError> {
198    let header = &frame.header;
199    let kind = header.msg_type.as_str();
200    let mut body = String::new();
201    put(&mut body, kind, TAG_MSG_TYPE, kind)?;
202    put(&mut body, kind, TAG_SENDER_COMP_ID, &header.sender_comp_id)?;
203    put(&mut body, kind, TAG_TARGET_COMP_ID, &header.target_comp_id)?;
204    if let Some(qualifier) = &header.target_sub_id {
205        put(&mut body, kind, TAG_TARGET_SUB_ID, qualifier)?;
206    }
207    if let Some(qualifier) = &header.sender_sub_id {
208        put(&mut body, kind, TAG_SENDER_SUB_ID, qualifier)?;
209    }
210    put(&mut body, kind, TAG_SEQ, &header.seq.to_string())?;
211    let sent = format_time(header.sending_time, true);
212    put(&mut body, kind, TAG_SENDING_TIME, &sent)?;
213    if header.poss_dup {
214        put(&mut body, kind, TAG_POSS_DUP, "Y")?;
215    }
216    if let Some(first) = header.orig_sending_time {
217        let first = format_time(first, true);
218        put(&mut body, kind, TAG_ORIG_SENDING_TIME, &first)?;
219    }
220    for field in &frame.body {
221        if ENVELOPE.contains(&field.tag) {
222            return Err(FixError::InvalidField {
223                msg_type: kind.to_owned(),
224                tag: field.tag,
225            });
226        }
227        put(&mut body, kind, field.tag, &field.value)?;
228    }
229    if body.len() > MAX_BODY_LENGTH {
230        return Err(FixError::TooLong {
231            length: body.len(),
232            limit: MAX_BODY_LENGTH,
233        });
234    }
235    let mut out = format!("8={BEGIN_STRING}\u{1}9={}\u{1}", body.len()).into_bytes();
236    out.extend_from_slice(body.as_bytes());
237    let sum = checksum(&out);
238    out.extend_from_slice(format!("10={sum:03}\u{1}").as_bytes());
239    Ok(out)
240}
241
242fn body_length(digits: &[u8]) -> Result<usize, FixError> {
243    let numeric = !digits.is_empty()
244        && digits.len() <= LENGTH_DIGITS
245        && digits.iter().all(u8::is_ascii_digit);
246    if !numeric {
247        return Err(FixError::Garbled("the body length is not a number"));
248    }
249    let length = digits.iter().fold(0_usize, |length, digit| {
250        length
251            .saturating_mul(10)
252            .saturating_add(usize::from(digit.saturating_sub(b'0')))
253    });
254    if length > MAX_BODY_LENGTH {
255        return Err(FixError::TooLong {
256            length,
257            limit: MAX_BODY_LENGTH,
258        });
259    }
260    Ok(length)
261}
262
263fn found_checksum(trailer: &[u8]) -> Option<u8> {
264    let digits = trailer.strip_prefix(b"10=")?.strip_suffix(&[SOH])?;
265    if digits.len() != 3 || !digits.iter().all(u8::is_ascii_digit) {
266        return None;
267    }
268    let value = digits.iter().fold(0_u16, |value, digit| {
269        value
270            .saturating_mul(10)
271            .saturating_add(u16::from(digit.saturating_sub(b'0')))
272    });
273    u8::try_from(value).ok()
274}
275
276fn fields(body: &[u8]) -> Result<Vec<Field>, FixError> {
277    let Some(inner) = body.strip_suffix(&[SOH]) else {
278        return Err(FixError::Garbled(
279            "the body does not end with the delimiter",
280        ));
281    };
282    inner
283        .split(|byte| *byte == SOH)
284        .map(|piece| {
285            let text = std::str::from_utf8(piece)
286                .map_err(|_| FixError::Garbled("a field is not valid UTF-8"))?;
287            let (tag, value) = text
288                .split_once('=')
289                .ok_or(FixError::Garbled("a field has no value"))?;
290            let numeric =
291                !tag.is_empty() && tag.len() <= 9 && tag.bytes().all(|b| b.is_ascii_digit());
292            let tag = tag
293                .parse::<u32>()
294                .ok()
295                .filter(|tag| numeric && *tag > 0)
296                .ok_or(FixError::Garbled("a tag is not a positive number"))?;
297            if value.is_empty() {
298                return Err(FixError::Garbled("a field has an empty value"));
299            }
300            Ok(Field::new(tag, value))
301        })
302        .collect()
303}
304
305fn header_of(all: Vec<Field>) -> Result<Frame, FixError> {
306    let mut fields = all.into_iter();
307    let msg_type = match fields.next() {
308        Some(field) if field.tag == TAG_MSG_TYPE => field.value,
309        _ => return Err(FixError::Garbled("the message type is not the third field")),
310    };
311    let mut slots: [(u32, Option<String>); 8] = [
312        (TAG_SENDER_COMP_ID, None),
313        (TAG_TARGET_COMP_ID, None),
314        (TAG_TARGET_SUB_ID, None),
315        (TAG_SENDER_SUB_ID, None),
316        (TAG_SEQ, None),
317        (TAG_SENDING_TIME, None),
318        (TAG_POSS_DUP, None),
319        (TAG_ORIG_SENDING_TIME, None),
320    ];
321    let mut body = Vec::new();
322    for field in fields {
323        match slots.iter_mut().find(|(tag, _)| *tag == field.tag) {
324            Some((_, slot)) if slot.is_none() => *slot = Some(field.value),
325            Some(_) => {
326                return Err(FixError::InvalidField {
327                    msg_type,
328                    tag: field.tag,
329                });
330            }
331            None => body.push(field),
332        }
333    }
334    let [
335        sender,
336        target,
337        target_sub,
338        sender_sub,
339        seq,
340        sent,
341        poss_dup,
342        first,
343    ] = slots;
344    let missing = |tag: u32| FixError::MissingField {
345        msg_type: msg_type.clone(),
346        tag,
347    };
348    let invalid = |tag: u32| FixError::InvalidField {
349        msg_type: msg_type.clone(),
350        tag,
351    };
352    let seq = seq
353        .1
354        .ok_or_else(|| missing(TAG_SEQ))?
355        .parse::<u64>()
356        .map_err(|_| invalid(TAG_SEQ))?;
357    let sending_time = sent
358        .1
359        .ok_or_else(|| missing(TAG_SENDING_TIME))
360        .and_then(|text| parse_time(&text).ok_or_else(|| invalid(TAG_SENDING_TIME)))?;
361    let orig_sending_time = first
362        .1
363        .map(|text| parse_time(&text).ok_or_else(|| invalid(TAG_ORIG_SENDING_TIME)))
364        .transpose()?;
365    let poss_dup = match poss_dup.1.as_deref() {
366        None | Some("N") => false,
367        Some("Y") => true,
368        Some(_) => return Err(invalid(TAG_POSS_DUP)),
369    };
370    let header = Header {
371        sender_comp_id: sender.1.ok_or_else(|| missing(TAG_SENDER_COMP_ID))?,
372        target_comp_id: target.1.ok_or_else(|| missing(TAG_TARGET_COMP_ID))?,
373        target_sub_id: target_sub.1,
374        sender_sub_id: sender_sub.1,
375        seq,
376        sending_time,
377        poss_dup,
378        orig_sending_time,
379        msg_type,
380    };
381    Ok(Frame { header, body })
382}
383
384/// Takes one whole message off the front of `buffer` and decodes it.
385///
386/// Returns `Ok(None)` and leaves the buffer untouched while the message is incomplete. A
387/// message whose length is known is removed from the buffer even when it is rejected, so the
388/// next call reads the message after it. [`FixError::Garbled`] for bytes that do not open a
389/// message and [`FixError::TooLong`] leave the buffer as it is: the stream has lost its
390/// framing and must be abandoned.
391///
392/// # Errors
393///
394/// Returns [`FixError::Garbled`] when the bytes are not a message, [`FixError::TooLong`] when
395/// the announced body exceeds [`MAX_BODY_LENGTH`], [`FixError::Checksum`] when the checksum
396/// does not match, and [`FixError::MissingField`] or [`FixError::InvalidField`] when the
397/// header is incomplete or holds a value of the wrong form.
398pub fn decode(buffer: &mut Vec<u8>) -> Result<Option<Frame>, FixError> {
399    match take(buffer)? {
400        None => Ok(None),
401        Some(Ok(frame)) => Ok(Some(frame)),
402        Some(Err(unreadable)) => Err(unreadable.error),
403    }
404}
405
406/// A whole message that was taken off the stream and cannot be read.
407#[derive(Debug, Clone, PartialEq, Eq)]
408pub(crate) struct Unreadable {
409    /// Sequence number of the message, when its bytes are intact and carry exactly one.
410    pub(crate) seq: Option<u64>,
411    /// Type of the message (35), when its bytes are intact and name one.
412    pub(crate) msg_type: Option<String>,
413    /// Why the message cannot be read.
414    pub(crate) error: FixError,
415}
416
417fn scanned_seq(body: &[u8]) -> Option<u64> {
418    let mut numbers = body
419        .split(|byte| *byte == SOH)
420        .filter_map(|piece| piece.strip_prefix(b"34="));
421    let digits = numbers.next()?;
422    if numbers.next().is_some() {
423        return None;
424    }
425    std::str::from_utf8(digits).ok()?.parse().ok()
426}
427
428fn scanned_type(body: &[u8]) -> Option<String> {
429    let value = body
430        .split(|byte| *byte == SOH)
431        .find_map(|piece| piece.strip_prefix(b"35="))?;
432    Some(std::str::from_utf8(value).ok()?.to_owned())
433}
434
435/// Takes one whole message off the front of `buffer`, as [`decode`] does, and keeps apart
436/// the message that was removed but cannot be read from the stream that lost its framing.
437pub(crate) fn take(buffer: &mut Vec<u8>) -> Result<Option<Result<Frame, Unreadable>>, FixError> {
438    let common = buffer.len().min(PREFIX.len());
439    if buffer.get(..common) != PREFIX.get(..common) {
440        return Err(FixError::Garbled("the bytes do not open a message"));
441    }
442    let Some(rest) = buffer.get(PREFIX.len()..) else {
443        return Ok(None);
444    };
445    let Some(end) = rest.iter().position(|byte| *byte == SOH) else {
446        return body_length(
447            rest.get(..rest.len().min(LENGTH_DIGITS + 1))
448                .unwrap_or(rest),
449        )
450        .map(|_| None)
451        .or_else(|error| {
452            if rest.is_empty() {
453                Ok(None)
454            } else {
455                Err(error)
456            }
457        });
458    };
459    let length = body_length(rest.get(..end).unwrap_or(rest))?;
460    let start = PREFIX.len().saturating_add(end).saturating_add(1);
461    let trailer = start.saturating_add(length);
462    let total = trailer.saturating_add(TRAILER_LENGTH);
463    if buffer.len() < total {
464        return Ok(None);
465    }
466    let message: Vec<u8> = buffer.drain(..total).collect();
467    let (counted, tail) = message.split_at_checked(trailer).unwrap_or((&message, &[]));
468    let damaged = |error| {
469        Ok(Some(Err(Unreadable {
470            seq: None,
471            msg_type: None,
472            error,
473        })))
474    };
475    let Some(found) = found_checksum(tail) else {
476        return damaged(FixError::Garbled(
477            "the checksum field does not close the message",
478        ));
479    };
480    let computed = checksum(counted);
481    if computed != found {
482        return damaged(FixError::Checksum { computed, found });
483    }
484    let body = counted.get(start..).unwrap_or(&[]);
485    Ok(Some(fields(body).and_then(header_of).map_err(|error| {
486        Unreadable {
487            seq: scanned_seq(body),
488            msg_type: scanned_type(body),
489            error,
490        }
491    })))
492}
493
494fn civil(days: i64) -> (i64, i64, i64) {
495    let shifted = days.saturating_add(719_468);
496    let era = shifted.div_euclid(146_097);
497    let day_of_era = shifted.rem_euclid(146_097);
498    let year_of_era =
499        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
500    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
501    let shifted_month = (5 * day_of_year + 2) / 153;
502    let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
503    let month = if shifted_month < 10 {
504        shifted_month + 3
505    } else {
506        shifted_month - 9
507    };
508    let year = year_of_era.saturating_add(era.saturating_mul(400));
509    (if month <= 2 { year + 1 } else { year }, month, day)
510}
511
512fn days_before(year: i64, month: i64, day: i64) -> i64 {
513    let year = if month <= 2 { year - 1 } else { year };
514    let era = year.div_euclid(400);
515    let year_of_era = year.rem_euclid(400);
516    let shifted_month = (month + 9) % 12;
517    let day_of_year = (153 * shifted_month + 2) / 5 + day - 1;
518    let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
519    era * 146_097 + day_of_era - 719_468
520}
521
522/// Writes a moment as the UTC timestamp of the wire, `YYYYMMDD-HH:MM:SS`, followed by the
523/// milliseconds when `millis` is set.
524#[must_use]
525pub fn format_time(moment: Timestamp, millis: bool) -> String {
526    let total = moment.as_millis();
527    let (year, month, day) = civil(total.div_euclid(MILLIS_PER_DAY));
528    let rest = total.rem_euclid(MILLIS_PER_DAY);
529    let (hour, minute, second) = (rest / 3_600_000, rest / 60_000 % 60, rest / 1_000 % 60);
530    let mut text = format!("{year:04}{month:02}{day:02}-{hour:02}:{minute:02}:{second:02}");
531    if millis {
532        let _ = write!(text, ".{:03}", rest % 1_000);
533    }
534    text
535}
536
537fn number(text: &str, from: usize, to: usize) -> Option<i64> {
538    let digits = text.get(from..to)?;
539    if digits.bytes().all(|byte| byte.is_ascii_digit()) {
540        digits.parse().ok()
541    } else {
542        None
543    }
544}
545
546/// Reads a UTC timestamp of the wire, `YYYYMMDD-HH:MM:SS` with or without milliseconds.
547///
548/// Returns `None` when the text has another form or names a moment that does not exist.
549#[must_use]
550pub fn parse_time(text: &str) -> Option<Timestamp> {
551    let millis = match text.len() {
552        17 => 0,
553        21 if text.get(17..18) == Some(".") => number(text, 18, 21)?,
554        _ => return None,
555    };
556    let punctuated = text.get(8..9) == Some("-")
557        && text.get(11..12) == Some(":")
558        && text.get(14..15) == Some(":");
559    if !punctuated {
560        return None;
561    }
562    let (year, month, day) = (
563        number(text, 0, 4)?,
564        number(text, 4, 6)?,
565        number(text, 6, 8)?,
566    );
567    let (hour, minute, second) = (
568        number(text, 9, 11)?,
569        number(text, 12, 14)?,
570        number(text, 15, 17)?,
571    );
572    let days = days_before(year, month, day);
573    let exists = (1..=12).contains(&month) && civil(days) == (year, month, day);
574    if !exists || hour > 23 || minute > 59 || second > 59 {
575        return None;
576    }
577    let of_day = hour * 3_600_000 + minute * 60_000 + second * 1_000 + millis;
578    Timestamp::from_millis(days.checked_mul(MILLIS_PER_DAY)?.checked_add(of_day)?).ok()
579}
580
581#[cfg(test)]
582mod tests {
583    use super::{
584        BEGIN_STRING, Field, Frame, Header, MAX_BODY_LENGTH, SOH, checksum, decode, encode,
585        format_time, parse_time, take,
586    };
587    use crate::common::errors::FixError;
588    use crate::common::time::Timestamp;
589
590    fn wire(text: &str) -> Vec<u8> {
591        text.replace('|', "\u{1}").into_bytes()
592    }
593
594    fn sealed(body: &str) -> Vec<u8> {
595        let body = body.replace('|', "\u{1}");
596        let mut out = format!("8={BEGIN_STRING}\u{1}9={}\u{1}{body}", body.len()).into_bytes();
597        let sum = checksum(&out);
598        out.extend_from_slice(format!("10={sum:03}\u{1}").as_bytes());
599        out
600    }
601
602    fn moment(millis: i64) -> Timestamp {
603        Timestamp::from_millis(millis).expect("moment")
604    }
605
606    fn heartbeat(seq: u64) -> Frame {
607        let header = Header::new(
608            "0",
609            "demo.broker.1",
610            "CSERVER",
611            seq,
612            moment(1_484_640_184_155),
613        )
614        .qualified("QUOTE", "QUOTE");
615        Frame::new(header, vec![Field::new(112, "ping")])
616    }
617
618    #[test]
619    fn header_is_written_in_the_order_the_server_asks_for() {
620        let bytes = encode(&heartbeat(7)).expect("encode");
621        let text = String::from_utf8(bytes)
622            .expect("text")
623            .replace('\u{1}', "|");
624        assert_eq!(
625            text,
626            "8=FIX.4.4|9=90|35=0|49=demo.broker.1|56=CSERVER|57=QUOTE|50=QUOTE|34=7|52=20170117-08:03:04.155|112=ping|10=146|"
627        );
628    }
629
630    #[test]
631    fn body_length_and_checksum_match_values_computed_independently() {
632        let example = wire(
633            "8=FIX.4.4|9=111|35=A|34=1|49=CSERVER|50=TRADE|52=20170117-08:03:04.509|56=live.theBroker.12345|57=any_string|98=0|108=30|141=Y|10=028|",
634        );
635        let mut buffer = example.clone();
636        let frame = decode(&mut buffer).expect("decode").expect("whole message");
637        assert!(buffer.is_empty());
638        assert_eq!(frame.header.msg_type, "A");
639        assert_eq!(frame.header.seq, 1);
640        assert_eq!(frame.header.sender_comp_id, "CSERVER");
641        assert_eq!(frame.header.sender_sub_id.as_deref(), Some("TRADE"));
642        assert_eq!(frame.header.target_sub_id.as_deref(), Some("any_string"));
643        assert_eq!(
644            format_time(frame.header.sending_time, true),
645            "20170117-08:03:04.509"
646        );
647        assert_eq!(
648            frame.body,
649            vec![
650                Field::new(98, "0"),
651                Field::new(108, "30"),
652                Field::new(141, "Y")
653            ]
654        );
655        let body = example
656            .get(..example.len() - 7)
657            .expect("message without the trailer");
658        assert_eq!(checksum(body), 28);
659    }
660
661    #[test]
662    fn message_round_trips_with_the_marks_of_a_repetition() {
663        let mut frame = heartbeat(12);
664        frame.header.poss_dup = true;
665        frame.header.orig_sending_time = Some(moment(1_484_640_000_000));
666        let mut bytes = encode(&frame).expect("encode");
667        assert_eq!(decode(&mut bytes).expect("decode"), Some(frame));
668    }
669
670    #[test]
671    fn messages_are_framed_across_partial_reads_and_several_per_read() {
672        let first = encode(&heartbeat(1)).expect("first");
673        let second = encode(&heartbeat(2)).expect("second");
674        let mut stream = first.clone();
675        stream.extend_from_slice(&second);
676        let mut buffer = Vec::new();
677        let mut decoded = Vec::new();
678        for byte in &stream {
679            buffer.push(*byte);
680            while let Some(frame) = decode(&mut buffer).expect("decode") {
681                decoded.push(frame.header.seq);
682            }
683        }
684        assert_eq!(decoded, vec![1, 2]);
685        let mut both = stream.clone();
686        assert_eq!(
687            decode(&mut both).expect("first").map(|f| f.header.seq),
688            Some(1)
689        );
690        assert_eq!(
691            decode(&mut both).expect("second").map(|f| f.header.seq),
692            Some(2)
693        );
694        assert_eq!(decode(&mut both).expect("empty"), None);
695    }
696
697    #[test]
698    fn bad_checksum_is_rejected_and_the_next_message_is_still_read() {
699        let mut bytes = encode(&heartbeat(1)).expect("encode");
700        let at = bytes.len() - 2;
701        let digit = bytes.get_mut(at).expect("last digit of the checksum");
702        *digit = if *digit == b'9' { b'0' } else { *digit + 1 };
703        bytes.extend_from_slice(&encode(&heartbeat(2)).expect("next"));
704        assert!(matches!(decode(&mut bytes), Err(FixError::Checksum { .. })));
705        assert_eq!(
706            decode(&mut bytes).expect("next").map(|f| f.header.seq),
707            Some(2)
708        );
709    }
710
711    #[test]
712    fn unreadable_message_keeps_its_number_only_when_its_bytes_are_intact() {
713        let seq = |bytes: &mut Vec<u8>| match take(bytes) {
714            Ok(Some(Err(unreadable))) => Ok(unreadable.seq),
715            other => Err(other),
716        };
717        assert_eq!(seq(&mut sealed("35=0|49=A|56=B|34=7|")), Ok(Some(7)));
718        assert_eq!(seq(&mut sealed("35=0|49=A|56=B|34=7|58=|")), Ok(Some(7)));
719        assert_eq!(seq(&mut sealed("35=0|49=A|56=B|34=7|34=8|")), Ok(None));
720        assert_eq!(seq(&mut sealed("35=0|49=A|56=B|34=x|")), Ok(None));
721        assert_eq!(seq(&mut sealed("35=0|49=A|56=B|")), Ok(None));
722        let mut damaged = sealed("35=0|49=A|56=B|34=7|");
723        let at = damaged.len() - 2;
724        let digit = damaged.get_mut(at).expect("last digit of the checksum");
725        *digit = if *digit == b'9' { b'0' } else { *digit + 1 };
726        assert_eq!(seq(&mut damaged), Ok(None));
727    }
728
729    #[test]
730    fn bad_body_length_is_rejected() {
731        let good = String::from_utf8(encode(&heartbeat(1)).expect("encode")).expect("text");
732        let mut short = good.replace("9=90", "9=89").into_bytes();
733        assert!(matches!(decode(&mut short), Err(FixError::Garbled(_))));
734        let mut long = good.replace("9=90", "9=91").into_bytes();
735        assert_eq!(decode(&mut long).expect("incomplete"), None);
736        long.push(b'8');
737        assert!(decode(&mut long).is_err());
738        let mut huge = wire("8=FIX.4.4|9=9999999|35=0|");
739        assert_eq!(
740            decode(&mut huge),
741            Err(FixError::TooLong {
742                length: 9_999_999,
743                limit: MAX_BODY_LENGTH
744            })
745        );
746        let mut endless = wire("8=FIX.4.4|9=12345678");
747        assert!(matches!(decode(&mut endless), Err(FixError::Garbled(_))));
748        let mut worded = wire("8=FIX.4.4|9=1x|");
749        assert!(matches!(decode(&mut worded), Err(FixError::Garbled(_))));
750    }
751
752    #[test]
753    fn incomplete_openings_wait_for_more_bytes() {
754        for opening in [
755            "",
756            "8",
757            "8=FIX.4.4",
758            "8=FIX.4.4|9=",
759            "8=FIX.4.4|9=1",
760            "8=FIX.4.4|9=10|35",
761        ] {
762            let mut buffer = wire(opening);
763            assert_eq!(decode(&mut buffer), Ok(None), "{opening}");
764            assert_eq!(buffer, wire(opening));
765        }
766    }
767
768    #[test]
769    fn header_fields_are_required_and_typed() {
770        let cases = [
771            ("49=A|56=B|34=1|52=20170117-08:03:04|", "the message type"),
772            ("35=0|56=B|34=1|52=20170117-08:03:04|", "sender"),
773            ("35=0|49=A|34=1|52=20170117-08:03:04|", "target"),
774            ("35=0|49=A|56=B|52=20170117-08:03:04|", "sequence number"),
775            ("35=0|49=A|56=B|34=1|", "sending time"),
776            (
777                "35=0|49=A|56=B|34=x|52=20170117-08:03:04|",
778                "sequence number form",
779            ),
780            ("35=0|49=A|56=B|34=1|52=2017|", "sending time form"),
781            (
782                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|43=X|",
783                "repetition mark",
784            ),
785            (
786                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|122=x|",
787                "first sending time",
788            ),
789            (
790                "35=0|49=A|49=A|56=B|34=1|52=20170117-08:03:04|",
791                "repeated field",
792            ),
793            (
794                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|112|",
795                "field without value",
796            ),
797            (
798                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|112=|",
799                "empty value",
800            ),
801            (
802                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|x=1|",
803                "worded tag",
804            ),
805            ("35=0|49=A|56=B|34=1|52=20170117-08:03:04|0=1|", "tag zero"),
806            (
807                "35=0|49=A|56=B|34=1|52=20170117-08:03:04|112=a",
808                "no closing delimiter",
809            ),
810        ];
811        for (body, what) in cases {
812            let mut buffer = sealed(body);
813            assert!(decode(&mut buffer).is_err(), "{what}");
814            assert!(buffer.is_empty(), "{what}");
815        }
816        let mut plain = sealed("35=0|49=A|56=B|34=1|52=20170117-08:03:04|43=N|");
817        let frame = decode(&mut plain).expect("decode").expect("message");
818        assert!(!frame.header.poss_dup);
819        assert_eq!(frame.header.target_sub_id, None);
820    }
821
822    #[test]
823    fn values_that_cannot_be_framed_are_refused_before_encoding() {
824        let refused = |field: Field| {
825            let mut frame = heartbeat(1);
826            frame.body = vec![field];
827            encode(&frame)
828        };
829        for field in [
830            Field::new(112, ""),
831            Field::new(112, "a\u{1}b"),
832            Field::new(8, "FIX.4.4"),
833            Field::new(9, "5"),
834            Field::new(10, "000"),
835            Field::new(0, "x"),
836            Field::new(34, "9"),
837            Field::new(35, "D"),
838            Field::new(43, "Y"),
839            Field::new(49, "other"),
840            Field::new(50, "QUOTE"),
841            Field::new(52, "20170117-08:03:04"),
842            Field::new(56, "other"),
843            Field::new(57, "QUOTE"),
844            Field::new(122, "20170117-08:03:04"),
845        ] {
846            assert_eq!(
847                refused(field.clone()),
848                Err(FixError::InvalidField {
849                    msg_type: "0".to_owned(),
850                    tag: field.tag
851                })
852            );
853        }
854        let long = Field::new(58, "x".repeat(MAX_BODY_LENGTH));
855        assert!(matches!(refused(long), Err(FixError::TooLong { .. })));
856        let mut unnamed = heartbeat(1);
857        unnamed.header.sender_comp_id = String::new();
858        assert!(encode(&unnamed).is_err());
859    }
860
861    #[test]
862    fn password_never_appears_in_debug_output() {
863        let field = Field::new(554, "hunter2");
864        assert_eq!(format!("{field:?}"), "554=***");
865        assert_eq!(format!("{:?}", Field::new(553, "12345")), "553=12345");
866    }
867
868    #[test]
869    fn malformed_input_never_panics() {
870        let good = encode(&heartbeat(3)).expect("encode");
871        let mut state = 0x9e37_79b9_7f4a_7c15_u64;
872        let mut next = || {
873            state ^= state << 13;
874            state ^= state >> 7;
875            state ^= state << 17;
876            state
877        };
878        let interesting = [SOH, b'=', b'8', b'9', b'1', b'0', b'|', 0, 255, b'F'];
879        for round in 0..4_000 {
880            let mut bytes = good.clone();
881            let edits = 1 + next() % 4;
882            for _ in 0..edits {
883                let at = usize::try_from(next()).unwrap_or(0) % bytes.len();
884                let pick = usize::try_from(next()).unwrap_or(0) % interesting.len();
885                let byte = interesting.get(pick).copied().unwrap_or(0);
886                match next() % 3 {
887                    0 => {
888                        if let Some(slot) = bytes.get_mut(at) {
889                            *slot = byte;
890                        }
891                    }
892                    1 => bytes.insert(at, byte),
893                    _ => {
894                        bytes.remove(at);
895                    }
896                }
897                if bytes.is_empty() {
898                    bytes.push(byte);
899                }
900            }
901            if round % 2 == 0 {
902                bytes.extend_from_slice(&good);
903            }
904            for _ in 0..8 {
905                let before = bytes.len();
906                match decode(&mut bytes) {
907                    Ok(None) => break,
908                    Err(_) if bytes.len() == before => break,
909                    Ok(Some(_)) | Err(_) => {}
910                }
911            }
912        }
913        for length in 0..good.len() {
914            let mut cut = good.get(..length).expect("prefix").to_vec();
915            assert_eq!(decode(&mut cut), Ok(None), "prefix of {length} bytes");
916        }
917    }
918
919    #[test]
920    fn timestamps_are_written_and_read_as_utc() {
921        let cases = [
922            (0, "19700101-00:00:00.000"),
923            (951_782_400_000, "20000229-00:00:00.000"),
924            (1_484_640_184_155, "20170117-08:03:04.155"),
925            (4_102_444_799_999, "20991231-23:59:59.999"),
926        ];
927        for (millis, text) in cases {
928            assert_eq!(format_time(moment(millis), true), text);
929            assert_eq!(parse_time(text), Some(moment(millis)));
930        }
931        assert_eq!(
932            format_time(moment(1_484_640_184_155), false),
933            "20170117-08:03:04"
934        );
935        assert_eq!(
936            parse_time("20170117-08:03:04"),
937            Some(moment(1_484_640_184_000))
938        );
939        for text in [
940            "",
941            "20170117",
942            "20170117 08:03:04",
943            "20170117-08-03-04",
944            "20170117-08:03:04.1",
945            "20170117-08:03:04,155",
946            "20170117-08:03:04.1x5",
947            "20170230-08:03:04",
948            "20171301-08:03:04",
949            "20170100-08:03:04",
950            "20170117-24:03:04",
951            "20170117-08:60:04",
952            "20170117-08:03:60",
953            "2017011x-08:03:04",
954            "19691231-23:59:59",
955            "+0170117-08:03:04",
956        ] {
957            assert_eq!(parse_time(text), None, "{text}");
958        }
959    }
960}