-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtpu.cpp
327 lines (286 loc) · 7.16 KB
/
gtpu.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <iostream>
#include "gtpu.hpp"
#include "med/encode.hpp"
#include "med/decode.hpp"
#include "med/printer.hpp"
#include "med/encoder_context.hpp"
#include "med/decoder_context.hpp"
#include "med/octet_encoder.hpp"
#include "med/octet_decoder.hpp"
#include "ut/ut.hpp"
struct msg_data_s
{
char const* name;
std::initializer_list<uint8_t> msg;
friend std::ostream& operator << (std::ostream& o, msg_data_s const& d)
{
return o << d.name << " size=" << d.msg.size();
}
};
struct Gtpu : testing::TestWithParam<msg_data_s>
{
};
TEST(decode, g_pdu)
{
//sample G-PDU payload
uint8_t const payload[] = {1,4,56,7,89,32,77,83,90,91};
//contruct G-PDU directly in buffer
uint8_t buf[sizeof(gtpu::header_s)+sizeof(payload)];
auto ph = gtpu::header_s::from(buf);
std::memcpy(ph->gpdu(1234, sizeof(payload)), payload, sizeof(payload));
//now decode it with codec
std::size_t alloc_buf[1024];
med::allocator alloc{alloc_buf};
med::decoder_context<med::allocator> ctx{buf, &alloc};
gtpu::proto proto;
decode(med::octet_decoder{ctx}, proto);
gtpu::g_pdu const* pmsg = proto.cselect();
ASSERT_NE(nullptr, pmsg);
EXPECT_EQ(1, proto.header().version());
EXPECT_TRUE(proto.header().is_gtpu());
EXPECT_EQ(0, proto.header().sn());
EXPECT_EQ(0, proto.header().npdu());
EXPECT_EQ(1234, proto.header().get_teid());
ASSERT_EQ(sizeof(payload), pmsg->size());
ASSERT_TRUE(Matches(payload, pmsg->data(), pmsg->size()));
}
TEST(encode, g_pdu)
{
gtpu::proto proto;
uint8_t buffer[1024];
med::encoder_context<> ctx{buffer};
uint32_t const teid = 0x04030201;
proto.header().set_teid(teid);
gtpu::g_pdu& msg = proto.select();
uint8_t const pdu[] = {1,2,3,4,5};
msg.set(sizeof(pdu), pdu);
{
encode(med::octet_encoder{ctx}, proto);
uint8_t const encoded[] = {
0x30, //version,flags(-)
0xFF, //message type
0,5, //length
4,3,2,1, //teid
1,2,3,4,5 //G-PDU
};
EXPECT_EQ(sizeof(encoded), ctx.buffer().get_offset());
EXPECT_TRUE(Matches(encoded, buffer));
//check encoded with codec is properly peek-previewed with header_s
auto ph = gtpu::header_s::from(encoded);
EXPECT_EQ(teid, ph->teid());
auto payload = ph->gpdu();
ASSERT_FALSE(payload.empty());
EXPECT_EQ(sizeof(pdu), ph->length());
EXPECT_TRUE(Matches(pdu, payload.ro, sizeof(pdu)));
}
}
//only as example how to fill out
TEST(encode, DISABLED_eh_chain)
{
gtpu::proto proto;
std::size_t buffer[1024];
med::encoder_context<> ctx{buffer};
proto.ref<gtpu::end_marker>(); //just select, nothing to fill in msg
proto.header().set_teid(0);
proto.header().sn(1);
auto& opt = proto.header().ref<gtpu::opt_header>();
opt.ref<gtpu::ext::header>().ref<gtpu::ext::udp_port>().set(0x1234);
auto& next = opt.ref<gtpu::ext::next_header>();
auto* p = next.push_back();
ASSERT_NE(nullptr, p);
p->ref<gtpu::ext::pdcp_pdu>().set(0x8765);
p = next.push_back();
ASSERT_NE(nullptr, p);
p->ref<gtpu::ext::long_pdcp_pdu>().set(0x38765);
p = next.push_back();
ASSERT_NE(nullptr, p);
p->ref<gtpu::ext::sci>().set(0x37);
p = next.push_back();
ASSERT_NE(nullptr, p);
auto& rc = p->ref<gtpu::ext::ran_container>();
uint8_t const data[] = {1,2,3,4,5,6,7,8,9,10};
rc.set(sizeof(data), data);
//NOTE: need to explicitly terminate the chain
p = next.push_back();
ASSERT_NE(nullptr, p);
p->ref<gtpu::ext::no_more>();
encode(med::octet_encoder{ctx}, proto);
{
uint8_t const encoded[] = {
0x36, //version,flags(E+S)
0xFE, //message type
0,36, //length
0,0,0,0, //teid
0,1, //sn (+S-flag)
0, //npdu number
0x40, //next eh = UDP port
1, //4 octets
0x12,0x34, //port
0xC0, //next eh = PDCP PDU
1, //4 octets
0x87, 0x65,
0x82, //next eh = long PDCP PDU
2, //8 octets : ofs=20
0x03, 0x87, 0x65,
0,0,0, //spare
0x20, //next eh = SCI
1, //4 octets
0x37,
0, //spare
0x81, //next eh = RAN container
3, //12 octets
1,2,3,4,5,6,7,8,9,10,
0, // next eh = no_more
};
EXPECT_EQ(sizeof(encoded), ctx.buffer().get_offset());
EXPECT_TRUE(Matches(encoded, ctx.buffer().get_start()));
}
}
TEST_P(Gtpu, codec)
{
std::size_t alloc_buf[1024];
med::allocator alloc{alloc_buf};
gtpu::proto proto;
auto& msg = GetParam().msg;
{
med::decoder_context<med::allocator> ctx{msg.begin(), msg.size(), &alloc};
decode(med::octet_decoder{ctx}, proto);
auto& opt_header = proto.header().ref<gtpu::opt_header>();
if (!(proto.header().vf() & gtpu::version_flags::S))
{
opt_header.ref<gtpu::sequence_number>().clear();
}
if (!(proto.header().vf() & gtpu::version_flags::NP))
{
opt_header.ref<gtpu::npdu_number>().clear();
}
if (!(proto.header().vf() & gtpu::version_flags::E))
{
opt_header.ref<gtpu::ext::header>().clear();
}
}
{
uint8_t buffer[1024];
med::encoder_context<> ctx{buffer};
encode(med::octet_encoder{ctx}, proto);
EXPECT_EQ(msg.size(), ctx.buffer().get_offset());
EXPECT_TRUE(Matches(msg.begin(), buffer, msg.size()));
}
}
msg_data_s const messages[] = {
{"echo-request", { //0
0x32, //version,flags(S)
1, //message type
0,12, //length
0,0,0,0, //teid
0,1, //sn (+S-flag)
0, //npdu number
0, //next eh type
0xFF, //tag=private extension
0,5, //length
0,45, //extension id
1,2,3 //extension value
}},
{"echo-response", { //1
0x32, //version,flags(S)
2, //message type
0,6, //length
0,0,0,0, //teid
0,1, //sn (+S-flag)
0, //npdu number
0, //next eh type
14, //tag=recovery
45, //reset counter
}},
{"error-ind", { //2
0x30, //version,flags(-)
26, //message type
0,12, //length
0,0,0,0, //teid
16, //tag=teid data
1,2,3,4,
133, //tag=peer address
0,4, //len
192,168,1,3, //IPv4
}},
{"supported-eh", { //3
0x30, //version,flags(.)
31, //message type
0,5, //length
0,0,0,0, //teid
141, //tag=list
3, //len
1,3,7, //eh type(s)
}},
{"end-marker", { //4
0x30, //version,flags(-)
0xFE, //message type
0,0, //length
0,0,0,0, //teid
}},
{"gpdu", { //5
0x30, //version,flags(-)
0xFF, //message type
0,5, //length
4,3,2,1, //teid
1,2,3,4,5 //G-PDU
}},
{"eh-chain", { //6
0x36, //version,flags(E+S)
0xFE, //message type = End Marker
0,36, //length
0,0,0,0, //teid
0,1, //sn (+S-flag)
0, //npdu number
0x40, //next eh = UDP port
1, //4 octets
0x12,0x34, //port
0xC0, //next eh = PDCP PDU
1, //4 octets
0x87, 0x65,
0x82, //next eh = long PDCP PDU
2, //8 octets
0x03, 0x87, 0x65,
0,0,0, //spare
0x20, //next eh = SCI
1, //4 octets
0x37,
0, //spare
0x81, //next eh = RAN container
3, //12 octets
1,2,3,4,5,6,7,8,9,10,
0, // next eh = no_more
}},
{"eh-chain-any", { //7
0x36, //version,flags(E+S)
0xFE, //message type
0,36, //length
0,0,0,0, //teid
0,1, //sn (+S-flag)
0, //npdu number
0x40, //next eh = UDP port
1, //4 octets
0x12,0x34, //port
0xC0, //next eh = PDCP PDU
1, //4 octets
0x87, 0x65,
0x82, //next eh = long PDCP PDU
2, //8 octets
0x03, 0x87, 0x65,
0,0,0, //spare
0x20, //next eh = SCI
1, //4 octets
0x37,
0, //spare
0x87, //next eh = unknown container
3, //12 octets
1,2,3,4,5,6,7,8,9,10,
0, // next eh = no_more
}},
};
INSTANTIATE_TEST_SUITE_P(Suite, Gtpu, testing::ValuesIn(messages));
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}