2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/temp_file.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
|
|
|
#include "sandboxed_api/util/fileops.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace sapi {
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace {
|
|
|
|
|
2020-04-02 22:42:17 +08:00
|
|
|
using ::testing::Eq;
|
|
|
|
using ::testing::IsTrue;
|
|
|
|
using ::testing::Ne;
|
|
|
|
using ::testing::StartsWith;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(TempFileTest, CreateTempDirTest) {
|
|
|
|
const std::string prefix = GetTestTempPath("MakeTempDirTest_");
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string path, CreateTempDir(prefix));
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(path, StartsWith(prefix));
|
|
|
|
EXPECT_THAT(file_util::fileops::Exists(path, false), IsTrue());
|
2020-04-02 22:42:17 +08:00
|
|
|
EXPECT_THAT(CreateTempDir("non_existing_dir/prefix"),
|
|
|
|
StatusIs(absl::StatusCode::kUnknown));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TempFileTest, MakeTempFileTest) {
|
|
|
|
const std::string prefix = GetTestTempPath("MakeTempDirTest_");
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
auto result_or = CreateNamedTempFile(prefix);
|
|
|
|
ASSERT_THAT(result_or.status(), IsOk());
|
2020-04-02 22:42:17 +08:00
|
|
|
auto [path, fd] = std::move(result_or).value();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(path, StartsWith(prefix));
|
|
|
|
EXPECT_THAT(file_util::fileops::Exists(path, false), IsTrue());
|
|
|
|
EXPECT_THAT(fcntl(fd, F_GETFD), Ne(-1));
|
|
|
|
EXPECT_THAT(close(fd), Eq(0));
|
2020-04-02 22:42:17 +08:00
|
|
|
EXPECT_THAT(CreateNamedTempFile("non_existing_dir/prefix"),
|
|
|
|
StatusIs(absl::StatusCode::kUnknown));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2021-01-14 01:25:25 +08:00
|
|
|
} // namespace sapi
|