Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestFileBackup.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. std::vector<int>::iterator iSize = FileSize.begin();
  67. for (StringContainer::iterator iFile = FileName.begin(); // Create random data.
  68. iFile != FileName.end();
  69. iFile++, iSize++) {
  70. for (int iChar = 0; iChar < *iSize; iChar++) {
  71. FileContent[*iFile].push_back(RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)]);
  72. }
  73. remove(iFile->c_str());
  74. remove(FileBackup::GetBackupFileName(*iFile).c_str());
  75. std::ofstream Output; // Write the test file.
  76. Output.open(iFile->c_str(), std::ios::trunc);
  77. if (!Output) {
  78. std::string Temp;
  79. Temp = "Initialize: Error opening the test file " + *iFile;
  80. Temp += " to copy to: ";
  81. Temp += strerror(errno);
  82. throw std::runtime_error(Temp);
  83. }
  84. Output << FileContent[*iFile];
  85. if (Output.bad() || Output.fail()) {
  86. std::string Temp;
  87. Temp = "Initialize: Error writing the test file " + *iFile;
  88. Temp += ": ";
  89. Temp += strerror(errno);
  90. throw std::runtime_error(Temp);
  91. }
  92. Output.close();
  93. }
  94. }
  95. void
  96. OverwriteFile(std::string File) {
  97. std::ofstream Output;
  98. Output.open(File.c_str(), std::ios::trunc); // Overwrite the test file.
  99. if (!Output) {
  100. std::string Temp;
  101. Temp = "OverwriteFile: Error opening the file " + File;
  102. Temp += " to overwrite: ";
  103. Temp += strerror(errno);
  104. throw std::runtime_error(Temp);
  105. }
  106. for (int j = 0; j < 223; j++) {
  107. Output << RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)];
  108. }
  109. if (Output.bad() || Output.fail()) {
  110. std::string Temp;
  111. Temp = "OverwriteFile: Error overwriting the file " + File;
  112. Temp += ": ";
  113. Temp += strerror(errno);
  114. throw std::runtime_error(Temp);
  115. }
  116. Output.close();
  117. if (!FileBackup::FileExists(File)) {
  118. std::string Temp;
  119. Temp = "OverwriteFile: File " + File;
  120. Temp += " was created but was determined to not exist.\n";
  121. throw std::runtime_error(Temp);
  122. }
  123. }
  124. bool
  125. TestBackupRestore() {
  126. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  127. TestFileBackup.CreateBackupFile(NoFileName[i]);
  128. OverwriteFile(NoFileName[i]);
  129. }
  130. for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files that exist.
  131. TestFileBackup.CreateBackupFile(FileName[i]);
  132. OverwriteFile(FileName[i]);
  133. }
  134. TestFileBackup.RestoreAllFilesFromBackup();
  135. bool ResultIsPass = true;
  136. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  137. if (FileBackup::FileExists(NoFileName[i])) {
  138. std::string Temp;
  139. Temp = "TestBackupRestore: File " + NoFileName[i];
  140. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  141. Temp += "but was determined to exist.\n";
  142. throw std::runtime_error(Temp);
  143. }
  144. }
  145. for (int i = 0; i < FileName.size(); i++) { // Check content of restored files.
  146. std::ifstream Input;
  147. Input.open(FileName[i].c_str());
  148. if (!Input) {
  149. std::string Temp;
  150. Temp = "TestBackupRestore: Error opening the restored file " + FileName[i];
  151. Temp += ": ";
  152. Temp += strerror(errno);
  153. throw std::runtime_error(Temp);
  154. }
  155. std::string RestoredContent;
  156. Input >> RestoredContent;
  157. Input.close();
  158. if (!Input) {
  159. std::string Temp;
  160. Temp = "TestBackupRestore: Error closing the restored file " + FileName[i];
  161. Temp += ": ";
  162. Temp += strerror(errno);
  163. throw std::runtime_error(Temp);
  164. }
  165. if (RestoredContent != FileContent[FileName[i]]) {
  166. std::string Temp;
  167. Temp = "***Error--File " + FileName[i];
  168. Temp += " was not restored correctly.\n";
  169. Error(Temp);
  170. ResultIsPass = false;
  171. }
  172. }
  173. return ResultIsPass;
  174. }
  175. bool
  176. TestRemoveAllBackupFiles() {
  177. TestFileBackup.RemoveAllBackupFiles();
  178. std::string BackupFileName;
  179. bool ResultIsPass = true;
  180. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  181. BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
  182. if (FileBackup::FileExists(BackupFileName)) {
  183. std::string Temp;
  184. Temp = "***Error--Backup file " + BackupFileName;
  185. Temp += " was not removed.\n";
  186. Error(Temp);
  187. ResultIsPass = false;
  188. }
  189. }
  190. for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
  191. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  192. if (FileBackup::FileExists(BackupFileName)) {
  193. std::string Temp;
  194. Temp = "***Error--Backup file " + BackupFileName;
  195. Temp += " was not removed.\n";
  196. Error(Temp);
  197. ResultIsPass = false;
  198. }
  199. }
  200. return ResultIsPass;
  201. }
  202. bool
  203. TestFileExists() {
  204. bool ResultIsPass = true;
  205. for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
  206. std::string BackupFileName;
  207. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  208. if (FileBackup::FileExists(BackupFileName)) {
  209. std::string Temp;
  210. Temp = "***Error--File " + BackupFileName;
  211. Temp += " incorrectly determined to exist.\n";
  212. Error(Temp);
  213. ResultIsPass = false;
  214. }
  215. if (!FileBackup::FileExists(FileName[i])) {
  216. std::string Temp;
  217. Temp = "***Error--File " + FileName[i];
  218. Temp += " incorrectly determined to not exist.\n";
  219. Error(Temp);
  220. ResultIsPass = false;
  221. }
  222. }
  223. return ResultIsPass;
  224. }
  225. bool
  226. TestBackupRestoreWithNoFiles() {
  227. /// Unit under test.
  228. FileBackup TestNoFileBackup;
  229. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  230. TestNoFileBackup.CreateBackupFile(NoFileName[i]);
  231. OverwriteFile(NoFileName[i]);
  232. }
  233. TestNoFileBackup.RestoreAllFilesFromBackup();
  234. bool ResultIsPass = true;
  235. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  236. if (FileBackup::FileExists(NoFileName[i])) {
  237. std::string Temp;
  238. Temp = "TestBackupRestoreWithNoFiles: File " + NoFileName[i];
  239. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  240. Temp += "but was determined to exist.\n";
  241. throw std::runtime_error(Temp);
  242. }
  243. }
  244. return ResultIsPass;
  245. }
  246. bool
  247. TestBackupRestoreWithNoFilesNoOverwrite() {
  248. /// Unit under test.
  249. FileBackup TestNoFileBackup;
  250. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  251. TestNoFileBackup.CreateBackupFile(NoFileName[i]);
  252. }
  253. TestNoFileBackup.RestoreAllFilesFromBackup();
  254. bool ResultIsPass = true;
  255. for (int i = 0; i < NoFileName.size(); i++) { // Check that files don't exist.
  256. if (FileBackup::FileExists(NoFileName[i])) {
  257. std::string Temp;
  258. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + NoFileName[i];
  259. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  260. Temp += "but was determined to exist.\n";
  261. throw std::runtime_error(Temp);
  262. }
  263. }
  264. return ResultIsPass;
  265. }
  266. void
  267. Finalize() {
  268. for (int i = 0; i < FileName.size(); i++) {
  269. remove(FileName[i].c_str());
  270. }
  271. }
  272. /// Unit tests for FileBackup.
  273. //
  274. // This function creates several files of varying size, and verifies
  275. // the functionality of the FileBackup backup, restore, and delete
  276. // functions.
  277. //
  278. int main(int argc, char* argv[]) {
  279. try { // Catch anything that breaks loose.
  280. Initialize(); // Create test files.
  281. // Test FileExists.
  282. if (!TestFileExists()) {
  283. ErrorExit("TestFileExists() failure.\n");
  284. }
  285. // Test backup/restore.
  286. if (!TestBackupRestore()) {
  287. ErrorExit("TestBackupRestore() failure.\n");
  288. }
  289. // Test cleanup.
  290. if (!TestRemoveAllBackupFiles()) {
  291. ErrorExit("TestRemoveAllBackupFiles() failure.\n");
  292. }
  293. Finalize(); // Remove test files.
  294. // Test backup/restore with no files.
  295. if (!TestBackupRestoreWithNoFiles()) {
  296. ErrorExit("TestBackupRestoreWithNoFiles() failure.\n");
  297. }
  298. // Test backup/restore with no files and no overwriting of files.
  299. if (!TestBackupRestoreWithNoFilesNoOverwrite()) {
  300. ErrorExit("TestBackupRestoreWithNoFilesNoOverwrite() failure.\n");
  301. }
  302. } // That's all folks.
  303. catch(std::exception& e) { // Report any normal exceptions.
  304. std::cerr << "FileBackup exception: " << e.what() << std::endl;
  305. return -1;
  306. }
  307. catch(...) { // Report any unexpected exceptions.
  308. std::cerr << "Panic! Unknown Exception!" << std::endl;
  309. return -1;
  310. }
  311. return 0; // Normally we return zero.
  312. }