Skip to content

Commit 64d277b

Browse files
tests: Add fuzzing harness for LimitedString (serialize.h)
1 parent f205cf7 commit 64d277b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/test/fuzz/string.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <rpc/server.h>
1313
#include <rpc/util.h>
1414
#include <script/descriptor.h>
15+
#include <serialize.h>
16+
#include <streams.h>
1517
#include <test/fuzz/FuzzedDataProvider.h>
1618
#include <test/fuzz/fuzz.h>
1719
#include <test/fuzz/util.h>
@@ -24,6 +26,7 @@
2426
#include <util/system.h>
2527
#include <util/translation.h>
2628
#include <util/url.h>
29+
#include <version.h>
2730

2831
#include <cstdint>
2932
#include <string>
@@ -86,4 +89,30 @@ void test_one_input(const std::vector<uint8_t>& buffer)
8689
(void)urlDecode(random_string_1);
8790
(void)ValidAsCString(random_string_1);
8891
(void)_(random_string_1.c_str());
92+
93+
{
94+
CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
95+
std::string s;
96+
LimitedString<10> limited_string = LIMITED_STRING(s, 10);
97+
data_stream << random_string_1;
98+
try {
99+
data_stream >> limited_string;
100+
assert(data_stream.empty());
101+
assert(s.size() <= random_string_1.size());
102+
assert(s.size() <= 10);
103+
if (!random_string_1.empty()) {
104+
assert(!s.empty());
105+
}
106+
} catch (const std::ios_base::failure&) {
107+
}
108+
}
109+
{
110+
CDataStream data_stream{SER_NETWORK, INIT_PROTO_VERSION};
111+
const LimitedString<10> limited_string = LIMITED_STRING(random_string_1, 10);
112+
data_stream << limited_string;
113+
std::string deserialized_string;
114+
data_stream >> deserialized_string;
115+
assert(data_stream.empty());
116+
assert(deserialized_string == random_string_1);
117+
}
89118
}

0 commit comments

Comments
 (0)