|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | #include "port/gtest.h"
|
| 16 | +#include "port/protobuf.h" |
16 | 17 | #include "src/libfuzzer/libfuzzer_macro.h"
|
17 | 18 | #include "src/mutator_test_proto2.pb.h"
|
18 | 19 |
|
19 |
| -static bool reached = false; |
20 |
| -static bool postprocessed = false; |
| 20 | +using protobuf_mutator::protobuf::util::MessageDifferencer; |
| 21 | +using ::testing::_; |
| 22 | +using ::testing::AllOf; |
| 23 | +using ::testing::DoAll; |
| 24 | +using ::testing::Ref; |
| 25 | +using ::testing::SaveArg; |
| 26 | +using ::testing::SaveArgPointee; |
| 27 | +using ::testing::StrictMock; |
| 28 | + |
| 29 | +static class MockFuzzer* mock_fuzzer; |
| 30 | + |
| 31 | +class MockFuzzer { |
| 32 | + public: |
| 33 | + MockFuzzer() { mock_fuzzer = this; } |
| 34 | + ~MockFuzzer() { mock_fuzzer = nullptr; } |
| 35 | + MOCK_METHOD(void, PostProcess, |
| 36 | + (protobuf_mutator::Msg * message, unsigned int seed)); |
| 37 | + MOCK_METHOD(void, TestOneInput, (const protobuf_mutator::Msg& message)); |
| 38 | +}; |
21 | 39 |
|
22 | 40 | protobuf_mutator::libfuzzer::PostProcessorRegistration<protobuf_mutator::Msg>
|
23 | 41 | reg = {[](protobuf_mutator::Msg* message, unsigned int seed) {
|
24 |
| - static unsigned int first_seed = seed; |
25 |
| - EXPECT_EQ(seed, first_seed); |
26 |
| - postprocessed = true; |
| 42 | + mock_fuzzer->PostProcess(message, seed); |
27 | 43 | }};
|
28 | 44 |
|
29 | 45 | DEFINE_TEXT_PROTO_FUZZER(const protobuf_mutator::Msg& message) {
|
30 |
| - reached = true; |
31 |
| - EXPECT_TRUE(message.IsInitialized()); |
32 |
| - EXPECT_TRUE(postprocessed); |
| 46 | + mock_fuzzer->TestOneInput(message); |
| 47 | +} |
| 48 | + |
| 49 | +MATCHER_P(IsMessageEq, msg, "") { |
| 50 | + return MessageDifferencer::Equals(arg, msg.get()); |
33 | 51 | }
|
| 52 | +MATCHER(IsInitialized, "") { return arg.IsInitialized(); } |
34 | 53 |
|
35 | 54 | TEST(LibFuzzerTest, LLVMFuzzerTestOneInput) {
|
36 |
| - for (int i = 0; i < 10; ++i) { |
37 |
| - reached = false; |
38 |
| - postprocessed = false; |
39 |
| - LLVMFuzzerTestOneInput((const uint8_t*)"", 0); |
40 |
| - EXPECT_TRUE(reached); |
41 |
| - } |
| 55 | + unsigned int seed = 0; |
| 56 | + testing::StrictMock<MockFuzzer> mock; |
| 57 | + protobuf_mutator::Msg msg; |
| 58 | + EXPECT_CALL(mock, PostProcess(_, _)) |
| 59 | + .WillOnce(DoAll(SaveArgPointee<0>(&msg), SaveArg<1>(&seed))); |
| 60 | + EXPECT_CALL( |
| 61 | + mock, TestOneInput(AllOf(IsMessageEq(std::cref(msg)), IsInitialized()))); |
| 62 | + LLVMFuzzerTestOneInput((const uint8_t*)"", 0); |
| 63 | + |
| 64 | + EXPECT_CALL(mock, PostProcess(_, seed)).WillOnce(SaveArgPointee<0>(&msg)); |
| 65 | + EXPECT_CALL( |
| 66 | + mock, TestOneInput(AllOf(IsMessageEq(std::cref(msg)), IsInitialized()))); |
| 67 | + LLVMFuzzerTestOneInput((const uint8_t*)"", 0); |
| 68 | +} |
42 | 69 | }
|
0 commit comments