1 /*
2  * Library block_vector functions test program
3  *
4  * Copyright (C) 2010-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser 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 Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <common.h>
23 #include <file_stream.h>
24 #include <types.h>
25 
26 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
27 #include <stdlib.h>
28 #endif
29 
30 #include "fsext_test_functions.h"
31 #include "fsext_test_libcerror.h"
32 #include "fsext_test_libfdata.h"
33 #include "fsext_test_libfcache.h"
34 #include "fsext_test_libfsext.h"
35 #include "fsext_test_macros.h"
36 #include "fsext_test_memory.h"
37 #include "fsext_test_unused.h"
38 
39 #include "../libfsext/libfsext_block_vector.h"
40 #include "../libfsext/libfsext_inode.h"
41 #include "../libfsext/libfsext_io_handle.h"
42 
43 /* ext3 inode */
44 uint8_t fsext_test_inode_data1[ 128 ] = {
45 	0x00, 0x04, 0x00, 0x00, 0x3d, 0x13, 0xc1, 0x3f, 0x44, 0x13, 0xc1, 0x3f, 0x44, 0x13, 0xc1, 0x3f,
46 	0x00, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 	0x00, 0x00, 0x00, 0x00, 0xa5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
53 
54 uint8_t fsext_test_block_data1[ 1024 ] = {
55 	0x52, 0x21, 0x00, 0x00, 0x53, 0x21, 0x00, 0x00, 0x54, 0x21, 0x00, 0x00, 0x55, 0x21, 0x00, 0x00,
56 	0x56, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
104 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
106 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
107 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
110 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
112 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
116 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
117 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
119 
120 #if defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT )
121 
122 /* Tests the libfsext_block_vector_initialize function
123  * Returns 1 if successful or 0 if not
124  */
fsext_test_block_vector_initialize(void)125 int fsext_test_block_vector_initialize(
126      void )
127 {
128 	libcerror_error_t *error        = NULL;
129 	libfdata_vector_t *block_vector = NULL;
130 	libfsext_inode_t *inode         = NULL;
131 	libfsext_io_handle_t *io_handle = NULL;
132 	int result                      = 0;
133 
134 	/* Initialize test
135 	 */
136 	result = libfsext_io_handle_initialize(
137 	          &io_handle,
138 	          &error );
139 
140 	FSEXT_TEST_ASSERT_EQUAL_INT(
141 	 "result",
142 	 result,
143 	 1 );
144 
145 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
146 	 "io_handle",
147 	 io_handle );
148 
149 	FSEXT_TEST_ASSERT_IS_NULL(
150 	 "error",
151 	 error );
152 
153 	result = libfsext_inode_initialize(
154 	          &inode,
155 	          &error );
156 
157 	FSEXT_TEST_ASSERT_EQUAL_INT(
158 	 "result",
159 	 result,
160 	 1 );
161 
162 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
163 	 "inode",
164 	 inode );
165 
166 	FSEXT_TEST_ASSERT_IS_NULL(
167 	 "error",
168 	 error );
169 
170 	io_handle->format_version = 3;
171 	io_handle->inode_size     = 0;
172 
173 	result = libfsext_inode_read_data(
174 	          inode,
175 	          io_handle,
176 	          fsext_test_inode_data1,
177 	          128,
178 	          &error );
179 
180 	FSEXT_TEST_ASSERT_EQUAL_INT(
181 	 "result",
182 	 result,
183 	 1 );
184 
185 	FSEXT_TEST_ASSERT_IS_NULL(
186 	 "error",
187 	 error );
188 
189 	/* Test regular cases
190 	 */
191 	io_handle->block_size = 1024;
192 
193 	result = libfsext_block_vector_initialize(
194 	          &block_vector,
195 	          io_handle,
196 	          inode,
197 	          &error );
198 
199 	FSEXT_TEST_ASSERT_EQUAL_INT(
200 	 "result",
201 	 result,
202 	 1 );
203 
204 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
205 	 "block_vector",
206 	 block_vector );
207 
208 	FSEXT_TEST_ASSERT_IS_NULL(
209 	 "error",
210 	 error );
211 
212 	result = libfdata_vector_free(
213 	          &block_vector,
214 	          &error );
215 
216 	FSEXT_TEST_ASSERT_EQUAL_INT(
217 	 "result",
218 	 result,
219 	 1 );
220 
221 	FSEXT_TEST_ASSERT_IS_NULL(
222 	 "block_vector",
223 	 block_vector );
224 
225 	FSEXT_TEST_ASSERT_IS_NULL(
226 	 "error",
227 	 error );
228 
229 	/* Test error cases
230 	 */
231 	result = libfsext_block_vector_initialize(
232 	          NULL,
233 	          io_handle,
234 	          inode,
235 	          &error );
236 
237 	FSEXT_TEST_ASSERT_EQUAL_INT(
238 	 "result",
239 	 result,
240 	 -1 );
241 
242 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
243 	 "error",
244 	 error );
245 
246 	libcerror_error_free(
247 	 &error );
248 
249 	result = libfsext_block_vector_initialize(
250 	          &block_vector,
251 	          NULL,
252 	          inode,
253 	          &error );
254 
255 	FSEXT_TEST_ASSERT_EQUAL_INT(
256 	 "result",
257 	 result,
258 	 -1 );
259 
260 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
261 	 "error",
262 	 error );
263 
264 	libcerror_error_free(
265 	 &error );
266 
267 	result = libfsext_block_vector_initialize(
268 	          &block_vector,
269 	          io_handle,
270 	          NULL,
271 	          &error );
272 
273 	FSEXT_TEST_ASSERT_EQUAL_INT(
274 	 "result",
275 	 result,
276 	 -1 );
277 
278 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
279 	 "error",
280 	 error );
281 
282 	libcerror_error_free(
283 	 &error );
284 
285 	io_handle->block_size = 0;
286 
287 	result = libfsext_block_vector_initialize(
288 	          &block_vector,
289 	          io_handle,
290 	          inode,
291 	          &error );
292 
293 	FSEXT_TEST_ASSERT_EQUAL_INT(
294 	 "result",
295 	 result,
296 	 -1 );
297 
298 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
299 	 "error",
300 	 error );
301 
302 	libcerror_error_free(
303 	 &error );
304 
305 	io_handle->block_size = 1024;
306 
307 #if defined( HAVE_FSEXT_TEST_MEMORY )
308 
309 	/* Test libfsext_block_vector_initialize with malloc failing in libfdata_vector_initialize
310 	 */
311 	fsext_test_malloc_attempts_before_fail = 0;
312 
313 	result = libfsext_block_vector_initialize(
314 		  &block_vector,
315 		  io_handle,
316 		  inode,
317 		  &error );
318 
319 	if( fsext_test_malloc_attempts_before_fail != -1 )
320 	{
321 		fsext_test_malloc_attempts_before_fail = -1;
322 
323 		if( block_vector != NULL )
324 		{
325 			libfdata_vector_free(
326 			 &block_vector,
327 			 NULL );
328 		}
329 	}
330 	else
331 	{
332 		FSEXT_TEST_ASSERT_EQUAL_INT(
333 		 "result",
334 		 result,
335 		 -1 );
336 
337 		FSEXT_TEST_ASSERT_IS_NULL(
338 		 "block_vector",
339 		 block_vector );
340 
341 		FSEXT_TEST_ASSERT_IS_NOT_NULL(
342 		 "error",
343 		 error );
344 
345 		libcerror_error_free(
346 		 &error );
347 	}
348 #endif /* defined( HAVE_FSEXT_TEST_MEMORY ) */
349 
350 	/* Clean up
351 	 */
352 	result = libfsext_inode_free(
353 	          &inode,
354 	          &error );
355 
356 	FSEXT_TEST_ASSERT_EQUAL_INT(
357 	 "result",
358 	 result,
359 	 1 );
360 
361 	FSEXT_TEST_ASSERT_IS_NULL(
362 	 "inode",
363 	 inode );
364 
365 	FSEXT_TEST_ASSERT_IS_NULL(
366 	 "error",
367 	 error );
368 
369 	result = libfsext_io_handle_free(
370 	          &io_handle,
371 	          &error );
372 
373 	FSEXT_TEST_ASSERT_EQUAL_INT(
374 	 "result",
375 	 result,
376 	 1 );
377 
378 	FSEXT_TEST_ASSERT_IS_NULL(
379 	 "io_handle",
380 	 io_handle );
381 
382 	FSEXT_TEST_ASSERT_IS_NULL(
383 	 "error",
384 	 error );
385 
386 	return( 1 );
387 
388 on_error:
389 	if( error != NULL )
390 	{
391 		libcerror_error_free(
392 		 &error );
393 	}
394 	if( block_vector != NULL )
395 	{
396 		libfdata_vector_free(
397 		 &block_vector,
398 		 NULL );
399 	}
400 	if( inode != NULL )
401 	{
402 		libfsext_inode_free(
403 		 &inode,
404 		 NULL );
405 	}
406 	if( io_handle != NULL )
407 	{
408 		libfsext_io_handle_free(
409 		 &io_handle,
410 		 NULL );
411 	}
412 	return( 1 );
413 }
414 
415 /* Tests the libfsext_block_vector_read_element_data function
416  * Returns 1 if successful or 0 if not
417  */
fsext_test_block_vector_read_element_data(void)418 int fsext_test_block_vector_read_element_data(
419      void )
420 {
421 	libbfio_handle_t *file_io_handle = NULL;
422 	libcerror_error_t *error         = NULL;
423 	libfcache_cache_t *block_cache   = NULL;
424 	libfdata_vector_t *block_vector  = NULL;
425 	libfsext_inode_t *inode          = NULL;
426 	libfsext_io_handle_t *io_handle  = NULL;
427 	int result                       = 0;
428 
429 	/* Initialize test
430 	 */
431 	result = libfsext_io_handle_initialize(
432 	          &io_handle,
433 	          &error );
434 
435 	FSEXT_TEST_ASSERT_EQUAL_INT(
436 	 "result",
437 	 result,
438 	 1 );
439 
440 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
441 	 "io_handle",
442 	 io_handle );
443 
444 	FSEXT_TEST_ASSERT_IS_NULL(
445 	 "error",
446 	 error );
447 
448 	result = libfsext_inode_initialize(
449 	          &inode,
450 	          &error );
451 
452 	FSEXT_TEST_ASSERT_EQUAL_INT(
453 	 "result",
454 	 result,
455 	 1 );
456 
457 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
458 	 "inode",
459 	 inode );
460 
461 	FSEXT_TEST_ASSERT_IS_NULL(
462 	 "error",
463 	 error );
464 
465 	io_handle->format_version = 3;
466 	io_handle->inode_size     = 0;
467 
468 	result = libfsext_inode_read_data(
469 	          inode,
470 	          io_handle,
471 	          fsext_test_inode_data1,
472 	          128,
473 	          &error );
474 
475 	FSEXT_TEST_ASSERT_EQUAL_INT(
476 	 "result",
477 	 result,
478 	 1 );
479 
480 	FSEXT_TEST_ASSERT_IS_NULL(
481 	 "error",
482 	 error );
483 
484 	io_handle->block_size = 1024;
485 
486 	result = libfsext_block_vector_initialize(
487 	          &block_vector,
488 	          io_handle,
489 	          inode,
490 	          &error );
491 
492 	FSEXT_TEST_ASSERT_EQUAL_INT(
493 	 "result",
494 	 result,
495 	 1 );
496 
497 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
498 	 "block_vector",
499 	 block_vector );
500 
501 	FSEXT_TEST_ASSERT_IS_NULL(
502 	 "error",
503 	 error );
504 
505 	/* Initialize file IO handle
506 	 */
507 	result = fsext_test_open_file_io_handle(
508 	          &file_io_handle,
509 	          fsext_test_block_data1,
510 	          1024,
511 	          &error );
512 
513 	FSEXT_TEST_ASSERT_EQUAL_INT(
514 	 "result",
515 	 result,
516 	 1 );
517 
518 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
519 	 "file_io_handle",
520 	 file_io_handle );
521 
522 	FSEXT_TEST_ASSERT_IS_NULL(
523 	 "error",
524 	 error );
525 
526 	/* Test error cases
527 	 */
528 	result = libfsext_block_vector_read_element_data(
529 	          NULL,
530 	          file_io_handle,
531 	          block_vector,
532 	          (libfdata_cache_t *) block_cache,
533 	          0,
534 	          0,
535 	          0,
536 	          1024,
537 	          0,
538 	          0,
539 	          &error );
540 
541 	FSEXT_TEST_ASSERT_EQUAL_INT(
542 	 "result",
543 	 result,
544 	 -1 );
545 
546 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
547 	 "error",
548 	 error );
549 
550 	libcerror_error_free(
551 	 &error );
552 
553 	result = libfsext_block_vector_read_element_data(
554 	          io_handle,
555 	          NULL,
556 	          block_vector,
557 	          (libfdata_cache_t *) block_cache,
558 	          0,
559 	          0,
560 	          0,
561 	          1024,
562 	          0,
563 	          0,
564 	          &error );
565 
566 	FSEXT_TEST_ASSERT_EQUAL_INT(
567 	 "result",
568 	 result,
569 	 -1 );
570 
571 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
572 	 "error",
573 	 error );
574 
575 	libcerror_error_free(
576 	 &error );
577 
578 	result = libfsext_block_vector_read_element_data(
579 	          io_handle,
580 	          file_io_handle,
581 	          NULL,
582 	          (libfdata_cache_t *) block_cache,
583 	          0,
584 	          0,
585 	          0,
586 	          1024,
587 	          0,
588 	          0,
589 	          &error );
590 
591 	FSEXT_TEST_ASSERT_EQUAL_INT(
592 	 "result",
593 	 result,
594 	 -1 );
595 
596 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
597 	 "error",
598 	 error );
599 
600 	libcerror_error_free(
601 	 &error );
602 
603 	result = libfsext_block_vector_read_element_data(
604 	          io_handle,
605 	          file_io_handle,
606 	          block_vector,
607 	          NULL,
608 	          0,
609 	          0,
610 	          0,
611 	          1024,
612 	          0,
613 	          0,
614 	          &error );
615 
616 	FSEXT_TEST_ASSERT_EQUAL_INT(
617 	 "result",
618 	 result,
619 	 -1 );
620 
621 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
622 	 "error",
623 	 error );
624 
625 	libcerror_error_free(
626 	 &error );
627 
628 	result = libfsext_block_vector_read_element_data(
629 	          io_handle,
630 	          file_io_handle,
631 	          block_vector,
632 	          (libfdata_cache_t *) block_cache,
633 	          0,
634 	          0,
635 	          0,
636 	          0,
637 	          0,
638 	          0,
639 	          &error );
640 
641 	FSEXT_TEST_ASSERT_EQUAL_INT(
642 	 "result",
643 	 result,
644 	 -1 );
645 
646 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
647 	 "error",
648 	 error );
649 
650 	libcerror_error_free(
651 	 &error );
652 
653 	result = libfsext_block_vector_read_element_data(
654 	          io_handle,
655 	          file_io_handle,
656 	          block_vector,
657 	          (libfdata_cache_t *) block_cache,
658 	          0,
659 	          0,
660 	          0,
661 	          (size64_t) SSIZE_MAX + 1,
662 	          0,
663 	          0,
664 	          &error );
665 
666 	FSEXT_TEST_ASSERT_EQUAL_INT(
667 	 "result",
668 	 result,
669 	 -1 );
670 
671 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
672 	 "error",
673 	 error );
674 
675 	libcerror_error_free(
676 	 &error );
677 
678 #if defined( HAVE_FSEXT_TEST_MEMORY )
679 
680 	/* Test libfsext_block_vector_read_element_data with malloc failing in libfsext_block_initialize
681 	 */
682 	fsext_test_malloc_attempts_before_fail = 0;
683 
684 	result = libfsext_block_vector_read_element_data(
685 	          io_handle,
686 	          file_io_handle,
687 	          block_vector,
688 	          (libfdata_cache_t *) block_cache,
689 	          0,
690 	          0,
691 	          0,
692 	          1024,
693 	          0,
694 	          0,
695 	          &error );
696 
697 	if( fsext_test_malloc_attempts_before_fail != -1 )
698 	{
699 		fsext_test_malloc_attempts_before_fail = -1;
700 	}
701 	else
702 	{
703 		FSEXT_TEST_ASSERT_EQUAL_INT(
704 		 "result",
705 		 result,
706 		 -1 );
707 
708 		FSEXT_TEST_ASSERT_IS_NOT_NULL(
709 		 "error",
710 		 error );
711 
712 		libcerror_error_free(
713 		 &error );
714 	}
715 #endif /* defined( HAVE_FSEXT_TEST_MEMORY ) */
716 
717 	/* Clean up file IO handle
718 	 */
719 	result = fsext_test_close_file_io_handle(
720 	          &file_io_handle,
721 	          &error );
722 
723 	FSEXT_TEST_ASSERT_EQUAL_INT(
724 	 "result",
725 	 result,
726 	 0 );
727 
728 	FSEXT_TEST_ASSERT_IS_NULL(
729 	 "error",
730 	 error );
731 
732 	/* Clean up
733 	 */
734 	result = libfdata_vector_free(
735 	          &block_vector,
736 	          &error );
737 
738 	FSEXT_TEST_ASSERT_EQUAL_INT(
739 	 "result",
740 	 result,
741 	 1 );
742 
743 	FSEXT_TEST_ASSERT_IS_NULL(
744 	 "block_vector",
745 	 block_vector );
746 
747 	FSEXT_TEST_ASSERT_IS_NULL(
748 	 "error",
749 	 error );
750 
751 	result = libfsext_inode_free(
752 	          &inode,
753 	          &error );
754 
755 	FSEXT_TEST_ASSERT_EQUAL_INT(
756 	 "result",
757 	 result,
758 	 1 );
759 
760 	FSEXT_TEST_ASSERT_IS_NULL(
761 	 "inode",
762 	 inode );
763 
764 	FSEXT_TEST_ASSERT_IS_NULL(
765 	 "error",
766 	 error );
767 
768 	result = libfsext_io_handle_free(
769 	          &io_handle,
770 	          &error );
771 
772 	FSEXT_TEST_ASSERT_EQUAL_INT(
773 	 "result",
774 	 result,
775 	 1 );
776 
777 	FSEXT_TEST_ASSERT_IS_NULL(
778 	 "io_handle",
779 	 io_handle );
780 
781 	FSEXT_TEST_ASSERT_IS_NULL(
782 	 "error",
783 	 error );
784 
785 	return( 1 );
786 
787 on_error:
788 	if( error != NULL )
789 	{
790 		libcerror_error_free(
791 		 &error );
792 	}
793 	if( file_io_handle != NULL )
794 	{
795 		libbfio_handle_free(
796 		 &file_io_handle,
797 		 NULL );
798 	}
799 	if( block_vector != NULL )
800 	{
801 		libfdata_vector_free(
802 		 &block_vector,
803 		 NULL );
804 	}
805 	if( inode != NULL )
806 	{
807 		libfsext_inode_free(
808 		 &inode,
809 		 NULL );
810 	}
811 	if( io_handle != NULL )
812 	{
813 		libfsext_io_handle_free(
814 		 &io_handle,
815 		 NULL );
816 	}
817 	return( 1 );
818 }
819 
820 #endif /* defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT ) */
821 
822 /* The main program
823  */
824 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSEXT_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSEXT_TEST_ATTRIBUTE_UNUSED)825 int wmain(
826      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
827      wchar_t * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
828 #else
829 int main(
830      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
831      char * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
832 #endif
833 {
834 	FSEXT_TEST_UNREFERENCED_PARAMETER( argc )
835 	FSEXT_TEST_UNREFERENCED_PARAMETER( argv )
836 
837 #if defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT )
838 
839 	FSEXT_TEST_RUN(
840 	 "libfsext_block_vector_initialize",
841 	 fsext_test_block_vector_initialize );
842 
843 	FSEXT_TEST_RUN(
844 	 "libfsext_block_vector_read_element_data",
845 	 fsext_test_block_vector_read_element_data );
846 
847 #endif /* defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT ) */
848 
849 	return( EXIT_SUCCESS );
850 
851 on_error:
852 	return( EXIT_FAILURE );
853 }
854 
855