Debian Codegraph @

fuzzing/apps/mosquitto_passwd/mosquitto_passwd_fuzz_load.cpp

raw
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 /*
27 * Test loading a file
28 */
29
30
31 /* The fuzz-only main function. */
32 extern "C" int mosquitto_passwd_fuzz_main(int argc, char *argv[]);
33
34
35 void run_mosquitto_passwd(char *filename)
36 {
37 char *argv[5];
38 int argc = 5;
39
40 argv[0] = strdup("mosquitto_passwd");
41 argv[1] = strdup("-b");
42 argv[2] = filename;
43 argv[3] = strdup("username");
44 argv[4] = strdup("password");
45
46 mosquitto_passwd_fuzz_main(argc, argv);
47
48 free(argv[0]);
49 free(argv[1]);
50 free(argv[3]);
51 free(argv[4]);
52 }
53
54
55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57 char filename[100];
58 FILE *fptr;
59
60 umask(0077);
61
62 snprintf(filename, sizeof(filename), "mosquitto_passwd_%d", getpid());
63 fptr = fopen(filename, "wb");
64 if(!fptr){
65 return 1;
66 }
67 fwrite(data, 1, size, fptr);
68 fclose(fptr);
69
70 run_mosquitto_passwd(filename);
71
72 unlink(filename);
73
74 return 0;
75 }