summaryrefslogtreecommitdiff
path: root/decoders/dtls/test/dtls_test.cpp
blob: 438dd34f0ad409faf0b2b93dbb740669b2796338 (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
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
#include <gtest/gtest.h>
#include <unistd.h>
#include <stdio.h>
#include "stellar/dtls.h"
#include <pcap/pcap.h>

#ifdef __cplusplus
extern "C"
{
#endif

#include "dtls_parse.h"
extern int32_t dtls_parser_header(const char *payload, uint32_t payload_len, struct dtls_header *header, void *logger);
extern int32_t dtls_parser_handshake_header(const char *dtls_payload, uint32_t dtls_payload_len, 
										struct dtls_parse_ctx **parse_ctx, struct dtls_handshake *handshake_hs, void *logger);
extern int dtls_try_assemble_fragments(struct dtls_parse_ctx **parse_ctx, struct dtls_handshake *handshake_hs);
extern int32_t dtls_parser_hello(struct session *sess, struct dtls_handshake *handshake, struct dtls_message *dtls_msg, void *logger);
#ifdef __cplusplus
}
#endif

TEST(dtls_decoder, dtls_try_assemble_fragments)
{
    const uint8_t packet_fragment1_bytes[]={
            0x16, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x72,                       //DTLS HEAD
            0x01, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 
            0x00, 0x00, 0x00, 0x66,                             //HANDSHAKE HEAD msg_length=204 fragment_offset=0, fragment_length=102
            0xfe, 0xfd, 0x68, 0xbb, 0xd5, 0x17, 0x52,
            0x32, 0x94, 0x28, 0x59, 0x31, 0x9a, 0xa1, 0x7d,
            0x80, 0xf1, 0x46, 0x4c, 0x52, 0x89, 0xb7, 0xb3,
            0x19, 0xda, 0xce, 0xb4, 0x75, 0xae, 0x52, 0xde,
            0x9e, 0xdc, 0x66, 0x00, 0x00, 0x00, 0x38, 0xc0,
            0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc,
            0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f, 0x00,
            0x9e, 0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0,
            0x23, 0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a, 0xc0,
            0x14, 0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00,
            0x33, 0x00, 0x9d, 0x00, 0x9c, 0x00, 0x3d, 0x00,
            0x3c, 0x00, 0x35, 0x00, 0x2f, 0x00, 0xff, 0x01,
            0x00, 0x00, 0x6a, 0x00, 0x00, 0x00, 0x0e, 
    };
    uint32_t fragment1_len=127;

    const uint8_t packet_fragment2_bytes[]={
            0x16, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x4c,                   //DTLS HEAD
            0x01, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 
            0x66, 0x00, 0x00, 0x33,                         //HANDSHAKE HEAD msg_length=204 fragment_offset=102, fragment_length=51
            0x00, 0x0c, 0x00, 0x00, 0x09, 0x6c, 0x6f, 0x63, 
            0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, 0x00, 0x0b, 
            0x00, 0x04, 0x03, 0x00, 0x01, 0x02, 0x00, 0x0a, 
            0x00, 0x0c, 0x00, 0x0a, 0x00, 0x1d, 0x00, 0x17, 
            0x00, 0x1e, 0x00, 0x19, 0x00, 0x18, 0x00, 0x23, 
            0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 
            0x00, 0x00, 0x00,
    };
    uint32_t fragment2_len=76;

    const uint8_t packet_fragment3_bytes[]={
            0x16, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x4c,                   //DTLS HEAD
            0x01, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00, 0x00, 
            0x99, 0x00, 0x00, 0x33,                         //HANDSHAKE HEAD msg_length=204 fragment_offset=153, fragment_length=51
            0x0d, 0x00, 0x30, 0x00, 0x2e, 0x04, 0x03, 0x05, 
            0x03, 0x06, 0x03, 0x08, 0x07, 0x08, 0x08, 0x08, 
            0x1a, 0x08, 0x1b, 0x08, 0x1c, 0x08, 0x09, 0x08, 
            0x0a, 0x08, 0x0b, 0x08, 0x04, 0x08, 0x05, 0x08, 
            0x06, 0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x03, 
            0x03, 0x03, 0x01, 0x03, 0x02, 0x04, 0x02, 0x05, 
            0x02, 0x06, 0x02
    };
    uint32_t fragment3_len=76;
    
    struct dtls_parse_ctx *parse_ctx=NULL;
    struct dtls_header header;
    struct dtls_handshake handshake_hs;

    //packet Out of order arrival
    dtls_parser_header((const char *)packet_fragment3_bytes, fragment3_len, &header, NULL);
    int ret=dtls_parser_handshake_header(header.dtls_payload, header.length, &parse_ctx, &handshake_hs, NULL);
    EXPECT_EQ(ret , -1);
    EXPECT_TRUE(parse_ctx!=NULL);
    EXPECT_EQ(parse_ctx->fragments_head->msg_type , dtls_type_client_hello);
    EXPECT_EQ(parse_ctx->fragments_head->message_seq , 0);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_content_length , 204);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_filled_size , 51);
    
    dtls_parser_header((const char *)packet_fragment1_bytes, fragment1_len, &header, NULL);
    ret=dtls_parser_handshake_header(header.dtls_payload, header.length, &parse_ctx, &handshake_hs, NULL);
    EXPECT_EQ(ret , -1);
    EXPECT_TRUE(parse_ctx!=NULL);
    EXPECT_EQ(parse_ctx->fragments_head->msg_type , dtls_type_client_hello);
    EXPECT_EQ(parse_ctx->fragments_head->message_seq , 0);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_content_length , 204);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_filled_size , 153);
    // EXPECT_EQ(*parse_ctx->fragments_head->fragment_content , 254);

    dtls_parser_header((const char *)packet_fragment2_bytes, fragment2_len, &header, NULL);
    ret=dtls_parser_handshake_header(header.dtls_payload, header.length, &parse_ctx, &handshake_hs, NULL);
    EXPECT_EQ(ret , 0);
    EXPECT_TRUE(parse_ctx!=NULL);
    EXPECT_EQ(parse_ctx->fragments_head->msg_type , dtls_type_client_hello);
    EXPECT_EQ(parse_ctx->fragments_head->message_seq , 0);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_content_length , 204);
    EXPECT_EQ(parse_ctx->fragments_head->fragment_filled_size , 204);

    struct dtls_message dtls_msg;
    memset(&dtls_msg, 0, sizeof(struct dtls_message));
    dtls_parser_hello(NULL, &handshake_hs, &dtls_msg, NULL);
    EXPECT_EQ(dtls_msg.msg_type , DTLS_MSG_TYPE_CLIENT_HELLO);
    ASSERT_TRUE(dtls_msg.msg_content!=NULL);
    struct dtls_handshake *dtls_hs=(struct dtls_handshake *)dtls_msg.msg_content;
    char sni_str[512]={0};
    memcpy(sni_str, dtls_hs->sni, dtls_hs->sni_sz);
    sni_str[dtls_hs->sni_sz]='\0';
    EXPECT_STREQ(sni_str, "localhost");

    if(dtls_hs->extensions!=NULL) utarray_free(dtls_hs->extensions);
    free(dtls_msg.msg_content);
    free(parse_ctx->fragments_head->fragment_content);
    free(parse_ctx->fragments_head);
    free(parse_ctx);
    return ;
}

