summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/monkey/fuzz/mk_fuzz_me.c
blob: 0a88d3f1f4c13b3178161372bbcc07bd555b27f1 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <monkey/mk_lib.h>

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

#define API_ADDR   "127.0.0.1"
#define API_PORT   "8080"

/* Main context set as global so the signal handler can use it */
mk_ctx_t *ctx;

void cb_main(mk_request_t *request, void *data)
{
    (void) data;

    mk_http_status(request, 200);
    mk_http_header(request, "X-Monkey", 8, "OK", 2);
    mk_http_send(request, ":)\n", 3, NULL);
    mk_http_done(request);
}

void cb_test_chunks(mk_request_t *request, void *data)
{
    int i = 0;
    int len;
    char tmp[32];
    (void) data;

    mk_http_status(request, 200);
    mk_http_header(request, "X-Monkey", 8, "OK", 2);

    for (i = 0; i < 1000; i++) {
        len = snprintf(tmp, sizeof(tmp) -1, "test-chunk %6i\n ", i);
        mk_http_send(request, tmp, len, NULL);
    }
    mk_http_done(request);
}

void cb_test_big_chunk(mk_request_t *request, void *data)
{
    size_t chunk_size = 1024000000;
    char *chunk;
    (void) data;

    mk_http_status(request, 200);
    mk_http_header(request, "X-Monkey", 8, "OK", 2);

    chunk = calloc(1, chunk_size);
    mk_http_send(request, chunk, chunk_size, NULL);
    free(chunk);
    mk_http_done(request);
}


static void signal_handler(int signal)
{
    write(STDERR_FILENO, "[engine] caught signal\n", 23);

    switch (signal) {
    case SIGTERM:
    case SIGINT:
        mk_stop(ctx);
        mk_destroy(ctx);
        _exit(EXIT_SUCCESS);
    default:
        break;
    }
}

static void signal_init()
{
    signal(SIGINT,  &signal_handler);
    signal(SIGTERM, &signal_handler);
}

static void cb_queue_message(mk_mq_t *queue, void *data, size_t size, void *ctx)
{
    size_t i;
    char *buf;
    (void) ctx;
    (void) queue;

    printf("=== cb queue message === \n");
    printf(" => %lu bytes\n", size);
    printf(" => ");

    buf = data;
    for (i = 0; i < size; i++) {
        printf("%c", buf[i]);
    }
    printf("\n\n");
}


HFND_FUZZING_ENTRY_FUNCTION(int argc, const char *const *argv)
{
    int i = 0;
    int len;
    int vid;
    int qid;
    char msg[800000];

    signal_init();

    ctx = mk_create();
    if (!ctx) {
        return -1;
    }

    /* Create a message queue and a callback for each message */
    qid = mk_mq_create(ctx, "/data", cb_queue_message, NULL);

    mk_config_set(ctx,
                  "Listen", API_PORT,
                  NULL);

    vid = mk_vhost_create(ctx, NULL);
    mk_vhost_set(ctx, vid,
                 "Name", "monotop",
                 NULL);
    mk_vhost_handler(ctx, vid, "/test_chunks", cb_test_chunks, NULL);
    mk_vhost_handler(ctx, vid, "/test_big_chunk", cb_test_big_chunk, NULL);
    mk_vhost_handler(ctx, vid, "/", cb_main, NULL);

    mk_info("Service: http://%s:%s/test_chunks",  API_ADDR, API_PORT);
    mk_start(ctx);

    for (i = 0; i < 5; i++) {
        len = snprintf(msg, sizeof(msg) - 1, "[...] message ID: %i\n", i);
        mk_mq_send(ctx, qid, &msg, len);
    }

    sleep(3600);

    mk_stop(ctx);
    mk_destroy(ctx);

    return 0;
}