summaryrefslogtreecommitdiff
path: root/infra/packet_manager/test/gtest_gtp2_utils.cpp
blob: 584abf78d73431ebe9691b19e6acdf8937a4a309 (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
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
#include <gtest/gtest.h>

#include "packet_helper.h"

/*
 * User Datagram Protocol, Src Port: 2123, Dst Port: 12491
 * GPRS Tunneling Protocol V2
 *     Flags: 0x48
 *         010. .... = Version: 2
 *         ...0 .... = Piggybacking flag (P): 0
 *         .... 1... = TEID flag (T): 1
 *         .... .0.. = Message Priority(MP): 0
 *     Message Type: Delete Session Response (37)
 *     Message Length: 19
 *     Tunnel Endpoint Identifier: 0x0e05cd4d (235261261)
 *     Sequence Number: 0x000ce7 (3303)
 *     Spare: 0
 *     Cause : Request accepted (16)
 *         IE Type: Cause (2)
 *         IE Length: 2
 *         0000 .... = CR flag: 0
 *         .... 0000 = Instance: 0
 *         Cause: Request accepted (16)
 *         0000 0... = Spare bit(s): 0
 *         .... .0.. = PCE (PDN Connection IE Error): False
 *         .... ..0. = BCE (Bearer Context IE Error): False
 *         .... ...0 = CS (Cause Source): Originated by node sending the message
 *     Recovery (Restart Counter) : 13
 *         IE Type: Recovery (Restart Counter) (3)
 *         IE Length: 1
 *         0000 .... = CR flag: 0
 *         .... 0000 = Instance: 0
 *         Restart Counter: 13
 */

unsigned char gtp2_c_pkt1[] = {
    0x48, 0x25, 0x00, 0x13, 0x0e, 0x05, 0xcd, 0x4d, 0x00, 0x0c, 0xe7, 0x00, 0x02, 0x00, 0x02, 0x00, 0x10, 0x00, 0x03, 0x00, 0x01, 0x00, 0x0d};

// have teid

TEST(GTP2_UTILS, C_GET1)
{
    const struct gtp2_hdr *hdr = (struct gtp2_hdr *)gtp2_c_pkt1;

    EXPECT_TRUE(calc_gtp2_hdr_len((const char *)gtp2_c_pkt1, sizeof(gtp2_c_pkt1)) == 12);
    EXPECT_TRUE(gtp2_hdr_get_flags(hdr) == 0x48);
    EXPECT_TRUE(gtp2_hdr_get_version(hdr) == 2);
    EXPECT_TRUE(gtp2_hdr_get_piggyback_flag(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_teid_flag(hdr) == 1);
    EXPECT_TRUE(gtp2_hdr_get_spare_flag(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_msg_type(hdr) == 37);
    EXPECT_TRUE(gtp2_hdr_get_msg_len(hdr) == 19);
    EXPECT_TRUE(gtp2_hdr_get_teid(hdr) == 0x0e05cd4d);
    EXPECT_TRUE(gtp2_hdr_get_seq(hdr) == 0x000ce7);
    EXPECT_TRUE(gtp2_hdr_get_spare(hdr) == 0);

    char buff[1024] = {0};
    gtp2_hdr_to_str(hdr, buff, sizeof(buff));
    printf("%s\n", buff);
}

TEST(GTP2_UTILS, C_SET1)
{
    char buff[12] = {0};

    struct gtp2_hdr *hdr = (struct gtp2_hdr *)buff;
    gtp2_hdr_set_flags(hdr, 0x48);
    gtp2_hdr_set_version(hdr, 2);
    gtp2_hdr_set_piggyback_flag(hdr, 0);
    gtp2_hdr_set_teid_flag(hdr, 1);
    gtp2_hdr_set_spare_flag(hdr, 0);
    gtp2_hdr_set_msg_type(hdr, 37);
    gtp2_hdr_set_msg_len(hdr, 19);
    gtp2_hdr_set_teid(hdr, 0x0e05cd4d);
    gtp2_hdr_set_seq(hdr, 0x000ce7);
    gtp2_hdr_set_spare(hdr, 0);

    EXPECT_TRUE(memcmp(buff, gtp2_c_pkt1, 12) == 0);
}

/*
 * User Datagram Protocol, Src Port: 2123, Dst Port: 2123
 * GPRS Tunneling Protocol V2
 *     Flags: 0x40
 *         010. .... = Version: 2
 *         ...0 .... = Piggybacking flag (P): 0
 *         .... 0... = TEID flag (T): 0
 *         .... .0.. = Message Priority(MP): 0
 *     Message Type: Echo Request (1)
 *     Message Length: 9
 *     Sequence Number: 0x0041d4 (16852)
 *     Spare: 0
 *     Recovery (Restart Counter) : 5
 *         IE Type: Recovery (Restart Counter) (3)
 *         IE Length: 1
 *         0000 .... = CR flag: 0
 *         .... 0000 = Instance: 0
 *         Restart Counter: 5
 */

unsigned char gtp2_c_pkt2[] = {
    0x40, 0x01, 0x00, 0x09, 0x00, 0x41, 0xd4, 0x00, 0x03, 0x00, 0x01, 0x00, 0x05};

// no teid

TEST(GTP2_UTILS, C_GET2)
{
    const struct gtp2_hdr *hdr = (struct gtp2_hdr *)gtp2_c_pkt2;

    EXPECT_TRUE(calc_gtp2_hdr_len((const char *)gtp2_c_pkt2, sizeof(gtp2_c_pkt2)) == 8);
    EXPECT_TRUE(gtp2_hdr_get_flags(hdr) == 0x40);
    EXPECT_TRUE(gtp2_hdr_get_version(hdr) == 2);
    EXPECT_TRUE(gtp2_hdr_get_piggyback_flag(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_teid_flag(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_spare_flag(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_msg_type(hdr) == 1);
    EXPECT_TRUE(gtp2_hdr_get_msg_len(hdr) == 9);
    EXPECT_TRUE(gtp2_hdr_get_teid(hdr) == 0);
    EXPECT_TRUE(gtp2_hdr_get_seq(hdr) == 0x0041d4);
    EXPECT_TRUE(gtp2_hdr_get_spare(hdr) == 0);

    char buff[1024] = {0};
    gtp2_hdr_to_str(hdr, buff, sizeof(buff));
    printf("%s\n", buff);
}

TEST(GTP2_UTILS, C_SET2)
{
    char buff[8] = {0};

    struct gtp2_hdr *hdr = (struct gtp2_hdr *)buff;
    gtp2_hdr_set_flags(hdr, 0x40);
    gtp2_hdr_set_version(hdr, 2);
    gtp2_hdr_set_piggyback_flag(hdr, 0);
    gtp2_hdr_set_teid_flag(hdr, 0);
    gtp2_hdr_set_spare_flag(hdr, 0);
    gtp2_hdr_set_msg_type(hdr, 1);
    gtp2_hdr_set_msg_len(hdr, 9);
    gtp2_hdr_set_teid(hdr, 0);
    gtp2_hdr_set_seq(hdr, 0x0041d4);
    gtp2_hdr_set_spare(hdr, 0);

    EXPECT_TRUE(memcmp(buff, gtp2_c_pkt2, 8) == 0);
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}