summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/internal/http_client.c
blob: a37daa355e74d8f9fdc012338847b628f702de62 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_socket.h>
#include <fluent-bit/flb_http_client.h>

#include "flb_tests_internal.h"

struct test_ctx {
    struct flb_upstream   *u;
    struct flb_connection *u_conn;
    struct flb_config     *config;
};

struct test_ctx* test_ctx_create()
{
    struct test_ctx *ret_ctx = NULL;

    ret_ctx = flb_malloc(sizeof(struct test_ctx));
    if (!TEST_CHECK(ret_ctx != NULL)) {
        flb_errno();
        TEST_MSG("flb_malloc(test_ctx) failed");
        return NULL;
    }
    ret_ctx->u = NULL;
    ret_ctx->u_conn = NULL;

    ret_ctx->config = flb_config_init();
    if(!TEST_CHECK(ret_ctx->config != NULL)) {
        TEST_MSG("flb_config_init failed");
        flb_free(ret_ctx);
        return NULL;
    }

    ret_ctx->u = flb_upstream_create(ret_ctx->config, "127.0.0.1", 80, 0, NULL);
    if (!TEST_CHECK(ret_ctx->u != NULL)) {
        TEST_MSG("flb_upstream_create failed");
        flb_config_exit(ret_ctx->config);
        flb_free(ret_ctx);
        return NULL;
    }

    ret_ctx->u_conn = flb_calloc(1, sizeof(struct flb_connection));
    if(!TEST_CHECK(ret_ctx->u_conn != NULL)) {
        flb_errno();
        TEST_MSG("flb_malloc(flb_connection) failed");
        flb_upstream_destroy(ret_ctx->u);
        flb_config_exit(ret_ctx->config);
        flb_free(ret_ctx);
        return NULL;
    }

    ret_ctx->u_conn->upstream = ret_ctx->u;

    return ret_ctx;
}

int test_ctx_destroy(struct test_ctx* ctx)
{
    if (!TEST_CHECK(ctx != NULL)) {
        return -1;
    }
    if (ctx->u_conn) {
        flb_free(ctx->u_conn);
    }
    if (ctx->u) {
        flb_upstream_destroy(ctx->u);
    }
    if (ctx->config) {
        flb_config_exit(ctx->config);
    }

    flb_free(ctx);
    return 0;
}

void test_http_buffer_increase()
{
    int ret;
    size_t s;
    struct test_ctx *ctx;
    struct flb_http_client *c;
    struct flb_http_response *resp;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    TEST_CHECK(c != NULL);

    /* Do buffer tests */
    resp = &c->resp;
    TEST_CHECK(resp->data_len == 0);
    TEST_CHECK(resp->data_size == FLB_HTTP_DATA_SIZE_MAX);
    TEST_CHECK(resp->data_size_max == FLB_HTTP_DATA_SIZE_MAX);

    /* Invalid size */
    ret = flb_http_buffer_size(c, 1);
    TEST_CHECK(ret == -1);

    /* Increase max size to 8KB */
    flb_http_buffer_size(c, 8192);

    /* Request to allocate +4KB */
    ret = flb_http_buffer_increase(c, 4096, &s);
    TEST_CHECK(ret == 0);
    TEST_CHECK(s == 4096);

    /* Request to allocate 1 byte, it should fail */
    ret = flb_http_buffer_increase(c, 1, &s);
    TEST_CHECK(ret == -1);

    /* Test unlimited */
    flb_http_buffer_size(c, 0);
    ret = flb_http_buffer_increase(c, 1, &s);
    TEST_CHECK(ret == 0 && s == 1);

    /* Payload test */
    memcpy(c->resp.data, "00abc11def22ghi__PAYLOAD__8yz9900", 33);
    c->resp.data[33] = '\0';
    c->resp.data_len = 33;
    c->resp.payload = c->resp.data + 15;
    c->resp.payload_size = 11;

    ret = flb_http_buffer_increase(c, 819200, &s);
    TEST_CHECK(ret == 0);

    ret = strncmp(c->resp.payload, "__PAYLOAD__", 11);
    TEST_CHECK(ret == 0);

    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_add_get_header()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    char *ua = "Fluent-Bit";
    char *host = "127.0.0.1:80";
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Check User-Agent */
    ret = flb_http_add_header(c, "User-Agent", 10, ua, strlen(ua));
    TEST_CHECK(ret == 0);

    ret_str = flb_http_get_header(c, "User-Agent", 10);
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, ua, strlen(ua)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, ua);
    }
    flb_sds_destroy(ret_str);


    /* Check Host */
    ret_str = flb_http_get_header(c, "Host", 4);
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, host, strlen(host)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, host);
    }

    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_set_keepalive()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Set keepalive header */
    ret = flb_http_set_keepalive(c);
    TEST_CHECK(ret == 0);

    ret_str = flb_http_get_header(c, FLB_HTTP_HEADER_CONNECTION,
                                  strlen(FLB_HTTP_HEADER_CONNECTION));
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Compare */
    if (!TEST_CHECK(flb_sds_cmp(ret_str, FLB_HTTP_HEADER_KA, strlen(FLB_HTTP_HEADER_KA)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, FLB_HTTP_HEADER_KA);
    }
    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_strip_port_from_host()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    char *host_port = "127.0.0.1:80";
    char *host = "127.0.0.1";
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Check Host. It should contain port number */
    ret_str = flb_http_get_header(c, "Host", 4);
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, host_port, strlen(host_port)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, host_port);
    }
    flb_sds_destroy(ret_str);


    ret = flb_http_strip_port_from_host(c);
    TEST_CHECK(ret == 0);

    /* Check Host. Port number should be removed. */
    ret_str = flb_http_get_header(c, "Host", 4);
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, host, strlen(host)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, host);
    }

    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_encoding_gzip()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    char *gzip = "gzip";
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Check encoding. It should be error */
    ret_str = flb_http_get_header(c, FLB_HTTP_HEADER_CONTENT_ENCODING,
                                  strlen(FLB_HTTP_HEADER_CONTENT_ENCODING));
    if (!TEST_CHECK(ret_str == NULL)) {
        TEST_MSG("Got encoding? Header:%s", ret_str);
        flb_sds_destroy(ret_str);
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    ret = flb_http_set_content_encoding_gzip(c);
    TEST_CHECK(ret == 0);

    /* Check Encoding */
    ret_str = flb_http_get_header(c, FLB_HTTP_HEADER_CONTENT_ENCODING,
                                  strlen(FLB_HTTP_HEADER_CONTENT_ENCODING));
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, gzip, strlen(gzip)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, gzip);
    }

    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_add_basic_auth_header()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    char *expect = "Basic dXNlcjpwYXNzd29yZA=="; /* user:password in base64 */
    char *auth = FLB_HTTP_HEADER_AUTH;
    const char *user = "user";
    char *passwd = "password";
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Check Autholization. It should be error. */
    ret_str = flb_http_get_header(c, auth, strlen(auth));
    if (!TEST_CHECK(ret_str == NULL)) {
        TEST_MSG("Got auth? Header:%s", ret_str);
        flb_sds_destroy(ret_str);
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    ret = flb_http_basic_auth(c, user, passwd);
    TEST_CHECK(ret == 0);

    /* Check Autholization. */
    ret_str = flb_http_get_header(c, auth, strlen(auth));
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, expect, strlen(expect)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, expect);
    }

    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

