summaryrefslogtreecommitdiff
path: root/plugin/business/tsg-http/test/test_http_lua.cpp
blob: bf99e46a278a15b3fcdff51e24d3e4b4d1393ba0 (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <tfe_utils.h>
#include <tfe_http.h>
#include "http_lua.h"

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <string.h>
#include <stdio.h>
#include <gtest/gtest.h>

bool g_print_to_stderr = true;

struct lua_http_head_field
{
    TAILQ_ENTRY(lua_http_head_field) next;
   	char * value;
    struct http_field_name *field;
};

struct lua_http_headers
{
	TAILQ_HEAD(http_field_list_head, lua_http_head_field) lua_http_field_list;
	int nvlen;
	uint8_t flag;
};

struct def_lua_http_headers
{
    const char *filed_name;
	const char *value;
    enum tfe_http_std_field field_id;
};

struct def_lua_http_headers def_lua_http_head_value[]={{"accept", "text/html", TFE_HTTP_UNKNOWN_FIELD},
											  {"accept-encoding", "gzip, deflate, br", TFE_HTTP_UNKNOWN_FIELD},
											  {"accept-language", "zh-CN,zh;q=0.9", TFE_HTTP_UNKNOWN_FIELD},
											  {"pragma", "no-cache", TFE_HTTP_PRAGMA},
											  {"user-agent", "Mozilla/5.0", TFE_HTTP_USER_AGENT},
											  {"cache-control", "max-age=3600", TFE_HTTP_CACHE_CONTROL},
											  {"content-encoding", "gzip", TFE_HTTP_CONT_ENCODING},
											  {"content-type", "text/html", TFE_HTTP_CONT_TYPE},
											  {"server", "nginx", TFE_HTTP_SERVER},
											  {"expires", "Tue, 12 Jul 2022 07:11:56 GMT", TFE_HTTP_EXPIRES}};


struct tsg_lua_pattern
{
	int thread_num;
	struct elua_script **elua_ctx;
	struct tsg_lua_script *lua_script;
    struct lua_http_headers lua_http_head_list;
};
struct tsg_lua_pattern *g_tsg_lua_pattern=NULL;

struct lua_http_headers *lua_get_http_head_list()
{
	return &g_tsg_lua_pattern->lua_http_head_list;
}

struct http_field_name *lua_http_field_name_dup(const struct http_field_name * orig)
{
	struct http_field_name * field_name_item = ALLOC(struct http_field_name, 1);
	assert(field_name_item != NULL);

	if (orig->field_id == TFE_HTTP_UNKNOWN_FIELD)
	{
		field_name_item->field_id = TFE_HTTP_UNKNOWN_FIELD;
		field_name_item->field_name = tfe_strdup(orig->field_name);
	}
	else
	{
		field_name_item->field_id = orig->field_id;
		field_name_item->field_name = NULL;
	}
	return field_name_item;
}

const char *lua_http_field_iterate(const struct tfe_http_half * half, void ** iter, struct http_field_name * field)
{
	struct lua_http_head_field **http_head_field  = (struct lua_http_head_field **) iter;

	if (*http_head_field == NULL)
		*http_head_field = TAILQ_FIRST(&(lua_get_http_head_list()->lua_http_field_list));
	else
		*http_head_field = TAILQ_NEXT(*http_head_field, next);

	if (*http_head_field == NULL) return NULL;

	field->field_id = (*http_head_field)->field->field_id;
	field->field_name = (*http_head_field)->field->field_name;
	return (*http_head_field)->value;
}

int lua_http_field_write(struct tfe_http_half * half, const struct http_field_name * field, const char * value)
{
	if (value != NULL)
	{
		struct lua_http_head_field * http_head_field = ALLOC(struct lua_http_head_field, 1);
		http_head_field->field = lua_http_field_name_dup(field);
		http_head_field->value = tfe_strdup(value);
		TAILQ_INSERT_TAIL(&g_tsg_lua_pattern->lua_http_head_list.lua_http_field_list, http_head_field, next);
	}
	else
	{
		struct lua_http_head_field * http_head_field = NULL; 	struct lua_http_head_field * http_head_field_peer = NULL;
		TAILQ_FOREACH_SAFE(http_head_field, &g_tsg_lua_pattern->lua_http_head_list.lua_http_field_list, next, http_head_field_peer)
		{
			if (http_field_name_compare(http_head_field->field, field) != 0)
				continue;

			TAILQ_REMOVE(&g_tsg_lua_pattern->lua_http_head_list.lua_http_field_list, http_head_field, next);
			http_field_name_destory(http_head_field->field);
			free(http_head_field->value);
			free(http_head_field);
		}
	}

	return 0;
}

const char *lua_http_field_read(const struct tfe_http_half * half, const struct http_field_name * field)
{
	struct lua_http_head_field * http_head_field = NULL;
	struct lua_http_head_field * http_head_field_node = NULL;

	TAILQ_FOREACH(http_head_field, &g_tsg_lua_pattern->lua_http_head_list.lua_http_field_list, next)
	{
		if (http_field_name_compare(http_head_field->field, field) != 0) continue;
		http_head_field_node = http_head_field;
		break;
	}

	return http_head_field_node != NULL ? http_head_field->value : NULL;
}

static int lua_http_default_headers_init(struct def_lua_http_headers *lua_http_head_value)
{
	for(size_t i=0; i < sizeof(def_lua_http_head_value)/sizeof(struct def_lua_http_headers); i++)
	{
		struct http_field_name field;
		field.field_id = lua_http_head_value[i].field_id;
		field.field_name = lua_http_head_value[i].filed_name;

		struct lua_http_head_field * http_head_field = ALLOC(struct lua_http_head_field, 1);
		http_head_field->field = lua_http_field_name_dup(&field);
		http_head_field->value = tfe_strdup(lua_http_head_value[i].value);
		TAILQ_INSERT_TAIL(&g_tsg_lua_pattern->lua_http_head_list.lua_http_field_list, http_head_field, next);
	}

	return 0;
}

int http_lua_profile_for_test(int profile_id, struct elua_script ***elua_ctx, char **profile_msg, size_t *msg_len, int *timeout)
{
	size_t input_sz;
	const char* filename="./test_data/http_session.lua";
	char *input= tfe_read_file(filename, &input_sz);

	*profile_msg=tfe_strdup(input);
	*msg_len=input_sz;
	*timeout=1000;
	*elua_ctx=g_tsg_lua_pattern->elua_ctx;
	FREE(&input);
	return 0;
}

void lua_http_session_destory(struct tfe_http_session *session)
{
	if(session != NULL)
	{
		char *uri=(char *)session->req->req_spec.uri;
		if(uri)
			FREE(&uri);
		struct tfe_http_half_ops *ops=(struct tfe_http_half_ops *)session->resp->ops;
		if(ops)
			FREE(&ops);
		if(session->resp)
			FREE(&session->resp);
		if(session->req)
			FREE(&session->req);
		FREE(&session);
	}

	return;
}

const struct tfe_http_session *lua_http_session_init()
{
	struct tfe_http_session *session =ALLOC(struct tfe_http_session, 1);

	struct tfe_http_half * in_req_half=ALLOC(struct tfe_http_half, 1);
	session->req = in_req_half;

	struct tfe_http_half * in_resp_half=ALLOC(struct tfe_http_half, 1);
	session->resp = in_resp_half;

	struct tfe_http_half_ops *ops = ALLOC(struct tfe_http_half_ops, 1);
	in_resp_half->ops = ops;
	in_req_half->ops = ops;

	ops->ops_http_field_iterate = lua_http_field_iterate;
	ops->ops_http_field_write = lua_http_field_write;
	ops->ops_http_field_read = lua_http_field_read;

	return session;
}

TEST(TSG_LUA_SCRIPT, Lua_TimeOut)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;

	struct timespec start_time, end_time;

	memset(&tsg_ctx, 0, sizeof(tsg_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.http_req_uri=2;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;

	clock_gettime(CLOCK_REALTIME, &(start_time));
	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret!=0);

	clock_gettime(CLOCK_REALTIME, &(end_time));
	printf("take time %lu(s)\n", end_time.tv_sec - start_time.tv_sec);

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Req_Uri)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx, 0, sizeof(tsg_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_REQ_HDR;
	tsg_ctx.http_req_uri=1;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;
	tsg_ctx.session->req->req_spec.uri = tfe_strdup("forecast");

	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);

	EXPECT_STREQ(tsg_ctx.rewrite_uri,"team");
	FREE(&tsg_ctx.rewrite_uri);
	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Req_Header)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx, 0, sizeof(tsg_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_REQ_HDR;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;
	tsg_ctx.session->req->req_spec.method = TFE_HTTP_METHOD_GET;

	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);

	const char* user_agent_val=tfe_http_std_field_read(tsg_ctx.session->resp, TFE_HTTP_USER_AGENT);
	EXPECT_TRUE(user_agent_val!=NULL);
	EXPECT_STREQ(user_agent_val, "curl-v1.1");

	const char* x_tg_val=tfe_http_nonstd_field_read(tsg_ctx.session->resp, "x-tg-construct-by");
	EXPECT_TRUE(x_tg_val!=NULL);
	EXPECT_STREQ(x_tg_val, "tfe");

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Resp_Header)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx,0,sizeof(struct tsg_script_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_RESP_HDR;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;
	tsg_ctx.session->resp->resp_spec.resp_code = 200;

	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);

	const char* content_type_val=tfe_http_std_field_read(tsg_ctx.session->resp, TFE_HTTP_CONT_TYPE);
	EXPECT_TRUE(content_type_val!=NULL);
	EXPECT_STREQ(content_type_val, "utf8");

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Req_Data)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx,0,sizeof(struct tsg_script_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_REQ_BODY_END;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;

	const char *input="This is request data";
	tsg_ctx.http_body = evbuffer_new();
	evbuffer_add(tsg_ctx.http_body, input, strlen(input));

	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);
	ASSERT_TRUE(tsg_ctx.http_lua_body!=NULL);

	char *__http_body=(char *) evbuffer_pullup(tsg_ctx.http_lua_body, -1);
	EXPECT_STREQ(__http_body, "This is request data set req body");
	printf("__http_body: %s\n",__http_body);

	evbuffer_free(tsg_ctx.http_body);
	evbuffer_free(tsg_ctx.http_lua_body);

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Resq_Data)
{
	int ret=0;
	int profile_id=3,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx,0,sizeof(struct tsg_script_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_RESP_BODY_END;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;

	const char *input="This is response data";
	tsg_ctx.http_body = evbuffer_new();
	evbuffer_add(tsg_ctx.http_body, input, strlen(input));

	ret = execute_lua_script_rule(lua_script, profile_id, NULL, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);
	ASSERT_TRUE(tsg_ctx.http_lua_body!=NULL);

	char *__http_body=(char *) evbuffer_pullup(tsg_ctx.http_lua_body, -1);
	EXPECT_STREQ(__http_body, "This is response data set resp body");
	printf("__http_body: %s\n",__http_body);

	evbuffer_free(tsg_ctx.http_body);
	evbuffer_free(tsg_ctx.http_lua_body);

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

TEST(TSG_LUA_SCRIPT, Lua_Http_Session)
{
	int ret=0;
	int profile_id=0,thread_id=0;
	struct tsg_script_ctx tsg_ctx;
	memset(&tsg_ctx,0,sizeof(struct tsg_script_ctx));

	tsg_ctx.session=lua_http_session_init();
	tsg_ctx.events = EV_HTTP_RESP_HDR;

	struct tsg_lua_script * lua_script=g_tsg_lua_pattern->lua_script;
	lua_script->http_lua_profile = http_lua_profile_for_test;
	tsg_ctx.elua_ctx=http_lua_ctx_new(lua_script, thread_id);

	ret = execute_lua_script_rule(lua_script, profile_id, tsg_ctx.elua_ctx, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);

	const char* content_type_val=tfe_http_std_field_read(tsg_ctx.session->resp, TFE_HTTP_CONT_TYPE);
	EXPECT_TRUE(content_type_val!=NULL);
	EXPECT_STREQ(content_type_val, "utf8");

	tsg_ctx.events = EV_HTTP_RESP_BODY_END;
	tsg_ctx.http_body = evbuffer_new();
	const char *user_input="This is response data";
	evbuffer_add(tsg_ctx.http_body, user_input, strlen(user_input));

	ret = execute_lua_script_rule(lua_script, profile_id, tsg_ctx.elua_ctx, thread_id, (void *)&tsg_ctx);
	EXPECT_TRUE(ret==0);
	EXPECT_TRUE(tsg_ctx.http_lua_body!=NULL);

	char *__http_body=(char *) evbuffer_pullup(tsg_ctx.http_lua_body, -1);
	EXPECT_STREQ(__http_body, "This is response data set resp body");
	printf("__http_body: %s\n",__http_body);

	evbuffer_free(tsg_ctx.http_body);
	evbuffer_free(tsg_ctx.http_lua_body);

	elua_cleanup_script(g_tsg_lua_pattern->elua_ctx[thread_id]);
	FREE(&g_tsg_lua_pattern->elua_ctx[thread_id]);
	g_tsg_lua_pattern->elua_ctx[thread_id]=NULL;
	http_lua_ctx_free(lua_script, thread_id, tsg_ctx.elua_ctx);
	lua_http_session_destory((struct tfe_http_session *)tsg_ctx.session);
}

int main(int argc, char ** argv)
{
	struct tsg_lua_pattern *tsg_lua_pattern = ALLOC(struct tsg_lua_pattern, 1);
	TAILQ_INIT(&tsg_lua_pattern->lua_http_head_list.lua_http_field_list);

	int thread_num=1;
	struct tsg_lua_script *lua_script=ALLOC(struct tsg_lua_script, 1);

	http_lua_handle_create(lua_script, thread_num, "tfe");

	tsg_lua_pattern->elua_ctx = ALLOC(struct elua_script*, thread_num);
	tsg_lua_pattern->lua_script=lua_script;
	tsg_lua_pattern->thread_num=thread_num;
	g_tsg_lua_pattern = tsg_lua_pattern;

	lua_http_default_headers_init(def_lua_http_head_value);

	::testing::InitGoogleTest(&argc, argv);
	return RUN_ALL_TESTS();
}