/src/ostree/tests/fuzz-bsdiff.c
Line | Count | Source |
1 | | /* Copyright 2022 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include "config.h" |
14 | | |
15 | | #include "libglnx.h" |
16 | | #include "bsdiff/bsdiff.h" |
17 | | #include "bsdiff/bspatch.h" |
18 | | #include <glib.h> |
19 | | #include <stdlib.h> |
20 | | #include <gio/gio.h> |
21 | | #include <string.h> |
22 | | |
23 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
24 | | |
25 | | static int |
26 | | bzdiff_write (struct bsdiff_stream* stream, const void* buffer, int size) |
27 | 11.4k | { |
28 | 11.4k | GOutputStream *out = stream->opaque; |
29 | 11.4k | if (! g_output_stream_write (out, |
30 | 11.4k | buffer, |
31 | 11.4k | size, |
32 | 11.4k | NULL, |
33 | 11.4k | NULL)) { |
34 | 0 | return -1; |
35 | 0 | } |
36 | | |
37 | 11.4k | return 0; |
38 | 11.4k | } |
39 | | |
40 | | |
41 | | int |
42 | | LLVMFuzzerTestOneInput (const uint8_t *data, |
43 | | size_t size) |
44 | 1.19k | { |
45 | 640k | #define NEW_SIZE (512+24) |
46 | | |
47 | 1.19k | struct bsdiff_stream bsdiff_stream; |
48 | 1.19k | struct bspatch_stream bspatch_stream; |
49 | 1.19k | int i; |
50 | 1.19k | g_autofree guint8 *old = g_new (guint8, size); |
51 | 1.19k | g_autofree guint8 *new = g_new (guint8, NEW_SIZE); |
52 | 1.19k | g_autofree guint8 *new_generated = g_new0 (guint8, NEW_SIZE); |
53 | 1.19k | g_autoptr(GOutputStream) out = g_memory_output_stream_new_resizable (); |
54 | 1.19k | g_autoptr(GInputStream) in = NULL; |
55 | | |
56 | 1.19k | new[0] = 'A'; |
57 | 85.4M | for (i = 0; i < size; i++) { |
58 | 85.4M | old[i] = data[i]; |
59 | 85.4M | } |
60 | 639k | for (i = 0; i < NEW_SIZE; i++) { |
61 | 638k | new[i] = i; |
62 | 638k | } |
63 | | |
64 | 1.19k | bsdiff_stream.malloc = malloc; |
65 | 1.19k | bsdiff_stream.free = free; |
66 | 1.19k | bsdiff_stream.write = bzdiff_write; |
67 | 1.19k | bsdiff_stream.opaque = out; |
68 | 1.19k | bsdiff (old, size, new, NEW_SIZE, &bsdiff_stream); |
69 | | |
70 | 1.19k | return 0; |
71 | 1.19k | } |