blob: 4197a1cacca1442c8a85a2930d80460884de3bc6 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "garnet/lib/overnet/packet_protocol/packet_protocol_fuzzer.h"
using namespace overnet;
namespace {
class InputStream {
public:
InputStream(const uint8_t* data, size_t size)
: cur_(data), end_(data + size) {}
uint64_t Next64() {
uint64_t out;
if (!varint::Read(&cur_, end_, &out))
out = 0;
return out;
}
uint8_t NextByte() {
if (cur_ == end_)
return 0;
return *cur_++;
}
Slice NextSlice() {
auto len = NextByte();
auto prefix = NextByte();
return Slice::WithInitializerAndBorders(
len, Border::Prefix(prefix), [this, len](uint8_t* p) {
for (uint8_t i = 0; i < len; i++) {
*p++ = NextByte();
}
});
}
private:
const uint8_t* cur_;
const uint8_t* end_;
};
} // namespace
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
InputStream input(data, size);
PacketProtocolFuzzer fuzzer(PacketProtocol::NullCodec(), false);
for (;;) {
switch (input.NextByte()) {
default:
// input exhausted, or unknown op-code
return 0;
case 1: {
auto sender = input.NextByte();
auto slice = input.NextSlice();
if (!fuzzer.BeginSend(sender, slice))
return 0;
} break;
case 2: {
auto sender = input.NextByte();
auto status = input.NextByte();
if (!fuzzer.CompleteSend(sender, status))
return 0;
} break;
case 3:
if (!fuzzer.StepTime(input.Next64()))
return 0;
break;
}
}
}