浏览代码

rust-protocol: Implement a simple unit test.

父节点
当前提交
b2dfe684f6
共有 1 个文件被更改,包括 35 次插入7 次删除
  1. 35
    7
      common/rust-protocol/src/lib.rs

+ 35
- 7
common/rust-protocol/src/lib.rs 查看文件

2
 //! base station and all sensor/display nodes.
2
 //! base station and all sensor/display nodes.
3
 #![no_std]
3
 #![no_std]
4
 
4
 
5
+#[cfg(test)]
6
+extern crate std;
7
+
5
 use core::convert::TryInto;
8
 use core::convert::TryInto;
6
 
9
 
7
 use crc16::{State, KERMIT};
10
 use crc16::{State, KERMIT};
8
 use xxtea_nostd::{decrypt, encrypt};
11
 use xxtea_nostd::{decrypt, encrypt};
9
 
12
 
10
-#[derive(Debug, Clone)]
13
+#[derive(Debug, Clone, PartialEq)]
11
 pub enum Packet {
14
 pub enum Packet {
12
     GetSalt,
15
     GetSalt,
13
     Salt(u64),
16
     Salt(u64),
83
     }
86
     }
84
 }
87
 }
85
 
88
 
86
-#[derive(Debug, Clone)]
89
+#[derive(Debug, Clone, PartialEq)]
87
 pub struct Report {
90
 pub struct Report {
88
     pub count: u8,
91
     pub count: u8,
89
     pub values: [Value; 8],
92
     pub values: [Value; 8],
116
     }
119
     }
117
 }
120
 }
118
 
121
 
119
-#[derive(Debug, Clone)]
122
+#[derive(Debug, Clone, PartialEq)]
120
 pub struct GetValues {
123
 pub struct GetValues {
121
     pub count: u8,
124
     pub count: u8,
122
     pub location: Location,
125
     pub location: Location,
135
     }
138
     }
136
 }
139
 }
137
 
140
 
138
-#[derive(Debug, Clone)]
141
+#[derive(Debug, Clone, PartialEq)]
139
 pub struct Values {
142
 pub struct Values {
140
     pub count: u8,
143
     pub count: u8,
141
     pub location: Location,
144
     pub location: Location,
154
     }
157
     }
155
 }
158
 }
156
 
159
 
157
-#[derive(Debug, Clone, Copy)]
160
+#[derive(Debug, Clone, Copy, PartialEq)]
158
 pub enum Location {
161
 pub enum Location {
159
     Livingroom,
162
     Livingroom,
160
     Bathroom,
163
     Bathroom,
163
     Balcony,
166
     Balcony,
164
 }
167
 }
165
 
168
 
166
-#[derive(Debug, Clone, Copy)]
169
+#[derive(Debug, Clone, Copy, PartialEq)]
167
 pub enum Value {
170
 pub enum Value {
168
     Invalid,
171
     Invalid,
169
     Time(u64),
172
     Time(u64),
230
     }
233
     }
231
 }
234
 }
232
 
235
 
233
-#[derive(Debug, Clone, Copy)]
236
+#[derive(Debug, Clone, Copy, PartialEq)]
234
 pub enum ValueType {
237
 pub enum ValueType {
235
     Time,
238
     Time,
236
     Temperature,
239
     Temperature,
272
     InvalidLocation,
275
     InvalidLocation,
273
     TooShort,
276
     TooShort,
274
 }
277
 }
278
+
279
+#[cfg(test)]
280
+mod tests {
281
+    use super::*;
282
+    use std::println;
283
+
284
+    #[test]
285
+    fn test_identity() {
286
+        let packets = [Packet::GetSalt, Packet::Salt(0x12345678abcdef12)];
287
+
288
+        let key = [
289
+            0x12, 0x34, 0x56, 0x78, 0xab, 0xcd, 0xef, 0x12, 0x12, 0x34, 0x56, 0x78, 0xab, 0xcd,
290
+            0xef, 0x12,
291
+        ];
292
+
293
+        for packet in packets.iter() {
294
+            let mut encoded = [0u8; 32];
295
+            packet.encode_and_encrypt(&key, 0x1234123412341234, &mut encoded);
296
+            assert_eq!(encoded[0], 0x34);
297
+            assert_eq!(encoded[7], 0x12);
298
+            let decoded = Packet::decrypt_and_decode(&key, &mut encoded).unwrap();
299
+            // TODO: Compare encoded and decoded.
300
+        }
301
+    }
302
+}

正在加载...
取消
保存