libgit2/tests/odb/backend/backend_helpers.c
Patrick Steinhardt 6e010bb126 tests: odb: allow passing fake objects to the fake backend
Right now, the fake backend is quite restrained in the way how it
works: we pass it an OID which it is to return later as well as an error
code we want it to return. While this is sufficient for existing tests,
we can make the fake backend a little bit more generic in order to allow
us testing for additional scenarios.

To do so, we change the backend to not accept an error code and OID
which it is to return for queries, but instead a simple array of OIDs
with their respective blob contents. On each query, the fake backend
simply iterates through this array and returns the first matching
object.
2017-06-12 17:24:53 +02:00

133 lines
2.7 KiB
C

#include "clar_libgit2.h"
#include "git2/sys/odb_backend.h"
#include "backend_helpers.h"
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
{
const fake_object *obj = fake->objects;
while (obj && obj->oid) {
git_oid current_oid;
git_oid_fromstr(&current_oid, obj->oid);
if (git_oid_ncmp(&current_oid, oid, len) == 0) {
if (out)
*out = obj;
return 0;
}
obj++;
}
return GIT_ENOTFOUND;
}
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
fake = (fake_backend *)backend;
fake->exists_calls++;
return search_object(NULL, fake, oid, GIT_OID_RAWSZ) == GIT_OK;
}
static int fake_backend__read(
void **buffer_p, size_t *len_p, git_otype *type_p,
git_odb_backend *backend, const git_oid *oid)
{
const fake_object *obj;
fake_backend *fake;
fake = (fake_backend *)backend;
fake->read_calls++;
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
*len_p = strlen(obj->content);
*buffer_p = git__strdup(obj->content);
*type_p = GIT_OBJ_BLOB;
return 0;
}
return GIT_ENOTFOUND;
}
static int fake_backend__read_header(
size_t *len_p, git_otype *type_p,
git_odb_backend *backend, const git_oid *oid)
{
const fake_object *obj;
fake_backend *fake;
fake = (fake_backend *)backend;
fake->read_header_calls++;
if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
*len_p = strlen(obj->content);
*type_p = GIT_OBJ_BLOB;
return 0;
}
return GIT_ENOTFOUND;
}
static int fake_backend__read_prefix(
git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
git_odb_backend *backend, const git_oid *short_oid, size_t len)
{
const fake_object *obj;
fake_backend *fake;
fake = (fake_backend *)backend;
fake->read_prefix_calls++;
if (search_object(&obj, fake, short_oid, len) == 0) {
git_oid_fromstr(out_oid, obj->oid);
*len_p = strlen(obj->content);
*buffer_p = git__strdup(obj->content);
*type_p = GIT_OBJ_BLOB;
return 0;
}
return GIT_ENOTFOUND;
}
static void fake_backend__free(git_odb_backend *_backend)
{
fake_backend *backend;
backend = (fake_backend *)_backend;
git__free(backend);
}
int build_fake_backend(
git_odb_backend **out,
const fake_object *objects)
{
fake_backend *backend;
backend = git__calloc(1, sizeof(fake_backend));
GITERR_CHECK_ALLOC(backend);
backend->parent.version = GIT_ODB_BACKEND_VERSION;
backend->parent.refresh = NULL;
backend->objects = objects;
backend->parent.read = fake_backend__read;
backend->parent.read_prefix = fake_backend__read_prefix;
backend->parent.read_header = fake_backend__read_header;
backend->parent.exists = fake_backend__exists;
backend->parent.free = &fake_backend__free;
*out = (git_odb_backend *)backend;
return 0;
}