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