summaryrefslogtreecommitdiff
path: root/test/http_decoder_stub.cpp
blob: e719ebec15c3cd1b3f9425bd7063d19ccd6ee217 (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
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
/*
    Http Decoder Google Test stub module
*/
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include "http_decoder.h"



#ifdef __cplusplus
extern "C"
{
#endif
#include "session.h"
#include "session_exdata.h"
#include "session_mq.h"
#include "stellar.h"
#include "http_decoder_gtest.h"
#include "MESA_jump_layer.h"
extern struct fake_stellar *g_fake_stellar;

    static int g_topic_id = -1;
    static msg_free_cb_func *g_msg_free_cb = NULL;
    static void *g_msg_free_cb_arg = NULL;
    static on_msg_cb_func *g_topic_sub_cb = NULL;
    static void *g_topic_sub_cb_arg = NULL;

    int commit_test_result_json(cJSON *node, const char *name)
    {
        assert(node != NULL || name != NULL);
        if (g_fake_stellar->http_plug_test_result_root)
        {
            // cJSON_AddStringToObject(node, "__X_HTTP_RESULT_INDEX", name);
            cJSON_AddItemToArray(g_fake_stellar->http_plug_test_result_root, node);
            return 0;
        }
        return -1;
    }

    int packet_get_direction(const struct packet *pkt)
    {
        struct fake_packet *fpkt = (struct fake_packet *)pkt;
        return fpkt->dir;
    }

    int session_event_assign(struct session_event *ev, struct stellar *st, struct session *sess, int events, session_event_cb_func *cb, void *cb_arg)
    {
        DEBUG_PRINT("todo: fake session_event_assign()\n");
        return 0;
    }

    const char *session_get0_readable_addr(struct session *sess)
    {
        struct fake_session *fses = (struct fake_session *)sess;

        if (fses->readable_addr_cstr)
        {
            return fses->readable_addr_cstr;
        }

        char ip_src_buf[INET6_ADDRSTRLEN] = {};
        char ip_dst_buf[INET6_ADDRSTRLEN] = {};
        char port_src_buf[16] = {};
        char port_dst_buf[16] = {};
        char tuple4_buf[256] = {};

        if (SESSION_ADDR_TYPE_IPV4_TCP == fses->addr_type)
        {
            inet_ntop(AF_INET, &fses->addr->ipv4.saddr, ip_src_buf, INET_ADDRSTRLEN);
            inet_ntop(AF_INET, &fses->addr->ipv4.daddr, ip_dst_buf, INET_ADDRSTRLEN);
            sprintf(port_src_buf, "%u", ntohs(fses->addr->ipv4.sport));
            sprintf(port_dst_buf, "%u", ntohs(fses->addr->ipv4.dport));
        }
        else
        {
            inet_ntop(AF_INET6, fses->addr->ipv6.saddr, ip_src_buf, INET6_ADDRSTRLEN);
            inet_ntop(AF_INET6, fses->addr->ipv6.daddr, ip_dst_buf, INET6_ADDRSTRLEN);
            sprintf(port_src_buf, "%u", ntohs(fses->addr->ipv6.sport));
            sprintf(port_dst_buf, "%u", ntohs(fses->addr->ipv6.dport));
        }

        snprintf(tuple4_buf, sizeof(tuple4_buf), "%s.%s>%s.%s", ip_src_buf, port_src_buf, ip_dst_buf, port_dst_buf);
        fses->readable_addr_cstr = MMALLOC(char, strlen(tuple4_buf) + 1);
        memcpy(fses->readable_addr_cstr, tuple4_buf, strlen(tuple4_buf));

        return fses->readable_addr_cstr;
    }

    struct session_addr *session_get0_addr(struct session *sess, enum session_addr_type *addr_type)
    {
        struct fake_session *fses = (struct fake_session *)sess;
        *addr_type = fses->addr_type;
        return fses->addr;
    }

    static int __find_ex_data(struct fake_stellar *fst, const char *name)
    {
        int find_name_len = strlen(name);

        for (int i = 0; i < EX_DATA_MAX_SIZE; i++)
        {
            if ((fst->fake_exdata_mgr[i].name != NULL) && (strncasecmp(name, fst->fake_exdata_mgr[i].name, find_name_len) == 0) && (find_name_len == fst->fake_exdata_mgr[i].name_len))
            {
                return i;
            }
        }

        return -1;
    }

    static int __save_ex_data(struct fake_stellar *fst, const char *name, session_ex_free *free_func, void *arg)
    {
        for (int i = 0; i < EX_DATA_MAX_SIZE; i++)
        {
            if (fst->fake_exdata_mgr[i].name == NULL && fst->fake_exdata_mgr[i].name_len == 0)
            {
                fst->fake_exdata_mgr[i].name = MMALLOC(char, strlen(name) + 1);
                fst->fake_exdata_mgr[i].name_len = strlen(name) + 1;
                memcpy(fst->fake_exdata_mgr[i].name, name, strlen(name));
                fst->fake_exdata_mgr[i].free_func = free_func;
                fst->fake_exdata_mgr[i].arg = arg;
                return i;
            }
        }

        return -1;
    }

    int stellar_session_get_ex_new_index(struct stellar *st, const char *name, session_ex_free *free_func, void *arg)
    {
        int ex_id = __find_ex_data((struct fake_stellar *)st, name);
        if (-1 == ex_id)
        {
            ex_id = __save_ex_data((struct fake_stellar *)st, name, free_func, arg);
        }

        return ex_id;
    }

    int session_mq_get_topic_id(struct stellar *st, const char *topic_name)
    {
        return g_topic_id;
    }

    int session_get_current_thread_id(struct session *sess)
    {
        return 0;
    }

    int session_mq_destroy_topic(struct stellar *st, int topic_id)
    {
        return 0;
    }

    int session_set_ex_data(struct session *sess, int idx, void *ex_ptr)
    {
        struct fake_session *fses = (struct fake_session *)sess;
        fses->plug_exdata_array[idx] = ex_ptr;

        return 0;
    }

    void *session_get_ex_data(struct session *sess, int idx)
    {
        struct fake_session *fses = (struct fake_session *)sess;
        return fses->plug_exdata_array[idx];
    }

    void hdd_session_free_exdata(struct fake_session *fake_ses)
    {
        for (int i = 0; i < EX_DATA_MAX_SIZE; i++)
        {
            if (fake_ses->plug_exdata_array[i] != NULL)
            {
                fake_ses->fst->fake_exdata_mgr[i].free_func((struct session *)fake_ses, i, fake_ses->plug_exdata_array[i], NULL);
            }
        }
    }

    int stellar_plugin_register(struct stellar *st, int events, session_event_cb_func *cb, void *cb_arg)
    {
        return 0; // fix plugin id
    }

    extern int http_decoder_test_entry(struct session *sess, int topic_id, const void *data, void *cb_arg);
    int session_mq_publish_message(struct session *sess, int topic_id, void *data)
    {
        g_topic_sub_cb(sess, topic_id, data, g_topic_sub_cb_arg);
        g_msg_free_cb(data, g_msg_free_cb_arg);
        return 0;
    }

    int stellar_get_worker_thread_num(struct stellar *st)
    {
        return 1;
    }

    int session_mq_create_topic(struct stellar *st, const char *topic_name, msg_free_cb_func *free_cb, void *cb_arg)
    {
        g_msg_free_cb = free_cb;
        g_msg_free_cb_arg = cb_arg;

        g_topic_id = 0; // KISS, use fix value
        return g_topic_id;
    }

    const char *session_get0_current_payload(struct session *sess, size_t *payload_len)
    {
        struct fake_session *fses = (struct fake_session *)sess;
        struct fake_packet *fpkt = fses->fpkt;
        const char *payload_ptr = NULL;

        if (!fpkt || !fpkt->payload_data || fpkt->payload_data_len == 0)
        {
            return NULL;
        }

        int submit_len = MIN(fses->tcp_mss, fpkt->payload_data_len - fpkt->payload_submit_offset);
        if (submit_len <= 0)
        {
            *payload_len = 0;
            return NULL;
        }
        payload_ptr = fpkt->payload_data + fpkt->payload_submit_offset;
        *payload_len = submit_len;
        fpkt->payload_submit_offset += submit_len;

        return payload_ptr;
    }

    struct session_event *session_get_intrinsic_event(struct session *sess, int plugin_id)
    {
        return NULL;
    }

    int session_is_inner_most(struct session *sess, uint64_t *flag)
    {
        return 1; // no tunnel
    }

    int session_mq_subscribe_topic(struct stellar *st, int topic_id, on_msg_cb_func *sub_cb, void *cb_arg)
    {
        //todo, support multiple topic?
        g_topic_sub_cb = sub_cb;
        g_topic_sub_cb_arg = cb_arg;
        return 0;
    }

#ifdef __cplusplus
}
#endif