errors.c 5.74 KB
Newer Older
1
#include "clar_libgit2.h"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

void test_core_errors__public_api(void)
{
	char *str_in_error;

	giterr_clear();
	cl_assert(giterr_last() == NULL);

	giterr_set_oom();

	cl_assert(giterr_last() != NULL);
	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
	str_in_error = strstr(giterr_last()->message, "memory");
	cl_assert(str_in_error != NULL);

	giterr_clear();

	giterr_set_str(GITERR_REPOSITORY, "This is a test");

	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "This is a test");
	cl_assert(str_in_error != NULL);

	giterr_clear();
	cl_assert(giterr_last() == NULL);
}

29 30 31 32 33 34 35 36
#include "common.h"
#include "util.h"
#include "posix.h"

void test_core_errors__new_school(void)
{
	char *str_in_error;

37 38
	giterr_clear();
	cl_assert(giterr_last() == NULL);
39 40 41

	giterr_set_oom(); /* internal fn */

42 43 44
	cl_assert(giterr_last() != NULL);
	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
	str_in_error = strstr(giterr_last()->message, "memory");
45 46
	cl_assert(str_in_error != NULL);

47
	giterr_clear();
48 49 50

	giterr_set(GITERR_REPOSITORY, "This is a test"); /* internal fn */

51 52
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "This is a test");
53 54
	cl_assert(str_in_error != NULL);

55 56
	giterr_clear();
	cl_assert(giterr_last() == NULL);
57

58
	do {
59
		struct stat st;
60
		memset(&st, 0, sizeof(st));
nulltoken committed
61
		cl_assert(p_lstat("this_file_does_not_exist", &st) < 0);
62
		GIT_UNUSED(st);
63
	} while (false);
64 65
	giterr_set(GITERR_OS, "stat failed"); /* internal fn */

66 67
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "stat failed");
68 69 70 71 72
	cl_assert(str_in_error != NULL);
	cl_assert(git__prefixcmp(str_in_error, "stat failed: ") == 0);
	cl_assert(strlen(str_in_error) > strlen("stat failed: "));

#ifdef GIT_WIN32
73
	giterr_clear();
74 75 76 77 78

	/* The MSDN docs use this to generate a sample error */
	cl_assert(GetProcessId(NULL) == 0);
	giterr_set(GITERR_OS, "GetProcessId failed"); /* internal fn */

79 80
	cl_assert(giterr_last() != NULL);
	str_in_error = strstr(giterr_last()->message, "GetProcessId failed");
81 82 83 84 85
	cl_assert(str_in_error != NULL);
	cl_assert(git__prefixcmp(str_in_error, "GetProcessId failed: ") == 0);
	cl_assert(strlen(str_in_error) > strlen("GetProcessId failed: "));
#endif

86
	giterr_clear();
87
}
Edward Thomson committed
88 89 90 91 92 93 94 95

void test_core_errors__restore(void)
{
	git_error_state err_state = {0};

	giterr_clear();
	cl_assert(giterr_last() == NULL);

96
	cl_assert_equal_i(0, giterr_state_capture(&err_state, 0));
Edward Thomson committed
97 98 99 100

	memset(&err_state, 0x0, sizeof(git_error_state));

	giterr_set(42, "Foo: %s", "bar");
101
	cl_assert_equal_i(-1, giterr_state_capture(&err_state, -1));
Edward Thomson committed
102 103 104 105 106

	cl_assert(giterr_last() == NULL);

	giterr_set(99, "Bar: %s", "foo");

107
	giterr_state_restore(&err_state);
Edward Thomson committed
108 109 110 111

	cl_assert_equal_i(42, giterr_last()->klass);
	cl_assert_equal_s("Foo: bar", giterr_last()->message);
}
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
void test_core_errors__free_state(void)
{
	git_error_state err_state = {0};

	giterr_clear();

	giterr_set(42, "Foo: %s", "bar");
	cl_assert_equal_i(-1, giterr_state_capture(&err_state, -1));

	giterr_set(99, "Bar: %s", "foo");

	giterr_state_free(&err_state);

	cl_assert_equal_i(99, giterr_last()->klass);
	cl_assert_equal_s("Bar: foo", giterr_last()->message);

	giterr_state_restore(&err_state);

	cl_assert(giterr_last() == NULL);
}

