mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Added first basic test
This commit is contained in:
parent
267686afb6
commit
7e6872ee26
@ -1,12 +1,13 @@
|
|||||||
#include "sapi_minitar.h"
|
#include "sapi_minitar.h"
|
||||||
|
|
||||||
void create(const char* initial_filename, int compress, const char** argv,
|
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
|
// We split the filename path into dirname and filename. To the filename we
|
||||||
// prepend "/output/"" so that it will work with the security policy.
|
// prepend "/output/"" so that it will work with the security policy.
|
||||||
|
|
||||||
std::string abs_path = MakeAbsolutePathAtCWD(std::string(initial_filename));
|
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/");
|
std::string filename("/output/");
|
||||||
filename.append(filename_tmp);
|
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";
|
CHECK(sandbox.Init().ok()) << "Error during sandbox initialization";
|
||||||
LibarchiveApi api(&sandbox);
|
LibarchiveApi api(&sandbox);
|
||||||
|
|
||||||
|
std::cout << "AJUNGE AICI" << std::endl;
|
||||||
|
|
||||||
absl::StatusOr<archive*> ret = api.archive_write_new();
|
absl::StatusOr<archive*> ret = api.archive_write_new();
|
||||||
CHECK(ret.ok()) << "write_new call failed";
|
CHECK(ret.ok()) << "write_new call failed";
|
||||||
CHECK(ret.value() != NULL) << "Failed to create write archive";
|
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.
|
// to the client process.
|
||||||
sapi::v::RemotePtr a(ret.value());
|
sapi::v::RemotePtr a(ret.value());
|
||||||
|
|
||||||
|
std::cout << "AJUNGE AICI" << std::endl;
|
||||||
|
|
||||||
absl::StatusOr<int> ret2;
|
absl::StatusOr<int> ret2;
|
||||||
|
|
||||||
switch (compress) {
|
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,
|
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;
|
std::string tmp_dir;
|
||||||
if (do_extract) {
|
if (do_extract) {
|
||||||
tmp_dir = CreateTempDirAtCWD();
|
tmp_dir = CreateTempDirAtCWD();
|
||||||
|
@ -25,9 +25,10 @@
|
|||||||
#include "sandboxed_api/var_array.h"
|
#include "sandboxed_api/var_array.h"
|
||||||
|
|
||||||
void create(const char* filename, int compress, const char** argv,
|
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,
|
int copy_data(sapi::v::RemotePtr* ar, sapi::v::RemotePtr* aw,
|
||||||
LibarchiveApi& api, SapiLibarchiveSandboxExtract& sandbox);
|
LibarchiveApi& api, SapiLibarchiveSandboxExtract& sandbox);
|
||||||
|
@ -1,64 +1,108 @@
|
|||||||
|
// #include <gmock/gmock-more-matchers.h>
|
||||||
#include <gmock/gmock-more-matchers.h>
|
#include <gmock/gmock-more-matchers.h>
|
||||||
#include "sapi_minitar.h"
|
#include <unistd.h>
|
||||||
#include "gtest/gtest.h"
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "absl/strings/string_view.h"
|
||||||
#include "gmock/gmock.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 "sandboxed_api/util/status_matchers.h"
|
||||||
|
#include "sapi_minitar.h"
|
||||||
// #include "testing/base/public/gunit.h"
|
// #include "testing/base/public/gunit.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::Eq;
|
||||||
|
using ::testing::IsTrue;
|
||||||
|
|
||||||
using ::sandbox2::file_util::fileops::Exists;
|
using ::sandbox2::file_util::fileops::Exists;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class MiniTarTest : public ::testing::Test {
|
class MiniTarTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
static void SetUpTestSuite() {
|
static void SetUpTestSuite() {
|
||||||
std::cout << "SETUP INITIAL" << std::endl;
|
data_dir_ = CreateTempDirAtCWD();
|
||||||
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(Exists(data_dir_, false), IsTrue())
|
||||||
std::cout << "tmpdir = " << data_dir_ << std::endl;
|
<< "Test data directory was not created";
|
||||||
cnt = 0;
|
ASSERT_THAT(chdir(data_dir_.c_str()), Eq(0))
|
||||||
}
|
<< "Could not chdir into test data directory";
|
||||||
|
|
||||||
static void TearDownTestSuite() {
|
CreateAndWriteToFile("file1");
|
||||||
sandbox2::file_util::fileops::DeleteRecursively(data_dir_);
|
|
||||||
std::cout << "TEARDOWN END" << std::endl;
|
|
||||||
std::cout << "cnt = " << cnt << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
test_count_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void SetUp() override {
|
static void TearDownTestSuite() {
|
||||||
std::cout << "setup every test" << std::endl;
|
// The tests have the data directory as their working directory at the end
|
||||||
++cnt;
|
// 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 {
|
void SetUp() override {
|
||||||
std::cout << "teardown every test" << std::endl;
|
id_ = "test" + std::to_string(test_count_);
|
||||||
++cnt;
|
tmp_dir_ = CreateTempDirAtCWD();
|
||||||
}
|
ASSERT_THAT(Exists(tmp_dir_, false), IsTrue)
|
||||||
static int cnt;
|
<< "Could not create test specific temporary directory";
|
||||||
static std::string data_dir_;
|
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::data_dir_;
|
||||||
|
std::string MiniTarTest::init_wd_;
|
||||||
|
|
||||||
TEST_F(MiniTarTest, Test1) {
|
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) {
|
TEST(TESTEX1, TESTEX2) { ASSERT_THAT(true, IsTrue()) << "TEST"; }
|
||||||
ASSERT_THAT(true, IsTrue()) << "TEST";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST(TESTEX1, TESTEX2) {
|
|
||||||
ASSERT_THAT(true, IsTrue()) << "TEST";
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user