Skip to content

Commit 1b87492

Browse files
tyler92ashtum
authored andcommitted
Add fuzzing targets
1 parent e7f4919 commit 1b87492

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

test/fuzz/http_request.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright (c) 2024 Mikhail Khachayants
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
#include <boost/beast/http.hpp>
9+
#include <boost/beast/_experimental/test/stream.hpp>
10+
11+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
12+
{
13+
using namespace boost::beast;
14+
15+
error_code ec;
16+
flat_buffer buffer;
17+
net::io_context ioc;
18+
test::stream stream{ioc, {reinterpret_cast<const char*>(data), size}};
19+
stream.close_remote();
20+
21+
http::request_parser<http::dynamic_body> parser;
22+
http::read(stream, buffer, parser, ec);
23+
24+
return 0;
25+
}

test/fuzz/http_response.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright (c) 2024 Mikhail Khachayants
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
#include <boost/beast/http.hpp>
9+
#include <boost/beast/_experimental/test/stream.hpp>
10+
11+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
12+
{
13+
using namespace boost::beast;
14+
15+
error_code ec;
16+
flat_buffer buffer;
17+
net::io_context ioc;
18+
test::stream stream{ioc, {reinterpret_cast<const char*>(data), size}};
19+
stream.close_remote();
20+
21+
http::chunk_extensions ce;
22+
http::response_parser<http::dynamic_body> parser;
23+
24+
auto chunk_header_cb = [&ce](std::uint64_t, string_view extensions, error_code& ev)
25+
{
26+
ce.parse(extensions, ev);
27+
};
28+
29+
parser.on_chunk_header(chunk_header_cb);
30+
http::read(stream, buffer, parser, ec);
31+
32+
return 0;
33+
}

test/fuzz/seeds.tar

13 KB
Binary file not shown.

test/fuzz/websocket_server.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright (c) 2024 Mikhail Khachayants
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
#include <boost/beast/websocket.hpp>
9+
#include <boost/beast/_experimental/test/stream.hpp>
10+
11+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
12+
{
13+
using namespace boost::beast;
14+
15+
error_code ec;
16+
flat_buffer buffer;
17+
net::io_context ioc;
18+
test::stream remote{ioc};
19+
20+
websocket::stream<test::stream> ws{
21+
ioc, string_view{reinterpret_cast<const char*>(data), size}};
22+
23+
ws.set_option(websocket::stream_base::decorator(
24+
[](websocket::response_type& res)
25+
{
26+
res.set(http::field::server, "websocket-server-sync");
27+
}));
28+
29+
websocket::permessage_deflate pd;
30+
pd.server_enable = (size % 2) != 0;
31+
pd.compLevel = static_cast<int>(size % 9);
32+
ws.set_option(pd);
33+
34+
ws.next_layer().connect(remote);
35+
ws.next_layer().close_remote();
36+
ws.accept(ec);
37+
38+
if(!ec)
39+
{
40+
ws.read(buffer, ec);
41+
ws.text(ws.got_text());
42+
ws.write(buffer.data(), ec);
43+
}
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)