|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- // $Id$
- //
- // \file TestFileBackup.cpp
- //
- // Copyright (C) 2012, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This is the unit test for the class FileBackup.
- //
-
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <stdexcept>
- #include <map>
-
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
-
- #include "FileBackup.hpp"
-
- /// Output error.
- #define Error(msg) \
- { \
- std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
- std::cerr << msg; \
- }
-
- /// Exit with error.
- #define ErrorExit(msg) \
- { \
- Error(msg) \
- std::exit(-1); \
- }
-
- /// Vector of strings
- typedef std::vector<std::string> StringContainer;
-
- /// Names of files to back up that exist.
- StringContainer FileName;
-
- /// Container for the file content.
- 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();
-
- /// Unit under test.
- FileBackup TestFileBackup;
-
- /// Initialize test input.
- 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");
- FileSize.push_back(1024);
- FileName.push_back("File2.txt");
- FileSize.push_back(4000);
- FileName.push_back("File3.txt");
- FileSize.push_back(493);
- FileName.push_back("File4.txt");
- FileSize.push_back(203043);
-
- std::vector<int>::iterator iSize = FileSize.begin();
-
- for (StringContainer::iterator iFile = FileName.begin(); // Create random data.
- iFile != FileName.end();
- iFile++, iSize++) {
-
- for (int iChar = 0; iChar < *iSize; iChar++) {
-
- FileContent[*iFile].push_back(RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)]);
-
- }
-
- remove(iFile->c_str());
- remove(FileBackup::GetBackupFileName(*iFile).c_str());
- remove(FileBackup::GetFailedFileName(*iFile).c_str());
-
- std::ofstream Output; // Write the test file.
-
- Output.open(iFile->c_str(), std::ios::trunc);
- if (!Output) {
- std::string Temp;
-
- Temp = "Initialize: Error opening the test file " + *iFile;
- Temp += " to copy to: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- Output << FileContent[*iFile];
-
- if (Output.bad() || Output.fail()) {
- std::string Temp;
-
- Temp = "Initialize: Error writing the test file " + *iFile;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- Output.close();
-
- }
-
- }
-
- std::string
- OverwriteFile(std::string File) {
-
- std::ofstream Output;
- std::string NewContent;
-
- Output.open(File.c_str(), std::ios::trunc); // Overwrite the test file.
- if (!Output) {
- std::string Temp;
-
- Temp = "OverwriteFile: Error opening the file " + File;
- Temp += " to overwrite: ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- for (int j = 0; j < 223; j++) {
-
- NewContent.push_back(RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)]);
-
- }
-
- Output << NewContent; // Overwrite the test file.
-
- if (Output.bad() || Output.fail()) {
- std::string Temp;
-
- Temp = "OverwriteFile: Error overwriting the file " + File;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- Output.close();
-
- if (!FileBackup::FileExists(File)) {
- std::string Temp;
-
- Temp = "OverwriteFile: File " + File;
- Temp += " was created but was determined to not exist.\n";
- throw std::runtime_error(Temp);
-
- }
-
- return NewContent;
-
- }
-
- bool
- CompareContent(std::string FileName, std::string ExpectedContent) {
-
- std::ifstream Input;
-
- Input.open(FileName.c_str());
- if (!Input) {
- std::string Temp;
-
- Temp = "CompareContent: Error opening " + FileName;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- std::string ReadContent;
-
- Input >> ReadContent;
-
- Input.close();
- if (!Input) {
- std::string Temp;
-
- Temp = "CompareContent: Error closing " + FileName;
- Temp += ": ";
- Temp += strerror(errno);
- throw std::runtime_error(Temp);
-
- }
-
- if (ReadContent != ExpectedContent) {
- std::string Temp;
-
- Temp = "***Error--File " + FileName;
- Temp += " did not have the correct content.\n";
-
- Error(Temp);
- return false;
-
- }
-
- return true;
-
- }
-
- bool
- TestBackupRestore() {
-
- ContentContainer NewContent;
- ContentContainer OverwrittenContent;
- std::string Content;
- bool ResultIsPass = true;
-
- for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
-
- TestFileBackup.CreateBackupFile(NoFileName[i]);
- Content = OverwriteFile(NoFileName[i]); // Overwrite the file.
- NewContent[NoFileName[i]] = Content; // Save the new content.
-
- }
-
- for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files that exist.
-
- TestFileBackup.CreateBackupFile(FileName[i]);
- Content = OverwriteFile(FileName[i]); // Overwrite the file.
- OverwrittenContent[FileName[i]] = Content; // Save the new contents.
-
- }
-
- TestFileBackup.RestoreAllFilesFromBackup();
-
- for (int i = 0; i < NoFileName.size(); i++) {
-
- if (FileBackup::FileExists(NoFileName[i])) { // Check that files don't exist.
- std::string Temp;
-
- Temp = "TestBackupRestore: File " + NoFileName[i];
- Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- if (!CompareContent(FileBackup::GetFailedFileName(NoFileName[i]), // Check the .failed file content.
- NewContent[NoFileName[i]])) {
- std::string Temp;
-
- Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
- Temp += " create the .failed file.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- for (int i = 0; i < FileName.size(); i++) {
-
- if (!CompareContent(FileName[i], FileContent[FileName[i]])) { // Check content of restored files.
- std::string Temp;
-
- Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
- Temp += " restore the file.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- if (!CompareContent(FileBackup::GetFailedFileName(FileName[i]), // Check the .failed file content.
- OverwrittenContent[FileName[i]])) {
- std::string Temp;
-
- Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
- Temp += " create the .failed file.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- return ResultIsPass;
-
- }
-
- bool
- TestRemoveAllBackupFiles() {
-
- TestFileBackup.RemoveAllBackupFiles();
-
- std::string BackupFileName;
- std::string FailedFileName;
- bool ResultIsPass = true;
-
- for (int i = 0; i < NoFileName.size(); i++) {
-
- BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
-
- if (FileBackup::FileExists(BackupFileName)) { // Check that backup files don't exist.
- std::string Temp;
-
- Temp = "***Error--Backup file " + BackupFileName;
- Temp += " was not removed.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- FailedFileName = FileBackup::GetFailedFileName(NoFileName[i]);
-
- if (!FileBackup::FileExists(FailedFileName)) { // Check that .failed files exist.
- std::string Temp;
-
- Temp = "***Error--Failed file " + FailedFileName;
- Temp += " does not exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- for (int i = 0; i < FileName.size(); i++) {
-
- BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
-
- if (FileBackup::FileExists(BackupFileName)) { // Check that backup files don't exist.
- std::string Temp;
-
- Temp = "***Error--Backup file " + BackupFileName;
- Temp += " was not removed.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- FailedFileName = FileBackup::GetFailedFileName(FileName[i]);
-
- if (!FileBackup::FileExists(FailedFileName)) { // Check that .failed files exist.
- std::string Temp;
-
- Temp = "***Error--Failed file " + FailedFileName;
- Temp += " does not exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- return ResultIsPass;
-
- }
-
- bool
- TestFileExists() {
-
- bool ResultIsPass = true;
-
- for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
-
- std::string BackupFileName;
-
- BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
-
- if (FileBackup::FileExists(BackupFileName)) {
- std::string Temp;
-
- Temp = "***Error--File " + BackupFileName;
- Temp += " incorrectly determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- if (!FileBackup::FileExists(FileName[i])) {
- std::string Temp;
-
- Temp = "***Error--File " + FileName[i];
- Temp += " incorrectly determined to not exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- return ResultIsPass;
-
- }
-
- bool
- TestBackupRestoreWithNoFiles() {
-
- ContentContainer NewContent;
- std::string Content;
- std::string BackupFileName;
-
- /// Unit under test.
- FileBackup TestNoFileBackup;
-
- for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
-
- TestNoFileBackup.CreateBackupFile(NoFileName[i]);
- Content = OverwriteFile(NoFileName[i]); // Overwrite the file.
- NewContent[NoFileName[i]] = Content; // Save the new content.
-
- }
-
- TestNoFileBackup.RestoreAllFilesFromBackup();
-
- bool ResultIsPass = true;
-
- for (int i = 0; i < NoFileName.size(); i++) {
-
- if (FileBackup::FileExists(NoFileName[i])) { // Check that file doesn't exist.
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFiles: File " + NoFileName[i];
- Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
-
- if (FileBackup::FileExists(BackupFileName)) { // Check that backup file doesn't exist.
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + BackupFileName;
- Temp += " was not supposed to be created by CreateBackupFile, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- if (!CompareContent(FileBackup::GetFailedFileName(NoFileName[i]), // Check the .failed file content.
- NewContent[NoFileName[i]])) {
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFilesNoOverwrite: RestoreAllFilesFromBackup() did not correctly";
- Temp += " create the .failed file.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- return ResultIsPass;
-
- }
-
- bool
- TestBackupRestoreWithNoFilesNoOverwrite() {
-
- /// Unit under test.
- FileBackup TestNoFileBackup;
-
- for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
-
- TestNoFileBackup.CreateBackupFile(NoFileName[i]);
- }
-
- TestNoFileBackup.RestoreAllFilesFromBackup();
-
- bool ResultIsPass = true;
- std::string BackupFileName;
- std::string FailedFileName;
-
- for (int i = 0; i < NoFileName.size(); i++) {
-
- if (FileBackup::FileExists(NoFileName[i])) { // Check that file don't exist.
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + NoFileName[i];
- Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
-
- if (FileBackup::FileExists(BackupFileName)) { // Check that backup file doesn't exist.
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + BackupFileName;
- Temp += " was not supposed to be created by CreateBackupFile, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- FailedFileName = FileBackup::GetFailedFileName(NoFileName[i]);
-
- if (FileBackup::FileExists(FailedFileName)) { // Check that failed file doesn't exist.
- std::string Temp;
-
- Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + FailedFileName;
- Temp += " was not supposed to be created by RestoreAllFilesFromBackup, ";
- Temp += "but was determined to exist.\n";
- Error(Temp);
- ResultIsPass = false;
-
- }
-
- }
-
- return ResultIsPass;
-
- }
-
- void
- Finalize() {
-
- for (int i = 0; i < FileName.size(); i++) {
-
- remove(FileName[i].c_str());
- remove((FileBackup::GetBackupFileName(FileName[i]).c_str()));
- remove((FileBackup::GetFailedFileName(FileName[i]).c_str()));
-
- }
-
- for (int i = 0; i < NoFileName.size(); i++) {
-
- remove(NoFileName[i].c_str());
- remove((FileBackup::GetBackupFileName(NoFileName[i]).c_str()));
- remove((FileBackup::GetFailedFileName(NoFileName[i]).c_str()));
-
- }
-
- }
-
- /// Unit tests for FileBackup.
- //
- // This function creates several files of varying size, and verifies
- // the functionality of the FileBackup backup, restore, and delete
- // functions.
- //
- int main(int argc, char* argv[]) {
-
- try { // Catch anything that breaks loose.
-
- Initialize(); // Create test files.
-
- // Test FileExists.
- if (!TestFileExists()) {
- ErrorExit("TestFileExists() failure.\n");
- }
-
- // Test backup/restore.
- if (!TestBackupRestore()) {
- ErrorExit("TestBackupRestore() failure.\n");
- }
-
- // Test cleanup.
- if (!TestRemoveAllBackupFiles()) {
- ErrorExit("TestRemoveAllBackupFiles() failure.\n");
- }
-
- Finalize(); // Remove test files.
-
- // Test backup/restore with no files.
- if (!TestBackupRestoreWithNoFiles()) {
- ErrorExit("TestBackupRestoreWithNoFiles() failure.\n");
- }
-
- // Test backup/restore with no files and no overwriting of files.
- if (!TestBackupRestoreWithNoFilesNoOverwrite()) {
- ErrorExit("TestBackupRestoreWithNoFilesNoOverwrite() failure.\n");
- }
-
- Finalize();
-
- } // That's all folks.
-
- catch(std::exception& e) { // Report any normal exceptions.
-
- std::cerr << "FileBackup exception: " << e.what() << std::endl;
- return -1;
-
- }
- catch(...) { // Report any unexpected exceptions.
-
- std::cerr << "Panic! Unknown Exception!" << std::endl;
- return -1;
-
- }
-
- return 0; // Normally we return zero.
- }
|