Commit 2add34d0 by Patrick Steinhardt

tests: odb: move fake backend into its own file

The fake backend used by the test suite `odb::backend::nonrefreshing` is
useful to have some low-level tests for the ODB layer. As such, we move
the implementation into its own `backend_helpers` module.
parent 9927e958
#include "clar_libgit2.h"
#include "git2/sys/odb_backend.h"
#include "backend_helpers.h"
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->exists_calls++;
return (fake->error_code == 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)
{
fake_backend *fake;
GIT_UNUSED(buffer_p);
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->read_calls++;
*len_p = 0;
*buffer_p = NULL;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
static int fake_backend__read_header(
size_t *len_p, git_otype *type_p,
git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->read_header_calls++;
*len_p = 0;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
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)
{
fake_backend *fake;
GIT_UNUSED(buffer_p);
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(short_oid);
GIT_UNUSED(len);
fake = (fake_backend *)backend;
fake->read_prefix_calls++;
git_oid_cpy(out_oid, &fake->oid);
*len_p = 0;
*buffer_p = NULL;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
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,
git_error_code error_code,
const git_oid *oid)
{
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->error_code = error_code;
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;
git_oid_cpy(&backend->oid, oid);
*out = (git_odb_backend *)backend;
return 0;
}
#include "git2/sys/odb_backend.h"
typedef struct {
git_odb_backend parent;
git_error_code error_code;
git_oid oid;
int exists_calls;
int read_calls;
int read_header_calls;
int read_prefix_calls;
} fake_backend;
int build_fake_backend(
git_odb_backend **out,
git_error_code error_code,
const git_oid *oid);
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "git2/sys/odb_backend.h"
#include "repository.h" #include "repository.h"
#include "backend_helpers.h"
typedef struct fake_backend {
git_odb_backend parent;
git_error_code error_code;
int exists_calls;
int read_calls;
int read_header_calls;
int read_prefix_calls;
} fake_backend;
static git_repository *_repo; static git_repository *_repo;
static fake_backend *_fake; static fake_backend *_fake;
static git_oid _oid;
#define HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" #define HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" #define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->exists_calls++;
return (fake->error_code == 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)
{
fake_backend *fake;
GIT_UNUSED(buffer_p);
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->read_calls++;
*len_p = 0;
*buffer_p = NULL;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
static int fake_backend__read_header(
size_t *len_p, git_otype *type_p,
git_odb_backend *backend, const git_oid *oid)
{
fake_backend *fake;
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(oid);
fake = (fake_backend *)backend;
fake->read_header_calls++;
*len_p = 0;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
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)
{
fake_backend *fake;
GIT_UNUSED(buffer_p);
GIT_UNUSED(len_p);
GIT_UNUSED(type_p);
GIT_UNUSED(short_oid);
GIT_UNUSED(len);
fake = (fake_backend *)backend;
fake->read_prefix_calls++;
git_oid_cpy(out_oid, &_oid);
*len_p = 0;
*buffer_p = NULL;
*type_p = GIT_OBJ_BLOB;
return fake->error_code;
}
static void fake_backend__free(git_odb_backend *_backend)
{
fake_backend *backend;
backend = (fake_backend *)_backend;
git__free(backend);
}
static int build_fake_backend(
git_odb_backend **out,
git_error_code error_code)
{
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->error_code = error_code;
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;
}
static void setup_repository_and_backend(git_error_code error_code, const char *hash) static void setup_repository_and_backend(git_error_code error_code, const char *hash)
{ {
git_odb *odb = NULL; git_odb *odb = NULL;
git_odb_backend *backend = NULL; git_odb_backend *backend = NULL;
git_oid oid;
_repo = cl_git_sandbox_init("testrepo.git"); _repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(build_fake_backend(&backend, error_code)); cl_git_pass(git_oid_fromstr(&oid, hash));
cl_git_pass(build_fake_backend(&backend, error_code, &oid));
cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_add_backend(odb, backend, 10)); cl_git_pass(git_odb_add_backend(odb, backend, 10));
_fake = (fake_backend *)backend; _fake = (fake_backend *)backend;
cl_git_pass(git_oid_fromstr(&_oid, hash));
} }
void test_odb_backend_nonrefreshing__cleanup(void) void test_odb_backend_nonrefreshing__cleanup(void)
...@@ -162,7 +37,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void) ...@@ -162,7 +37,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
setup_repository_and_backend(GIT_ENOTFOUND, HASH); setup_repository_and_backend(GIT_ENOTFOUND, HASH);
cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(false, git_odb_exists(odb, &_oid)); cl_assert_equal_b(false, git_odb_exists(odb, &_fake->oid));
cl_assert_equal_i(1, _fake->exists_calls); cl_assert_equal_i(1, _fake->exists_calls);
} }
...@@ -174,7 +49,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void) ...@@ -174,7 +49,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
setup_repository_and_backend(GIT_ENOTFOUND, HASH); setup_repository_and_backend(GIT_ENOTFOUND, HASH);
cl_git_fail_with( cl_git_fail_with(
git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY), git_object_lookup(&obj, _repo, &_fake->oid, GIT_OBJ_ANY),
GIT_ENOTFOUND); GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_calls); cl_assert_equal_i(1, _fake->read_calls);
...@@ -187,7 +62,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void) ...@@ -187,7 +62,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
setup_repository_and_backend(GIT_ENOTFOUND, HASH); setup_repository_and_backend(GIT_ENOTFOUND, HASH);
cl_git_fail_with( cl_git_fail_with(
git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY), git_object_lookup_prefix(&obj, _repo, &_fake->oid, 7, GIT_OBJ_ANY),
GIT_ENOTFOUND); GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_prefix_calls); cl_assert_equal_i(1, _fake->read_prefix_calls);
...@@ -204,7 +79,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void) ...@@ -204,7 +79,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_fail_with( cl_git_fail_with(
git_odb_read_header(&len, &type, odb, &_oid), git_odb_read_header(&len, &type, odb, &_fake->oid),
GIT_ENOTFOUND); GIT_ENOTFOUND);
cl_assert_equal_i(1, _fake->read_header_calls); cl_assert_equal_i(1, _fake->read_header_calls);
...@@ -217,7 +92,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void) ...@@ -217,7 +92,7 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
setup_repository_and_backend(GIT_OK, HASH); setup_repository_and_backend(GIT_OK, HASH);
cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_assert_equal_b(true, git_odb_exists(odb, &_oid)); cl_assert_equal_b(true, git_odb_exists(odb, &_fake->oid));
cl_assert_equal_i(1, _fake->exists_calls); cl_assert_equal_i(1, _fake->exists_calls);
} }
...@@ -228,7 +103,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void) ...@@ -228,7 +103,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
setup_repository_and_backend(GIT_OK, EMPTY_HASH); setup_repository_and_backend(GIT_OK, EMPTY_HASH);
cl_git_pass(git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY)); cl_git_pass(git_object_lookup(&obj, _repo, &_fake->oid, GIT_OBJ_ANY));
cl_assert_equal_i(1, _fake->read_calls); cl_assert_equal_i(1, _fake->read_calls);
...@@ -241,7 +116,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void) ...@@ -241,7 +116,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
setup_repository_and_backend(GIT_OK, EMPTY_HASH); setup_repository_and_backend(GIT_OK, EMPTY_HASH);
cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY)); cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_fake->oid, 7, GIT_OBJ_ANY));
cl_assert_equal_i(1, _fake->read_prefix_calls); cl_assert_equal_i(1, _fake->read_prefix_calls);
...@@ -258,7 +133,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void) ...@@ -258,7 +133,7 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
cl_git_pass(git_repository_odb__weakptr(&odb, _repo)); cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
cl_git_pass(git_odb_read_header(&len, &type, odb, &_oid)); cl_git_pass(git_odb_read_header(&len, &type, odb, &_fake->oid));
cl_assert_equal_i(1, _fake->read_header_calls); cl_assert_equal_i(1, _fake->read_header_calls);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment