summaryrefslogtreecommitdiffstats
path: root/src/journal/fuzz-journald.c
blob: c96fad58e8dda990a8fe6742bd86e7a902f2a35e (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "fuzz-journald.h"
#include "journald-server.h"
#include "sd-event.h"

void dummy_server_init(Server *s, const uint8_t *buffer, size_t size) {
        *s = (Server) {
                .syslog_fd = -EBADF,
                .native_fd = -EBADF,
                .stdout_fd = -EBADF,
                .dev_kmsg_fd = -EBADF,
                .audit_fd = -EBADF,
                .hostname_fd = -EBADF,
                .notify_fd = -EBADF,
                .storage = STORAGE_NONE,
                .line_max = 64,
        };
        assert_se(sd_event_default(&s->event) >= 0);

        if (buffer) {
                s->buffer = memdup_suffix0(buffer, size);
                assert_se(s->buffer);
        }
}

void fuzz_journald_processing_function(
                const uint8_t *data,
                size_t size,
                void (*f)(Server *s, const char *buf, size_t raw_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len)
        ) {
        Server s;
        char *label = NULL;
        size_t label_len = 0;
        struct ucred *ucred = NULL;
        struct timeval *tv = NULL;

        if (size == 0)
                return;

        dummy_server_init(&s, data, size);
        (*f)(&s, s.buffer, size, ucred, tv, label, label_len);
        server_done(&s);
}