Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestFileBackup.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // $Id$
  2. //
  3. // \file TestFileBackup.cpp
  4. //
  5. // Copyright (C) 2012, ARM Research Labs, LLC.
  6. // See www.armresearch.com for the copyright terms.
  7. //
  8. // This is the unit test for the class FileBackup.
  9. //
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <vector>
  14. #include <stdexcept>
  15. #include <map>
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include <cerrno>
  19. #include "FileBackup.hpp"
  20. /// Output error.
  21. #define Error(msg) \
  22. { \
  23. std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
  24. std::cerr << msg; \
  25. }
  26. /// Exit with error.
  27. #define ErrorExit(msg) \
  28. { \
  29. Error(msg) \
  30. std::exit(-1); \
  31. }
  32. /// Vector of strings
  33. typedef std::vector<std::string> StringContainer;
  34. /// Names of files to back up that exist.
  35. StringContainer FileName;
  36. /// Container for the file content.
  37. typedef std::map<std::string, std::string> ContentContainer;
  38. /// Content of files to back up.
  39. ContentContainer FileContent;
  40. /// Names of files to back up that don't exist.
  41. StringContainer NoFileName;
  42. /// Random characters.
  43. const std::string RandomChar("abcdefghijklmnopqustuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+|=-");
  44. std::string::size_type CharLen = RandomChar.length();
  45. /// Unit under test.
  46. FileBackup TestFileBackup;
  47. /// Initialize test input.
  48. void
  49. Initialize() {
  50. NoFileName.push_back("NoFile1.txt");
  51. NoFileName.push_back("NoFile2.txt");
  52. NoFileName.push_back("NoFile3.txt");
  53. NoFileName.push_back("NoFile4.txt");
  54. for (int i = 0; i < NoFileName.size(); i++) {
  55. remove(NoFileName[i].c_str());
  56. }
  57. std::vector<int> FileSize;
  58. FileName.push_back("File1.txt");
  59. FileSize.push_back(1024);
  60. FileName.push_back("File2.txt");
  61. FileSize.push_back(4000);
  62. FileName.push_back("File3.txt");
  63. FileSize.push_back(493);
  64. FileName.push_back("File4.txt");
  65. FileSize.push_back(203043);
  66. const char *CharPtr = RandomChar.data();
  67. std::vector<int>::iterator iSize = FileSize.begin();
  68. for (StringContainer::iterator iFile = FileName.begin(); // Create random data.
  69. iFile != FileName.end();
  70. iFile++, iSize++) {
  71. for (int iChar = 0; iChar < *iSize; iChar++) {
  72. FileContent[*iFile].push_back(RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)]);
  73. }
  74. remove(iFile->c_str());
  75. remove(FileBackup::GetBackupFileName(*iFile).c_str());
  76. std::ofstream Output; // Write the test file.
  77. Output.open(iFile->c_str(), std::ios::trunc);
  78. if (!Output) {
  79. std::string Temp;
  80. Temp = "Initialize: Error opening the test file " + *iFile;
  81. Temp += " to copy to: ";
  82. Temp += strerror(errno);
  83. throw std::runtime_error(Temp);
  84. }
  85. Output << FileContent[*iFile];
  86. if (Output.bad() || Output.fail()) {
  87. std::string Temp;
  88. Temp = "Initialize: Error writing the test file " + *iFile;
  89. Temp += ": ";
  90. Temp += strerror(errno);
  91. throw std::runtime_error(Temp);
  92. }
  93. Output.close();
  94. }
  95. }
  96. void
  97. OverwriteFile(std::string File) {
  98. std::ofstream Output;
  99. Output.open(File.c_str(), std::ios::trunc); // Overwrite the test file.
  100. if (!Output) {
  101. std::string Temp;
  102. Temp = "OverwriteFile: Error opening the file " + File;
  103. Temp += " to overwrite: ";
  104. Temp += strerror(errno);
  105. throw std::runtime_error(Temp);
  106. }
  107. for (int j = 0; j < 223; j++) {
  108. Output << RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)];
  109. }
  110. if (Output.bad() || Output.fail()) {
  111. std::string Temp;
  112. Temp = "OverwriteFile: Error overwriting the file " + File;
  113. Temp += ": ";
  114. Temp += strerror(errno);
  115. throw std::runtime_error(Temp);
  116. }
  117. Output.close();
  118. if (!FileBackup::FileExists(File)) {
  119. std::string Temp;
  120. Temp = "OverwriteFile: File " + File;
  121. Temp += " was created but was determined to not exist.\n";
  122. throw std::runtime_error(Temp);
  123. }
  124. }
  125. bool
  126. TestBackupRestore() {
  127. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  128. TestFileBackup.CreateBackupFile(NoFileName[i]);
  129. OverwriteFile(NoFileName[i]);
  130. }
  131. for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files that exist.
  132. TestFileBackup.CreateBackupFile(FileName[i]);
  133. OverwriteFile(FileName[i]);
  134. }
  135. TestFileBackup.RestoreAllFilesFromBackup();
  136. bool ResultIsPass = true;
  137. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  138. if (FileBackup::FileExists(NoFileName[i])) {
  139. std::string Temp;
  140. Temp = "TestBackupRestore: File " + NoFileName[i];
  141. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  142. Temp += "but was determined to exist.\n";
  143. throw std::runtime_error(Temp);
  144. }
  145. }
  146. for (int i = 0; i < FileName.size(); i++) { // Check content of restored files.
  147. std::ifstream Input;
  148. Input.open(FileName[i].c_str());
  149. if (!Input) {
  150. std::string Temp;
  151. Temp = "TestBackupRestore: Error opening the restored file " + FileName[i];
  152. Temp += ": ";
  153. Temp += strerror(errno);
  154. throw std::runtime_error(Temp);
  155. }
  156. std::string RestoredContent;
  157. Input >> RestoredContent;
  158. Input.close();
  159. if (!Input) {
  160. std::string Temp;
  161. Temp = "TestBackupRestore: Error closing the restored file " + FileName[i];
  162. Temp += ": ";
  163. Temp += strerror(errno);
  164. throw std::runtime_error(Temp);
  165. }
  166. if (RestoredContent != FileContent[FileName[i]]) {
  167. std::string Temp;
  168. Temp = "***Error--File " + FileName[i];
  169. Temp += " was not restored correctly.\n";
  170. Error(Temp);
  171. ResultIsPass = false;
  172. }
  173. }
  174. return ResultIsPass;
  175. }
  176. bool
  177. TestRemoveAllBackupFiles() {
  178. TestFileBackup.RemoveAllBackupFiles();
  179. std::string BackupFileName;
  180. bool ResultIsPass = true;
  181. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  182. BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
  183. if (FileBackup::FileExists(BackupFileName)) {
  184. std::string Temp;
  185. Temp = "***Error--Backup file " + BackupFileName;
  186. Temp += " was not removed.\n";
  187. Error(Temp);
  188. ResultIsPass = false;
  189. }
  190. }
  191. for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
  192. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  193. if (FileBackup::FileExists(BackupFileName)) {
  194. std::string Temp;
  195. Temp = "***Error--Backup file " + BackupFileName;
  196. Temp += " was not removed.\n";
  197. Error(Temp);
  198. ResultIsPass = false;
  199. }
  200. }
  201. return ResultIsPass;
  202. }
  203. bool
  204. TestFileExists() {
  205. bool ResultIsPass = true;
  206. for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
  207. std::string BackupFileName;
  208. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  209. if (FileBackup::FileExists(BackupFileName)) {
  210. std::string Temp;
  211. Temp = "***Error--File " + BackupFileName;
  212. Temp += " incorrectly determined to exist.\n";
  213. Error(Temp);
  214. ResultIsPass = false;
  215. }
  216. if (!FileBackup::FileExists(FileName[i])) {
  217. std::string Temp;
  218. Temp = "***Error--File " + FileName[i];
  219. Temp += " incorrectly determined to not exist.\n";
  220. Error(Temp);
  221. ResultIsPass = false;
  222. }
  223. }
  224. return ResultIsPass;
  225. }
  226. void
  227. Finalize() {
  228. for (int i = 0; i < FileName.size(); i++) {
  229. remove(FileName[i].c_str());
  230. }
  231. }
  232. /// Unit tests for FileBackup.
  233. //
  234. // This function creates several files of varying size, and verifies
  235. // the functionality of the FileBackup backup, restore, and delete
  236. // functions.
  237. //
  238. int main(int argc, char* argv[]) {
  239. try { // Catch anything that breaks loose.
  240. Initialize(); // Create test files.
  241. // Test FileExists.
  242. if (!TestFileExists()) {
  243. ErrorExit("TestFileExists() failure.\n");
  244. }
  245. // Test backup/restore.
  246. if (!TestBackupRestore()) {
  247. ErrorExit("TestBackupRestore() failure.\n");
  248. }
  249. // Test cleanup.
  250. if (!TestRemoveAllBackupFiles()) {
  251. ErrorExit("TestRemoveAllBackupFiles() failure.\n");
  252. }
  253. Finalize(); // Remove test files.
  254. } // That's all folks.
  255. catch(std::exception& e) { // Report any normal exceptions.
  256. std::cerr << "FileBackup exception: " << e.what() << std::endl;
  257. return -1;
  258. }
  259. catch(...) { // Report any unexpected exceptions.
  260. std::cerr << "Panic! Unknown Exception!" << std::endl;
  261. return -1;
  262. }
  263. return 0; // Normally we return zero.
  264. }