aboutsummaryrefslogtreecommitdiff
path: root/src/test/fuzz/fuzz_diff.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-03-07 15:07:27 -0500
committerNick Mathewson <nickm@torproject.org>2017-03-16 14:42:55 -0400
commitdd92579b63fa012bcbe081ab66ff40d6a036b765 (patch)
treeb0a7bbb0085a25717ff566766178d848d53ff60f /src/test/fuzz/fuzz_diff.c
parent653c6d129efc93899ffb447d99ecee98dd7ab373 (diff)
downloadtor-dd92579b63fa012bcbe081ab66ff40d6a036b765.tar.gz
tor-dd92579b63fa012bcbe081ab66ff40d6a036b765.zip
Add fuzzers for consensus diff backend code
This takes two fuzzers: one which generates a diff and makes sure it works, and one which applies a diff. So far, they won't crash, but there's a bug in my string-manipulation code someplace that I'm having to work around, related to the case where you have a blank line at the end of a file, or where you diff a file with itself.
Diffstat (limited to 'src/test/fuzz/fuzz_diff.c')
-rw-r--r--src/test/fuzz/fuzz_diff.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/test/fuzz/fuzz_diff.c b/src/test/fuzz/fuzz_diff.c
new file mode 100644
index 0000000000..c241f63dc4
--- /dev/null
+++ b/src/test/fuzz/fuzz_diff.c
@@ -0,0 +1,67 @@
+/* Copyright (c) 2016, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define CONSDIFF_PRIVATE
+
+#include "orconfig.h"
+#include "or.h"
+#include "consdiff.h"
+
+#include "fuzzing.h"
+
+static int
+mock_consensus_compute_digest_(const char *c, consensus_digest_t *d)
+{
+ (void)c;
+ memset(d->sha3_256, 3, sizeof(d->sha3_256));
+ return 0;
+}
+
+int
+fuzz_init(void)
+{
+ MOCK(consensus_compute_digest, mock_consensus_compute_digest_);
+ return 0;
+}
+
+int
+fuzz_cleanup(void)
+{
+ UNMOCK(consensus_compute_digest);
+ return 0;
+}
+
+int
+fuzz_main(const uint8_t *stdin_buf, size_t data_size)
+{
+#define SEP "=====\n"
+#define SEPLEN strlen(SEP)
+ const uint8_t *separator = tor_memmem(stdin_buf, data_size, SEP, SEPLEN);
+ if (! separator)
+ return 0;
+ size_t c1_len = separator - stdin_buf;
+ char *c1 = tor_memdup_nulterm(stdin_buf, c1_len);
+ size_t c2_len = data_size - c1_len - SEPLEN;
+ char *c2 = tor_memdup_nulterm(separator + SEPLEN, c2_len);
+
+ char *c3 = consensus_diff_generate(c1, c2);
+
+ if (c3) {
+ char *c4 = consensus_diff_apply(c1, c3);
+ tor_assert(c4);
+ if (strcmp(c2, c4)) {
+ printf("%s\n", escaped(c1));
+ printf("%s\n", escaped(c2));
+ printf("%s\n", escaped(c3));
+ printf("%s\n", escaped(c4));
+ }
+ tor_assert(! strcmp(c2, c4));
+ tor_free(c3);
+ tor_free(c4);
+ }
+ tor_free(c1);
+ tor_free(c2);
+
+ return 0;
+}
+