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 <stddef.h>
32 : : #include <stdarg.h>
33 : : #include <setjmp.h>
34 : : #include <cmocka.h>
35 : : #include <errno.h>
36 : :
37 : : #include <cbt-util-priv.h>
38 : : #include <wrappers.h>
39 : : #include "test-suites.h"
40 : :
41 : 1 : void test_cbt_util_create_success(void **state)
42 : : {
43 : : int result;
44 : : int file_size;
45 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
46 : : void *log_meta;
47 : :
48 : 1 : file_size = 4194304 + sizeof(struct cbt_log_metadata);
49 : :
50 : 1 : log_meta = test_malloc(file_size);
51 : 1 : FILE *test_log = fmemopen(log_meta, file_size, "w+");
52 : :
53 : 1 : will_return(__wrap_fopen, test_log);
54 : 1 : expect_value(__wrap_fclose, fp, test_log);
55 : :
56 : 1 : result = cbt_util_create(5, args);
57 : :
58 : 1 : assert_int_equal(result, 0);
59 : 1 : assert_int_equal(((struct cbt_log_metadata*)log_meta)->consistent, 0);
60 : :
61 : 1 : test_free(log_meta);
62 : 1 : }
63 : :
64 : 1 : void test_cbt_util_create_file_open_failure(void **state)
65 : : {
66 : : int result;
67 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
68 : :
69 : 1 : will_return(__wrap_fopen, NULL);
70 : : /* TODO: Could do with this then making another call to mock to get the error code */
71 : :
72 : 1 : result = cbt_util_create(5, args);
73 : :
74 : 1 : assert_int_equal(result, -ENOENT);
75 : 1 : }
76 : :
77 : 1 : void test_cbt_util_create_metadata_write_failure(void **state)
78 : : {
79 : : int result;
80 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
81 : : void *log_meta[1];
82 : :
83 : 1 : FILE *test_log = fmemopen(log_meta, 1, "w+");
84 : :
85 : : /* Make IO errors happen immediately, not on flush */
86 : 1 : setbuf(test_log, NULL);
87 : :
88 : 1 : will_return(__wrap_fopen, test_log);
89 : 1 : expect_value(__wrap_fclose, fp, test_log);
90 : :
91 : 1 : result = cbt_util_create(5, args);
92 : :
93 : 1 : assert_int_equal(result, -EIO);
94 : 1 : }
95 : :
96 : 1 : void test_cbt_util_create_bitmap_write_failure(void **state)
97 : : {
98 : : int result;
99 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
100 : : void *log_meta;
101 : :
102 : 1 : log_meta = test_malloc(sizeof(struct cbt_log_metadata));
103 : :
104 : 1 : FILE *test_log = fmemopen(log_meta, sizeof(struct cbt_log_metadata), "w+");
105 : :
106 : : /* Make IO errors happen immediately, not on flush */
107 : 1 : setbuf(test_log, NULL);
108 : :
109 : 1 : will_return(__wrap_fopen, test_log);
110 : 1 : expect_value(__wrap_fclose, fp, test_log);
111 : :
112 : 1 : result = cbt_util_create(5, args);
113 : :
114 : 1 : assert_int_equal(result, -EIO);
115 : :
116 : 1 : test_free(log_meta);
117 : 1 : }
118 : :
119 : 1 : void test_cbt_util_create_log_data_allocation_failure(void **state)
120 : : {
121 : : int result;
122 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
123 : :
124 : 1 : malloc_succeeds(false);
125 : :
126 : 1 : result = cbt_util_create(5, args);
127 : :
128 : 1 : assert_int_equal(result, -ENOMEM);
129 : :
130 : 1 : disable_malloc_mock();
131 : 1 : }
132 : :
133 : 1 : void test_cbt_util_create_bitmap_allocation_failure(void **state)
134 : : {
135 : : int result;
136 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log", "-s", "4194304" };
137 : :
138 : 1 : malloc_succeeds(true);
139 : 1 : malloc_succeeds(false);
140 : :
141 : 1 : result = cbt_util_create(5, args);
142 : :
143 : 1 : assert_int_equal(result, -ENOMEM);
144 : :
145 : 1 : disable_malloc_mock();
146 : 1 : }
147 : :
148 : 1 : void test_cbt_util_create_no_name_failure(void **state)
149 : : {
150 : : int result;
151 : 1 : char* args[] = { "cbt-util", "-s", "4194304" };
152 : : struct printf_data *output;
153 : :
154 : 1 : output = setup_vprintf_mock(1024);
155 : :
156 : 1 : result = cbt_util_create(3, args);
157 : :
158 : 1 : assert_int_equal(result, -EINVAL);
159 : :
160 : 1 : free_printf_data(output);
161 : 1 : }
162 : :
163 : 1 : void test_cbt_util_create_no_size_failure(void **state)
164 : : {
165 : : int result;
166 : 1 : char* args[] = { "cbt-util", "-n", "test_disk.log" };
167 : : struct printf_data *output;
168 : :
169 : 1 : output = setup_vprintf_mock(1024);
170 : :
171 : 1 : result = cbt_util_create(3, args);
172 : :
173 : 1 : assert_int_equal(result, -EINVAL);
174 : :
175 : 1 : free_printf_data(output);
176 : 1 : }
177 : :
|