mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 22:02:37 +01:00
The logic is using bazel Runfiles API per Jon's suggestion. One of the scenarios used by the fuzzing framework is to place the binary and its runfiles into a folder, resulting in a directory layout like this: [temp dir] fuzzer fuzzer.runfiles/ Tested: bazel test -c opt explorer/fuzzing:explorer_fuzzer bazel build -c opt explorer/fuzzing:explorer_fuzzer + run the binary from various locations
61 lines
2.0 KiB
C++
61 lines
2.0 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "explorer/fuzzing/fuzzer_util.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <google/protobuf/text_format.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
static std::vector<llvm::StringRef>* carbon_files = nullptr;
|
|
|
|
// A workaround for https://github.com/carbon-language/carbon-lang/issues/1208.
|
|
TEST(FuzzerUtilTest, RunFuzzerOnCorpus) {
|
|
int file_count = 0;
|
|
for (const llvm::StringRef f : *carbon_files) {
|
|
llvm::outs() << "Processing " << f << "\n";
|
|
std::ifstream file(f.str(), std::ios::in);
|
|
ASSERT_TRUE(file.is_open());
|
|
std::stringstream contents;
|
|
contents << file.rdbuf();
|
|
Fuzzing::Carbon carbon_proto;
|
|
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(contents.str(),
|
|
&carbon_proto));
|
|
ParseAndExecute(carbon_proto.compilation_unit());
|
|
++file_count;
|
|
}
|
|
EXPECT_GT(file_count, 0);
|
|
}
|
|
|
|
TEST(FuzzerUtilTest, GetRunfilesFile) {
|
|
EXPECT_THAT(*Internal::GetRunfilesFile(
|
|
"carbon/explorer/fuzzing/fuzzer_corpus/empty.textproto"),
|
|
testing::EndsWith("/explorer/fuzzing/fuzzer_corpus/"
|
|
"empty.textproto"));
|
|
EXPECT_THAT(Internal::GetRunfilesFile(
|
|
"explorer/fuzzing/fuzzer_corpus/empty.textproto")
|
|
.error()
|
|
.message(),
|
|
testing::EndsWith("fuzzer_util_test.runfiles/explorer/fuzzing/"
|
|
"fuzzer_corpus/empty.textproto doesn't exist"));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|
|
|
|
auto main(int argc, char** argv) -> int {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
Carbon::Testing::carbon_files =
|
|
new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
|
|
return RUN_ALL_TESTS();
|
|
}
|