+
+ #[test]
+ fn test_buffer_basic() {
+ let mut buffer = KissBuffer::new();
+
+ // initial write is not a complete frame
+ let buf = buffer.buf_remaining();
+ buf[0] = FEND;
+ buffer.did_write(1);
+ assert!(buffer.next_frame().is_none());
+
+ // complete the frame
+ let buf = buffer.buf_remaining();
+ buf[0] = 0x10;
+ buf[1] = 0x01;
+ buf[2] = FEND;
+ buffer.did_write(3);
+
+ // everything should parse
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.len, 4);
+ assert_eq!(&next.data[0..4], &[FEND, 0x10, 0x01, FEND]);
+ assert_eq!(next.port().unwrap(), 1);
+ assert_eq!(next.command().unwrap(), KissCommand::DataFrame);
+ let mut payload_buf = [0u8; 1024];
+ let n = next.decode_payload(&mut payload_buf).unwrap();
+ assert_eq!(n, 1);
+ assert_eq!(&payload_buf[0..n], &[0x01]);
+ }
+
+ #[test]
+ fn test_buffer_double() {
+ let mut buffer = KissBuffer::new();
+ let buf = buffer.buf_remaining();
+ buf[0..8].copy_from_slice(&[FEND, 0x10, 0x01, FEND, FEND, 0x20, 02, FEND]);
+ buffer.did_write(8);
+
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 1);
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 2);
+ assert!(buffer.next_frame().is_none());
+ }
+
+ #[test]
+ fn test_buffer_double_shared_fend() {
+ let mut buffer = KissBuffer::new();
+ let buf = buffer.buf_remaining();
+ buf[0..7].copy_from_slice(&[FEND, 0x10, 0x01, FEND, 0x20, 02, FEND]);
+ buffer.did_write(7);
+
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 1);
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 2);
+ assert!(buffer.next_frame().is_none());
+ }
+
+ #[test]
+ fn test_buffer_extra_fend() {
+ let mut buffer = KissBuffer::new();
+ let buf = buffer.buf_remaining();
+ buf[0..10].copy_from_slice(&[FEND, FEND, FEND, 0x10, 0x01, FEND, FEND, 0x20, 02, FEND]);
+ buffer.did_write(10);
+
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 1);
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 2);
+ assert!(buffer.next_frame().is_none());
+ }
+
+ #[test]
+ fn test_buffer_oversize_frame() {
+ let mut buffer = KissBuffer::new();
+ let buf = buffer.buf_remaining();
+ buf[0] = FEND;
+ let len = buf.len();
+ assert_eq!(len, MAX_FRAME_LEN);
+ buffer.did_write(len);
+ assert!(buffer.next_frame().is_none());
+
+ let buf = buffer.buf_remaining();
+ let len = buf.len();
+ assert_eq!(len, MAX_FRAME_LEN); // should have flushed
+ for i in 0..len / 2 {
+ buf[i] = 0x00;
+ }
+ buffer.did_write(len / 2);
+ assert!(buffer.next_frame().is_none());
+
+ // confirm we resync if input goes back to normal
+ let buf = buffer.buf_remaining();
+ buf[0..4].copy_from_slice(&[FEND, 0x10, 0x01, FEND]);
+ buffer.did_write(4);
+ let next = buffer.next_frame().unwrap();
+ assert_eq!(next.port().unwrap(), 1);
+ assert!(buffer.next_frame().is_none());
+ }