]> git.proxmox.com Git - libgit2.git/blob - tests/buf/oom.c
New upstream version 1.3.0+dfsg.1
[libgit2.git] / tests / buf / oom.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3
4 /* Override default allocators with ones that will fail predictably. */
5
6 static git_allocator std_alloc;
7 static git_allocator oom_alloc;
8
9 static void *oom_malloc(size_t n, const char *file, int line)
10 {
11 /* Reject any allocation of more than 100 bytes */
12 return (n > 100) ? NULL : std_alloc.gmalloc(n, file, line);
13 }
14
15 static void *oom_realloc(void *p, size_t n, const char *file, int line)
16 {
17 /* Reject any allocation of more than 100 bytes */
18 return (n > 100) ? NULL : std_alloc.grealloc(p, n, file, line);
19 }
20
21 void test_buf_oom__initialize(void)
22 {
23 git_stdalloc_init_allocator(&std_alloc);
24 git_stdalloc_init_allocator(&oom_alloc);
25
26 oom_alloc.gmalloc = oom_malloc;
27 oom_alloc.grealloc = oom_realloc;
28
29 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, &oom_alloc));
30 }
31
32 void test_buf_oom__cleanup(void)
33 {
34 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_ALLOCATOR, NULL));
35 }
36
37 void test_buf_oom__grow(void)
38 {
39 git_buf buf = GIT_BUF_INIT;
40
41 cl_git_pass(git_buf_grow(&buf, 42));
42 cl_assert(!git_buf_oom(&buf));
43
44 cl_assert(git_buf_grow(&buf, 101) == -1);
45 cl_assert(git_buf_oom(&buf));
46
47 git_buf_dispose(&buf);
48 }
49
50 void test_buf_oom__grow_by(void)
51 {
52 git_buf buf = GIT_BUF_INIT;
53
54 cl_git_pass(git_buf_grow_by(&buf, 42));
55 cl_assert(!git_buf_oom(&buf));
56
57 cl_assert(git_buf_grow_by(&buf, 101) == -1);
58 cl_assert(git_buf_oom(&buf));
59 }