git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / unit-tests / u-odb-inmemory.c
| 1 | #include "unit-test.h" |
| 2 | #include "hex.h" |
| 3 | #include "object-file.h" |
| 4 | #include "odb/source-inmemory.h" |
| 5 | #include "odb/streaming.h" |
| 6 | #include "oidset.h" |
| 7 | #include "repository.h" |
| 8 | #include "strbuf.h" |
| 9 | |
| 10 | #define RANDOM_OID "da39a3ee5e6b4b0d3255bfef95601890afd80709" |
| 11 | #define FOOBAR_OID "f6ea0495187600e7b2288c8ac19c5886383a4632" |
| 12 | |
| 13 | static struct repository repo = { |
| 14 | .hash_algo = &hash_algos[GIT_HASH_SHA1], |
| 15 | .object_storage_format = OBJECT_STORAGE_FORMAT_FILES, |
| 16 | }; |
| 17 | static struct object_database *odb; |
| 18 | |
| 19 | static void cl_assert_object_info(struct odb_source_inmemory *source, |
| 20 | const struct object_id *oid, |
| 21 | enum object_type expected_type, |
| 22 | const char *expected_content) |
| 23 | { |
| 24 | enum object_type actual_type; |
| 25 | size_t actual_size; |
| 26 | void *actual_content; |
| 27 | struct object_info oi = { |
| 28 | .typep = &actual_type, |
| 29 | .sizep = &actual_size, |
| 30 | .contentp = &actual_content, |
| 31 | }; |
| 32 | |
| 33 | cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0, NULL)); |
| 34 | cl_assert_equal_u(actual_size, strlen(expected_content)); |
| 35 | cl_assert_equal_u(actual_type, expected_type); |
| 36 | cl_assert_equal_s((char *) actual_content, expected_content); |
| 37 | |
| 38 | free(actual_content); |
| 39 | } |
| 40 | |
| 41 | static void cl_assert_write_object(struct odb_source_inmemory *source, |
| 42 | const char *content, |
| 43 | enum object_type type, |
| 44 | struct object_id *oid) |
| 45 | { |
| 46 | size_t content_len = strlen(content); |
| 47 | hash_object_file(repo.hash_algo, content, content_len, type, oid); |
| 48 | cl_must_pass(odb_source_write_object(&source->base, content, content_len, |
| 49 | type, oid, NULL, NULL, 0)); |
| 50 | } |
| 51 | |
| 52 | void test_odb_inmemory__initialize(void) |
| 53 | { |
| 54 | odb = odb_new(&repo, 0); |
| 55 | } |
| 56 | |
| 57 | void test_odb_inmemory__cleanup(void) |
| 58 | { |
| 59 | odb_free(odb); |
| 60 | } |
| 61 | |
| 62 | void test_odb_inmemory__new(void) |
| 63 | { |
| 64 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 65 | cl_assert_equal_i(source->base.type, ODB_SOURCE_INMEMORY); |
| 66 | odb_source_free(&source->base); |
| 67 | } |
| 68 | |
| 69 | void test_odb_inmemory__read_missing_object(void) |
| 70 | { |
| 71 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 72 | struct object_id oid; |
| 73 | const char *end; |
| 74 | |
| 75 | cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo)); |
| 76 | cl_assert_equal_i(odb_source_read_object_info(&source->base, &oid, NULL, 0, NULL), |
| 77 | ODB_READ_NOT_FOUND); |
| 78 | |
| 79 | odb_source_free(&source->base); |
| 80 | } |
| 81 | |
| 82 | void test_odb_inmemory__read_empty_tree(void) |
| 83 | { |
| 84 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 85 | cl_assert_object_info(source, repo.hash_algo->empty_tree, OBJ_TREE, ""); |
| 86 | odb_source_free(&source->base); |
| 87 | } |
| 88 | |
| 89 | void test_odb_inmemory__read_written_object(void) |
| 90 | { |
| 91 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 92 | const char data[] = "foobar"; |
| 93 | struct object_id written_oid; |
| 94 | |
| 95 | cl_assert_write_object(source, data, OBJ_BLOB, &written_oid); |
| 96 | cl_assert_equal_s(oid_to_hex(&written_oid), FOOBAR_OID); |
| 97 | cl_assert_object_info(source, &written_oid, OBJ_BLOB, "foobar"); |
| 98 | |
| 99 | odb_source_free(&source->base); |
| 100 | } |
| 101 | |
| 102 | void test_odb_inmemory__read_stream_object(void) |
| 103 | { |
| 104 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 105 | struct odb_stream *stream; |
| 106 | struct object_id written_oid; |
| 107 | const char data[] = "foobar"; |
| 108 | char buf[3] = { 0 }; |
| 109 | |
| 110 | cl_assert_write_object(source, data, OBJ_BLOB, &written_oid); |
| 111 | |
| 112 | cl_must_pass(odb_source_read_object_stream(&stream, &source->base, |
| 113 | &written_oid)); |
| 114 | cl_assert_equal_i(stream->type, OBJ_BLOB); |
| 115 | cl_assert_equal_u(stream->size, 6); |
| 116 | |
| 117 | cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2); |
| 118 | cl_assert_equal_s(buf, "fo"); |
| 119 | cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2); |
| 120 | cl_assert_equal_s(buf, "ob"); |
| 121 | cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2); |
| 122 | cl_assert_equal_s(buf, "ar"); |
| 123 | cl_assert_equal_i(odb_stream_read(stream, buf, 2), 0); |
| 124 | |
| 125 | odb_stream_close(stream); |
| 126 | odb_source_free(&source->base); |
| 127 | } |
| 128 | |
| 129 | static int add_one_object(const struct object_id *oid, |
| 130 | struct object_info *oi UNUSED, |
| 131 | void *payload) |
| 132 | { |
| 133 | struct oidset *actual_oids = payload; |
| 134 | cl_must_pass(oidset_insert(actual_oids, oid)); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | void test_odb_inmemory__for_each_object(void) |
| 139 | { |
| 140 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 141 | struct odb_for_each_object_options opts = { 0 }; |
| 142 | struct oidset expected_oids = OIDSET_INIT; |
| 143 | struct oidset actual_oids = OIDSET_INIT; |
| 144 | struct strbuf buf = STRBUF_INIT; |
| 145 | |
| 146 | cl_must_pass(odb_source_for_each_object(&source->base, NULL, |
| 147 | add_one_object, &actual_oids, &opts)); |
| 148 | cl_assert_equal_u(oidset_size(&actual_oids), 0); |
| 149 | |
| 150 | for (int i = 0; i < 10; i++) { |
| 151 | struct object_id written_oid; |
| 152 | |
| 153 | strbuf_reset(&buf); |
| 154 | strbuf_addf(&buf, "%d", i); |
| 155 | |
| 156 | cl_assert_write_object(source, buf.buf, OBJ_BLOB, &written_oid); |
| 157 | cl_must_pass(oidset_insert(&expected_oids, &written_oid)); |
| 158 | } |
| 159 | |
| 160 | cl_must_pass(odb_source_for_each_object(&source->base, NULL, |
| 161 | add_one_object, &actual_oids, &opts)); |
| 162 | cl_assert_equal_b(oidset_equal(&expected_oids, &actual_oids), true); |
| 163 | |
| 164 | odb_source_free(&source->base); |
| 165 | oidset_clear(&expected_oids); |
| 166 | oidset_clear(&actual_oids); |
| 167 | strbuf_release(&buf); |
| 168 | } |
| 169 | |
| 170 | static int abort_after_two_objects(const struct object_id *oid UNUSED, |
| 171 | struct object_info *oi UNUSED, |
| 172 | void *payload) |
| 173 | { |
| 174 | unsigned *counter = payload; |
| 175 | (*counter)++; |
| 176 | if (*counter == 2) |
| 177 | return 123; |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | void test_odb_inmemory__for_each_object_can_abort_iteration(void) |
| 182 | { |
| 183 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 184 | struct odb_for_each_object_options opts = { 0 }; |
| 185 | struct object_id written_oid; |
| 186 | unsigned counter = 0; |
| 187 | |
| 188 | cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid); |
| 189 | cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid); |
| 190 | cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid); |
| 191 | |
| 192 | cl_assert_equal_i(odb_source_for_each_object(&source->base, NULL, |
| 193 | abort_after_two_objects, |
| 194 | &counter, &opts), |
| 195 | 123); |
| 196 | cl_assert_equal_u(counter, 2); |
| 197 | |
| 198 | odb_source_free(&source->base); |
| 199 | } |
| 200 | |
| 201 | void test_odb_inmemory__count_objects(void) |
| 202 | { |
| 203 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 204 | struct object_id written_oid; |
| 205 | unsigned long count; |
| 206 | |
| 207 | cl_must_pass(odb_source_count_objects(&source->base, 0, &count)); |
| 208 | cl_assert_equal_u(count, 0); |
| 209 | |
| 210 | cl_assert_write_object(source, "1", OBJ_BLOB, &written_oid); |
| 211 | cl_assert_write_object(source, "2", OBJ_BLOB, &written_oid); |
| 212 | cl_assert_write_object(source, "3", OBJ_BLOB, &written_oid); |
| 213 | |
| 214 | cl_must_pass(odb_source_count_objects(&source->base, 0, &count)); |
| 215 | cl_assert_equal_u(count, 3); |
| 216 | |
| 217 | odb_source_free(&source->base); |
| 218 | } |
| 219 | |
| 220 | void test_odb_inmemory__find_abbrev_len(void) |
| 221 | { |
| 222 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 223 | struct object_id oid1, oid2; |
| 224 | unsigned abbrev_len; |
| 225 | |
| 226 | /* |
| 227 | * The two blobs we're about to write share the first 10 hex characters |
| 228 | * of their object IDs ("a09f43dc45"), so at least 11 characters are |
| 229 | * needed to tell them apart: |
| 230 | * |
| 231 | * "368317" -> a09f43dc4562d45115583f5094640ae237df55f7 |
| 232 | * "514796" -> a09f43dc45fef837235eb7e6b1a6ca5e169a3981 |
| 233 | * |
| 234 | * With only one blob written we expect a length of 4. |
| 235 | */ |
| 236 | cl_assert_write_object(source, "368317", OBJ_BLOB, &oid1); |
| 237 | cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4, |
| 238 | &abbrev_len)); |
| 239 | cl_assert_equal_u(abbrev_len, 4); |
| 240 | |
| 241 | /* |
| 242 | * With both objects present, the shared 10-character prefix means we |
| 243 | * need at least 11 characters to uniquely identify either object. |
| 244 | */ |
| 245 | cl_assert_write_object(source, "514796", OBJ_BLOB, &oid2); |
| 246 | cl_must_pass(odb_source_find_abbrev_len(&source->base, &oid1, 4, |
| 247 | &abbrev_len)); |
| 248 | cl_assert_equal_u(abbrev_len, 11); |
| 249 | |
| 250 | odb_source_free(&source->base); |
| 251 | } |
| 252 | |
| 253 | void test_odb_inmemory__freshen_object(void) |
| 254 | { |
| 255 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 256 | struct object_id written_oid; |
| 257 | struct object_id oid; |
| 258 | const char *end; |
| 259 | |
| 260 | cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo)); |
| 261 | cl_assert_equal_i(odb_source_freshen_object(&source->base, &oid, NULL), 0); |
| 262 | |
| 263 | cl_assert_write_object(source, "foobar", OBJ_BLOB, &written_oid); |
| 264 | cl_assert_equal_i(odb_source_freshen_object(&source->base, |
| 265 | &written_oid, NULL), 1); |
| 266 | |
| 267 | odb_source_free(&source->base); |
| 268 | } |
| 269 | |
| 270 | struct membuf_write_stream { |
| 271 | struct odb_stream base; |
| 272 | const char *buf; |
| 273 | size_t offset; |
| 274 | }; |
| 275 | |
| 276 | static ssize_t membuf_write_stream_read(struct odb_stream *stream, |
| 277 | char *buf, size_t len) |
| 278 | { |
| 279 | struct membuf_write_stream *s = container_of(stream, struct membuf_write_stream, base); |
| 280 | size_t chunk_size = 2; |
| 281 | |
| 282 | if (s->offset == s->base.size) |
| 283 | return 0; |
| 284 | |
| 285 | if (chunk_size > len) |
| 286 | chunk_size = len; |
| 287 | if (chunk_size > s->base.size - s->offset) |
| 288 | chunk_size = s->base.size - s->offset; |
| 289 | |
| 290 | memcpy(buf, s->buf + s->offset, chunk_size); |
| 291 | |
| 292 | s->offset += chunk_size; |
| 293 | |
| 294 | return chunk_size; |
| 295 | } |
| 296 | |
| 297 | void test_odb_inmemory__write_object_stream(void) |
| 298 | { |
| 299 | struct odb_source_inmemory *source = odb_source_inmemory_new(odb); |
| 300 | const char data[] = "foobar"; |
| 301 | struct membuf_write_stream stream = { |
| 302 | .base = { |
| 303 | .read = membuf_write_stream_read, |
| 304 | .size = strlen(data), |
| 305 | .type = OBJ_BLOB, |
| 306 | }, |
| 307 | .buf = data, |
| 308 | }; |
| 309 | struct object_id written_oid; |
| 310 | |
| 311 | cl_must_pass(odb_source_write_object_stream(&source->base, &stream.base, |
| 312 | &written_oid)); |
| 313 | cl_assert_equal_s(oid_to_hex(&written_oid), FOOBAR_OID); |
| 314 | cl_assert_object_info(source, &written_oid, OBJ_BLOB, "foobar"); |
| 315 | |
| 316 | odb_source_free(&source->base); |
| 317 | } |