Skip to main content

echonet/protocol/
message_test.rs

1// Copyright (C) 2022 The uecho-rs Authors All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#[cfg(test)]
16mod tests {
17
18    use crate::protocol::esv::*;
19    use crate::protocol::message::*;
20    use hex;
21
22    #[test]
23    fn new_message() {
24        let mut msg = Message::new();
25        assert!(msg.is_format1());
26    }
27
28    #[test]
29    fn message_setter() {
30        let mut msg = Message::new();
31
32        let test_tid = 0x1234 as TID;
33        msg.set_tid(test_tid);
34        assert_eq!(msg.tid(), test_tid);
35
36        let test_seoj = 0x2345 as u32;
37        msg.set_seoj(test_seoj);
38        assert_eq!(msg.seoj(), test_seoj);
39
40        let test_deoj = 0x3456 as u32;
41        msg.set_deoj(test_deoj);
42        assert_eq!(msg.deoj(), test_deoj);
43
44        let test_esv = ESV::WriteRequest;
45        msg.set_esv(test_esv);
46        assert_eq!(msg.esv(), test_esv);
47    }
48
49    #[test]
50    fn message_parse() {
51        fn message_parse(msg_bytes: &[u8]) {
52            // Checks parsed result
53
54            let mut msg = Message::new();
55
56            assert!(msg.parse(msg_bytes), "{}", hex::encode_upper(msg_bytes));
57            assert_eq!(msg.tid(), 0x0101);
58            assert_eq!(msg.seoj(), 0x0A0B0C0);
59            assert_eq!(msg.deoj(), 0x0D0E0F0);
60            assert_eq!(msg.esv(), ESV::Notification);
61
62            let opc = msg.opc();
63            for i in 0..opc {
64                let prop = msg.property(i);
65                let prop_size = prop.size();
66                assert_eq!(prop_size, (i + 1));
67                let prop_data = prop.data();
68                assert_eq!(prop_data.len(), (i + 1));
69                for j in 0..prop_size {
70                    let prop_val = (0x61 + i + j) as u8;
71                    assert_eq!(prop_data[j], prop_val)
72                }
73            }
74
75            // Checks exported message
76
77            let msg_bytes = msg.bytes();
78            let mut parsed_msg = Message::new();
79            assert!(parsed_msg.parse(&msg_bytes));
80            assert!(msg.equals(&parsed_msg));
81        }
82
83        let mut test_msg = [
84            HEADER_EHD1_ECHONET,
85            HEADER_EHD2_FORMAT1,
86            0x01,
87            0x01,
88            0xA0,
89            0xB0,
90            0xC0,
91            0xD0,
92            0xE0,
93            0xF0,
94            ESV::to_u8(ESV::Notification),
95            3,
96            1,
97            1,
98            0x61, // a
99            2,
100            2,
101            0x62, // b
102            0x63, // c
103            3,
104            3,
105            0x63, // d
106            0x64, // e
107            0x65, // f
108        ];
109
110        let mut prop_data_size = 0;
111        for n in 0..3 {
112            let opc_idx = FRAME_HEADER_SIZE + FORMAT1_HEADER_SIZE - 1;
113            let test_msg_size = FRAME_HEADER_SIZE + FORMAT1_HEADER_SIZE + prop_data_size;
114            test_msg[opc_idx] = n;
115            message_parse(&test_msg[..test_msg_size]);
116            prop_data_size += (1 + 1 + (n + 1)) as usize;
117        }
118    }
119
120    #[test]
121    fn message_parse_requests() {
122        let mut test_msgs = Vec::new();
123        test_msgs.push(hex::decode("1081000100F00100F0016201D600").ok().unwrap());
124
125        for test_msg_bytes in test_msgs {
126            let mut msg = Message::new();
127            assert!(
128                msg.parse(&test_msg_bytes),
129                "{}",
130                hex::encode_upper(&test_msg_bytes)
131            );
132        }
133    }
134
135    #[test]
136    fn message_from_message() {
137        let msg_bytes = hex::decode("108100010EF0010EF0017201D607020F2001029101")
138            .ok()
139            .unwrap();
140        let _msg = Message::from_bytes(&msg_bytes);
141    }
142}