Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 06b22ed

Browse files
author
Alex Helfet
committed
Added packet parsing unit tests.
1 parent d70a1d4 commit 06b22ed

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

src/decoder.rs

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,103 @@ impl<R: Read> Decoder<R> {
6262
}
6363
}
6464

65-
// TODO: Parse tests.
6665
/// Decode a single packet from a slice of bytes.
6766
pub fn from_slice(s: &[u8]) -> Result<Packet> {
6867
let mut d = Decoder::new(Cursor::new(Vec::from(s)));
6968
d.read_packet()
7069
}
7170

71+
#[cfg(test)]
72+
mod tests {
73+
use super::from_slice;
74+
use error::{Error, ErrorKind, Result};
75+
use packet::{Kind, Packet};
76+
use std::io;
77+
78+
#[test]
79+
fn header() {
80+
let p = decode_one(&[0x01, 0x11]);
81+
assert_eq!(p.header, 0x01);
82+
}
83+
84+
#[test]
85+
fn instrumentation_payload_1_byte() {
86+
let p = decode_one(&[0x01, 0x11]);
87+
match p.kind {
88+
Kind::Instrumentation(ref i) => {
89+
assert_eq!(*i.payload, [0x11]);
90+
},
91+
_ => panic!()
92+
}
93+
}
94+
95+
#[test]
96+
fn instrumentation_payload_2_bytes() {
97+
let p = decode_one(&[0x02, 0x11, 0x12]);
98+
match p.kind {
99+
Kind::Instrumentation(ref i) => {
100+
assert_eq!(*i.payload, [0x11, 0x12]);
101+
},
102+
_ => panic!()
103+
}
104+
}
105+
106+
#[test]
107+
fn instrumentation_payload_4_bytes() {
108+
let p = decode_one(&[0x03, 0x11, 0x12, 0x13, 0x14]);
109+
match p.kind {
110+
Kind::Instrumentation(ref i) => {
111+
assert_eq!(*i.payload, [0x11, 0x12, 0x13, 0x14]);
112+
},
113+
_ => panic!()
114+
}
115+
}
116+
117+
#[test]
118+
fn instrumentation_stim_port() {
119+
let p = decode_one(&[0b00000_001, 0x11]);
120+
match p.kind {
121+
Kind::Instrumentation(ref i) => {
122+
assert_eq!(i.port, 0);
123+
},
124+
_ => panic!()
125+
}
126+
127+
let p = decode_one(&[0b11111_001, 0x11]);
128+
match p.kind {
129+
Kind::Instrumentation(ref i) => {
130+
assert_eq!(i.port, 31);
131+
},
132+
_ => panic!()
133+
}
134+
}
135+
136+
#[test]
137+
fn unknown_header() {
138+
let p = try_decode_one(&[0x00]);
139+
match p {
140+
Err(Error(ErrorKind::UnknownHeader(0x00), _)) => (),
141+
_ => panic!(),
142+
}
143+
}
144+
145+
#[test]
146+
fn eof_before_payload() {
147+
let p = try_decode_one(&[0x01 /* Missing payload bytes */ ]);
148+
match p {
149+
Err(Error(ErrorKind::Io(ref e), _))
150+
if e.kind() == io::ErrorKind::UnexpectedEof => (),
151+
_ => panic!(),
152+
}
153+
}
154+
155+
fn decode_one(data: &[u8]) -> Packet {
156+
try_decode_one(data).unwrap()
157+
}
158+
159+
fn try_decode_one(data: &[u8]) -> Result<Packet> {
160+
let p = from_slice(data);
161+
println!("{:#?}", p);
162+
p
163+
}
164+
}

src/packet.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Defines ITM packets and their possible contents.
22
33
use heapless::Vec as HVec;
4+
use std::fmt::{self, Debug, Formatter};
45

56
pub const MAX_PAYLOAD_SIZE: usize = 4;
67

78
/// Represents a complete received packet.
9+
#[derive(Debug)]
810
pub struct Packet {
911
/// The header byte received for this packet.
1012
pub header: u8,
@@ -14,6 +16,7 @@ pub struct Packet {
1416
}
1517

1618
/// The type of a packet.
19+
#[derive(Debug)]
1720
pub enum Kind {
1821
/// Data from a software application
1922
Instrumentation(Instrumentation),
@@ -32,3 +35,15 @@ pub struct Instrumentation {
3235
/// Stimulus port this packet was sent from.
3336
pub port: u8,
3437
}
38+
39+
impl Debug for Instrumentation {
40+
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
41+
// TODO: impl Debug for heapless::Vec, then this can just be derived.
42+
// Eq, PartialEq, Clone would be good too.
43+
44+
f.debug_struct("Instrumentation")
45+
.field("payload", &&*self.payload)
46+
.field("port", &self.port)
47+
.finish()
48+
}
49+
}

0 commit comments

Comments
 (0)