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/alloc.h> 24 #include <grpc/support/log.h> 25 #include <gtest/gtest.h> 26 #include <stdio.h> 27 #include <sys/types.h> 28 29 #include "src/core/lib/gpr/env.h" 30 #include "src/core/lib/iomgr/load_file.h" 31 #include "test/core/util/test_config.h" 32 33 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); 34 extern bool squelch; 35 extern bool leak_check; 36 37 // In some distros, gflags is in the namespace google, and in some others, 38 // in gflags. This hack is enabling us to find both. 39 namespace google {} 40 namespace gflags {} 41 using namespace google; 42 using namespace gflags; 43 44 DEFINE_string(file, "", "Use this file as test data"); 45 DEFINE_string(directory, "", "Use this directory as test data"); 46 47 class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {}; 48 49 TEST_P(FuzzerCorpusTest, RunOneExample) { 50 gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str()); 51 grpc_slice buffer; 52 squelch = false; 53 leak_check = false; 54 GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", 55 grpc_load_file(GetParam().c_str(), 0, &buffer))); 56 LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer), 57 GRPC_SLICE_LENGTH(buffer)); 58 grpc_slice_unref(buffer); 59 } 60 61 class ExampleGenerator 62 : public ::testing::internal::ParamGeneratorInterface<std::string> { 63 public: 64 virtual ::testing::internal::ParamIteratorInterface<std::string>* Begin() 65 const; 66 virtual ::testing::internal::ParamIteratorInterface<std::string>* End() const; 67 68 private: 69 void Materialize() const { 70 if (examples_.empty()) { 71 if (!FLAGS_file.empty()) examples_.push_back(FLAGS_file); 72 if (!FLAGS_directory.empty()) { 73 char* test_srcdir = gpr_getenv("TEST_SRCDIR"); 74 if (test_srcdir != nullptr) { 75 FLAGS_directory = test_srcdir + 76 std::string("/com_github_grpc_grpc/") + 77 FLAGS_directory; 78 } 79 DIR* dp; 80 struct dirent* ep; 81 dp = opendir(FLAGS_directory.c_str()); 82 83 if (dp != nullptr) { 84 while ((ep = readdir(dp)) != nullptr) { 85 if (ep->d_type == DT_REG) { 86 examples_.push_back(FLAGS_directory + "/" + ep->d_name); 87 } 88 } 89 90 (void)closedir(dp); 91 } else { 92 perror("Couldn't open the directory"); 93 abort(); 94 } 95 gpr_free(test_srcdir); 96 } 97 } 98 } 99 100 mutable std::vector<std::string> examples_; 101 }; 102 103 class ExampleIterator 104 : public ::testing::internal::ParamIteratorInterface<std::string> { 105 public: 106 ExampleIterator(const ExampleGenerator& base_, 107 std::vector<std::string>::const_iterator begin) 108 : base_(base_), begin_(begin), current_(begin) {} 109 110 virtual const ExampleGenerator* BaseGenerator() const { return &base_; } 111 112 virtual void Advance() { current_++; } 113 virtual ExampleIterator* Clone() const { return new ExampleIterator(*this); } 114 virtual const std::string* Current() const { return &*current_; } 115 116 virtual bool Equals(const ParamIteratorInterface<std::string>& other) const { 117 return &base_ == other.BaseGenerator() && 118 current_ == dynamic_cast<const ExampleIterator*>(&other)->current_; 119 } 120 121 private: 122 ExampleIterator(const ExampleIterator& other) 123 : base_(other.base_), begin_(other.begin_), current_(other.current_) {} 124 125 const ExampleGenerator& base_; 126 const std::vector<std::string>::const_iterator begin_; 127 std::vector<std::string>::const_iterator current_; 128 }; 129 130 ::testing::internal::ParamIteratorInterface<std::string>* 131 ExampleGenerator::Begin() const { 132 Materialize(); 133 return new ExampleIterator(*this, examples_.begin()); 134 } 135 136 ::testing::internal::ParamIteratorInterface<std::string>* 137 ExampleGenerator::End() const { 138 Materialize(); 139 return new ExampleIterator(*this, examples_.end()); 140 } 141 142 INSTANTIATE_TEST_CASE_P( 143 CorpusExamples, FuzzerCorpusTest, 144 ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator)); 145 146 int main(int argc, char** argv) { 147 grpc_test_init(argc, argv); 148 ParseCommandLineFlags(&argc, &argv, true); 149 ::testing::InitGoogleTest(&argc, argv); 150 151 return RUN_ALL_TESTS(); 152 } 153