/tests/t03-objwrite.c

https://github.com/thepian/libgit2 · C · 255 lines · 183 code · 48 blank · 24 comment · 25 complexity · 65a28b2285ed9e996a2fffa59c0fbca7 MD5 · raw file

  1. /*
  2. * This file is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2,
  4. * as published by the Free Software Foundation.
  5. *
  6. * In addition to the permissions in the GNU General Public License,
  7. * the authors give you unlimited permission to link the compiled
  8. * version of this file into combinations with other programs,
  9. * and to distribute those combinations without any restriction
  10. * coming from the use of this file. (The General Public License
  11. * restrictions do apply in other respects; for example, they cover
  12. * modification of the file, and distribution when not linked into
  13. * a combined executable.)
  14. *
  15. * This file is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301, USA.
  24. */
  25. #include "test_lib.h"
  26. #include "fileops.h"
  27. #include "odb.h"
  28. static char *odb_dir = "test-objects";
  29. #include "t03-data.h"
  30. static int make_odb_dir(void)
  31. {
  32. if (p_mkdir(odb_dir, 0755) < 0) {
  33. int err = errno;
  34. fprintf(stderr, "can't make directory \"%s\"", odb_dir);
  35. if (err == EEXIST)
  36. fprintf(stderr, " (already exists)");
  37. fprintf(stderr, "\n");
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. static int check_object_files(object_data *d)
  43. {
  44. if (git_futils_exists(d->dir) < 0)
  45. return -1;
  46. if (git_futils_exists(d->file) < 0)
  47. return -1;
  48. return 0;
  49. }
  50. static int cmp_objects(git_rawobj *o1, git_rawobj *o2)
  51. {
  52. if (o1->type != o2->type)
  53. return -1;
  54. if (o1->len != o2->len)
  55. return -1;
  56. if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0))
  57. return -1;
  58. return 0;
  59. }
  60. static int remove_object_files(object_data *d)
  61. {
  62. if (p_unlink(d->file) < 0) {
  63. fprintf(stderr, "can't delete object file \"%s\"\n", d->file);
  64. return -1;
  65. }
  66. if ((p_rmdir(d->dir) < 0) && (errno != ENOTEMPTY)) {
  67. fprintf(stderr, "can't remove directory \"%s\"\n", d->dir);
  68. return -1;
  69. }
  70. if (p_rmdir(odb_dir) < 0) {
  71. fprintf(stderr, "can't remove directory \"%s\"\n", odb_dir);
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static int streaming_write(git_oid *oid, git_odb *odb, git_rawobj *raw)
  77. {
  78. git_odb_stream *stream;
  79. int error;
  80. if ((error = git_odb_open_wstream(&stream, odb, raw->len, raw->type)) < GIT_SUCCESS)
  81. return error;
  82. stream->write(stream, raw->data, raw->len);
  83. error = stream->finalize_write(oid, stream);
  84. stream->free(stream);
  85. return error;
  86. }
  87. BEGIN_TEST(write0, "write loose commit object")
  88. git_odb *db;
  89. git_oid id1, id2;
  90. git_odb_object *obj;
  91. must_pass(make_odb_dir());
  92. must_pass(git_odb_open(&db, odb_dir));
  93. must_pass(git_oid_fromstr(&id1, commit.id));
  94. must_pass(streaming_write(&id2, db, &commit_obj));
  95. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  96. must_pass(check_object_files(&commit));
  97. must_pass(git_odb_read(&obj, db, &id1));
  98. must_pass(cmp_objects(&obj->raw, &commit_obj));
  99. git_odb_object_close(obj);
  100. git_odb_close(db);
  101. must_pass(remove_object_files(&commit));
  102. END_TEST
  103. BEGIN_TEST(write1, "write loose tree object")
  104. git_odb *db;
  105. git_oid id1, id2;
  106. git_odb_object *obj;
  107. must_pass(make_odb_dir());
  108. must_pass(git_odb_open(&db, odb_dir));
  109. must_pass(git_oid_fromstr(&id1, tree.id));
  110. must_pass(streaming_write(&id2, db, &tree_obj));
  111. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  112. must_pass(check_object_files(&tree));
  113. must_pass(git_odb_read(&obj, db, &id1));
  114. must_pass(cmp_objects(&obj->raw, &tree_obj));
  115. git_odb_object_close(obj);
  116. git_odb_close(db);
  117. must_pass(remove_object_files(&tree));
  118. END_TEST
  119. BEGIN_TEST(write2, "write loose tag object")
  120. git_odb *db;
  121. git_oid id1, id2;
  122. git_odb_object *obj;
  123. must_pass(make_odb_dir());
  124. must_pass(git_odb_open(&db, odb_dir));
  125. must_pass(git_oid_fromstr(&id1, tag.id));
  126. must_pass(streaming_write(&id2, db, &tag_obj));
  127. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  128. must_pass(check_object_files(&tag));
  129. must_pass(git_odb_read(&obj, db, &id1));
  130. must_pass(cmp_objects(&obj->raw, &tag_obj));
  131. git_odb_object_close(obj);
  132. git_odb_close(db);
  133. must_pass(remove_object_files(&tag));
  134. END_TEST
  135. BEGIN_TEST(write3, "write zero-length object")
  136. git_odb *db;
  137. git_oid id1, id2;
  138. git_odb_object *obj;
  139. must_pass(make_odb_dir());
  140. must_pass(git_odb_open(&db, odb_dir));
  141. must_pass(git_oid_fromstr(&id1, zero.id));
  142. must_pass(streaming_write(&id2, db, &zero_obj));
  143. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  144. must_pass(check_object_files(&zero));
  145. must_pass(git_odb_read(&obj, db, &id1));
  146. must_pass(cmp_objects(&obj->raw, &zero_obj));
  147. git_odb_object_close(obj);
  148. git_odb_close(db);
  149. must_pass(remove_object_files(&zero));
  150. END_TEST
  151. BEGIN_TEST(write4, "write one-byte long object")
  152. git_odb *db;
  153. git_oid id1, id2;
  154. git_odb_object *obj;
  155. must_pass(make_odb_dir());
  156. must_pass(git_odb_open(&db, odb_dir));
  157. must_pass(git_oid_fromstr(&id1, one.id));
  158. must_pass(streaming_write(&id2, db, &one_obj));
  159. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  160. must_pass(check_object_files(&one));
  161. must_pass(git_odb_read(&obj, db, &id1));
  162. must_pass(cmp_objects(&obj->raw, &one_obj));
  163. git_odb_object_close(obj);
  164. git_odb_close(db);
  165. must_pass(remove_object_files(&one));
  166. END_TEST
  167. BEGIN_TEST(write5, "write two-byte long object")
  168. git_odb *db;
  169. git_oid id1, id2;
  170. git_odb_object *obj;
  171. must_pass(make_odb_dir());
  172. must_pass(git_odb_open(&db, odb_dir));
  173. must_pass(git_oid_fromstr(&id1, two.id));
  174. must_pass(streaming_write(&id2, db, &two_obj));
  175. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  176. must_pass(check_object_files(&two));
  177. must_pass(git_odb_read(&obj, db, &id1));
  178. must_pass(cmp_objects(&obj->raw, &two_obj));
  179. git_odb_object_close(obj);
  180. git_odb_close(db);
  181. must_pass(remove_object_files(&two));
  182. END_TEST
  183. BEGIN_TEST(write6, "write an object which is several bytes long")
  184. git_odb *db;
  185. git_oid id1, id2;
  186. git_odb_object *obj;
  187. must_pass(make_odb_dir());
  188. must_pass(git_odb_open(&db, odb_dir));
  189. must_pass(git_oid_fromstr(&id1, some.id));
  190. must_pass(streaming_write(&id2, db, &some_obj));
  191. must_be_true(git_oid_cmp(&id1, &id2) == 0);
  192. must_pass(check_object_files(&some));
  193. must_pass(git_odb_read(&obj, db, &id1));
  194. must_pass(cmp_objects(&obj->raw, &some_obj));
  195. git_odb_object_close(obj);
  196. git_odb_close(db);
  197. must_pass(remove_object_files(&some));
  198. END_TEST
  199. BEGIN_SUITE(objwrite)
  200. ADD_TEST(write0);
  201. ADD_TEST(write1);
  202. ADD_TEST(write2);
  203. ADD_TEST(write3);
  204. ADD_TEST(write4);
  205. ADD_TEST(write5);
  206. ADD_TEST(write6);
  207. END_SUITE