/src/mosquitto/fuzzing/broker/broker_fuzz_test_config.cpp
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2023 Cedalo GmbH |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include <cstdio> |
20 | | #include <cstdint> |
21 | | #include <cstdlib> |
22 | | #include <cstring> |
23 | | #include <sys/stat.h> |
24 | | #include <unistd.h> |
25 | | |
26 | | #include "broker_fuzz.h" |
27 | | |
28 | | /* |
29 | | * Broker check of config only, the config isn't used |
30 | | */ |
31 | | |
32 | | /* The broker fuzz-only main function. */ |
33 | | extern "C" int mosquitto_fuzz_main(int argc, char *argv[]); |
34 | | |
35 | | |
36 | | void run_broker(char *filename) |
37 | 8.61k | { |
38 | 8.61k | char *argv[5]; |
39 | 8.61k | int argc = 5; |
40 | | |
41 | 8.61k | argv[0] = strdup("mosquitto"); |
42 | 8.61k | argv[1] = strdup("--test-config"); |
43 | 8.61k | argv[2] = strdup("-q"); |
44 | 8.61k | argv[3] = strdup("-c"); |
45 | 8.61k | argv[4] = strdup(filename); |
46 | | |
47 | 8.61k | mosquitto_fuzz_main(argc, argv); |
48 | | |
49 | 51.6k | for(int i=0; i<argc; i++){ |
50 | 43.0k | free(argv[i]); |
51 | 43.0k | } |
52 | 8.61k | } |
53 | | |
54 | | |
55 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
56 | 8.61k | { |
57 | 8.61k | char filename[100]; |
58 | 8.61k | FILE *fptr; |
59 | | |
60 | 8.61k | if(size < kMinInputLength){ |
61 | 4 | return 0; |
62 | 4 | } |
63 | | |
64 | 8.61k | umask(0077); |
65 | | |
66 | 8.61k | snprintf(filename, sizeof(filename), "/tmp/mosquitto_%d.conf", getpid()); |
67 | 8.61k | fptr = fopen(filename, "wb"); |
68 | 8.61k | if(!fptr){ |
69 | 0 | return 1; |
70 | 0 | } |
71 | 8.61k | fwrite(data, 1, size, fptr); |
72 | 8.61k | fclose(fptr); |
73 | | |
74 | 8.61k | run_broker(filename); |
75 | | |
76 | 8.61k | unlink(filename); |
77 | | |
78 | 8.61k | return 0; |
79 | 8.61k | } |