2020-10-01 08:15:33 +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.
|
2020-09-30 06:50:43 +08:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
#include "sapi_minitar.h" // NOLINT(build/include)
|
2020-09-30 06:50:43 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-10-03 00:52:13 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/path.h"
|
2020-09-30 03:27:40 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
2020-09-30 06:50:43 +08:00
|
|
|
using ::sandbox2::file::JoinPath;
|
2020-10-07 18:07:13 +08:00
|
|
|
using ::sapi::IsOk;
|
2020-09-30 03:27:40 +08:00
|
|
|
using ::testing::Eq;
|
2020-09-30 06:50:43 +08:00
|
|
|
using ::testing::IsTrue;
|
2020-10-01 08:15:33 +08:00
|
|
|
using ::testing::StrEq;
|
2020-09-30 03:27:40 +08:00
|
|
|
|
|
|
|
using ::sandbox2::file_util::fileops::Exists;
|
2020-10-01 08:15:33 +08:00
|
|
|
using ::sandbox2::util::VecStringToCharPtrArr;
|
2020-09-30 03:27:40 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// We will use a fixture class for testing which allows us to override the
|
|
|
|
// SetUp and TearDown functions. Also, data that needs to be initialized
|
|
|
|
// or destroyed only once (the test files and directories) will be handled
|
|
|
|
// in the SetUpTestSuite and TearDownTestSuite functions which are executed
|
|
|
|
// only once.
|
|
|
|
// All of the testing data will be placed in a temporary directory and each
|
|
|
|
// test will have it's own temporary directory. At the end of each test
|
|
|
|
// and all of the tests, the temporary data is deleted.
|
2020-09-30 03:27:40 +08:00
|
|
|
class MiniTarTest : public ::testing::Test {
|
2020-09-30 06:50:43 +08:00
|
|
|
protected:
|
2020-10-01 08:15:33 +08:00
|
|
|
// Before running the tests, we create a temporary directory which will
|
|
|
|
// store generated files and directories used for testing.
|
|
|
|
// The directory will look as follows:
|
|
|
|
// -file1
|
|
|
|
// -dir1 - file2
|
|
|
|
// - dir2 - file3
|
2020-09-30 06:50:43 +08:00
|
|
|
static void SetUpTestSuite() {
|
2020-10-07 18:07:13 +08:00
|
|
|
absl::StatusOr<std::string> tmp_status = CreateTempDirAtCWD();
|
|
|
|
ASSERT_THAT(tmp_status, IsOk());
|
2020-10-08 17:06:50 +08:00
|
|
|
data_dir_ = new std::string(std::move(tmp_status).value());
|
2020-10-07 18:07:13 +08:00
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
init_wd_ = new std::string(sandbox2::file_util::fileops::GetCWD());
|
2020-09-30 06:50:43 +08:00
|
|
|
ASSERT_THAT(Exists(data_dir_, false), IsTrue())
|
|
|
|
<< "Test data directory was not created";
|
2020-10-01 08:15:33 +08:00
|
|
|
ASSERT_THAT(chdir(data_dir_.data()), Eq(0))
|
2020-09-30 06:50:43 +08:00
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CreateAndWriteToFile(kFile1);
|
|
|
|
ASSERT_THAT(mkdir(kDir1.data(), 0755), Eq(0)) << "Could not create dir1";
|
|
|
|
CreateAndWriteToFile(kFile2);
|
|
|
|
ASSERT_THAT(mkdir(kDir2.data(), 0755), Eq(0)) << "Could not create dir2";
|
|
|
|
CreateAndWriteToFile(kFile3);
|
2020-09-30 06:50:43 +08:00
|
|
|
|
|
|
|
test_count_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TearDownTestSuite() {
|
|
|
|
// The tests have the data directory as their working directory at the end
|
|
|
|
// so we move to the initial working directory in order to not delete the
|
|
|
|
// directory that we are inside of.
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(chdir(init_wd_->data()), Eq(0))
|
2020-09-30 06:50:43 +08:00
|
|
|
<< "Could not chdir into initial working directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(*data_dir_),
|
2020-09-30 06:50:43 +08:00
|
|
|
IsTrue)
|
|
|
|
<< "Error during test data deletion";
|
2020-10-08 17:06:50 +08:00
|
|
|
delete init_wd_;
|
|
|
|
delete data_dir_;
|
2020-09-30 06:50:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override {
|
2020-10-01 08:15:33 +08:00
|
|
|
// We use a unique id based on test count to make sure that files created
|
|
|
|
// during tests do not overlap.
|
2020-09-30 06:50:43 +08:00
|
|
|
id_ = "test" + std::to_string(test_count_);
|
2020-10-07 18:07:13 +08:00
|
|
|
|
|
|
|
absl::StatusOr<std::string> tmp_status = CreateTempDirAtCWD();
|
|
|
|
ASSERT_THAT(tmp_status, IsOk());
|
|
|
|
tmp_dir_ = tmp_status.value();
|
|
|
|
|
2020-09-30 06:50:43 +08:00
|
|
|
ASSERT_THAT(Exists(tmp_dir_, false), IsTrue)
|
|
|
|
<< "Could not create test specific temporary directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(chdir(data_dir_->data()), Eq(0))
|
2020-09-30 06:50:43 +08:00
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
2020-10-01 08:15:33 +08:00
|
|
|
// Move to another directory before deleting the temporary folder.
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(chdir(data_dir_->data()), Eq(0))
|
2020-09-30 06:50:43 +08:00
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
|
|
|
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(tmp_dir_),
|
|
|
|
IsTrue)
|
|
|
|
<< "Error during test temporary directory deletion";
|
|
|
|
++test_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the file specified and writes the same filename.
|
2020-10-01 08:15:33 +08:00
|
|
|
// This is done in order to not have completely empty files for the
|
|
|
|
// archiving step.
|
2020-09-30 06:50:43 +08:00
|
|
|
static void CreateAndWriteToFile(absl::string_view file) {
|
|
|
|
std::ofstream fin(file.data());
|
|
|
|
ASSERT_THAT(fin.is_open(), IsTrue()) << "Could not create" << file;
|
|
|
|
fin << file;
|
|
|
|
fin.close();
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// Checks if the files exists and if the contents are correct.
|
|
|
|
// In these tests, each file contains the relative path from the test
|
|
|
|
// directory.
|
|
|
|
// Example: dir1/dir2/file3 will contain dir1/dir2/file3.
|
|
|
|
// What the files contain does not matter as much, the only important thing
|
|
|
|
// is that they are not empty so we can check if the contents are preserved.
|
|
|
|
static void CheckFile(const std::string& file) {
|
|
|
|
ASSERT_THAT(Exists(file, false), IsTrue()) << "Could not find " << file;
|
|
|
|
std::ifstream fin(file);
|
|
|
|
ASSERT_THAT(fin.is_open(), IsTrue()) << "Error when opening " << file;
|
|
|
|
|
|
|
|
std::string file_contents((std::istreambuf_iterator<char>(fin)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
EXPECT_THAT(file_contents, StrEq(file))
|
|
|
|
<< "Contents of " << file << " are different after extraction";
|
|
|
|
fin.close();
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:50:43 +08:00
|
|
|
static int test_count_;
|
2020-10-08 17:06:50 +08:00
|
|
|
static std::string* data_dir_;
|
|
|
|
static std::string* init_wd_;
|
2020-10-07 20:52:55 +08:00
|
|
|
std::string tmp_dir_;
|
|
|
|
std::string id_;
|
|
|
|
|
|
|
|
static constexpr absl::string_view kFile1 = "file1";
|
|
|
|
static constexpr absl::string_view kFile2 = "dir1/file2";
|
|
|
|
static constexpr absl::string_view kFile3 = "dir1/dir2/file3";
|
|
|
|
static constexpr absl::string_view kDir1 = "dir1";
|
|
|
|
static constexpr absl::string_view kDir2 = "dir1/dir2";
|
2020-09-30 03:27:40 +08:00
|
|
|
};
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
int MiniTarTest::test_count_;
|
2020-10-08 17:06:50 +08:00
|
|
|
std::string* MiniTarTest::data_dir_;
|
|
|
|
std::string* MiniTarTest::init_wd_;
|
2020-09-30 03:27:40 +08:00
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
// The tests have the following pattern:
|
|
|
|
// 1) From inside the test data directory, call the create function with
|
|
|
|
// different arguments.
|
|
|
|
// 2) Move to the test specific temporary directory created during the
|
|
|
|
// set up phase.
|
|
|
|
// 3) Extract the archive created at step 1.
|
|
|
|
// 4) Check that the files in the archive have been extracted correctly
|
|
|
|
// by first checking if they exist and then checking if the content is the
|
|
|
|
// same as in the original file.
|
|
|
|
TEST_F(MiniTarTest, TestFileSimple) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data()};
|
2020-09-30 03:27:40 +08:00
|
|
|
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
2020-09-30 06:50:43 +08:00
|
|
|
<< "Could not chdir into test data directory";
|
2020-09-30 03:27:40 +08:00
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
2020-09-30 03:27:40 +08:00
|
|
|
}
|
|
|
|
|
2020-10-01 08:15:33 +08:00
|
|
|
TEST_F(MiniTarTest, TestMultipleFiles) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data(), kFile2.data(), kFile3.data()};
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
ASSERT_THAT(Exists(id_.data(), false), IsTrue())
|
|
|
|
<< "Archive file was not created";
|
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestDirectorySimple) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kDir2.data()};
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestDirectoryNested) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kDir1.data()};
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-09-30 03:27:40 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestComplex) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
|
|
|
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestCompress) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
|
2020-10-01 08:15:33 +08:00
|
|
|
int compress = 'Z';
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestGZIP) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
|
2020-10-01 08:15:33 +08:00
|
|
|
int compress = 'z';
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MiniTarTest, TestBZIP2) {
|
2020-10-07 20:52:55 +08:00
|
|
|
std::vector<std::string> v = {kFile1.data(), kDir1.data()};
|
2020-10-01 08:15:33 +08:00
|
|
|
int compress = 'j';
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
CreateArchive(id_.data(), compress, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-01 08:15:33 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-01 08:15:33 +08:00
|
|
|
}
|
2020-09-30 03:27:40 +08:00
|
|
|
|
2020-10-03 00:52:13 +08:00
|
|
|
TEST_F(MiniTarTest, TestPaths) {
|
2020-10-07 20:52:55 +08:00
|
|
|
// These should be equivalent to kFile1 and kDir1 after cleaning.
|
|
|
|
std::vector<std::string> v = {JoinPath("a/b/../../c/../", kFile1).data(),
|
|
|
|
JoinPath("d/../e/././///../", kDir1).data()};
|
2020-10-07 18:07:13 +08:00
|
|
|
ASSERT_THAT(CreateArchive(id_.data(), 0, VecStringToCharPtrArr(v), false),
|
|
|
|
IsOk());
|
2020-10-03 00:52:13 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(chdir(tmp_dir_.data()), Eq(0))
|
|
|
|
<< "Could not chdir into test data directory";
|
2020-10-08 17:06:50 +08:00
|
|
|
ASSERT_THAT(ExtractArchive(JoinPath(*data_dir_, id_).data(), 1, 0, false),
|
2020-10-07 18:07:13 +08:00
|
|
|
IsOk());
|
2020-10-03 00:52:13 +08:00
|
|
|
|
2020-10-07 20:52:55 +08:00
|
|
|
CheckFile(std::string(kFile1));
|
|
|
|
CheckFile(std::string(kFile2));
|
|
|
|
CheckFile(std::string(kFile3));
|
2020-10-03 00:52:13 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 03:27:40 +08:00
|
|
|
} // namespace
|