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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use rustc_hash::FxHashMap as HashMap;
use crate::model::{ModelFactory, template::TemplateData, value::ValueData};
use crate::{Decoder, Value};
struct TestCase {
input: Vec<u8>,
result: TemplateData,
}
fn do_test(test_cases: Vec<TestCase>) {
for tt in test_cases {
let mut msg = ModelFactory::new();
let mut d = Decoder::new_from_xml(include_str!("templates/base.xml")).unwrap();
d.decode_slice(&tt.input, &mut msg).unwrap();
assert_eq!(msg.data.unwrap(), tt.result, "{} failed", tt.result.name);
}
}
#[test]
fn test_model_data() {
do_test(vec![
TestCase {
input: vec![
0xc0, 0x82, 0x61, 0x62, 0xe3, 0x64, 0x65, 0xe6, 0x83, 0x67, 0x68, 0x69, 0x84, 0x6b,
0x6c, 0x6d,
],
result: TemplateData {
name: "String".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"MandatoryAscii".to_string(),
ValueData::Value(Some(Value::ASCIIString("abc".to_string()))),
),
(
"OptionalAscii".to_string(),
ValueData::Value(Some(Value::ASCIIString("def".to_string()))),
),
(
"MandatoryUnicode".to_string(),
ValueData::Value(Some(Value::UnicodeString("ghi".to_string()))),
),
(
"OptionalUnicode".to_string(),
ValueData::Value(Some(Value::UnicodeString("klm".to_string()))),
),
])),
},
},
TestCase {
input: vec![0xc0, 0x83, 0x81, 0xc1, 0x82, 0xb3],
result: TemplateData {
name: "ByteVector".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"MandatoryVector".to_string(),
ValueData::Value(Some(Value::Bytes(vec![193]))),
),
(
"OptionalVector".to_string(),
ValueData::Value(Some(Value::Bytes(vec![179]))),
),
])),
},
},
TestCase {
input: vec![
0xc0, 0x85, 0x81, 0x81, 0x82, 0x83, 0x83, 0x84, 0x81, 0xc0, 0x82,
],
result: TemplateData {
name: "Sequence".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"TestData".to_string(),
ValueData::Value(Some(Value::UInt32(1))),
),
(
"OuterSequence".to_string(),
ValueData::Sequence(vec![ValueData::Group(HashMap::from_iter([
(
"OuterTestData".to_string(),
ValueData::Value(Some(Value::UInt32(2))),
),
(
"InnerSequence".to_string(),
ValueData::Sequence(vec![
ValueData::Group(HashMap::from_iter([(
"InnerTestData".to_string(),
ValueData::Value(Some(Value::UInt32(3))),
)])),
ValueData::Group(HashMap::from_iter([(
"InnerTestData".to_string(),
ValueData::Value(Some(Value::UInt32(4))),
)])),
]),
),
]))]),
),
(
"NextOuterSequence".to_string(),
ValueData::Sequence(vec![ValueData::Group(HashMap::from_iter([(
"NextOuterTestData".to_string(),
ValueData::Value(Some(Value::UInt32(2))),
)]))]),
),
])),
},
},
TestCase {
input: vec![0xc0, 0x86, 0x81, 0xc0, 0x82, 0x83],
result: TemplateData {
name: "Group".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"TestData".to_string(),
ValueData::Value(Some(Value::UInt32(1))),
),
(
"OuterGroup".to_string(),
ValueData::Group(HashMap::from_iter([
(
"OuterTestData".to_string(),
ValueData::Value(Some(Value::UInt32(2))),
),
(
"InnerGroup".to_string(),
ValueData::Group(HashMap::from_iter([(
"InnerTestData".to_string(),
ValueData::Value(Some(Value::UInt32(3))),
)])),
),
])),
),
])),
},
},
TestCase {
input: vec![0xe0, 0x88, 0x86, 0x87],
result: TemplateData {
name: "StaticReference".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"PreRefData".to_string(),
ValueData::Value(Some(Value::UInt32(6))),
),
(
"TestData".to_string(),
ValueData::Value(Some(Value::UInt32(7))),
),
])),
},
},
TestCase {
input: vec![0xc0, 0x89, 0x86, 0xe0, 0x87, 0x85],
result: TemplateData {
name: "DynamicReference".to_string(),
value: ValueData::Group(HashMap::from_iter([
(
"PreRefData".to_string(),
ValueData::Value(Some(Value::UInt32(6))),
),
(
"templateRef:0".to_string(),
ValueData::DynamicTemplateRef(Box::new(TemplateData {
name: "RefData".to_string(),
value: ValueData::Group(HashMap::from_iter([(
"TestData".to_string(),
ValueData::Value(Some(Value::UInt32(5))),
)])),
})),
),
])),
},
},
]);
}