diff options
author | Craig Tiller <ctiller@google.com> | 2017-10-06 10:24:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-06 10:24:57 -0700 |
commit | 1b11c745417dc54ccc31bba4cdfb2adb02c09ab9 (patch) | |
tree | 2e787355aee5e2c3c0d30e16c3b536e1016c93a3 | |
parent | 12f38b8ecc112d46f372f4c20e0d47a365a43a7e (diff) | |
parent | d3568c0a206b6c4b7529176e181f04c6624b1295 (diff) |
Merge pull request #12769 from ctiller/fuzzy
Moving to allow fuzzer test to run entire corpora under bazel
-rw-r--r-- | test/core/slice/BUILD | 14 | ||||
-rw-r--r-- | test/core/util/BUILD | 8 | ||||
-rw-r--r-- | test/core/util/fuzzer_corpus_test.cc | 136 | ||||
-rw-r--r-- | test/core/util/grpc_fuzzer.bzl | 20 |
4 files changed, 164 insertions, 14 deletions
diff --git a/test/core/slice/BUILD b/test/core/slice/BUILD index f86a3a6082..ad2308a4d6 100644 --- a/test/core/slice/BUILD +++ b/test/core/slice/BUILD @@ -21,10 +21,22 @@ licenses(["notice"]) # Apache v2 load("//test/core/util:grpc_fuzzer.bzl", "grpc_fuzzer") grpc_fuzzer( + name = "percent_encode_fuzzer", + srcs = ["percent_encode_fuzzer.c"], + language = "C", + corpus = "percent_encode_corpus", + deps = [ + "//:gpr", + "//:grpc", + "//test/core/util:grpc_test_util", + ], +) + +grpc_fuzzer( name = "percent_decode_fuzzer", srcs = ["percent_decode_fuzzer.c"], language = "C", - corpus = "response_corpus", + corpus = "percent_decode_corpus", deps = [ "//:gpr", "//:grpc", diff --git a/test/core/util/BUILD b/test/core/util/BUILD index 10eefe159a..abb50a0c99 100644 --- a/test/core/util/BUILD +++ b/test/core/util/BUILD @@ -89,12 +89,16 @@ grpc_cc_library( ) grpc_cc_library( - name = "one_corpus_entry_fuzzer", - srcs = ["one_corpus_entry_fuzzer.c"], + name = "fuzzer_corpus_test", + srcs = ["fuzzer_corpus_test.cc"], deps = [ ":gpr_test_util", "//:grpc", ], + external_deps = [ + "gtest", + "gflags", + ], ) sh_library( diff --git a/test/core/util/fuzzer_corpus_test.cc b/test/core/util/fuzzer_corpus_test.cc new file mode 100644 index 0000000000..a5e99a1bac --- /dev/null +++ b/test/core/util/fuzzer_corpus_test.cc @@ -0,0 +1,136 @@ +/* + * + * Copyright 2016 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include <stdbool.h> + +#include <dirent.h> +#include <gflags/gflags.h> +#include <grpc/support/log.h> +#include <gtest/gtest.h> +#include <stdio.h> +#include <sys/types.h> + +#include "src/core/lib/iomgr/load_file.h" +#include "test/core/util/test_config.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); +extern "C" bool squelch; +extern "C" bool leak_check; + +DEFINE_string(file, "", "Use this file as test data"); +DEFINE_string(directory, "", "Use this directory as test data"); + +class FuzzerCorpusTest : public ::testing::TestWithParam<std::string> {}; + +TEST_P(FuzzerCorpusTest, RunOneExample) { + gpr_log(GPR_DEBUG, "Example file: %s", GetParam().c_str()); + grpc_slice buffer; + squelch = false; + leak_check = false; + GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file", + grpc_load_file(GetParam().c_str(), 0, &buffer))); + LLVMFuzzerTestOneInput(GRPC_SLICE_START_PTR(buffer), + GRPC_SLICE_LENGTH(buffer)); + grpc_slice_unref(buffer); +} + +class ExampleGenerator + : public ::testing::internal::ParamGeneratorInterface<std::string> { + public: + virtual ::testing::internal::ParamIteratorInterface<std::string>* Begin() + const; + virtual ::testing::internal::ParamIteratorInterface<std::string>* End() const; + + private: + void Materialize() const { + if (examples_.empty()) { + if (!FLAGS_file.empty()) examples_.push_back(FLAGS_file); + if (!FLAGS_directory.empty()) { + DIR* dp; + struct dirent* ep; + dp = opendir(FLAGS_directory.c_str()); + + if (dp != NULL) { + while ((ep = readdir(dp)) != nullptr) { + if (ep->d_type == DT_REG) { + examples_.push_back(FLAGS_directory + "/" + ep->d_name); + } + } + + (void)closedir(dp); + } else { + perror("Couldn't open the directory"); + abort(); + } + } + } + } + + mutable std::vector<std::string> examples_; +}; + +class ExampleIterator + : public ::testing::internal::ParamIteratorInterface<std::string> { + public: + ExampleIterator(const ExampleGenerator& base_, + std::vector<std::string>::const_iterator begin) + : base_(base_), begin_(begin), current_(begin) {} + + virtual const ExampleGenerator* BaseGenerator() const { return &base_; } + + virtual void Advance() { current_++; } + virtual ExampleIterator* Clone() const { return new ExampleIterator(*this); } + virtual const std::string* Current() const { return &*current_; } + + virtual bool Equals(const ParamIteratorInterface<std::string>& other) const { + return &base_ == other.BaseGenerator() && + current_ == dynamic_cast<const ExampleIterator*>(&other)->current_; + } + + private: + ExampleIterator(const ExampleIterator& other) + : base_(other.base_), begin_(other.begin_), current_(other.current_) {} + + const ExampleGenerator& base_; + const std::vector<std::string>::const_iterator begin_; + std::vector<std::string>::const_iterator current_; +}; + +::testing::internal::ParamIteratorInterface<std::string>* +ExampleGenerator::Begin() const { + Materialize(); + return new ExampleIterator(*this, examples_.begin()); +} + +::testing::internal::ParamIteratorInterface<std::string>* +ExampleGenerator::End() const { + Materialize(); + return new ExampleIterator(*this, examples_.end()); +} + +INSTANTIATE_TEST_CASE_P( + CorpusExamples, FuzzerCorpusTest, + ::testing::internal::ParamGenerator<std::string>(new ExampleGenerator)); + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::gflags::ParseCommandLineFlags(&argc, &argv, true); + ::testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} diff --git a/test/core/util/grpc_fuzzer.bzl b/test/core/util/grpc_fuzzer.bzl index 41f6cdc8ba..55b6f1c1a5 100644 --- a/test/core/util/grpc_fuzzer.bzl +++ b/test/core/util/grpc_fuzzer.bzl @@ -12,19 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("//bazel:grpc_build_system.bzl", "grpc_cc_binary") +load("//bazel:grpc_build_system.bzl", "grpc_cc_test") def grpc_fuzzer(name, corpus, srcs = [], deps = [], **kwargs): - grpc_cc_binary( - name = '%s/one_entry.bin' % name, + grpc_cc_test( + name = name, srcs = srcs, - deps = deps + ["//test/core/util:one_corpus_entry_fuzzer"], + deps = deps + ["//test/core/util:fuzzer_corpus_test"], + data = [corpus], + args = ['--directory', '$(location %s)' % corpus], + external_deps = [ + 'gtest', + ], **kwargs ) - for entry in native.glob(['%s/*' % corpus]): - native.sh_test( - name = '%s/one_entry/%s' % (name, entry), - data = [':%s/one_entry.bin' % name, entry], - srcs = ['//test/core/util:fuzzer_one_entry_runner'], - args = ['$(location :%s/one_entry.bin)' % name, '$(location %s)' % entry] - ) |