aboutsummaryrefslogtreecommitdiff
path: root/src/tests/handshake_types.rs
blob: 227bdad118f39b8b2bc33e6aae84bfb346ba7d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::protocol::message::handshake::{VariantMap, HandshakeSerialize, HandshakeDeserialize, HandshakeQRead};
use crate::protocol::primitive::{Variant};

#[test]
pub fn serialize_variantmap() {
    let mut test_variantmap = VariantMap::new();
    test_variantmap.insert("Configured".to_string(), Variant::bool(true));
    let bytes = [0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0,
         0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100,
         0, 0, 0, 1, 0, 1].to_vec();
    assert_eq!(
        test_variantmap.serialize().unwrap(),
        bytes
    );
}

#[test]
pub fn read_variantmap() {
    use std::io::Cursor;

    let test_bytes: Vec<u8> = vec![
        // len
        0, 0, 0, 74,
        // var
        0, 0, 0, 10, // 4
        // var
        0, 0, 0, 10, 0, // 5
        // strlen, str
        0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, // 24
        // bool
        0, 0, 0, 1, 0, 1, // 6
        // var
        0, 0, 0, 10, 0, // 5
        // strlen, str
        0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100, // 24
        // bool
        0, 0, 0, 1, 0, 1, //6
        // extra
        0, 0, 0, 1];

    let mut buf: Vec<u8> = [0; 78].to_vec();
    let len = VariantMap::read(&mut Cursor::new(&test_bytes), &mut buf).unwrap();

    assert_eq!(len, 78);

    let result_bytes: Vec<u8> = vec![
        // len
        0, 0, 0, 74,
        // var
        0, 0, 0, 10,
        // var
        0, 0, 0, 10, 0,
        // strlen, str
        0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100,
        // bool
        0, 0, 0, 1, 0, 1,
        // var
        0, 0, 0, 10, 0,
        // strlen, str
        0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100,
        // bool
        0, 0, 0, 1, 0, 1];
    assert_eq!(buf, result_bytes);
}

#[test]
pub fn deserialize_variantmap() {
    let test_bytes: &[u8] = &[0, 0, 0, 39, 0, 0, 0, 10, 0, 0, 0, 10, 0,
         0, 0, 0, 20, 0, 67, 0, 111, 0, 110, 0, 102, 0, 105, 0, 103, 0, 117, 0, 114, 0, 101, 0, 100,
         0, 0, 0, 1, 0, 1, 0, 0, 0, 1];
    let mut test_variantmap = VariantMap::new();
    test_variantmap.insert("Configured".to_string(), Variant::bool(true));

    let (len, res) = VariantMap::parse(test_bytes).unwrap();

    assert_eq!(len, 43);
    assert_eq!(res, test_variantmap);
}

#[test]
pub fn deserialize_variantmap_utf8() {
    let test_bytes: &[u8] = &[0, 0, 0, 29, 0, 0, 0, 10, 0, 0, 0, 12, 0,
                              0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114, 101, 100,
                              0, 0, 0, 1, 0, 1,
//                              0, 0, 0, 10, 67, 111, 110, 102, 105, 103, 117, 114
//                              0, 0, 0, 1, 0, 1,
                              0, 0, 0, 1];
    let mut test_variantmap = VariantMap::new();
    test_variantmap.insert("Configured".to_string(), Variant::bool(true));

    let (len, res) = VariantMap::parse(test_bytes).unwrap();

    assert_eq!(len, 33);
    assert_eq!(res, test_variantmap);
}