mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-26 17:37:47 +01:00
init
This commit is contained in:
113
tests/repo/ChunkTest.cpp
Normal file
113
tests/repo/ChunkTest.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "ChunkTest.h"
|
||||
#include "../../src/repo/Serialize.h"
|
||||
#include "../utils/Cleaner.h"
|
||||
|
||||
std::unique_ptr<TestGroup> ChunkTest::operator()() {
|
||||
auto tg = std::make_unique<TestGroup>("Chunk tests");
|
||||
|
||||
tg->addTest(std::make_unique<Test>("De/serialize test", std::function<bool()>([]() {
|
||||
std::vector<char> s1, s2;
|
||||
{
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1(666, {0}, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2(777, {1}, data2);
|
||||
|
||||
s1 = Serialize::serialize(o1);
|
||||
s2 = Serialize::serialize(o2);
|
||||
}
|
||||
{
|
||||
auto s1b = s1.cbegin();
|
||||
auto s2b = s2.cbegin();
|
||||
Chunk o1(s1b, s1.cend()), o2(s2b, s2.cend());
|
||||
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1e(666, {0}, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2e(777, {1}, data2);
|
||||
|
||||
HelpfulAssert<Object::idType>()(o1.id, o1e.id);
|
||||
HelpfulAssert<Object::idType>()(o2.id, o2e.id);
|
||||
HelpfulAssert<int>()((int) o1.type, (int) o1e.type);
|
||||
HelpfulAssert<int>()((int) o2.type, (int) o2e.type);
|
||||
|
||||
HelpfulAssert<size_t>()(o1.data.size(), o1e.data.size());
|
||||
HelpfulAssert<size_t>()(o2.data.size(), o2e.data.size());
|
||||
for (int i = 0; i < o1.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o1.data[i], o1e.data[i]);
|
||||
}
|
||||
for (int i = 0; i < o2.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o2.data[i], o2e.data[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < o1.md5.size(); i++) {
|
||||
HelpfulAssert<char>()(o1.md5[i], o1e.md5[i]);
|
||||
}
|
||||
for (int i = 0; i < o2.md5.size(); i++) {
|
||||
HelpfulAssert<char>()(o2.md5[i], o2e.md5[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
})));
|
||||
|
||||
tg->addTest(std::make_unique<Test>("Garbage throw test", std::function<bool()>([]() {
|
||||
std::vector<char> e{'a', 'b'};
|
||||
auto eb = e.cbegin();
|
||||
try {
|
||||
Chunk o1(eb, e.cend());
|
||||
} catch (...) {
|
||||
return true;
|
||||
}
|
||||
throw Exception("Object constructed with garbage data!");
|
||||
})));
|
||||
tg->addTest(std::make_unique<Test>("Garbage throw test2", std::function<bool()>([]() {
|
||||
std::vector<char> e{'s', 'e', 'm', 'b', 'a', 'a'};
|
||||
auto eb = e.cbegin();
|
||||
try {
|
||||
Chunk o1(eb, e.cend());
|
||||
} catch (...) {
|
||||
return true;
|
||||
}
|
||||
throw Exception("Object constructed with garbage data!");
|
||||
})));
|
||||
tg->addTest(std::make_unique<Test>("Garbage throw test3", std::function<bool()>([]() {
|
||||
std::vector<char> s1, s2;
|
||||
{
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1(666, {0}, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2(777, {1}, data2);
|
||||
|
||||
s1 = Serialize::serialize(o1);
|
||||
s2 = Serialize::serialize(o2);
|
||||
}
|
||||
{
|
||||
s1.resize(s1.size() - 1);
|
||||
s2.resize(s1.size() - 2);
|
||||
bool fail = false;
|
||||
|
||||
try {
|
||||
Chunk o1 = Serialize::deserialize<Chunk>(s1);
|
||||
fail = true;
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
try {
|
||||
Chunk o2 = Serialize::deserialize<Chunk>(s2);
|
||||
fail = true;
|
||||
} catch (...) {
|
||||
}
|
||||
|
||||
if (fail)
|
||||
throw Exception("Object constructed with garbage data!");
|
||||
}
|
||||
return true;
|
||||
})));
|
||||
|
||||
return tg;
|
||||
}
|
||||
21
tests/repo/ChunkTest.h
Normal file
21
tests/repo/ChunkTest.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_CHUNKTEST_H
|
||||
#define SEMBACKUP_CHUNKTEST_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "../../src/repo/objects/Chunk.h"
|
||||
#include "../utils/TestGroupGenerator.h"
|
||||
|
||||
class ChunkTest : public TestGroupGenerator {
|
||||
public:
|
||||
std::unique_ptr<TestGroup> operator()() override;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_CHUNKTEST_H
|
||||
301
tests/repo/FileRepositoryTest.cpp
Normal file
301
tests/repo/FileRepositoryTest.cpp
Normal file
@@ -0,0 +1,301 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "FileRepositoryTest.h"
|
||||
|
||||
std::unique_ptr<TestGroup> FileRepositoryTest::operator()() {
|
||||
auto tg = std::make_unique<TestGroup>("FileRepository tests");
|
||||
|
||||
tg->addTest(std::make_unique<Test>("De/serialize test", std::function<bool()>([]() {
|
||||
Cleaner c({"testrepo"});
|
||||
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo");
|
||||
FileRepository repo(conf);
|
||||
repo.init();
|
||||
std::vector<char>
|
||||
data1{'a', 'b', 'c', 'e'};
|
||||
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
o2k[1] = 2;
|
||||
|
||||
Chunk o1(666, o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', static_cast<char>(255)};
|
||||
Chunk o2(777, o2k, data2);
|
||||
|
||||
repo.putObject(o1);
|
||||
repo.putObject(o2);
|
||||
|
||||
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 666);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 777);
|
||||
}
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo");
|
||||
FileRepository repo(conf);
|
||||
repo.open();
|
||||
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
o2k[1] = 2;
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 666);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 777);
|
||||
|
||||
auto o1o = repo.getObject(666);
|
||||
auto o2o = repo.getObject(777);
|
||||
|
||||
auto o1ob = o1o.cbegin();
|
||||
auto o2ob = o2o.cbegin();
|
||||
|
||||
Chunk o1(o1ob, o1o.cend()), o2(o2ob, o2o.cend());
|
||||
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1e(666, o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', static_cast<char>(255)};
|
||||
Chunk o2e(777, o2k, data2);
|
||||
|
||||
HelpfulAssert<Object::idType>()(o1.id, o1e.id);
|
||||
HelpfulAssert<Object::idType>()(o2.id, o2e.id);
|
||||
HelpfulAssert<int>()((int) o1.type, (int) o1e.type);
|
||||
HelpfulAssert<int>()((int) o2.type, (int) o2e.type);
|
||||
auto o1d = o1.data;
|
||||
auto o1ed = o1e.data;
|
||||
auto o2d = o2.data;
|
||||
auto o2ed = o2e.data;
|
||||
HelpfulAssert<size_t>()(o1.data.size(), o1e.data.size());
|
||||
HelpfulAssert<size_t>()(o2.data.size(), o2e.data.size());
|
||||
for (int i = 0; i < o1.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o1.data[i], o1e.data[i]);
|
||||
}
|
||||
for (int i = 0; i < o2.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o2.data[i], o2e.data[i]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
})));
|
||||
|
||||
|
||||
tg->addTest(std::make_unique<Test>("De/serialize with sync/read filter", std::function<bool()>([]() {
|
||||
Cleaner c({"testrepo"});
|
||||
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo")
|
||||
.add("compression", "shiftC")
|
||||
.add("compression-level", "1")
|
||||
.add("encryption", "shiftE")
|
||||
.add("password", "\a")
|
||||
.add("salt", "a");
|
||||
|
||||
FileRepository repo(conf);
|
||||
repo.init();
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
o2k[1] = 2;
|
||||
std::vector<char>
|
||||
data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1(666, o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2(777, o2k, data2);
|
||||
|
||||
repo.putObject(o1);
|
||||
repo.putObject(o2);
|
||||
|
||||
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 666);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 777);
|
||||
}
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo")
|
||||
.add("compression", "shiftC")
|
||||
.add("compression-level", "1")
|
||||
.add("encryption", "shiftE")
|
||||
.add("password", "\b")
|
||||
.add("salt", "a");
|
||||
|
||||
FileRepository repo(conf);
|
||||
|
||||
bool err = false;
|
||||
|
||||
try {
|
||||
repo.open();
|
||||
err = true;
|
||||
} catch (...) {}
|
||||
|
||||
|
||||
try {
|
||||
auto o1o = repo.getObject(666);
|
||||
auto o1ob = o1o.cbegin();
|
||||
|
||||
Chunk o1(o1ob, o1o.cend());
|
||||
err = true;
|
||||
} catch (...) {}
|
||||
|
||||
try {
|
||||
auto o2o = repo.getObject(777);
|
||||
auto o2ob = o2o.cbegin();
|
||||
|
||||
Chunk o2(o2ob, o2o.cend());
|
||||
err = true;
|
||||
} catch (...) {}
|
||||
|
||||
if (err)
|
||||
throw Exception("Object constructed with garbage data!");
|
||||
}
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo")
|
||||
.add("compression", "shiftC")
|
||||
.add("compression-level", "1")
|
||||
.add("encryption", "shiftE")
|
||||
.add("password", "\a")
|
||||
.add("salt", "a");
|
||||
|
||||
FileRepository repo(conf);
|
||||
repo.open();
|
||||
|
||||
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
o2k[1] = 2;
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 666);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 777);
|
||||
|
||||
auto o1o = repo.getObject(666);
|
||||
auto o2o = repo.getObject(777);
|
||||
|
||||
auto o1ob = o1o.cbegin();
|
||||
auto o2ob = o2o.cbegin();
|
||||
|
||||
Chunk o1(o1ob, o1o.cend()), o2(o2ob, o2o.cend());
|
||||
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1e(666, o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2e(777, o2k, data2);
|
||||
|
||||
HelpfulAssert<Object::idType>()(o1.id, o1e.id);
|
||||
HelpfulAssert<Object::idType>()(o2.id, o2e.id);
|
||||
HelpfulAssert<int>()((int) o1.type, (int) o1e.type);
|
||||
HelpfulAssert<int>()((int) o2.type, (int) o2e.type);
|
||||
auto o1d = o1.data;
|
||||
auto o1ed = o1e.data;
|
||||
auto o2d = o2.data;
|
||||
auto o2ed = o2e.data;
|
||||
HelpfulAssert<size_t>()(o1.data.size(), o1e.data.size());
|
||||
HelpfulAssert<size_t>()(o2.data.size(), o2e.data.size());
|
||||
for (int i = 0; i < o1.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o1.data[i], o1e.data[i]);
|
||||
}
|
||||
for (int i = 0; i < o2.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o2.data[i], o2e.data[i]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
})));
|
||||
|
||||
tg->addTest(std::make_unique<Test>("IDs test (disabled)", std::function<bool()>([]() {
|
||||
return true;
|
||||
Cleaner c({"testrepo"});
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo");
|
||||
FileRepository repo(conf);
|
||||
repo.init();
|
||||
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
|
||||
std::vector<char>
|
||||
data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1(repo.getId(), o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2(repo.getId(), o2k, data2);
|
||||
|
||||
HelpfulAssert<int>()(o1.id, 1);
|
||||
HelpfulAssert<int>()(o2.id, 2);
|
||||
|
||||
repo.putObject(o1);
|
||||
repo.putObject(o2);
|
||||
|
||||
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 1);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 2);
|
||||
}
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo");
|
||||
FileRepository repo(conf);
|
||||
repo.open();
|
||||
auto o1o = repo.getObject(1);
|
||||
auto o2o = repo.getObject(2);
|
||||
|
||||
auto o1ob = o1o.cbegin();
|
||||
auto o2ob = o2o.cbegin();
|
||||
|
||||
Chunk o1(o1ob, o1o.cend()), o2(o2ob, o2o.cend());
|
||||
std::string o1k(16, '\0');
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
std::vector<char> data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1e(1, o1k, data1);
|
||||
std::vector<char> data2{'q', 'w', 'e', 'r', 'b'};
|
||||
Chunk o2e(2, o2k, data2);
|
||||
|
||||
HelpfulAssert<Object::idType>()(o1.id, o1e.id);
|
||||
HelpfulAssert<Object::idType>()(o2.id, o2e.id);
|
||||
HelpfulAssert<int>()((int) o1.type, (int) o1e.type);
|
||||
HelpfulAssert<int>()((int) o2.type, (int) o2e.type);
|
||||
auto o1d = o1.data;
|
||||
auto o1ed = o1e.data;
|
||||
auto o2d = o2.data;
|
||||
auto o2ed = o2e.data;
|
||||
HelpfulAssert<size_t>()(o1.data.size(), o1e.data.size());
|
||||
HelpfulAssert<size_t>()(o2.data.size(), o2e.data.size());
|
||||
for (int i = 0; i < o1.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o1.data[i], o1e.data[i]);
|
||||
}
|
||||
for (int i = 0; i < o2.data.size(); i++) {
|
||||
HelpfulAssert<char>()(o2.data[i], o2e.data[i]);
|
||||
}
|
||||
|
||||
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o1k), 1);
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 2);
|
||||
|
||||
|
||||
repo.deleteObject(o1);
|
||||
}
|
||||
{
|
||||
Config conf;
|
||||
conf.add("repo", "testrepo");
|
||||
FileRepository repo(conf);
|
||||
repo.open();
|
||||
|
||||
std::string o2k(16, '\0');
|
||||
o2k[0] = 1;
|
||||
HelpfulAssert<Object::idType>()(repo.getObjectId(Object::ObjectType::Chunk, o2k), 2);
|
||||
|
||||
auto id = repo.getId();
|
||||
HelpfulAssert<int>()(id, 1);
|
||||
std::vector<char>
|
||||
data1{'a', 'b', 'c', 'e'};
|
||||
Chunk o1(id, o2k, data1);
|
||||
HelpfulAssert<int>()(repo.getId(), 3);
|
||||
}
|
||||
return true;
|
||||
})));
|
||||
|
||||
|
||||
return tg;
|
||||
}
|
||||
24
tests/repo/FileRepositoryTest.h
Normal file
24
tests/repo/FileRepositoryTest.h
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_FILEREPOSITORYTEST_H
|
||||
#define SEMBACKUP_FILEREPOSITORYTEST_H
|
||||
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "../../src/repo/FileRepository.h"
|
||||
#include "../../src/repo/objects/Chunk.h"
|
||||
#include "../utils/Cleaner.h"
|
||||
#include "../utils/TestGroupGenerator.h"
|
||||
|
||||
class FileRepositoryTest : public TestGroupGenerator {
|
||||
public:
|
||||
std::unique_ptr<TestGroup> operator()() override;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_FILEREPOSITORYTEST_H
|
||||
Reference in New Issue
Block a user