2020-06-09 18:24:30 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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 "sandboxed_api/tools/clang_generator/emitter.h"
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2020-06-09 18:24:30 +08:00
|
|
|
#include "gmock/gmock.h"
|
2020-06-10 20:34:35 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "absl/memory/memory.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-06-10 20:34:35 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "sandboxed_api/sandbox2/testing.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/frontend_action_test_util.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/generator.h"
|
2020-06-09 18:24:30 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ::testing::MatchesRegex;
|
2020-09-25 16:13:50 +08:00
|
|
|
using ::testing::SizeIs;
|
2020-06-09 18:24:30 +08:00
|
|
|
using ::testing::StrEq;
|
2020-06-10 20:34:35 +08:00
|
|
|
using ::testing::StrNe;
|
2020-06-09 18:24:30 +08:00
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
class EmitterForTesting : public Emitter {
|
|
|
|
public:
|
|
|
|
using Emitter::functions_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmitterTest : public FrontendActionTest {};
|
|
|
|
|
|
|
|
TEST_F(EmitterTest, BasicFunctionality) {
|
|
|
|
GeneratorOptions options;
|
|
|
|
options.out_file = "input.h";
|
|
|
|
options.set_function_names<std::initializer_list<std::string>>(
|
|
|
|
{"ExposedFunction"});
|
|
|
|
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
RunFrontendAction(R"(extern "C" void ExposedFunction() {})",
|
|
|
|
absl::make_unique<GeneratorAction>(emitter, options));
|
|
|
|
|
|
|
|
EXPECT_THAT(emitter.functions_, SizeIs(1));
|
|
|
|
|
|
|
|
absl::StatusOr<std::string> header = emitter.EmitHeader(options);
|
|
|
|
EXPECT_THAT(header, IsOk());
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:24:30 +08:00
|
|
|
TEST(IncludeGuard, CreatesRandomizedGuardForEmptyFilename) {
|
|
|
|
// Copybara will transform the string. This is intentional.
|
|
|
|
constexpr absl::string_view kGeneratedHeaderPrefix =
|
|
|
|
"SANDBOXED_API_GENERATED_HEADER_";
|
|
|
|
|
|
|
|
const std::string include_guard = GetIncludeGuard("");
|
|
|
|
EXPECT_THAT(include_guard, MatchesRegex(absl::StrCat(kGeneratedHeaderPrefix,
|
|
|
|
R"([0-9A-F]+_)")));
|
2020-06-10 20:34:35 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(GetIncludeGuard(""), StrNe(include_guard));
|
2020-06-09 18:24:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IncludeGuard, BasicFunctionality) {
|
|
|
|
EXPECT_THAT(GetIncludeGuard("boost/graph/compressed_sparse_row_graph.hpp"),
|
|
|
|
StrEq("BOOST_GRAPH_COMPRESSED_SPARSE_ROW_GRAPH_HPP_"));
|
|
|
|
|
|
|
|
// "SAPI_" prefix is there to avoid generating guards starting with "_"
|
|
|
|
EXPECT_THAT(GetIncludeGuard("/usr/include/unistd.h"),
|
|
|
|
StrEq("SAPI_USR_INCLUDE_UNISTD_H_"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IncludeGuard, AvoidReservedIdentifiers) {
|
|
|
|
EXPECT_THAT(GetIncludeGuard("9p.h"), StrEq("SAPI_9P_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("double__under.h"), StrEq("DOUBLE_UNDER_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("_single.h"), StrEq("SAPI_SINGLE_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("__double.h"), StrEq("SAPI_DOUBLE_H_"));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sapi
|