|
|
|
|
|
|
|
|
/// Vector of strings
|
|
|
/// Vector of strings
|
|
|
typedef std::vector<std::string> StringContainer;
|
|
|
typedef std::vector<std::string> StringContainer;
|
|
|
|
|
|
|
|
|
/// Names of files to back up.
|
|
|
|
|
|
|
|
|
/// Names of files to back up that exist.
|
|
|
StringContainer FileName;
|
|
|
StringContainer FileName;
|
|
|
|
|
|
|
|
|
/// Container for the file content.
|
|
|
/// Container for the file content.
|
|
|
|
|
|
|
|
|
/// Content of files to back up.
|
|
|
/// Content of files to back up.
|
|
|
ContentContainer FileContent;
|
|
|
ContentContainer FileContent;
|
|
|
|
|
|
|
|
|
|
|
|
/// Names of files to back up that don't exist.
|
|
|
|
|
|
StringContainer NoFileName;
|
|
|
|
|
|
|
|
|
/// Random characters.
|
|
|
/// Random characters.
|
|
|
const std::string RandomChar("abcdefghijklmnopqustuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|=-");
|
|
|
const std::string RandomChar("abcdefghijklmnopqustuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|=-");
|
|
|
std::string::size_type CharLen = RandomChar.length();
|
|
|
std::string::size_type CharLen = RandomChar.length();
|
|
|
|
|
|
|
|
|
void
|
|
|
void
|
|
|
Initialize() {
|
|
|
Initialize() {
|
|
|
|
|
|
|
|
|
|
|
|
NoFileName.push_back("NoFile1.txt");
|
|
|
|
|
|
NoFileName.push_back("NoFile2.txt");
|
|
|
|
|
|
NoFileName.push_back("NoFile3.txt");
|
|
|
|
|
|
NoFileName.push_back("NoFile4.txt");
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NoFileName.size(); i++) {
|
|
|
|
|
|
|
|
|
|
|
|
remove(NoFileName[i].c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
std::vector<int> FileSize;
|
|
|
std::vector<int> FileSize;
|
|
|
|
|
|
|
|
|
FileName.push_back("File1.txt");
|
|
|
FileName.push_back("File1.txt");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
TestBackupRestore() {
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
OverwriteFile(std::string File) {
|
|
|
|
|
|
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files.
|
|
|
|
|
|
|
|
|
std::ofstream Output;
|
|
|
|
|
|
|
|
|
|
|
|
Output.open(File.c_str(), std::ios::trunc); // Overwrite the test file.
|
|
|
|
|
|
if (!Output) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
TestFileBackup.CreateBackupFile(FileName[i]);
|
|
|
|
|
|
|
|
|
Temp = "OverwriteFile: Error opening the file " + File;
|
|
|
|
|
|
Temp += " to overwrite: ";
|
|
|
|
|
|
Temp += strerror(errno);
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
std::ofstream Output;
|
|
|
|
|
|
|
|
|
|
|
|
Output.open(FileName[i].c_str(), std::ios::trunc); // Overwrite the test file.
|
|
|
|
|
|
if (!Output) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Temp = "TestBackupRestore: Error opening the test file " + FileName[i];
|
|
|
|
|
|
Temp += " to overwrite: ";
|
|
|
|
|
|
Temp += strerror(errno);
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
for (int j = 0; j < 223; j++) {
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Output << RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)];
|
|
|
|
|
|
|
|
|
for (int j = 0; j < 223; j++) {
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Output << RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)];
|
|
|
|
|
|
|
|
|
if (Output.bad() || Output.fail()) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Temp = "OverwriteFile: Error overwriting the file " + File;
|
|
|
|
|
|
Temp += ": ";
|
|
|
|
|
|
Temp += strerror(errno);
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
if (Output.bad() || Output.fail()) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Temp = "TestBackupRestore: Error overwriting the test file " + FileName[i];
|
|
|
|
|
|
Temp += ": ";
|
|
|
|
|
|
Temp += strerror(errno);
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
Output.close();
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!FileBackup::FileExists(File)) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
Output.close();
|
|
|
|
|
|
|
|
|
Temp = "OverwriteFile: File " + File;
|
|
|
|
|
|
Temp += " was created but was determined to not exist.\n";
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
TestBackupRestore() {
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
|
|
|
|
|
|
|
|
|
|
|
|
TestFileBackup.CreateBackupFile(NoFileName[i]);
|
|
|
|
|
|
OverwriteFile(NoFileName[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files that exist.
|
|
|
|
|
|
|
|
|
|
|
|
TestFileBackup.CreateBackupFile(FileName[i]);
|
|
|
|
|
|
OverwriteFile(FileName[i]);
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ResultIsPass = true;
|
|
|
bool ResultIsPass = true;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
|
|
|
|
|
|
|
|
|
|
|
|
if (FileBackup::FileExists(NoFileName[i])) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
|
|
|
Temp = "TestBackupRestore: File " + NoFileName[i];
|
|
|
|
|
|
Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
|
|
|
|
|
|
Temp += "but was determined to exist.\n";
|
|
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Check content of restored files.
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Check content of restored files.
|
|
|
|
|
|
|
|
|
std::ifstream Input;
|
|
|
std::ifstream Input;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TestFileBackup.RemoveAllBackupFiles();
|
|
|
TestFileBackup.RemoveAllBackupFiles();
|
|
|
|
|
|
|
|
|
|
|
|
std::string BackupFileName;
|
|
|
bool ResultIsPass = true;
|
|
|
bool ResultIsPass = true;
|
|
|
|
|
|
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
|
|
|
|
|
|
|
|
|
for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
|
|
|
|
|
|
|
|
|
std::ifstream Input;
|
|
|
|
|
|
std::string BackupFileName;
|
|
|
|
|
|
|
|
|
BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
|
|
|
|
|
|
|
|
|
|
|
|
if (FileBackup::FileExists(BackupFileName)) {
|
|
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
|
|
|
Temp = "***Error--Backup file " + BackupFileName;
|
|
|
|
|
|
Temp += " was not removed.\n";
|
|
|
|
|
|
Error(Temp);
|
|
|
|
|
|
ResultIsPass = false;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
|
|
|
|
|
|
|
|
|
BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
|
|
|
BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
|
|
|
|
|
|
|
|
|
Input.open(BackupFileName.c_str());
|
|
|
|
|
|
Input.close();
|
|
|
|
|
|
if (Input) {
|
|
|
|
|
|
|
|
|
if (FileBackup::FileExists(BackupFileName)) {
|
|
|
std::string Temp;
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
|
Temp = "***Error--Backup file " + BackupFileName;
|
|
|
Temp = "***Error--Backup file " + BackupFileName;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Initialize(); // Create test files.
|
|
|
Initialize(); // Create test files.
|
|
|
|
|
|
|
|
|
|
|
|
// Test FileExists.
|
|
|
|
|
|
if (!TestFileExists()) {
|
|
|
|
|
|
ErrorExit("TestFileExists() failure.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
// Test backup/restore.
|
|
|
// Test backup/restore.
|
|
|
if (!TestBackupRestore()) {
|
|
|
if (!TestBackupRestore()) {
|
|
|
ErrorExit("TestBackupRestore() failure.\n");
|
|
|
ErrorExit("TestBackupRestore() failure.\n");
|
|
|
|
|
|
|
|
|
ErrorExit("TestRemoveAllBackupFiles() failure.\n");
|
|
|
ErrorExit("TestRemoveAllBackupFiles() failure.\n");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
// Test FileExists.
|
|
|
|
|
|
if (!TestFileExists()) {
|
|
|
|
|
|
ErrorExit("TestFileExists() failure.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Finalize(); // Remove test files.
|
|
|
Finalize(); // Remove test files.
|
|
|
|
|
|
|
|
|
} // That's all folks.
|
|
|
} // That's all folks.
|