Skip to content

Commit 3c50b67

Browse files
committed
Use gmock in LibFuzzerTest
1 parent d82b5fb commit 3c50b67

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

cmake/external/googletest.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(GTEST_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/${GTEST_TARGET})
1818
set(GTEST_INCLUDE_DIRS ${GTEST_INSTALL_DIR}/include)
1919
include_directories(${GTEST_INCLUDE_DIRS})
2020

21-
set(GTEST_LIBRARIES gtest)
21+
set(GTEST_LIBRARIES gtest gmock)
2222
set(GTEST_MAIN_LIBRARIES gtest_main)
2323
set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
2424

port/gtest.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef PORT_GTEST_H_
1616
#define PORT_GTEST_H_
1717

18+
#include "gmock/gmock.h"
1819
#include "gtest/gtest.h"
1920

2021
#endif // PORT_GTEST_H_

src/libfuzzer/libfuzzer_test.cc

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,57 @@
1313
// limitations under the License.
1414

1515
#include "port/gtest.h"
16+
#include "port/protobuf.h"
1617
#include "src/libfuzzer/libfuzzer_macro.h"
1718
#include "src/mutator_test_proto2.pb.h"
1819

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+
};
2139

2240
protobuf_mutator::libfuzzer::PostProcessorRegistration<protobuf_mutator::Msg>
2341
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);
2743
}};
2844

2945
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());
3351
}
52+
MATCHER(IsInitialized, "") { return arg.IsInitialized(); }
3453

3554
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+
}
4269
}

0 commit comments

Comments
 (0)