|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "repository.h" |
| 3 | +#include "backend_helpers.h" |
| 4 | + |
| 5 | +#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" |
| 6 | + |
| 7 | +static git_repository *_repo; |
| 8 | +static git_odb *_odb; |
| 9 | +static git_odb_object *_obj; |
| 10 | +static git_oid _oid; |
| 11 | + |
| 12 | +static void setup_backend(const fake_object *objs) |
| 13 | +{ |
| 14 | + git_odb_backend *backend; |
| 15 | + |
| 16 | + cl_git_pass(build_fake_backend(&backend, objs)); |
| 17 | + |
| 18 | + cl_git_pass(git_repository_odb__weakptr(&_odb, _repo)); |
| 19 | + cl_git_pass(git_odb_add_backend(_odb, backend, 10)); |
| 20 | +} |
| 21 | + |
| 22 | +static void assert_object_contains(git_odb_object *obj, const char *expected) |
| 23 | +{ |
| 24 | + const char *actual = (const char *) git_odb_object_data(obj); |
| 25 | + |
| 26 | + cl_assert_equal_s(actual, expected); |
| 27 | +} |
| 28 | + |
| 29 | +void test_odb_backend_simple__initialize(void) |
| 30 | +{ |
| 31 | + _repo = cl_git_sandbox_init("testrepo.git"); |
| 32 | + _odb = NULL; |
| 33 | + _obj = NULL; |
| 34 | +} |
| 35 | + |
| 36 | +void test_odb_backend_simple__cleanup(void) |
| 37 | +{ |
| 38 | + git_odb_object_free(_obj); |
| 39 | + cl_git_sandbox_cleanup(); |
| 40 | + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1)); |
| 41 | +} |
| 42 | + |
| 43 | +void test_odb_backend_simple__read_of_object_succeeds(void) |
| 44 | +{ |
| 45 | + const fake_object objs[] = { |
| 46 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 47 | + { NULL, NULL } |
| 48 | + }; |
| 49 | + |
| 50 | + setup_backend(objs); |
| 51 | + |
| 52 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 53 | + cl_git_pass(git_odb_read(&_obj, _odb, &_oid)); |
| 54 | + |
| 55 | + assert_object_contains(_obj, objs[0].content); |
| 56 | +} |
| 57 | + |
| 58 | +void test_odb_backend_simple__read_of_nonexisting_object_fails(void) |
| 59 | +{ |
| 60 | + const fake_object objs[] = { |
| 61 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 62 | + { NULL, NULL } |
| 63 | + }; |
| 64 | + |
| 65 | + setup_backend(objs); |
| 66 | + |
| 67 | + cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633")); |
| 68 | + cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid)); |
| 69 | +} |
| 70 | + |
| 71 | +void test_odb_backend_simple__read_with_hash_mismatch_fails(void) |
| 72 | +{ |
| 73 | + const fake_object objs[] = { |
| 74 | + { "1234567890123456789012345678901234567890", "nonmatching content" }, |
| 75 | + { NULL, NULL } |
| 76 | + }; |
| 77 | + |
| 78 | + setup_backend(objs); |
| 79 | + |
| 80 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 81 | + cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid)); |
| 82 | +} |
| 83 | + |
| 84 | +void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void) |
| 85 | +{ |
| 86 | + const fake_object objs[] = { |
| 87 | + { "1234567890123456789012345678901234567890", "nonmatching content" }, |
| 88 | + { NULL, NULL } |
| 89 | + }; |
| 90 | + |
| 91 | + setup_backend(objs); |
| 92 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 93 | + |
| 94 | + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)); |
| 95 | + cl_git_pass(git_odb_read(&_obj, _odb, &_oid)); |
| 96 | + |
| 97 | + assert_object_contains(_obj, objs[0].content); |
| 98 | +} |
| 99 | + |
| 100 | +void test_odb_backend_simple__read_prefix_succeeds(void) |
| 101 | +{ |
| 102 | + const fake_object objs[] = { |
| 103 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 104 | + { NULL, NULL } |
| 105 | + }; |
| 106 | + |
| 107 | + setup_backend(objs); |
| 108 | + |
| 109 | + cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632")); |
| 110 | + cl_git_pass(git_odb_read(&_obj, _odb, &_oid)); |
| 111 | + |
| 112 | + assert_object_contains(_obj, objs[0].content); |
| 113 | +} |
| 114 | + |
| 115 | +void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void) |
| 116 | +{ |
| 117 | + const fake_object objs[] = { |
| 118 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 119 | + { NULL, NULL } |
| 120 | + }; |
| 121 | + char *hash = "f6ea0495187600e8"; |
| 122 | + |
| 123 | + setup_backend(objs); |
| 124 | + |
| 125 | + cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash))); |
| 126 | + cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid)); |
| 127 | +} |
| 128 | + |
| 129 | +void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void) |
| 130 | +{ |
| 131 | + const fake_object objs[] = { |
| 132 | + { "1234567890111111111111111111111111111111", "first content" }, |
| 133 | + { "1234567890222222222222222222222222222222", "second content" }, |
| 134 | + { NULL, NULL } |
| 135 | + }; |
| 136 | + |
| 137 | + setup_backend(objs); |
| 138 | + |
| 139 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 140 | + cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7)); |
| 141 | +} |
| 142 | + |
| 143 | +void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void) |
| 144 | +{ |
| 145 | + const fake_object objs[] = { |
| 146 | + { "1234567890111111111111111111111111111111", "first content" }, |
| 147 | + { "1234567890111111111111111111111111111112", "second content" }, |
| 148 | + { NULL, NULL } |
| 149 | + }; |
| 150 | + |
| 151 | + setup_backend(objs); |
| 152 | + |
| 153 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 154 | + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)); |
| 155 | + cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39)); |
| 156 | + cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40)); |
| 157 | + assert_object_contains(_obj, objs[0].content); |
| 158 | +} |
| 159 | + |
| 160 | +void test_odb_backend_simple__exists_succeeds(void) |
| 161 | +{ |
| 162 | + const fake_object objs[] = { |
| 163 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 164 | + { NULL, NULL } |
| 165 | + }; |
| 166 | + |
| 167 | + setup_backend(objs); |
| 168 | + |
| 169 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 170 | + cl_assert(git_odb_exists(_odb, &_oid)); |
| 171 | +} |
| 172 | + |
| 173 | +void test_odb_backend_simple__exists_fails_for_nonexisting_object(void) |
| 174 | +{ |
| 175 | + const fake_object objs[] = { |
| 176 | + { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" }, |
| 177 | + { NULL, NULL } |
| 178 | + }; |
| 179 | + |
| 180 | + setup_backend(objs); |
| 181 | + |
| 182 | + cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633")); |
| 183 | + cl_assert(git_odb_exists(_odb, &_oid) == 0); |
| 184 | +} |
| 185 | + |
| 186 | +void test_odb_backend_simple__exists_prefix_succeeds(void) |
| 187 | +{ |
| 188 | + const fake_object objs[] = { |
| 189 | + { "1234567890111111111111111111111111111111", "first content" }, |
| 190 | + { "1234567890222222222222222222222222222222", "second content" }, |
| 191 | + { NULL, NULL } |
| 192 | + }; |
| 193 | + git_oid found; |
| 194 | + |
| 195 | + setup_backend(objs); |
| 196 | + |
| 197 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 198 | + cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12)); |
| 199 | + cl_assert(git_oid_equal(&found, &_oid)); |
| 200 | +} |
| 201 | + |
| 202 | +void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void) |
| 203 | +{ |
| 204 | + const fake_object objs[] = { |
| 205 | + { "1234567890111111111111111111111111111111", "first content" }, |
| 206 | + { "1234567890222222222222222222222222222222", "second content" }, |
| 207 | + { NULL, NULL } |
| 208 | + }; |
| 209 | + |
| 210 | + setup_backend(objs); |
| 211 | + |
| 212 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 213 | + cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7)); |
| 214 | +} |
| 215 | + |
| 216 | +void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void) |
| 217 | +{ |
| 218 | + const fake_object objs[] = { |
| 219 | + { "1234567890111111111111111111111111111111", "first content" }, |
| 220 | + { "1234567890111111111111111111111111111112", "second content" }, |
| 221 | + { NULL, NULL } |
| 222 | + }; |
| 223 | + git_oid found; |
| 224 | + |
| 225 | + setup_backend(objs); |
| 226 | + |
| 227 | + cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid)); |
| 228 | + cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0)); |
| 229 | + cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39)); |
| 230 | + cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40)); |
| 231 | + cl_assert(git_oid_equal(&found, &_oid)); |
| 232 | +} |
0 commit comments