errors.c 6 KB
Newer Older
1
#include "clar_libgit2.h"
2 3 4 5 6

void test_core_errors__public_api(void)
{
	char *str_in_error;

7 8
	git_error_clear();
	cl_assert(git_error_last() == NULL);
9

10
	git_error_set_oom();
11

12 13 14
	cl_assert(git_error_last() != NULL);
	cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY);
	str_in_error = strstr(git_error_last()->message, "memory");
15 16
	cl_assert(str_in_error != NULL);

17
	git_error_clear();
18

19
	git_error_set_str(GIT_ERROR_REPOSITORY, "This is a test");
20

21 22
	cl_assert(git_error_last() != NULL);
	str_in_error = strstr(git_error_last()->message, "This is a test");
23 24
	cl_assert(str_in_error != NULL);

25 26
	git_error_clear();
	cl_assert(git_error_last() == NULL);
27 28
}

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
	git_error_clear();
	cl_assert(git_error_last() == NULL);
39

40
	git_error_set_oom(); /* internal fn */
41

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

47
	git_error_clear();
48

49
	git_error_set(GIT_ERROR_REPOSITORY, "This is a test"); /* internal fn */
50

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

55 56
	git_error_clear();
	cl_assert(git_error_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
	git_error_set(GIT_ERROR_OS, "stat failed"); /* internal fn */
65

66 67
	cl_assert(git_error_last() != NULL);
	str_in_error = strstr(git_error_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
	git_error_clear();
74 75 76

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

79 80
	cl_assert(git_error_last() != NULL);
	str_in_error = strstr(git_error_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
	git_error_clear();
87
}
Edward Thomson committed
88 89 90 91 92

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

93 94
	git_error_clear();
	cl_assert(git_error_last() == NULL);
Edward Thomson committed
95

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

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

100 101
	git_error_set(42, "Foo: %s", "bar");
	cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
Edward Thomson committed
102

103
	cl_assert(git_error_last() == NULL);
Edward Thomson committed
104

105
	git_error_set(99, "Bar: %s", "foo");
Edward Thomson committed
106

107
	git_error_state_restore(&err_state);
Edward Thomson committed
108

109 110
	cl_assert_equal_i(42, git_error_last()->klass);
	cl_assert_equal_s("Foo: bar", git_error_last()->message);
Edward Thomson committed
111
}
112

113 114 115 116
void test_core_errors__free_state(void)
{
	git_error_state err_state = {0};

117
	git_error_clear();
118

119 120
	git_error_set(42, "Foo: %s", "bar");
	cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
121

122
	git_error_set(99, "Bar: %s", "foo");
123

124
	git_error_state_free(&err_state);
125

126 127
	cl_assert_equal_i(99, git_error_last()->klass);
	cl_assert_equal_s("Bar: foo", git_error_last()->message);
128

129
	git_error_state_restore(&err_state);
130

131
	cl_assert(git_error_last() == NULL);
132 133
}

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
	git_error_clear();
140

141 142
	git_error_set_oom(); /* internal fn */
	oom_error = git_error_last();
143
	cl_assert(oom_error);
144

145
	cl_assert_equal_i(-1, git_error_state_capture(&err_state, -1));
146

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

151
	git_error_state_restore(&err_state);
152

153 154
	cl_assert(git_error_last()->klass == GIT_ERROR_NOMEMORY);
	cl_assert_(git_error_last() == oom_error, "static oom error not restored");
155

156
	git_error_clear();
157 158
}

159 160
static int test_arraysize_multiply(size_t nelem, size_t size)
{
161
	size_t out;
162
	GIT_ERROR_CHECK_ALLOC_MULTIPLY(&out, nelem, size);
163 164 165 166 167 168 169 170 171 172 173 174 175 176
	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 *)));

177 178
	cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
	cl_assert_equal_s("Out of memory", git_error_last()->message);
179 180 181 182
}

static int test_arraysize_add(size_t one, size_t two)
{
183
	size_t out;
184
	GIT_ERROR_CHECK_ALLOC_ADD(&out, one, two);
185 186 187 188 189 190 191 192 193 194 195 196
	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));

197 198
	cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
	cl_assert_equal_s("Out of memory", git_error_last()->message);
199
}
200 201 202

void test_core_errors__integer_overflow_sets_oom(void)
{
203 204
	size_t out;

205
	git_error_clear();
206
	cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX-1, 1));
207
	cl_assert_equal_p(NULL, git_error_last());
208

209
	git_error_clear();
210
	cl_assert(!GIT_ADD_SIZET_OVERFLOW(&out, 42, 69));
211
	cl_assert_equal_p(NULL, git_error_last());
212

213
	git_error_clear();
214
	cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
215 216
	cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
	cl_assert_equal_s("Out of memory", git_error_last()->message);
217

218
	git_error_clear();
219
	cl_assert(GIT_ADD_SIZET_OVERFLOW(&out, SIZE_MAX, SIZE_MAX));
220 221
	cl_assert_equal_i(GIT_ERROR_NOMEMORY, git_error_last()->klass);
	cl_assert_equal_s("Out of memory", git_error_last()->message);
222
}