Browse Source

Updated unit tests to check backup/restore of files that don't exist.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@6 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 13 years ago
parent
commit
d199b0715a
1 changed files with 97 additions and 37 deletions
  1. 97
    37
      CommonTests/TestFileBackup.cpp

+ 97
- 37
CommonTests/TestFileBackup.cpp View File

@@ -38,7 +38,7 @@
/// Vector of strings
typedef std::vector<std::string> StringContainer;
/// Names of files to back up.
/// Names of files to back up that exist.
StringContainer FileName;
/// Container for the file content.
@@ -47,6 +47,9 @@ typedef std::map<std::string, std::string> ContentContainer;
/// Content of files to back up.
ContentContainer FileContent;
/// Names of files to back up that don't exist.
StringContainer NoFileName;
/// Random characters.
const std::string RandomChar("abcdefghijklmnopqustuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|=-");
std::string::size_type CharLen = RandomChar.length();
@@ -58,6 +61,16 @@ FileBackup TestFileBackup;
void
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;
FileName.push_back("File1.txt");
@@ -117,43 +130,64 @@ Initialize() {
}
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]);
}
@@ -161,6 +195,20 @@ TestBackupRestore() {
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.
std::ifstream Input;
@@ -213,18 +261,30 @@ TestRemoveAllBackupFiles() {
TestFileBackup.RemoveAllBackupFiles();
std::string BackupFileName;
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]);
Input.open(BackupFileName.c_str());
Input.close();
if (Input) {
if (FileBackup::FileExists(BackupFileName)) {
std::string Temp;
Temp = "***Error--Backup file " + BackupFileName;
@@ -300,6 +360,11 @@ int main(int argc, char* argv[]) {
Initialize(); // Create test files.
// Test FileExists.
if (!TestFileExists()) {
ErrorExit("TestFileExists() failure.\n");
}
// Test backup/restore.
if (!TestBackupRestore()) {
ErrorExit("TestBackupRestore() failure.\n");
@@ -310,11 +375,6 @@ int main(int argc, char* argv[]) {
ErrorExit("TestRemoveAllBackupFiles() failure.\n");
}
// Test FileExists.
if (!TestFileExists()) {
ErrorExit("TestFileExists() failure.\n");
}
Finalize(); // Remove test files.
} // That's all folks.

Loading…
Cancel
Save