Commit 953427b3 by Edward Thomson Committed by GitHub

Merge pull request #4269 from pks-t/pks/tests

Test improvements
parents 2ca088bd a180e7d9
......@@ -468,9 +468,6 @@ IF (MSVC)
SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
SET(WIN_RC "src/win32/git2.rc")
# Precompiled headers
ELSE ()
SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
......@@ -481,9 +478,7 @@ ELSE ()
SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
ENDIF()
IF (WIN32 AND NOT CYGWIN)
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
ENDIF ()
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
......
......@@ -165,4 +165,9 @@ void test_core_structinit__compare(void)
CHECK_MACRO_FUNC_INIT_EQUAL( \
git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_init_options);
/* submodule update */
CHECK_MACRO_FUNC_INIT_EQUAL( \
git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
GIT_PROXY_OPTIONS_INIT, git_proxy_init_options);
}
......@@ -2,6 +2,7 @@
#include "buffer.h"
#include "path.h"
#include "posix.h"
#include "git2/sys/repository.h"
static git_repository *repo;
static git_buf file_path_buf = GIT_BUF_INIT;
......
......@@ -4,7 +4,7 @@
static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
{
const fake_object *obj = fake->objects;
const fake_object *obj = fake->objects, *found = NULL;
while (obj && obj->oid) {
git_oid current_oid;
......@@ -12,15 +12,18 @@ static int search_object(const fake_object **out, fake_backend *fake, const git_
git_oid_fromstr(&current_oid, obj->oid);
if (git_oid_ncmp(&current_oid, oid, len) == 0) {
if (out)
*out = obj;
return 0;
if (found)
return GIT_EAMBIGUOUS;
found = obj;
}
obj++;
}
return GIT_ENOTFOUND;
if (found && out)
*out = found;
return found ? GIT_OK : GIT_ENOTFOUND;
}
static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
......@@ -31,7 +34,27 @@ static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
fake->exists_calls++;
return search_object(NULL, fake, oid, GIT_OID_RAWSZ) == GIT_OK;
return search_object(NULL, fake, oid, GIT_OID_HEXSZ) == GIT_OK;
}
static int fake_backend__exists_prefix(
git_oid *out, git_odb_backend *backend, const git_oid *oid, size_t len)
{
const fake_object *obj;
fake_backend *fake;
int error;
fake = (fake_backend *)backend;
fake->exists_prefix_calls++;
if ((error = search_object(&obj, fake, oid, len)) < 0)
return error;
if (out)
git_oid_fromstr(out, obj->oid);
return 0;
}
static int fake_backend__read(
......@@ -40,19 +63,20 @@ static int fake_backend__read(
{
const fake_object *obj;
fake_backend *fake;
int error;
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;
}
if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
return error;
return GIT_ENOTFOUND;
*len_p = strlen(obj->content);
*buffer_p = git__strdup(obj->content);
*type_p = GIT_OBJ_BLOB;
return 0;
}
static int fake_backend__read_header(
......@@ -61,18 +85,19 @@ static int fake_backend__read_header(
{
const fake_object *obj;
fake_backend *fake;
int error;
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;
}
if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
return error;
return GIT_ENOTFOUND;
*len_p = strlen(obj->content);
*type_p = GIT_OBJ_BLOB;
return 0;
}
static int fake_backend__read_prefix(
......@@ -81,20 +106,21 @@ static int fake_backend__read_prefix(
{
const fake_object *obj;
fake_backend *fake;
int error;
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;
}
if ((error = search_object(&obj, fake, short_oid, len)) < 0)
return error;
return GIT_ENOTFOUND;
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;
}
static void fake_backend__free(git_odb_backend *_backend)
......@@ -124,6 +150,7 @@ int build_fake_backend(
backend->parent.read_prefix = fake_backend__read_prefix;
backend->parent.read_header = fake_backend__read_header;
backend->parent.exists = fake_backend__exists;
backend->parent.exists_prefix = fake_backend__exists_prefix;
backend->parent.free = &fake_backend__free;
*out = (git_odb_backend *)backend;
......
......@@ -9,6 +9,7 @@ typedef struct {
git_odb_backend parent;
int exists_calls;
int exists_prefix_calls;
int read_calls;
int read_header_calls;
int read_prefix_calls;
......
#include "clar_libgit2.h"
#include "repository.h"
#include "backend_helpers.h"
#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
static git_repository *_repo;
static git_odb *_odb;
static git_odb_object *_obj;
static git_oid _oid;
static void setup_backend(const fake_object *objs)
{
git_odb_backend *backend;
cl_git_pass(build_fake_backend(&backend, objs));
cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
cl_git_pass(git_odb_add_backend(_odb, backend, 10));
}
static void assert_object_contains(git_odb_object *obj, const char *expected)
{
const char *actual = (const char *) git_odb_object_data(obj);
cl_assert_equal_s(actual, expected);
}
void test_odb_backend_simple__initialize(void)
{
_repo = cl_git_sandbox_init("testrepo.git");
_odb = NULL;
_obj = NULL;
}
void test_odb_backend_simple__cleanup(void)
{
git_odb_object_free(_obj);
cl_git_sandbox_cleanup();
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
}
void test_odb_backend_simple__read_of_object_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
{
const fake_object objs[] = {
{ "1234567890123456789012345678901234567890", "nonmatching content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
{
const fake_object objs[] = {
{ "1234567890123456789012345678901234567890", "nonmatching content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_prefix_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632"));
cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
char *hash = "f6ea0495187600e8";
setup_backend(objs);
cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash)));
cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
}
void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7));
}
void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890111111111111111111111111111112", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39));
cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40));
assert_object_contains(_obj, objs[0].content);
}
void test_odb_backend_simple__exists_succeeds(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_assert(git_odb_exists(_odb, &_oid));
}
void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
{
const fake_object objs[] = {
{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
cl_assert(git_odb_exists(_odb, &_oid) == 0);
}
void test_odb_backend_simple__exists_prefix_succeeds(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
git_oid found;
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12));
cl_assert(git_oid_equal(&found, &_oid));
}
void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890222222222222222222222222222222", "second content" },
{ NULL, NULL }
};
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7));
}
void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
{
const fake_object objs[] = {
{ "1234567890111111111111111111111111111111", "first content" },
{ "1234567890111111111111111111111111111112", "second content" },
{ NULL, NULL }
};
git_oid found;
setup_backend(objs);
cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39));
cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40));
cl_assert(git_oid_equal(&found, &_oid));
}
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