Added first basic test

This commit is contained in:
Andrei Medar 2020-09-29 22:50:43 +00:00
parent 267686afb6
commit 7e6872ee26
3 changed files with 93 additions and 42 deletions

View File

@ -1,12 +1,13 @@
#include "sapi_minitar.h"
void create(const char* initial_filename, int compress, const char** argv,
int verbose /* = 1 */) {
bool verbose /* = true */) {
// We split the filename path into dirname and filename. To the filename we
// prepend "/output/"" so that it will work with the security policy.
std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename));
auto [archive_path, filename_tmp] = sandbox2::file::SplitPath(abs_path);
auto [archive_path, filename_tmp] =
std::move(sandbox2::file::SplitPath(abs_path));
std::string filename("/output/");
filename.append(filename_tmp);
@ -31,6 +32,8 @@ void create(const char* initial_filename, int compress, const char** argv,
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
LibarchiveApi api(&sandbox);
std::cout << "AJUNGE AICI" << std::endl;
absl::StatusOr<archive*> ret = api.archive_write_new();
CHECK(ret.ok()) << "write_new call failed";
CHECK(ret.value() != NULL) << "Failed to create write archive";
@ -39,6 +42,8 @@ void create(const char* initial_filename, int compress, const char** argv,
// to the client process.
sapi::v::RemotePtr a(ret.value());
std::cout << "AJUNGE AICI" << std::endl;
absl::StatusOr<int> ret2;
switch (compress) {
@ -252,7 +257,8 @@ void create(const char* initial_filename, int compress, const char** argv,
}
void extract(const char* filename, int do_extract, int flags,
int verbose /* = 1 */) {
bool verbose /* = true */) {
std::cout << "flags = " << flags << std::endl;
std::string tmp_dir;
if (do_extract) {
tmp_dir = CreateTempDirAtCWD();

View File

@ -25,9 +25,10 @@
#include "sandboxed_api/var_array.h"
void create(const char* filename, int compress, const char** argv,
int verbose = 1);
bool verbose = true);
void extract(const char* filename, int do_extract, int flags, int verbose = 1);
void extract(const char* filename, int do_extract, int flags,
bool verbose = true);
int copy_data(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw,
LibarchiveApi& api, SapiLibarchiveSandboxExtract& sandbox);

View File

@ -1,64 +1,108 @@
// #include <gmock/gmock-more-matchers.h>
#include <gmock/gmock-more-matchers.h>
#include "sapi_minitar.h"
#include "gtest/gtest.h"
#include <unistd.h>
#include <fstream>
#include <string>
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h"
#include "sandboxed_api/util/status_matchers.h"
#include "sapi_minitar.h"
// #include "testing/base/public/gunit.h"
// #include "testing/base/public/gunit.h"
using ::testing::IsTrue;
using ::sandbox2::file::JoinPath;
using ::testing::Eq;
using ::testing::IsTrue;
using ::sandbox2::file_util::fileops::Exists;
namespace {
class MiniTarTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
std::cout << "SETUP INITIAL" << std::endl;
data_dir_ = CreateTempDirAtCWD();
ASSERT_THAT(Exists(data_dir_, false), IsTrue()) << "Test data directory was not created";
std::cout << "tmpdir = " << data_dir_ << std::endl;
cnt = 0;
}
protected:
static void SetUpTestSuite() {
data_dir_ = CreateTempDirAtCWD();
init_wd_ = sandbox2::file_util::fileops::GetCWD();
ASSERT_THAT(Exists(data_dir_, false), IsTrue())
<< "Test data directory was not created";
ASSERT_THAT(chdir(data_dir_.c_str()), Eq(0))
<< "Could not chdir into test data directory";
static void TearDownTestSuite() {
sandbox2::file_util::fileops::DeleteRecursively(data_dir_);
std::cout << "TEARDOWN END" << std::endl;
std::cout << "cnt = " << cnt << std::endl;
}
CreateAndWriteToFile("file1");
test_count_ = 0;
}
void SetUp() override {
std::cout << "setup every test" << std::endl;
++cnt;
}
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.
ASSERT_THAT(chdir(init_wd_.c_str()), Eq(0))
<< "Could not chdir into initial working directory";
EXPECT_THAT(sandbox2::file_util::fileops::DeleteRecursively(data_dir_),
IsTrue)
<< "Error during test data deletion";
}
void TearDown() override {
std::cout << "teardown every test" << std::endl;
++cnt;
}
static int cnt;
static std::string data_dir_;
void SetUp() override {
id_ = "test" + std::to_string(test_count_);
tmp_dir_ = CreateTempDirAtCWD();
ASSERT_THAT(Exists(tmp_dir_, false), IsTrue)
<< "Could not create test specific temporary directory";
ASSERT_THAT(chdir(data_dir_.c_str()), Eq(0))
<< "Could not chdir into test data directory";
}
void TearDown() override {
// Move to another directory before deleting the temporary folder
ASSERT_THAT(chdir(data_dir_.c_str()), Eq(0))
<< "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.
// This is done in order to not have completely empty files for the archiving
// step.
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();
}
static int test_count_;
static std::string data_dir_;
static std::string init_wd_;
std::string tmp_dir_, id_;
};
int MiniTarTest::cnt;
int MiniTarTest::test_count_ = 0;
std::string MiniTarTest::data_dir_;
std::string MiniTarTest::init_wd_;
TEST_F(MiniTarTest, Test1) {
ASSERT_THAT(true, IsTrue()) << "TEST";
// ASSERT_THAT(true, IsTrue()) << "TEST";
const char* args[] = {"file1", nullptr};
create(id_.c_str(), 0, args, false);
ASSERT_THAT(chdir(tmp_dir_.c_str()), Eq(0))
<< "Could not chdir into test data directory";
extract(JoinPath(data_dir_, id_).c_str(), 1, 0, false);
EXPECT_THAT(Exists("file1", false), IsTrue()) << "Could not find file1";
}
TEST_F(MiniTarTest, Test2) { ASSERT_THAT(true, IsTrue()) << "TEST"; }
TEST_F(MiniTarTest, Test2) {
ASSERT_THAT(true, IsTrue()) << "TEST";
}
TEST(TESTEX1, TESTEX2) {
ASSERT_THAT(true, IsTrue()) << "TEST";
}
TEST(TESTEX1, TESTEX2) { ASSERT_THAT(true, IsTrue()) << "TEST"; }
} // namespace