Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #include <stdarg.h>
32 : : #include <stddef.h>
33 : : #include <setjmp.h>
34 : : #include <cmocka.h>
35 : :
36 : : #include "libvhd.h"
37 : : #include "vhd-wrappers.h"
38 : :
39 : : static int cookie = 0;
40 : : static int close_count = 0;
41 : : static int *open_return_errs = NULL;
42 : : static int n_return_errs = 0;
43 : : static int open_call_count = 0;
44 : :
45 : : static bool mock_malloc = false;
46 : : static bool use_real_allocator = false;
47 : :
48 : 2 : void reset_flags()
49 : : {
50 : 2 : close_count = 0;
51 : 2 : cookie = 0;
52 : 2 : open_return_errs = NULL;
53 : 2 : n_return_errs = 0;
54 : 2 : open_call_count = 0;
55 : 2 : }
56 : :
57 : 5 : int get_close_count()
58 : : {
59 : 5 : return close_count;
60 : : }
61 : :
62 : 1 : void set_open_errors(int nerrs, int* errs)
63 : : {
64 : 1 : open_return_errs = errs;
65 : 1 : n_return_errs = nerrs;
66 : 1 : }
67 : :
68 : 2 : void set_cookie()
69 : : {
70 : 2 : cookie = 1;
71 : 2 : }
72 : :
73 : 5 : char *__wrap_canonpath(const char *path, char *resolved_path)
74 : : {
75 : 5 : return (char*)mock();
76 : : }
77 : :
78 : 5 : int __wrap_vhd_snapshot(const char *snapshot, uint64_t bytes, const char *parent,
79 : : uint64_t mbytes, uint32_t flag)
80 : : {
81 : 5 : return (int)mock();
82 : : }
83 : :
84 : 6 : int __wrap_vhd_open(vhd_context_t *ctx, const char *file, int flags)
85 : : {
86 : : int ret;
87 [ + + ]: 6 : if(open_return_errs) {
88 : 2 : ret = open_return_errs[open_call_count];
89 : :
90 : : } else {
91 : 4 : ret = (int)mock();
92 : : }
93 : 6 : open_call_count++;
94 : 6 : return ret;
95 : : }
96 : :
97 : 3 : int __wrap_vhd_get_keyhash(vhd_context_t *ctx, struct vhd_keyhash* hash)
98 : : {
99 [ + + ]: 3 : if(cookie) {
100 : 2 : hash->cookie = 1;
101 : : }
102 : 3 : return (int)mock();
103 : : }
104 : :
105 : 4 : int __wrap_vhd_close(vhd_context_t *ctx)
106 : : {
107 : 4 : check_expected(ctx);
108 : 4 : close_count++;
109 : 4 : return (int)mock();
110 : : }
111 : :
112 : 1 : int __wrap_vhd_set_keyhash(vhd_context_t *ctx, struct vhd_keyhash* hash)
113 : : {
114 : 1 : return (int)mock();
115 : : }
116 : :
117 : : void *
118 : : __real_malloc(size_t size);
119 : :
120 : : void *
121 : 0 : __wrap_malloc(size_t size)
122 : : {
123 [ # # ]: 0 : if (!use_real_allocator) {
124 : 0 : bool succeed = true;
125 [ # # ]: 0 : if (mock_malloc) {
126 : 0 : succeed = (bool) mock();
127 : : }
128 [ # # ]: 0 : if (succeed) {
129 : 0 : void * result = test_malloc(size);
130 : : /*fprintf(stderr, "Allocated block of %zu bytes at %p\n", size, result);*/
131 : 0 : return result;
132 : : }
133 : : return NULL;
134 : : }
135 : 0 : return __real_malloc(size);
136 : : }
137 : :
138 : : void *
139 : 1 : __wrap_realloc(void *ptr, size_t size)
140 : : {
141 : 1 : bool succeed = true;
142 [ - + ]: 1 : if (mock_malloc) {
143 : 0 : succeed = (bool) mock();
144 : : }
145 [ + - ]: 1 : if (succeed) {
146 : 1 : void * result = test_realloc(ptr, size);
147 : : /*fprintf(stderr, "Reallocated %p to %zu bytes at %p\n", ptr, size, result);*/
148 : 1 : return result;
149 : : }
150 : : return NULL;
151 : : }
152 : :
153 : :
154 : : void __real_free(void *ptr);
155 : :
156 : : void
157 : 6 : __wrap_free(void *ptr)
158 : : {
159 [ + + ]: 6 : if (!use_real_allocator) {
160 : : /*fprintf(stderr, "Freeing block at %p\n", ptr);*/
161 : 1 : test_free(ptr);
162 : : } else {
163 : 5 : check_expected(ptr);
164 : 5 : __real_free(ptr);
165 : : }
166 : 6 : }
167 : :
168 : 1 : char *__wrap_get_current_dir_name(void)
169 : : {
170 : 1 : return (char *)mock();
171 : : }
172 : :
173 : 1 : char *__wrap_realpath(const char *path, char *resolved_path)
174 : : {
175 : 1 : return (char *)mock();
176 : : }
177 : :
178 : :
179 : 2 : void set_use_real_allocator(bool val)
180 : : {
181 : 2 : use_real_allocator = val;
182 : 2 : }
183 : :
|