Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TestFileBackup.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. remove(FileBackup::GetFailedFileName(*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. std::string
  97. OverwriteFile(std::string File) {
  98. std::ofstream Output;
  99. std::string NewContent;
  100. Output.open(File.c_str(), std::ios::trunc); // Overwrite the test file.
  101. if (!Output) {
  102. std::string Temp;
  103. Temp = "OverwriteFile: Error opening the file " + File;
  104. Temp += " to overwrite: ";
  105. Temp += strerror(errno);
  106. throw std::runtime_error(Temp);
  107. }
  108. for (int j = 0; j < 223; j++) {
  109. NewContent.push_back(RandomChar[static_cast<std::size_t>(std::rand() * (1.0 / (RAND_MAX + 1.0 )) * CharLen)]);
  110. }
  111. Output << NewContent; // Overwrite the test file.
  112. if (Output.bad() || Output.fail()) {
  113. std::string Temp;
  114. Temp = "OverwriteFile: Error overwriting the file " + File;
  115. Temp += ": ";
  116. Temp += strerror(errno);
  117. throw std::runtime_error(Temp);
  118. }
  119. Output.close();
  120. if (!FileBackup::FileExists(File)) {
  121. std::string Temp;
  122. Temp = "OverwriteFile: File " + File;
  123. Temp += " was created but was determined to not exist.\n";
  124. throw std::runtime_error(Temp);
  125. }
  126. return NewContent;
  127. }
  128. bool
  129. CompareContent(std::string FileName, std::string ExpectedContent) {
  130. std::ifstream Input;
  131. Input.open(FileName.c_str());
  132. if (!Input) {
  133. std::string Temp;
  134. Temp = "CompareContent: Error opening " + FileName;
  135. Temp += ": ";
  136. Temp += strerror(errno);
  137. throw std::runtime_error(Temp);
  138. }
  139. std::string ReadContent;
  140. Input >> ReadContent;
  141. Input.close();
  142. if (!Input) {
  143. std::string Temp;
  144. Temp = "CompareContent: Error closing " + FileName;
  145. Temp += ": ";
  146. Temp += strerror(errno);
  147. throw std::runtime_error(Temp);
  148. }
  149. if (ReadContent != ExpectedContent) {
  150. std::string Temp;
  151. Temp = "***Error--File " + FileName;
  152. Temp += " did not have the correct content.\n";
  153. Error(Temp);
  154. return false;
  155. }
  156. return true;
  157. }
  158. bool
  159. TestBackupRestore() {
  160. ContentContainer NewContent;
  161. ContentContainer OverwrittenContent;
  162. std::string Content;
  163. bool ResultIsPass = true;
  164. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  165. TestFileBackup.CreateBackupFile(NoFileName[i]);
  166. Content = OverwriteFile(NoFileName[i]); // Overwrite the file.
  167. NewContent[NoFileName[i]] = Content; // Save the new content.
  168. }
  169. for (int i = 0; i < FileName.size(); i++) { // Back up and overwrite the files that exist.
  170. TestFileBackup.CreateBackupFile(FileName[i]);
  171. Content = OverwriteFile(FileName[i]); // Overwrite the file.
  172. OverwrittenContent[FileName[i]] = Content; // Save the new contents.
  173. }
  174. TestFileBackup.RestoreAllFilesFromBackup();
  175. for (int i = 0; i < NoFileName.size(); i++) {
  176. if (FileBackup::FileExists(NoFileName[i])) { // Check that files don't exist.
  177. std::string Temp;
  178. Temp = "TestBackupRestore: File " + NoFileName[i];
  179. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  180. Temp += "but was determined to exist.\n";
  181. Error(Temp);
  182. ResultIsPass = false;
  183. }
  184. if (!CompareContent(FileBackup::GetFailedFileName(NoFileName[i]), // Check the .failed file content.
  185. NewContent[NoFileName[i]])) {
  186. std::string Temp;
  187. Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
  188. Temp += " create the .failed file.\n";
  189. Error(Temp);
  190. ResultIsPass = false;
  191. }
  192. }
  193. for (int i = 0; i < FileName.size(); i++) {
  194. if (!CompareContent(FileName[i], FileContent[FileName[i]])) { // Check content of restored files.
  195. std::string Temp;
  196. Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
  197. Temp += " restore the file.\n";
  198. Error(Temp);
  199. ResultIsPass = false;
  200. }
  201. if (!CompareContent(FileBackup::GetFailedFileName(FileName[i]), // Check the .failed file content.
  202. OverwrittenContent[FileName[i]])) {
  203. std::string Temp;
  204. Temp = "TestBackupRestore: RestoreAllFilesFromBackup() did not correctly";
  205. Temp += " create the .failed file.\n";
  206. Error(Temp);
  207. ResultIsPass = false;
  208. }
  209. }
  210. return ResultIsPass;
  211. }
  212. bool
  213. TestRemoveAllBackupFiles() {
  214. TestFileBackup.RemoveAllBackupFiles();
  215. std::string BackupFileName;
  216. std::string FailedFileName;
  217. bool ResultIsPass = true;
  218. for (int i = 0; i < NoFileName.size(); i++) {
  219. BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
  220. if (FileBackup::FileExists(BackupFileName)) { // Check that backup files don't exist.
  221. std::string Temp;
  222. Temp = "***Error--Backup file " + BackupFileName;
  223. Temp += " was not removed.\n";
  224. Error(Temp);
  225. ResultIsPass = false;
  226. }
  227. FailedFileName = FileBackup::GetFailedFileName(NoFileName[i]);
  228. if (!FileBackup::FileExists(FailedFileName)) { // Check that .failed files exist.
  229. std::string Temp;
  230. Temp = "***Error--Failed file " + FailedFileName;
  231. Temp += " does not exist.\n";
  232. Error(Temp);
  233. ResultIsPass = false;
  234. }
  235. }
  236. for (int i = 0; i < FileName.size(); i++) {
  237. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  238. if (FileBackup::FileExists(BackupFileName)) { // Check that backup files don't exist.
  239. std::string Temp;
  240. Temp = "***Error--Backup file " + BackupFileName;
  241. Temp += " was not removed.\n";
  242. Error(Temp);
  243. ResultIsPass = false;
  244. }
  245. FailedFileName = FileBackup::GetFailedFileName(FileName[i]);
  246. if (!FileBackup::FileExists(FailedFileName)) { // Check that .failed files exist.
  247. std::string Temp;
  248. Temp = "***Error--Failed file " + FailedFileName;
  249. Temp += " does not exist.\n";
  250. Error(Temp);
  251. ResultIsPass = false;
  252. }
  253. }
  254. return ResultIsPass;
  255. }
  256. bool
  257. TestFileExists() {
  258. bool ResultIsPass = true;
  259. for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
  260. std::string BackupFileName;
  261. BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
  262. if (FileBackup::FileExists(BackupFileName)) {
  263. std::string Temp;
  264. Temp = "***Error--File " + BackupFileName;
  265. Temp += " incorrectly determined to exist.\n";
  266. Error(Temp);
  267. ResultIsPass = false;
  268. }
  269. if (!FileBackup::FileExists(FileName[i])) {
  270. std::string Temp;
  271. Temp = "***Error--File " + FileName[i];
  272. Temp += " incorrectly determined to not exist.\n";
  273. Error(Temp);
  274. ResultIsPass = false;
  275. }
  276. }
  277. return ResultIsPass;
  278. }
  279. bool
  280. TestBackupRestoreWithNoFiles() {
  281. ContentContainer NewContent;
  282. std::string Content;
  283. std::string BackupFileName;
  284. /// Unit under test.
  285. FileBackup TestNoFileBackup;
  286. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  287. TestNoFileBackup.CreateBackupFile(NoFileName[i]);
  288. Content = OverwriteFile(NoFileName[i]); // Overwrite the file.
  289. NewContent[NoFileName[i]] = Content; // Save the new content.
  290. }
  291. TestNoFileBackup.RestoreAllFilesFromBackup();
  292. bool ResultIsPass = true;
  293. for (int i = 0; i < NoFileName.size(); i++) {
  294. if (FileBackup::FileExists(NoFileName[i])) { // Check that file doesn't exist.
  295. std::string Temp;
  296. Temp = "TestBackupRestoreWithNoFiles: File " + NoFileName[i];
  297. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  298. Temp += "but was determined to exist.\n";
  299. Error(Temp);
  300. ResultIsPass = false;
  301. }
  302. BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
  303. if (FileBackup::FileExists(BackupFileName)) { // Check that backup file doesn't exist.
  304. std::string Temp;
  305. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + BackupFileName;
  306. Temp += " was not supposed to be created by CreateBackupFile, ";
  307. Temp += "but was determined to exist.\n";
  308. Error(Temp);
  309. ResultIsPass = false;
  310. }
  311. if (!CompareContent(FileBackup::GetFailedFileName(NoFileName[i]), // Check the .failed file content.
  312. NewContent[NoFileName[i]])) {
  313. std::string Temp;
  314. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: RestoreAllFilesFromBackup() did not correctly";
  315. Temp += " create the .failed file.\n";
  316. Error(Temp);
  317. ResultIsPass = false;
  318. }
  319. }
  320. return ResultIsPass;
  321. }
  322. bool
  323. TestBackupRestoreWithNoFilesNoOverwrite() {
  324. /// Unit under test.
  325. FileBackup TestNoFileBackup;
  326. for (int i = 0; i < NoFileName.size(); i++) { // Backup and overwrite files that don't exist.
  327. TestNoFileBackup.CreateBackupFile(NoFileName[i]);
  328. }
  329. TestNoFileBackup.RestoreAllFilesFromBackup();
  330. bool ResultIsPass = true;
  331. std::string BackupFileName;
  332. std::string FailedFileName;
  333. for (int i = 0; i < NoFileName.size(); i++) {
  334. if (FileBackup::FileExists(NoFileName[i])) { // Check that file don't exist.
  335. std::string Temp;
  336. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + NoFileName[i];
  337. Temp += " was supposed to be deleted by RestoreAllFilesFromBackup, ";
  338. Temp += "but was determined to exist.\n";
  339. Error(Temp);
  340. ResultIsPass = false;
  341. }
  342. BackupFileName = FileBackup::GetBackupFileName(NoFileName[i]);
  343. if (FileBackup::FileExists(BackupFileName)) { // Check that backup file doesn't exist.
  344. std::string Temp;
  345. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + BackupFileName;
  346. Temp += " was not supposed to be created by CreateBackupFile, ";
  347. Temp += "but was determined to exist.\n";
  348. Error(Temp);
  349. ResultIsPass = false;
  350. }
  351. FailedFileName = FileBackup::GetFailedFileName(NoFileName[i]);
  352. if (FileBackup::FileExists(FailedFileName)) { // Check that failed file doesn't exist.
  353. std::string Temp;
  354. Temp = "TestBackupRestoreWithNoFilesNoOverwrite: File " + FailedFileName;
  355. Temp += " was not supposed to be created by RestoreAllFilesFromBackup, ";
  356. Temp += "but was determined to exist.\n";
  357. Error(Temp);
  358. ResultIsPass = false;
  359. }
  360. }
  361. return ResultIsPass;
  362. }
  363. void
  364. Finalize() {
  365. for (int i = 0; i < FileName.size(); i++) {
  366. remove(FileName[i].c_str());
  367. remove((FileBackup::GetBackupFileName(FileName[i]).c_str()));
  368. remove((FileBackup::GetFailedFileName(FileName[i]).c_str()));
  369. }
  370. for (int i = 0; i < NoFileName.size(); i++) {
  371. remove(NoFileName[i].c_str());
  372. remove((FileBackup::GetBackupFileName(NoFileName[i]).c_str()));
  373. remove((FileBackup::GetFailedFileName(NoFileName[i]).c_str()));
  374. }
  375. }
  376. /// Unit tests for FileBackup.
  377. //
  378. // This function creates several files of varying size, and verifies
  379. // the functionality of the FileBackup backup, restore, and delete
  380. // functions.
  381. //
  382. int main(int argc, char* argv[]) {
  383. try { // Catch anything that breaks loose.
  384. Initialize(); // Create test files.
  385. // Test FileExists.
  386. if (!TestFileExists()) {
  387. ErrorExit("TestFileExists() failure.\n");
  388. }
  389. // Test backup/restore.
  390. if (!TestBackupRestore()) {
  391. ErrorExit("TestBackupRestore() failure.\n");
  392. }
  393. // Test cleanup.
  394. if (!TestRemoveAllBackupFiles()) {
  395. ErrorExit("TestRemoveAllBackupFiles() failure.\n");
  396. }
  397. Finalize(); // Remove test files.
  398. // Test backup/restore with no files.
  399. if (!TestBackupRestoreWithNoFiles()) {
  400. ErrorExit("TestBackupRestoreWithNoFiles() failure.\n");
  401. }
  402. // Test backup/restore with no files and no overwriting of files.
  403. if (!TestBackupRestoreWithNoFilesNoOverwrite()) {
  404. ErrorExit("TestBackupRestoreWithNoFilesNoOverwrite() failure.\n");
  405. }
  406. Finalize();
  407. } // That's all folks.
  408. catch(std::exception& e) { // Report any normal exceptions.
  409. std::cerr << "FileBackup exception: " << e.what() << std::endl;
  410. return -1;
  411. }
  412. catch(...) { // Report any unexpected exceptions.
  413. std::cerr << "Panic! Unknown Exception!" << std::endl;
  414. return -1;
  415. }
  416. return 0; // Normally we return zero.
  417. }