C++ Library to handle BagIt structures. BagIt is a standard format to create transfer packages for digital preservation purposes. See https://en.wikipedia.org/wiki/BagIt for details http://andreas-romeyke.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.3 KiB

  1. #define BOOST_TEST_MODULE Mytest
  2. #include <boost/test/unit_test.hpp>
  3. #include <utility>
  4. #include <iostream>
  5. #include "bag.hpp"
  6. using namespace std;
  7. #include <boost/filesystem.hpp>
  8. namespace fs = boost::filesystem;
  9. struct TARGETFIXTURE {
  10. TARGETFIXTURE() : targetdir("/tmp/testdir") { BOOST_TEST_MESSAGE("setup targetdir"); }
  11. ~TARGETFIXTURE() {
  12. fs::path f { targetdir };
  13. fs::remove_all( f );
  14. BOOST_TEST_MESSAGE("teardown targetdir");
  15. }
  16. string targetdir;
  17. };
  18. bool check_constructor(string dir) {
  19. try {
  20. Bag p( std::move(dir) );
  21. return true;
  22. } catch (exception &e) {
  23. return false;
  24. }
  25. }
  26. bool check_validation(string dir) {
  27. Bag p( std::move(dir) );
  28. return p.validate();
  29. }
  30. bool check_store( string dir_load_from, string dir_store_to ) {
  31. Bag p( std::move(dir_load_from) );
  32. bool result = p.store ( dir_store_to );
  33. // clean dir_store_to
  34. fs::path f { dir_store_to };
  35. fs::remove_all( f );
  36. return result;
  37. }
  38. bool check_get_all_bag_files(string dir) {
  39. Bag p( std::move(dir) );
  40. list<string> results = p.get_all_bag_files();
  41. list<string> expected {
  42. "bagit.txt",
  43. "bag-info.txt"
  44. };
  45. bool is_valid = true;
  46. list<string>::iterator i;
  47. list<string>::iterator j;
  48. for (i=results.begin(), j=expected.begin(); i!=results.end() && j!=expected.end(); i++, j++) {
  49. if ((*i) != (*j)) {
  50. cout << "Expected: "<< (*j) << " Result:"<< (*i) << endl;
  51. is_valid = false;
  52. break;
  53. }
  54. }
  55. return is_valid;
  56. }
  57. BOOST_AUTO_TEST_CASE(constructor1) {
  58. BOOST_TEST(check_constructor(""));
  59. };
  60. BOOST_AUTO_TEST_CASE(constructor2) {
  61. BOOST_TEST(check_constructor("./"));
  62. };
  63. BOOST_AUTO_TEST_CASE(constructor3) {
  64. BOOST_TEST(check_constructor("../testbags/bag_minimal_ok/"));
  65. };
  66. BOOST_AUTO_TEST_CASE(constructor3_old) {
  67. BOOST_TEST(check_constructor("../testbags/bag_minimal_v0.97/"));
  68. };
  69. BOOST_AUTO_TEST_CASE(check_validation_ok) {
  70. BOOST_TEST(check_validation("../testbags/bag_minimal_ok/"));
  71. };
  72. BOOST_AUTO_TEST_CASE(check_validation_ok_old) {
  73. BOOST_TEST(check_validation("../testbags/bag_minimal_v0.97/"));
  74. };
  75. BOOST_AUTO_TEST_CASE(check_validation_buggy) {
  76. BOOST_TEST(!check_validation("../testbags/1008_buggy/"));
  77. };
  78. BOOST_AUTO_TEST_CASE(check_get_all_bag_files_ok) {
  79. BOOST_TEST(check_get_all_bag_files("../testbags/bag_minimal_ok"));
  80. };
  81. BOOST_AUTO_TEST_CASE(check_get_all_bag_files_ok_old) {
  82. BOOST_TEST(check_get_all_bag_files("../testbags/bag_minimal_v0.97"));
  83. };
  84. void test_valid_bagit_dirs () {
  85. list<string> valid_dirs {
  86. "valid_md5",
  87. "valid_md5_invalid_sha1_invalid_sha256_invalid_sha512",
  88. "valid_md5_invalid_sha1_invalid_sha256_valid_sha512",
  89. "valid_md5_invalid_sha1_valid_sha256_invalid_sha512",
  90. "valid_md5_invalid_sha1_valid_sha256_valid_sha512",
  91. "valid_md5_valid_sha1_invalid_sha256_invalid_sha512",
  92. "valid_md5_valid_sha1_invalid_sha256_valid_sha512",
  93. "valid_md5_valid_sha1_valid_sha256_invalid_sha512",
  94. "valid_md5_valid_sha1_valid_sha256_valid_sha512",
  95. "valid_multiple_valid_checksums",
  96. "valid_sha1",
  97. "valid_sha256",
  98. "valid_sha512",
  99. "valid_with_additional_meta_dir"
  100. };
  101. for (auto i=valid_dirs.begin(); i!=valid_dirs.end(); i++) {
  102. BOOST_TEST(check_validation((*i)));
  103. }
  104. }
  105. void test_invalid_bagit_dirs () {
  106. list<string> invalid_dirs {
  107. "invalid_data_file_in_tagmanifest",
  108. "invalid_duplicate_bagcount",
  109. "invalid_duplicate_bagging_date",
  110. "invalid_duplicate_baggroup",
  111. "invalid_duplicate_bagsize",
  112. "invalid_duplicate_entries_in_manifest",
  113. "invalid_duplicate_entries_in_tagmanifest",
  114. "invalid_duplicate_payload_oxum",
  115. "invalid_empty_manifest_but_entries_in_data",
  116. "invalid_empty_tagmanifest",
  117. "invalid_md5",
  118. "invalid_md5_invalid_sha1_invalid_sha256_invalid_sha512",
  119. "invalid_md5_invalid_sha1_invalid_sha256_valid_sha512",
  120. "invalid_md5_invalid_sha1_valid_sha256_invalid_sha512",
  121. "invalid_md5_invalid_sha1_valid_sha256_valid_sha512",
  122. "invalid_md5_valid_sha1_invalid_sha256_invalid_sha512",
  123. "invalid_md5_valid_sha1_invalid_sha256_valid_sha512",
  124. "invalid_md5_valid_sha1_valid_sha256_invalid_sha512",
  125. "invalid_md5_valid_sha1_valid_sha256_valid_sha512",
  126. "invalid_missed_baginfo",
  127. "invalid_missed_baginfo_in_tagmanifest",
  128. "invalid_missed_bagit",
  129. "invalid_missed_bagit_in_tagmanifest",
  130. "invalid_missed_bagit_version",
  131. "invalid_missed_data_dir",
  132. "invalid_missed_data_subdir",
  133. "invalid_missed_encoding",
  134. "invalid_missed_manifest",
  135. "invalid_missed_manifest_in_tagmanifest",
  136. "invalid_missed_referenced_data_file",
  137. "invalid_missed_referenced_file_in_subdir",
  138. "invalid_missed_tagmanifest",
  139. "invalid_payload_oxum",
  140. "invalid_sha1",
  141. "invalid_sha256",
  142. "invalid_sha512",
  143. "invalid_tagmanifest_files_with_different_entries",
  144. "outdated_bagit_version",
  145. "unreferenced_files_in_bag"
  146. };
  147. for (auto i=invalid_dirs.begin(); i!=invalid_dirs.end(); i++) {
  148. BOOST_TEST(! check_validation((*i)));
  149. }
  150. }
  151. BOOST_AUTO_TEST_CASE(check_valid_dirs) {
  152. check_valid_dirs();
  153. }
  154. BOOST_AUTO_TEST_CASE(check_invalid_dirs) {
  155. check_invalid_dirs();
  156. }
  157. BOOST_FIXTURE_TEST_CASE(check_store_ok, TARGETFIXTURE) {
  158. BOOST_TEST(check_store("../testbags/bag_minimal_ok/", targetdir));
  159. };