aboutsummaryrefslogtreecommitdiff
path: root/src/test/fuzz/span.cpp
diff options
context:
space:
mode:
authorpracticalswift <practicalswift@users.noreply.github.com>2020-03-27 21:12:28 +0000
committerpracticalswift <practicalswift@users.noreply.github.com>2020-03-29 13:17:04 +0000
commitf205cf7fef5618aaa96f016fda168eedfd9da437 (patch)
tree7086a7a15fef3e872683042ea85861b9a85ee8df /src/test/fuzz/span.cpp
parent9718f38f54357f15b8a27e060aed56f91015112d (diff)
tests: Add fuzzing harness for functions/classes in span.h
Diffstat (limited to 'src/test/fuzz/span.cpp')
-rw-r--r--src/test/fuzz/span.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/test/fuzz/span.cpp b/src/test/fuzz/span.cpp
new file mode 100644
index 0000000000..4aea530ef2
--- /dev/null
+++ b/src/test/fuzz/span.cpp
@@ -0,0 +1,39 @@
+// Copyright (c) 2020 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <span.h>
+#include <test/fuzz/FuzzedDataProvider.h>
+#include <test/fuzz/fuzz.h>
+#include <test/fuzz/util.h>
+
+#include <cassert>
+#include <cstddef>
+#include <cstdint>
+#include <string>
+#include <vector>
+
+void test_one_input(const std::vector<uint8_t>& buffer)
+{
+ FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
+
+ std::string str = fuzzed_data_provider.ConsumeBytesAsString(32);
+ const Span<const char> span = MakeSpan(str);
+ (void)span.data();
+ (void)span.begin();
+ (void)span.end();
+ if (span.size() > 0) {
+ const std::ptrdiff_t idx = fuzzed_data_provider.ConsumeIntegralInRange<std::ptrdiff_t>(0U, span.size() - 1U);
+ (void)span.first(idx);
+ (void)span.last(idx);
+ (void)span.subspan(idx);
+ (void)span.subspan(idx, span.size() - idx);
+ (void)span[idx];
+ }
+
+ std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32);
+ const Span<const char> another_span = MakeSpan(another_str);
+ assert((span <= another_span) != (span > another_span));
+ assert((span == another_span) != (span != another_span));
+ assert((span >= another_span) != (span < another_span));
+}