void test_http_add_proxy_auth_header()
{
    struct test_ctx *ctx;
    struct flb_http_client *c;
    flb_sds_t ret_str;
    char *expect = "Basic dXNlcjpwYXNzd29yZA=="; /* user:password in base64 */
    char *auth = FLB_HTTP_HEADER_PROXY_AUTH;
    const char *user = "user";
    char *passwd = "password";
    int ret;

    ctx = test_ctx_create();
    if (!TEST_CHECK(ctx != NULL)) {
        exit(EXIT_FAILURE);
    }

    /* Create HTTP client instance */
    c = flb_http_client(ctx->u_conn, FLB_HTTP_GET, "/", NULL, 0,
                        "127.0.0.1", 80, NULL, 0);
    if(!TEST_CHECK(c != NULL)) {
        TEST_MSG("flb_http_client failed");
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    /* Check autholization header. It should be error. */
    ret_str = flb_http_get_header(c, auth, strlen(auth));
    if (!TEST_CHECK(ret_str == NULL)) {
        TEST_MSG("Got auth? Header:%s", ret_str);
        flb_sds_destroy(ret_str);
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    ret = flb_http_proxy_auth(c, user, passwd);
    TEST_CHECK(ret == 0);

    /* Check autholization header. */
    ret_str = flb_http_get_header(c, auth, strlen(auth));
    if (!TEST_CHECK(ret_str != NULL)) {
        TEST_MSG("flb_http_get_header failed");
        flb_http_client_destroy(c);
        test_ctx_destroy(ctx);
        exit(EXIT_FAILURE);
    }

    if (!TEST_CHECK(flb_sds_cmp(ret_str, expect, strlen(expect)) == 0)) {
        TEST_MSG("strcmp failed. got=%s expect=%s", ret_str, expect);
    }

    flb_sds_destroy(ret_str);
    flb_http_client_destroy(c);
    test_ctx_destroy(ctx);
}

TEST_LIST = {
    { "http_buffer_increase"  , test_http_buffer_increase},
    { "add_get_header"        , test_http_add_get_header},
    { "set_keepalive"         , test_http_set_keepalive},
    { "strip_port_from_host"  , test_http_strip_port_from_host},
    { "encoding_gzip"         , test_http_encoding_gzip},
    { "add_basic_auth_header" , test_http_add_basic_auth_header},
    { "add_proxy_auth_header" , test_http_add_proxy_auth_header},
    { 0 }
};