1 /*
2  * Library extent_index type 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_libcerror.h"
31 #include "fsext_test_libfsext.h"
32 #include "fsext_test_macros.h"
33 #include "fsext_test_memory.h"
34 #include "fsext_test_unused.h"
35 
36 #include "../libfsext/libfsext_extent_index.h"
37 
38 #if defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT )
39 
40 /* Tests the libfsext_extent_index_initialize function
41  * Returns 1 if successful or 0 if not
42  */
fsext_test_extent_index_initialize(void)43 int fsext_test_extent_index_initialize(
44      void )
45 {
46 	libcerror_error_t *error              = NULL;
47 	libfsext_extent_index_t *extent_index = NULL;
48 	int result                            = 0;
49 
50 #if defined( HAVE_FSEXT_TEST_MEMORY )
51 	int number_of_malloc_fail_tests       = 1;
52 	int number_of_memset_fail_tests       = 1;
53 	int test_number                       = 0;
54 #endif
55 
56 	/* Test regular cases
57 	 */
58 	result = libfsext_extent_index_initialize(
59 	          &extent_index,
60 	          &error );
61 
62 	FSEXT_TEST_ASSERT_EQUAL_INT(
63 	 "result",
64 	 result,
65 	 1 );
66 
67 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
68 	 "extent_index",
69 	 extent_index );
70 
71 	FSEXT_TEST_ASSERT_IS_NULL(
72 	 "error",
73 	 error );
74 
75 	result = libfsext_extent_index_free(
76 	          &extent_index,
77 	          &error );
78 
79 	FSEXT_TEST_ASSERT_EQUAL_INT(
80 	 "result",
81 	 result,
82 	 1 );
83 
84 	FSEXT_TEST_ASSERT_IS_NULL(
85 	 "extent_index",
86 	 extent_index );
87 
88 	FSEXT_TEST_ASSERT_IS_NULL(
89 	 "error",
90 	 error );
91 
92 	/* Test error cases
93 	 */
94 	result = libfsext_extent_index_initialize(
95 	          NULL,
96 	          &error );
97 
98 	FSEXT_TEST_ASSERT_EQUAL_INT(
99 	 "result",
100 	 result,
101 	 -1 );
102 
103 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
104 	 "error",
105 	 error );
106 
107 	libcerror_error_free(
108 	 &error );
109 
110 	extent_index = (libfsext_extent_index_t *) 0x12345678UL;
111 
112 	result = libfsext_extent_index_initialize(
113 	          &extent_index,
114 	          &error );
115 
116 	extent_index = NULL;
117 
118 	FSEXT_TEST_ASSERT_EQUAL_INT(
119 	 "result",
120 	 result,
121 	 -1 );
122 
123 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
124 	 "error",
125 	 error );
126 
127 	libcerror_error_free(
128 	 &error );
129 
130 #if defined( HAVE_FSEXT_TEST_MEMORY )
131 
132 	for( test_number = 0;
133 	     test_number < number_of_malloc_fail_tests;
134 	     test_number++ )
135 	{
136 		/* Test libfsext_extent_index_initialize with malloc failing
137 		 */
138 		fsext_test_malloc_attempts_before_fail = test_number;
139 
140 		result = libfsext_extent_index_initialize(
141 		          &extent_index,
142 		          &error );
143 
144 		if( fsext_test_malloc_attempts_before_fail != -1 )
145 		{
146 			fsext_test_malloc_attempts_before_fail = -1;
147 
148 			if( extent_index != NULL )
149 			{
150 				libfsext_extent_index_free(
151 				 &extent_index,
152 				 NULL );
153 			}
154 		}
155 		else
156 		{
157 			FSEXT_TEST_ASSERT_EQUAL_INT(
158 			 "result",
159 			 result,
160 			 -1 );
161 
162 			FSEXT_TEST_ASSERT_IS_NULL(
163 			 "extent_index",
164 			 extent_index );
165 
166 			FSEXT_TEST_ASSERT_IS_NOT_NULL(
167 			 "error",
168 			 error );
169 
170 			libcerror_error_free(
171 			 &error );
172 		}
173 	}
174 	for( test_number = 0;
175 	     test_number < number_of_memset_fail_tests;
176 	     test_number++ )
177 	{
178 		/* Test libfsext_extent_index_initialize with memset failing
179 		 */
180 		fsext_test_memset_attempts_before_fail = test_number;
181 
182 		result = libfsext_extent_index_initialize(
183 		          &extent_index,
184 		          &error );
185 
186 		if( fsext_test_memset_attempts_before_fail != -1 )
187 		{
188 			fsext_test_memset_attempts_before_fail = -1;
189 
190 			if( extent_index != NULL )
191 			{
192 				libfsext_extent_index_free(
193 				 &extent_index,
194 				 NULL );
195 			}
196 		}
197 		else
198 		{
199 			FSEXT_TEST_ASSERT_EQUAL_INT(
200 			 "result",
201 			 result,
202 			 -1 );
203 
204 			FSEXT_TEST_ASSERT_IS_NULL(
205 			 "extent_index",
206 			 extent_index );
207 
208 			FSEXT_TEST_ASSERT_IS_NOT_NULL(
209 			 "error",
210 			 error );
211 
212 			libcerror_error_free(
213 			 &error );
214 		}
215 	}
216 #endif /* defined( HAVE_FSEXT_TEST_MEMORY ) */
217 
218 	return( 1 );
219 
220 on_error:
221 	if( error != NULL )
222 	{
223 		libcerror_error_free(
224 		 &error );
225 	}
226 	if( extent_index != NULL )
227 	{
228 		libfsext_extent_index_free(
229 		 &extent_index,
230 		 NULL );
231 	}
232 	return( 0 );
233 }
234 
235 /* Tests the libfsext_extent_index_free function
236  * Returns 1 if successful or 0 if not
237  */
fsext_test_extent_index_free(void)238 int fsext_test_extent_index_free(
239      void )
240 {
241 	libcerror_error_t *error = NULL;
242 	int result               = 0;
243 
244 	/* Test error cases
245 	 */
246 	result = libfsext_extent_index_free(
247 	          NULL,
248 	          &error );
249 
250 	FSEXT_TEST_ASSERT_EQUAL_INT(
251 	 "result",
252 	 result,
253 	 -1 );
254 
255 	FSEXT_TEST_ASSERT_IS_NOT_NULL(
256 	 "error",
257 	 error );
258 
259 	libcerror_error_free(
260 	 &error );
261 
262 	return( 1 );
263 
264 on_error:
265 	if( error != NULL )
266 	{
267 		libcerror_error_free(
268 		 &error );
269 	}
270 	return( 0 );
271 }
272 
273 #endif /* defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT ) */
274 
275 /* The main program
276  */
277 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSEXT_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSEXT_TEST_ATTRIBUTE_UNUSED)278 int wmain(
279      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
280      wchar_t * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
281 #else
282 int main(
283      int argc FSEXT_TEST_ATTRIBUTE_UNUSED,
284      char * const argv[] FSEXT_TEST_ATTRIBUTE_UNUSED )
285 #endif
286 {
287 	FSEXT_TEST_UNREFERENCED_PARAMETER( argc )
288 	FSEXT_TEST_UNREFERENCED_PARAMETER( argv )
289 
290 #if defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT )
291 
292 	FSEXT_TEST_RUN(
293 	 "libfsext_extent_index_initialize",
294 	 fsext_test_extent_index_initialize );
295 
296 	FSEXT_TEST_RUN(
297 	 "libfsext_extent_index_free",
298 	 fsext_test_extent_index_free );
299 
300 	/* TODO: add tests for libfsext_extent_index_read_data */
301 
302 #endif /* defined( __GNUC__ ) && !defined( LIBFSEXT_DLL_IMPORT ) */
303 
304 	return( EXIT_SUCCESS );
305 
306 on_error:
307 	return( EXIT_FAILURE );
308 }
309 
310