134 135 136
void test_core_errors__restore_oom(void)
{
	git_error_state err_state = {0};
137
	const git_error *oom_error = NULL;
138 139 140 141

	giterr_clear();

	giterr_set_oom(); /* internal fn */
142 143
	oom_error = giterr_last();
	cl_assert(oom_error);
144

145
	cl_assert_equal_i(-1, giterr_state_capture(&err_state, -1));
146 147

	cl_assert(giterr_last() == NULL);
148 149
	cl_assert_equal_i(GITERR_NOMEMORY, err_state.error_msg.klass);
	cl_assert_equal_s("Out of memory", err_state.error_msg.message);
150

151
	giterr_state_restore(&err_state);
152 153

	cl_assert(giterr_last()->klass == GITERR_NOMEMORY);
154
	cl_assert_(giterr_last() == oom_error, "static oom error not restored");
155 156 157 158

	giterr_clear();
}

159 160
static int test_arraysize_multiply(size_t nelem, size_t size)
{
161 162
	size_t out;
	GITERR_CHECK_ALLOC_MULTIPLY(&out, nelem, size);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	return 0;
}

void test_core_errors__integer_overflow_alloc_multiply(void)
{
	cl_git_pass(test_arraysize_multiply(10, 10));
	cl_git_pass(test_arraysize_multiply(1000, 1000));
	cl_git_pass(test_arraysize_multiply(SIZE_MAX/sizeof(void *), sizeof(void *)));
	cl_git_pass(test_arraysize_multiply(0, 10));
	cl_git_pass(test_arraysize_multiply(10, 0));

	cl_git_fail(test_arraysize_multiply(SIZE_MAX-1, sizeof(void *)));
	cl_git_fail(test_arraysize_multiply((SIZE_MAX/sizeof(void *))+1, sizeof(void *)));

	cl_assert_equal_i(GITERR_NOMEMORY, giterr_last()->klass);
	cl_assert_equal_s("Out of memory", giterr_last()->message);
}

static int test_arraysize_add(size_t one, size_t two)
{
183 184
	size_t out;
	GITERR_CHECK_ALLOC_ADD(&out, one, two);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	return 0;
}

void test_core_errors__integer_overflow_alloc_add(void)
{
	cl_git_pass(test_arraysize_add(10, 10));
	cl_git_pass(test_arraysize_add(1000, 1000));
	cl_git_pass(test_arraysize_add(SIZE_MAX-10, 10));

	cl_git_fail(test_arraysize_multiply(SIZE_MAX-1, 2));
	cl_git_fail(test_arraysize_multiply(SIZE_MAX, SIZE_MAX));

	cl_assert_equal_i(GITERR_NOMEMORY, giterr_last()->klass);
	cl_assert_equal_s("Out of memory", giterr_last()->message);
}
200 201 202

void test_core_errors__integer_overflow_sets_oom(void)
{
203 204
	size_t out;

205
	giterr_clear();
206
	cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX-1, 1));
207 208 209
	cl_assert_equal_p(NULL, giterr_last());

	giterr_clear();
210
	cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, 42, 69));
211 212 213
	cl_assert_equal_p(NULL, giterr_last());

	giterr_clear();
214
	cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
215 216 217 218
	cl_assert_equal_i(GITERR_NOMEMORY, giterr_last()->klass);
	cl_assert_equal_s("Out of memory", giterr_last()->message);

	giterr_clear();
219
	cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
220 221 222
	cl_assert_equal_i(GITERR_NOMEMORY, giterr_last()->klass);
	cl_assert_equal_s("Out of memory", giterr_last()->message);
}