Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2017, 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 <stdlib.h>
32 : : #include <string.h>
33 : : #include <stddef.h>
34 : : #include <stdarg.h>
35 : : #include <setjmp.h>
36 : : #include <cmocka.h>
37 : : #include <errno.h>
38 : : #include <uuid/uuid.h>
39 : :
40 : : #include <cbt-util-priv.h>
41 : : #include <wrappers.h>
42 : : #include "test-suites.h"
43 : :
44 : 1 : void test_cbt_util_get_flag(void **state)
45 : : {
46 : : int result;
47 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-f" };
48 : : void *log_meta;
49 : : struct printf_data *output;
50 : :
51 : 1 : log_meta = malloc(sizeof(struct cbt_log_metadata));
52 : :
53 : 1 : ((struct cbt_log_metadata*)log_meta)->consistent = 1;
54 : 1 : FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
55 : :
56 : 1 : will_return(__wrap_fopen, test_log);
57 : 1 : expect_value(__wrap_fclose, fp, test_log);
58 : :
59 : 1 : output = setup_vprintf_mock(1024);
60 : :
61 : 1 : result = cbt_util_get(4, args);
62 : :
63 : 1 : assert_int_equal(result, 0);
64 : 1 : assert_string_equal(output->buf, "1\n");
65 : 1 : free_printf_data(output);
66 : 1 : free(log_meta);
67 : 1 : }
68 : :
69 : 1 : void test_cbt_util_get_parent(void **state)
70 : : {
71 : : int result;
72 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-p" };
73 : : void *log_meta;
74 : : struct printf_data *output;
75 : : uuid_t parent;
76 : : char uuid_str[38];
77 : :
78 : 1 : uuid_generate_random(parent);
79 : :
80 : 1 : log_meta = malloc(sizeof(struct cbt_log_metadata));
81 : :
82 : 1 : uuid_copy(((struct cbt_log_metadata*)log_meta)->parent, parent);
83 : 1 : FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
84 : :
85 : 1 : will_return(__wrap_fopen, test_log);
86 : 1 : expect_value(__wrap_fclose, fp, test_log);
87 : :
88 : 1 : output = setup_vprintf_mock(1024);
89 : :
90 : 1 : result = cbt_util_get(4, args);
91 : :
92 : 1 : assert_int_equal(result, 0);
93 : 1 : uuid_unparse(parent, uuid_str);
94 : : strncat(uuid_str, "\n", 37);
95 : :
96 : 1 : assert_string_equal(output->buf, uuid_str);
97 : 1 : free_printf_data(output);
98 : 1 : free(log_meta);
99 : 1 : }
100 : :
101 : 1 : void test_cbt_util_get_child(void **state)
102 : : {
103 : : int result;
104 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-c" };
105 : : void *log_meta;
106 : : struct printf_data *output;
107 : : uuid_t child;
108 : : char uuid_str[38];
109 : :
110 : 1 : uuid_generate_random(child);
111 : :
112 : 1 : log_meta = malloc(sizeof(struct cbt_log_metadata));
113 : :
114 : 1 : uuid_copy(((struct cbt_log_metadata*)log_meta)->child, child);
115 : 1 : FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
116 : :
117 : 1 : will_return(__wrap_fopen, test_log);
118 : 1 : expect_value(__wrap_fclose, fp, test_log);
119 : :
120 : 1 : output = setup_vprintf_mock(1024);
121 : :
122 : 1 : result = cbt_util_get(4, args);
123 : :
124 : 1 : assert_int_equal(result, 0);
125 : 1 : uuid_unparse(child, uuid_str);
126 : : strncat(uuid_str, "\n", 37);
127 : :
128 : 1 : assert_string_equal(output->buf, uuid_str);
129 : 1 : free_printf_data(output);
130 : 1 : free(log_meta);
131 : 1 : }
132 : :
133 : 1 : void test_cbt_util_get_size(void **state)
134 : : {
135 : : int result;
136 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s" };
137 : : void *log_meta;
138 : : struct printf_data *output;
139 : :
140 : 1 : log_meta = malloc(sizeof(struct cbt_log_metadata));
141 : :
142 : 1 : ((struct cbt_log_metadata*)log_meta)->size = 4194304;
143 : 1 : FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
144 : :
145 : 1 : will_return(__wrap_fopen, test_log);
146 : 1 : expect_value(__wrap_fclose, fp, test_log);
147 : :
148 : 1 : output = setup_vprintf_mock(1024);
149 : :
150 : 1 : result = cbt_util_get(4, args);
151 : :
152 : 1 : assert_int_equal(result, 0);
153 : 1 : assert_string_equal(output->buf, "4194304\n");
154 : 1 : free_printf_data(output);
155 : 1 : free(log_meta);
156 : 1 : }
157 : :
158 : 1 : void test_cbt_util_get_bitmap(void **state)
159 : : {
160 : : int result;
161 : : int file_size;
162 : 1 : char* args[] = { "cbt-util", "get", "-b", "-n", "test_disk.log" };
163 : : void *log_meta;
164 : : struct fwrite_data *output;
165 : 1 : uint64_t size = 4194304;
166 : :
167 : 1 : uint64_t bmsize = bitmap_size(size);
168 : 1 : file_size = sizeof(struct cbt_log_metadata) + bmsize;
169 : 1 : log_meta = malloc(file_size);
170 : :
171 : : //Intialise size in metadata file
172 : 1 : ((struct cbt_log_metadata*)log_meta)->size = size;
173 : : //Fill bitmap with random bytes
174 : 1 : memcpy( log_meta + sizeof(struct cbt_log_metadata), (void*)memcpy, bmsize );
175 : 1 : FILE *test_log = fmemopen((void*)log_meta, file_size, "r");
176 : :
177 : 1 : will_return(__wrap_fopen, test_log);
178 : 1 : expect_value(__wrap_fclose, fp, test_log);
179 : 1 : enable_mock_fwrite();
180 : 1 : output = setup_fwrite_mock(bmsize);
181 : :
182 : 1 : result = cbt_util_get(5, args);
183 : 1 : assert_int_equal(result, 0);
184 : 1 : assert_memory_equal(output->buf, log_meta + sizeof(struct cbt_log_metadata), bmsize);
185 : :
186 : 1 : free_fwrite_data(output);
187 : 1 : free(log_meta);
188 : 1 : }
189 : :
190 : 1 : void test_cbt_util_get_bitmap_nodata_failure(void **state)
191 : : {
192 : :
193 : : int result;
194 : 1 : char* args[] = { "cbt-util", "get", "-b", "-n", "test_disk.log" };
195 : : void *log_meta;
196 : 1 : uint64_t size = 4194304;
197 : :
198 : 1 : log_meta = malloc(sizeof(struct cbt_log_metadata));
199 : : //Intialise size in metadata file
200 : 1 : ((struct cbt_log_metadata*)log_meta)->size = size;
201 : 1 : FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r");
202 : :
203 : 1 : will_return(__wrap_fopen, test_log);
204 : 1 : expect_value(__wrap_fclose, fp, test_log);
205 : :
206 : 1 : result = cbt_util_get(5, args);
207 : 1 : assert_int_equal(result, -EIO);
208 : :
209 : 1 : free(log_meta);
210 : :
211 : 1 : }
212 : :
213 : 1 : void test_cbt_util_get_bitmap_malloc_failure(void **state)
214 : : {
215 : : int result;
216 : : int file_size;
217 : 1 : char* args[] = { "cbt-util", "get", "-b", "-n", "test_disk.log" };
218 : : void *log_meta;
219 : :
220 : 1 : file_size = 4194304 + sizeof(struct cbt_log_metadata);
221 : 1 : log_meta = malloc(file_size);
222 : 1 : FILE *test_log = fmemopen((void*)log_meta, file_size, "r");
223 : :
224 : 1 : will_return(__wrap_fopen, test_log);
225 : 1 : expect_value(__wrap_fclose, fp, test_log);
226 : :
227 : 1 : malloc_succeeds(true);
228 : 1 : malloc_succeeds(false);
229 : :
230 : 1 : result = cbt_util_get(5, args);
231 : 1 : assert_int_equal(result, -ENOMEM);
232 : :
233 : 1 : disable_malloc_mock();
234 : 1 : free(log_meta);
235 : 1 : }
236 : :
237 : 1 : void test_cbt_util_get_no_bitmap_flag_failure(void **state)
238 : : {
239 : : int result;
240 : 1 : char* args[] = { "cbt-util", "get", "-n", "test_disk.log" };
241 : : struct printf_data *output;
242 : :
243 : 1 : output = setup_vprintf_mock(1024);
244 : :
245 : 1 : result = cbt_util_get(4, args);
246 : 1 : assert_int_equal(result, -EINVAL);
247 : 1 : free_printf_data(output);
248 : 1 : }
249 : :
250 : 1 : void test_cbt_util_get_nofile_failure(void **state)
251 : : {
252 : : int result;
253 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-c" };
254 : :
255 : 1 : will_return(__wrap_fopen, NULL);
256 : :
257 : 1 : result = cbt_util_get(4, args);
258 : :
259 : 1 : assert_int_equal(result, -ENOENT);
260 : 1 : }
261 : :
262 : 1 : void test_cbt_util_get_nodata_failure(void **state)
263 : : {
264 : : int result;
265 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-c" };
266 : : void *log_meta[1];
267 : :
268 : 1 : FILE *test_log = fmemopen((void*)log_meta, 1, "r");
269 : :
270 : 1 : will_return(__wrap_fopen, test_log);
271 : 1 : expect_value(__wrap_fclose, fp, test_log);
272 : :
273 : 1 : result = cbt_util_get(4, args);
274 : :
275 : 1 : assert_int_equal(result, -EIO);
276 : 1 : }
277 : :
278 : 1 : void test_cbt_util_get_malloc_failure(void **state)
279 : : {
280 : : int result;
281 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-c" };
282 : :
283 : 1 : malloc_succeeds(false);
284 : :
285 : 1 : result = cbt_util_get(4, args);
286 : 1 : assert_int_equal(result, -ENOMEM);
287 : :
288 : 1 : disable_malloc_mock();
289 : 1 : }
290 : :
291 : 1 : void test_cbt_util_get_no_name_failure(void **state)
292 : : {
293 : : int result;
294 : 1 : char* args[] = { "cbt-util", "-c" };
295 : : struct printf_data *output;
296 : :
297 : 1 : output = setup_vprintf_mock(1024);
298 : :
299 : 1 : result = cbt_util_get(2, args);
300 : 1 : assert_int_equal(result, -EINVAL);
301 : 1 : free_printf_data(output);
302 : 1 : }
303 : :
304 : 1 : void test_cbt_util_get_no_command_failure(void **state)
305 : : {
306 : : int result;
307 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log" };
308 : : struct printf_data *output;
309 : :
310 : 1 : output = setup_vprintf_mock(1024);
311 : :
312 : 1 : result = cbt_util_get(3, args);
313 : 1 : assert_int_equal(result, -EINVAL);
314 : 1 : free_printf_data(output);
315 : 1 : }
|