TEST(dtls_decoder, stun_identify_api)
{
    
    const uint8_t udp_payload[]={ 0x00, 0x01, 0x00, 0x50, 0x21, 0x12, 0xa4, 0x42,
                                    0x2f, 0x33, 0x69, 0x73, 0x50, 0x5a, 0x79, 0x4c,
                                    0x48, 0x30, 0x58, 0x41, 0x00, 0x06, 0x00, 0x09,
                                    0x39, 0x47, 0x2f, 0x77, 0x3a, 0x75, 0x67, 0x64,
                                    0x4e, 0x00, 0x00, 0x00, 0xc0, 0x57, 0x00, 0x04,
                                    0x00, 0x00, 0x03, 0xe7, 0x80, 0x2a, 0x00, 0x08,
                                    0x9e, 0x6c, 0x07, 0x60, 0x96, 0x03, 0xf8, 0x59,
                                    0x00, 0x25, 0x00, 0x00, 0x00, 0x24, 0x00, 0x04,
                                    0x6e, 0x00, 0x1e, 0xff, 0x00, 0x08, 0x00, 0x14,
                                    0xfb, 0x01, 0xdf, 0x37, 0x58, 0x2d, 0x37, 0x8b,
                                    0xe0, 0xeb, 0xf2, 0xc3, 0xbe, 0x36, 0xe4, 0x53,
                                    0x7e, 0x03, 0xab, 0xe7, 0x80, 0x28, 0x00, 0x04,
                                    0x3f, 0x4a, 0xd2, 0x71
                                    };
    
    uint32_t udp_payload_len=100;
    int ret=stun_identify((const char *)udp_payload, udp_payload_len);
    EXPECT_EQ(ret, 1);
    return ;
}

