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