Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); |
| 32 | extern "C" bool squelch; |
| 33 | extern "C" bool leak_check; |
| 34 | |
| 35 | DEFINE_string(file, "", "Use this file as test data"); |
| 36 | DEFINE_string(directory, "", "Use this directory as test data"); |
| 37 | |
| 38 | class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {}; |
| 39 | |
| 40 | TEST_P(FuzzerCorpusTest, RunOneExample) { |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 41 | gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str()); |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 42 | 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 | |
| 52 | class 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 Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 66 | dp = opendir(FLAGS_directory.c_str()); |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 67 | |
| 68 | if (dp != NULL) { |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 69 | while ((ep = readdir(dp)) != nullptr) { |
| 70 | if (ep->d_type == DT_REG) { |
| 71 | examples_.push_back(FLAGS_directory + "/" + ep->d_name); |
| 72 | } |
| 73 | } |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 74 | |
| 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 | |
| 87 | class ExampleIterator |
| 88 | : public ::testing::internal::ParamIteratorInterface<std::string> { |
| 89 | public: |
| 90 | ExampleIterator(const ExampleGenerator& base_, |
| 91 | std::vector<std::string>::const_iterator begin) |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 92 | : base_(base_), begin_(begin), current_(begin) {} |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 93 | |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 94 | virtual const ExampleGenerator* BaseGenerator() const { return &base_; } |
| 95 | |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 96 | virtual void Advance() { current_++; } |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 97 | virtual ExampleIterator* Clone() const { return new ExampleIterator(*this); } |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 98 | virtual const std::string* Current() const { return &*current_; } |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 99 | |
| 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 Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 107 | : base_(other.base_), begin_(other.begin_), current_(other.current_) {} |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 108 | |
| 109 | const ExampleGenerator& base_; |
| 110 | const std::vector<std::string>::const_iterator begin_; |
| 111 | std::vector<std::string>::const_iterator current_; |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | ::testing::internal::ParamIteratorInterface<std::string>* |
| 115 | ExampleGenerator::Begin() const { |
| 116 | Materialize(); |
| 117 | return new ExampleIterator(*this, examples_.begin()); |
| 118 | } |
| 119 | |
| 120 | ::testing::internal::ParamIteratorInterface<std::string>* |
| 121 | ExampleGenerator::End() const { |
| 122 | Materialize(); |
| 123 | return new ExampleIterator(*this, examples_.end()); |
| 124 | } |
| 125 | |
| 126 | INSTANTIATE_TEST_CASE_P( |
| 127 | CorpusExamples, FuzzerCorpusTest, |
| 128 | ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator)); |
| 129 | |
| 130 | int main(int argc, char** argv) { |
| 131 | grpc_test_init(argc, argv); |
Craig Tiller | d75d90f | 2017-10-03 09:58:21 -0700 | [diff] [blame^] | 132 | ::gflags::ParseCommandLineFlags(&argc, &argv, true); |
Craig Tiller | 2c357bc | 2017-09-29 11:10:14 -0700 | [diff] [blame] | 133 | ::testing::InitGoogleTest(&argc, argv); |
| 134 | |
| 135 | return RUN_ALL_TESTS(); |
| 136 | } |