TEST(dtls_decoder, dtls_identify_api)
{
    const uint8_t udp_payload[187]={  0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0x00, 0x00, 0x00, 0x00, 0xae, 0x01, 0x00, 0x00,
                                        0xa2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                        0xa2, 0xfe, 0xfd, 0xd6, 0xa5, 0x03, 0x59, 0xdf,
                                        0x0d, 0x31, 0x80, 0xff, 0xd5, 0x30, 0xc7, 0xd4,
                                        0x4d, 0x09, 0x4d, 0xb1, 0x2b, 0x46, 0x5f, 0x23,
                                        0x3f, 0x5b, 0xf8, 0xc1, 0x33, 0x08, 0x9c, 0x0d,
                                        0x57, 0x81, 0x24, 0x20, 0x8c, 0xba, 0x66, 0x7e,
                                        0x10, 0x34, 0x63, 0x68, 0x7b, 0x30, 0x40, 0x4c,
                                        0x20, 0x89, 0x35, 0x08, 0x2e, 0x41, 0x63, 0xd8,
                                        0x7a, 0xe2, 0xa7, 0xb0, 0xff, 0x51, 0x27, 0x72,
                                        0xff, 0xb5, 0xda, 0x61, 0x00, 0x00, 0x02, 0x00,
                                        0x9c, 0x01, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
                                        0x1a, 0x00, 0x18, 0x00, 0x00, 0x15, 0x68, 0x61,
                                        0x6e, 0x67, 0x7a, 0x68, 0x6f, 0x75, 0x2e, 0x63,
                                        0x69, 0x73, 0x63, 0x6f, 0x76, 0x6e, 0x70, 0x2e,
                                        0x63, 0x6f, 0x6d, 0x00, 0x16, 0x00, 0x00, 0x00,
                                        0x0d, 0x00, 0x30, 0x00, 0x2e, 0x04, 0x03, 0x05,
                                        0x03, 0x06, 0x03, 0x08, 0x07, 0x08, 0x08, 0x08,
                                        0x09, 0x08, 0x0a, 0x08, 0x0b, 0x08, 0x04, 0x08,
                                        0x05, 0x08, 0x06, 0x04, 0x01, 0x05, 0x01, 0x06,
                                        0x01, 0x03, 0x03, 0x02, 0x03, 0x03, 0x01, 0x02,
                                        0x01, 0x03, 0x02, 0x02, 0x02, 0x04, 0x02, 0x05,
                                        0x02, 0x06, 0x02};
    
    uint32_t udp_payload_len=187;
    
    int32_t ret=dtls_identify((const char *)udp_payload, udp_payload_len);
    EXPECT_EQ(ret, 1);
    return ;
}


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