blob: a5e99a1bacd36f175d9f901b7d3656c0ba0f59f0 [file] [log] [blame]
Craig Tiller2c357bc2017-09-29 11:10:14 -07001/*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19#include <stdbool.h>
20
21#include <dirent.h>
22#include <gflags/gflags.h>
23#include <grpc/support/log.h>
24#include <gtest/gtest.h>
25#include <stdio.h>
26#include <sys/types.h>
27
28#include "src/core/lib/iomgr/load_file.h"
29#include "test/core/util/test_config.h"
30
31extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
32extern "C" bool squelch;
33extern "C" bool leak_check;
34
35DEFINE_string(file, "", "Use this file as test data");
36DEFINE_string(directory, "", "Use this directory as test data");
37
38class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {};
39
40TEST_P(FuzzerCorpusTest, RunOneExample) {
Craig Tillerd75d90f2017-10-03 09:58:21 -070041 gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str());
Craig Tiller2c357bc2017-09-29 11:10:14 -070042 grpc_slice buffer;
43 squelch = false;
44 leak_check = false;
45 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
46 grpc_load_file(GetParam().c_str(), 0, &buffer)));
47 LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer),
48 GRPC_SLICE_LENGTH(buffer));
49 grpc_slice_unref(buffer);
50}
51
52class ExampleGenerator
53 : public ::testing::internal::ParamGeneratorInterface<std::string> {
54 public:
55 virtual ::testing::internal::ParamIteratorInterface<std::string>* Begin()
56 const;
57 virtual ::testing::internal::ParamIteratorInterface<std::string>* End() const;
58
59 private:
60 void Materialize() const {
61 if (examples_.empty()) {
62 if (!FLAGS_file.empty()) examples_.push_back(FLAGS_file);
63 if (!FLAGS_directory.empty()) {
64 DIR* dp;
65 struct dirent* ep;
Craig Tillerd75d90f2017-10-03 09:58:21 -070066 dp = opendir(FLAGS_directory.c_str());
Craig Tiller2c357bc2017-09-29 11:10:14 -070067
68 if (dp != NULL) {
Craig Tillerd75d90f2017-10-03 09:58:21 -070069 while ((ep = readdir(dp)) != nullptr) {
70 if (ep->d_type == DT_REG) {
71 examples_.push_back(FLAGS_directory + "/" + ep->d_name);
72 }
73 }
Craig Tiller2c357bc2017-09-29 11:10:14 -070074
75 (void)closedir(dp);
76 } else {
77 perror("Couldn't open the directory");
78 abort();
79 }
80 }
81 }
82 }
83
84 mutable std::vector<std::string> examples_;
85};
86
87class ExampleIterator
88 : public ::testing::internal::ParamIteratorInterface<std::string> {
89 public:
90 ExampleIterator(const ExampleGenerator& base_,
91 std::vector<std::string>::const_iterator begin)
Craig Tillerd75d90f2017-10-03 09:58:21 -070092 : base_(base_), begin_(begin), current_(begin) {}
Craig Tiller2c357bc2017-09-29 11:10:14 -070093
Craig Tiller2c357bc2017-09-29 11:10:14 -070094 virtual const ExampleGenerator* BaseGenerator() const { return &base_; }
95
Craig Tillerd75d90f2017-10-03 09:58:21 -070096 virtual void Advance() { current_++; }
Craig Tiller2c357bc2017-09-29 11:10:14 -070097 virtual ExampleIterator* Clone() const { return new ExampleIterator(*this); }
Craig Tillerd75d90f2017-10-03 09:58:21 -070098 virtual const std::string* Current() const { return &*current_; }
Craig Tiller2c357bc2017-09-29 11:10:14 -070099
100 virtual bool Equals(const ParamIteratorInterface<std::string>& other) const {
101 return &base_ == other.BaseGenerator() &&
102 current_ == dynamic_cast<const ExampleIterator*>(&other)->current_;
103 }
104
105 private:
106 ExampleIterator(const ExampleIterator& other)
Craig Tillerd75d90f2017-10-03 09:58:21 -0700107 : base_(other.base_), begin_(other.begin_), current_(other.current_) {}
Craig Tiller2c357bc2017-09-29 11:10:14 -0700108
109 const ExampleGenerator& base_;
110 const std::vector<std::string>::const_iterator begin_;
111 std::vector<std::string>::const_iterator current_;
Craig Tiller2c357bc2017-09-29 11:10:14 -0700112};
113
114::testing::internal::ParamIteratorInterface<std::string>*
115ExampleGenerator::Begin() const {
116 Materialize();
117 return new ExampleIterator(*this, examples_.begin());
118}
119
120::testing::internal::ParamIteratorInterface<std::string>*
121ExampleGenerator::End() const {
122 Materialize();
123 return new ExampleIterator(*this, examples_.end());
124}
125
126INSTANTIATE_TEST_CASE_P(
127 CorpusExamples, FuzzerCorpusTest,
128 ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator));
129
130int main(int argc, char** argv) {
131 grpc_test_init(argc, argv);
Craig Tillerd75d90f2017-10-03 09:58:21 -0700132 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
Craig Tiller2c357bc2017-09-29 11:10:14 -0700133 ::testing::InitGoogleTest(&argc, argv);
134
135 return RUN_ALL_TESTS();
136}