Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * Unit test for sys_rw.c
5 : *
6 : * Copyright (C) Ralph Böhme 2021
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 3 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License
19 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 : */
21 :
22 : #include <stdarg.h>
23 : #include <stddef.h>
24 : #include <stdint.h>
25 : #include <setjmp.h>
26 : #include <cmocka.h>
27 :
28 : #include "lib/replace/replace.h"
29 : #include "system/dir.h"
30 :
31 : #include "lib/util/sys_rw.c"
32 :
33 1 : static void test_sys_io_ranges_overlap(void **state)
34 : {
35 1 : bool overlap;
36 :
37 : /*
38 : * sys_io_ranges_overlap() args are:
39 : *
40 : * src size, src offset, dst size, dst offset
41 : */
42 :
43 : /* src and dst size 0 => no overlap */
44 1 : overlap = sys_io_ranges_overlap(0, 0, 0, 0);
45 1 : assert_false(overlap);
46 :
47 : /* dst size 0 => no overlap */
48 1 : overlap = sys_io_ranges_overlap(1, 0, 0, 0);
49 1 : assert_false(overlap);
50 :
51 : /* src size 0 => no overlap */
52 1 : overlap = sys_io_ranges_overlap(0, 0, 1, 0);
53 1 : assert_false(overlap);
54 :
55 : /* same range => overlap */
56 1 : overlap = sys_io_ranges_overlap(1, 0, 1, 0);
57 1 : assert_true(overlap);
58 :
59 : /*
60 : * |.|
61 : * |.|
62 : * src before dst => no overlap
63 : */
64 1 : overlap = sys_io_ranges_overlap(1, 0, 1, 1);
65 1 : assert_false(overlap);
66 :
67 : /*
68 : * |..|
69 : * |..|
70 : * src into dst => overlap
71 : */
72 1 : overlap = sys_io_ranges_overlap(2, 0, 2, 1);
73 1 : assert_true(overlap);
74 :
75 : /*
76 : * |....|
77 : * |..|
78 : * src encompasses dst => overlap
79 : */
80 1 : overlap = sys_io_ranges_overlap(4, 0, 1, 2);
81 1 : assert_true(overlap);
82 :
83 :
84 : /*
85 : * |..|
86 : * |..|
87 : * dst into src => overlap
88 : */
89 1 : overlap = sys_io_ranges_overlap(2, 1, 2, 0);
90 1 : assert_true(overlap);
91 :
92 : /*
93 : * |..|
94 : * |....|
95 : * dst encompasses src => overlap
96 : */
97 1 : overlap = sys_io_ranges_overlap(2, 1, 4, 0);
98 1 : assert_true(overlap);
99 1 : }
100 :
101 1 : static void test_sys_block_align(void **state)
102 : {
103 1 : int asize;
104 :
105 1 : asize = sys_block_align(0, 2);
106 1 : assert_int_equal(asize, 0);
107 1 : asize = sys_block_align(1, 2);
108 1 : assert_int_equal(asize, 2);
109 1 : asize = sys_block_align(2, 2);
110 1 : assert_int_equal(asize, 2);
111 1 : asize = sys_block_align(3, 2);
112 1 : assert_int_equal(asize, 4);
113 :
114 1 : asize = sys_block_align(0, 4);
115 1 : assert_int_equal(asize, 0);
116 1 : asize = sys_block_align(1, 4);
117 1 : assert_int_equal(asize, 4);
118 1 : asize = sys_block_align(3, 4);
119 1 : assert_int_equal(asize, 4);
120 1 : asize = sys_block_align(4, 4);
121 1 : assert_int_equal(asize, 4);
122 1 : asize = sys_block_align(5, 4);
123 1 : assert_int_equal(asize, 8);
124 1 : asize = sys_block_align(7, 4);
125 1 : assert_int_equal(asize, 8);
126 1 : asize = sys_block_align(8, 4);
127 1 : assert_int_equal(asize, 8);
128 1 : asize = sys_block_align(9, 4);
129 1 : assert_int_equal(asize, 12);
130 1 : }
131 :
132 1 : static void test_sys_block_align_truncate(void **state)
133 : {
134 1 : int asize;
135 :
136 1 : asize = sys_block_align_truncate(0, 2);
137 1 : assert_int_equal(asize, 0);
138 1 : asize = sys_block_align_truncate(1, 2);
139 1 : assert_int_equal(asize, 0);
140 1 : asize = sys_block_align_truncate(2, 2);
141 1 : assert_int_equal(asize, 2);
142 1 : asize = sys_block_align_truncate(3, 2);
143 1 : assert_int_equal(asize, 2);
144 1 : asize = sys_block_align_truncate(4, 2);
145 1 : assert_int_equal(asize, 4);
146 1 : asize = sys_block_align_truncate(5, 2);
147 1 : assert_int_equal(asize, 4);
148 :
149 1 : asize = sys_block_align_truncate(0, 4);
150 1 : assert_int_equal(asize, 0);
151 1 : asize = sys_block_align_truncate(1, 4);
152 1 : assert_int_equal(asize, 0);
153 1 : asize = sys_block_align_truncate(3, 4);
154 1 : assert_int_equal(asize, 0);
155 1 : asize = sys_block_align_truncate(4, 4);
156 1 : assert_int_equal(asize, 4);
157 1 : asize = sys_block_align_truncate(5, 4);
158 1 : assert_int_equal(asize, 4);
159 1 : asize = sys_block_align_truncate(7, 4);
160 1 : assert_int_equal(asize, 4);
161 1 : asize = sys_block_align_truncate(8, 4);
162 1 : assert_int_equal(asize, 8);
163 1 : asize = sys_block_align_truncate(9, 4);
164 1 : assert_int_equal(asize, 8);
165 1 : }
166 :
167 1 : int main(int argc, char **argv)
168 : {
169 1 : const struct CMUnitTest tests[] = {
170 : cmocka_unit_test(test_sys_io_ranges_overlap),
171 : cmocka_unit_test(test_sys_block_align),
172 : cmocka_unit_test(test_sys_block_align_truncate),
173 : };
174 :
175 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
176 :
177 1 : return cmocka_run_group_tests(tests, NULL, NULL);
178 : }
|