1 /*
2  * Library name functions 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_libcerror.h"
31 #include "fsapfs_test_libfsapfs.h"
32 #include "fsapfs_test_libuna.h"
33 #include "fsapfs_test_macros.h"
34 #include "fsapfs_test_types.h"
35 #include "fsapfs_test_unused.h"
36 #include "fsapfs_test_unicode_case_folding_mappings.h"
37 #include "fsapfs_test_unicode_decomposition_mappings.h"
38 
39 #include "../libfsapfs/libfsapfs_name.h"
40 
41 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
42 
43 /* Tests the libfsapfs_name_case_folding_mappings
44  * Returns 1 if successful or 0 if not
45  */
fsapfs_test_name_case_folding_mappings(void)46 int fsapfs_test_name_case_folding_mappings(
47      void )
48 {
49 	fsapfs_test_unicode_case_folding_mapping_t *mapping = NULL;
50 	libuna_unicode_character_t unicode_character        = 0;
51 	int mapping_index                                   = 0;
52 
53 	for( mapping_index = 0;
54 	     mapping_index < 1325;
55 	     mapping_index++ )
56 	{
57 		mapping           = &( fsapfs_test_unicode_case_folding_mappings[ mapping_index ] );
58 		unicode_character = mapping->unicode_character;
59 
60 		libfsapfs_name_get_case_folding_mapping(
61 		 unicode_character );
62 
63 		if( mapping->character != unicode_character )
64 		{
65 			goto on_error;
66 		}
67 	}
68 	return( 1 );
69 
70 on_error:
71 	return( 0 );
72 }
73 
74 /* Tests the libfsapfs_name_decomposition_mappings
75  * Returns 1 if successful or 0 if not
76  */
fsapfs_test_name_decomposition_mappings(void)77 int fsapfs_test_name_decomposition_mappings(
78      void )
79 {
80 	libfsapfs_name_decomposition_mapping_t single_nfd_mapping = { 1, { 0 } };
81 
82 	fsapfs_test_unicode_decomposition_mapping_t *mapping      = NULL;
83 	libfsapfs_name_decomposition_mapping_t *nfd_mapping       = NULL;
84 	libuna_unicode_character_t unicode_character              = 0;
85 	int character_index                                       = 0;
86 	int mapping_index                                         = 0;
87 
88 	for( mapping_index = 0;
89 	     mapping_index < 30592;
90 	     mapping_index++ )
91 	{
92 		mapping           = &( fsapfs_test_unicode_nfd_mappings[ mapping_index ] );
93 		unicode_character = mapping->unicode_character;
94 
95 		libfsapfs_name_get_decomposition_mapping(
96 		 unicode_character,
97 		 nfd_mapping,
98 		 single_nfd_mapping );
99 
100 		if( mapping->number_of_characters != nfd_mapping->number_of_characters )
101 		{
102 			goto on_error;
103 		}
104 		for( character_index = 0;
105 		     character_index < mapping->number_of_characters;
106 		     character_index++ )
107 		{
108 			if( mapping->characters[ character_index ] != nfd_mapping->characters[ character_index ] )
109 			{
110 				goto on_error;
111 			}
112 		}
113 	}
114 	return( 1 );
115 
116 on_error:
117 	return( 0 );
118 }
119 
120 /* Tests the libfsapfs_name_compare_with_utf8_string function
121  * Returns 1 if successful or 0 if not
122  */
fsapfs_test_name_compare_with_utf8_string(void)123 int fsapfs_test_name_compare_with_utf8_string(
124      void )
125 {
126         uint8_t utf8_string_equal[ 6 ]   = { 'e', 'q', 'u', 'a', 'l', 0 };
127         uint8_t utf8_string_greater[ 7 ] = { 'g', 'r', 'e', 'a', 't', 'e', 'r' };
128         uint8_t utf8_string_less[ 4 ]    = { 'l', 'e', 's', 's' };
129         uint8_t utf8_string_more[ 4 ]    = { 'm', 'o', 'r', 'e' };
130 	libcerror_error_t *error         = NULL;
131 	int result                       = 0;
132 
133 	/* Test regular cases
134 	 */
135 	result = libfsapfs_name_compare_with_utf8_string(
136 	          (uint8_t *) "equal",
137 	          6,
138 	          utf8_string_equal,
139 	          5,
140 	          0,
141 	          &error );
142 
143 	FSAPFS_TEST_ASSERT_EQUAL_INT(
144 	 "result",
145 	 result,
146 	 LIBUNA_COMPARE_EQUAL );
147 
148 	FSAPFS_TEST_ASSERT_IS_NULL(
149 	 "error",
150 	 error );
151 
152 	result = libfsapfs_name_compare_with_utf8_string(
153 	          (uint8_t *) "equal",
154 	          6,
155 	          utf8_string_equal,
156 	          6,
157 	          0,
158 	          &error );
159 
160 	FSAPFS_TEST_ASSERT_EQUAL_INT(
161 	 "result",
162 	 result,
163 	 LIBUNA_COMPARE_EQUAL );
164 
165 	FSAPFS_TEST_ASSERT_IS_NULL(
166 	 "error",
167 	 error );
168 
169 	result = libfsapfs_name_compare_with_utf8_string(
170 	          (uint8_t *) "great",
171 	          6,
172 	          utf8_string_greater,
173 	          7,
174 	          0,
175 	          &error );
176 
177 	FSAPFS_TEST_ASSERT_EQUAL_INT(
178 	 "result",
179 	 result,
180 	 LIBUNA_COMPARE_GREATER );
181 
182 	FSAPFS_TEST_ASSERT_IS_NULL(
183 	 "error",
184 	 error );
185 
186 	result = libfsapfs_name_compare_with_utf8_string(
187 	          (uint8_t *) "less",
188 	          5,
189 	          utf8_string_more,
190 	          4,
191 	          0,
192 	          &error );
193 
194 	FSAPFS_TEST_ASSERT_EQUAL_INT(
195 	 "result",
196 	 result,
197 	 LIBUNA_COMPARE_GREATER );
198 
199 	FSAPFS_TEST_ASSERT_IS_NULL(
200 	 "error",
201 	 error );
202 
203 	result = libfsapfs_name_compare_with_utf8_string(
204 	          (uint8_t *) "more",
205 	          5,
206 	          utf8_string_less,
207 	          4,
208 	          0,
209 	          &error );
210 
211 	FSAPFS_TEST_ASSERT_EQUAL_INT(
212 	 "result",
213 	 result,
214 	 LIBUNA_COMPARE_LESS );
215 
216 	FSAPFS_TEST_ASSERT_IS_NULL(
217 	 "error",
218 	 error );
219 
220 	result = libfsapfs_name_compare_with_utf8_string(
221 	          (uint8_t *) "lesser",
222 	          7,
223 	          utf8_string_less,
224 	          4,
225 	          0,
226 	          &error );
227 
228 	FSAPFS_TEST_ASSERT_EQUAL_INT(
229 	 "result",
230 	 result,
231 	 LIBUNA_COMPARE_LESS );
232 
233 	FSAPFS_TEST_ASSERT_IS_NULL(
234 	 "error",
235 	 error );
236 
237 	/* Test error cases
238 	 */
239 	result = libfsapfs_name_compare_with_utf8_string(
240 	          NULL,
241 	          6,
242 	          utf8_string_equal,
243 	          5,
244 	          0,
245 	          &error );
246 
247 	FSAPFS_TEST_ASSERT_EQUAL_INT(
248 	 "result",
249 	 result,
250 	 -1 );
251 
252 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
253 	 "error",
254 	 error );
255 
256 	libcerror_error_free(
257 	 &error );
258 
259 	result = libfsapfs_name_compare_with_utf8_string(
260 	          (uint8_t *) "equal",
261 	          (size_t) SSIZE_MAX + 1,
262 	          utf8_string_equal,
263 	          5,
264 	          0,
265 	          &error );
266 
267 	FSAPFS_TEST_ASSERT_EQUAL_INT(
268 	 "result",
269 	 result,
270 	 -1 );
271 
272 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
273 	 "error",
274 	 error );
275 
276 	libcerror_error_free(
277 	 &error );
278 
279 	result = libfsapfs_name_compare_with_utf8_string(
280 	          (uint8_t *) "equal",
281 	          6,
282 	          NULL,
283 	          5,
284 	          0,
285 	          &error );
286 
287 	FSAPFS_TEST_ASSERT_EQUAL_INT(
288 	 "result",
289 	 result,
290 	 -1 );
291 
292 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
293 	 "error",
294 	 error );
295 
296 	libcerror_error_free(
297 	 &error );
298 
299 	result = libfsapfs_name_compare_with_utf8_string(
300 	          (uint8_t *) "equal",
301 	          6,
302 	          utf8_string_equal,
303 	          (size_t) SSIZE_MAX + 1,
304 	          0,
305 	          &error );
306 
307 	FSAPFS_TEST_ASSERT_EQUAL_INT(
308 	 "result",
309 	 result,
310 	 -1 );
311 
312 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
313 	 "error",
314 	 error );
315 
316 	libcerror_error_free(
317 	 &error );
318 
319 	return( 1 );
320 
321 on_error:
322 	if( error != NULL )
323 	{
324 		libcerror_error_free(
325 		 &error );
326 	}
327 	return( 0 );
328 }
329 
330 /* Tests the libfsapfs_name_compare_with_utf16_string function
331  * Returns 1 if successful or 0 if not
332  */
fsapfs_test_name_compare_with_utf16_string(void)333 int fsapfs_test_name_compare_with_utf16_string(
334      void )
335 {
336         uint16_t utf16_string_equal[ 6 ]   = { 'e', 'q', 'u', 'a', 'l', 0 };
337         uint16_t utf16_string_greater[ 7 ] = { 'g', 'r', 'e', 'a', 't', 'e', 'r' };
338         uint16_t utf16_string_less[ 4 ]    = { 'l', 'e', 's', 's' };
339         uint16_t utf16_string_more[ 4 ]    = { 'm', 'o', 'r', 'e' };
340 	libcerror_error_t *error           = NULL;
341 	int result                         = 0;
342 
343 	/* Test regular cases
344 	 */
345 	result = libfsapfs_name_compare_with_utf16_string(
346 	          (uint8_t *) "equal",
347 	          6,
348 	          utf16_string_equal,
349 	          5,
350 	          0,
351 	          &error );
352 
353 	FSAPFS_TEST_ASSERT_EQUAL_INT(
354 	 "result",
355 	 result,
356 	 LIBUNA_COMPARE_EQUAL );
357 
358 	FSAPFS_TEST_ASSERT_IS_NULL(
359 	 "error",
360 	 error );
361 
362 	result = libfsapfs_name_compare_with_utf16_string(
363 	          (uint8_t *) "equal",
364 	          6,
365 	          utf16_string_equal,
366 	          6,
367 	          0,
368 	          &error );
369 
370 	FSAPFS_TEST_ASSERT_EQUAL_INT(
371 	 "result",
372 	 result,
373 	 LIBUNA_COMPARE_EQUAL );
374 
375 	FSAPFS_TEST_ASSERT_IS_NULL(
376 	 "error",
377 	 error );
378 
379 	result = libfsapfs_name_compare_with_utf16_string(
380 	          (uint8_t *) "great",
381 	          6,
382 	          utf16_string_greater,
383 	          7,
384 	          0,
385 	          &error );
386 
387 	FSAPFS_TEST_ASSERT_EQUAL_INT(
388 	 "result",
389 	 result,
390 	 LIBUNA_COMPARE_GREATER );
391 
392 	FSAPFS_TEST_ASSERT_IS_NULL(
393 	 "error",
394 	 error );
395 
396 	result = libfsapfs_name_compare_with_utf16_string(
397 	          (uint8_t *) "less",
398 	          5,
399 	          utf16_string_more,
400 	          4,
401 	          0,
402 	          &error );
403 
404 	FSAPFS_TEST_ASSERT_EQUAL_INT(
405 	 "result",
406 	 result,
407 	 LIBUNA_COMPARE_GREATER );
408 
409 	FSAPFS_TEST_ASSERT_IS_NULL(
410 	 "error",
411 	 error );
412 
413 	result = libfsapfs_name_compare_with_utf16_string(
414 	          (uint8_t *) "more",
415 	          5,
416 	          utf16_string_less,
417 	          4,
418 	          0,
419 	          &error );
420 
421 	FSAPFS_TEST_ASSERT_EQUAL_INT(
422 	 "result",
423 	 result,
424 	 LIBUNA_COMPARE_LESS );
425 
426 	FSAPFS_TEST_ASSERT_IS_NULL(
427 	 "error",
428 	 error );
429 
430 	result = libfsapfs_name_compare_with_utf16_string(
431 	          (uint8_t *) "lesser",
432 	          7,
433 	          utf16_string_less,
434 	          4,
435 	          0,
436 	          &error );
437 
438 	FSAPFS_TEST_ASSERT_EQUAL_INT(
439 	 "result",
440 	 result,
441 	 LIBUNA_COMPARE_LESS );
442 
443 	FSAPFS_TEST_ASSERT_IS_NULL(
444 	 "error",
445 	 error );
446 
447 	/* Test error cases
448 	 */
449 	result = libfsapfs_name_compare_with_utf16_string(
450 	          NULL,
451 	          6,
452 	          utf16_string_equal,
453 	          5,
454 	          0,
455 	          &error );
456 
457 	FSAPFS_TEST_ASSERT_EQUAL_INT(
458 	 "result",
459 	 result,
460 	 -1 );
461 
462 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
463 	 "error",
464 	 error );
465 
466 	libcerror_error_free(
467 	 &error );
468 
469 	result = libfsapfs_name_compare_with_utf16_string(
470 	          (uint8_t *) "equal",
471 	          (size_t) SSIZE_MAX + 1,
472 	          utf16_string_equal,
473 	          5,
474 	          0,
475 	          &error );
476 
477 	FSAPFS_TEST_ASSERT_EQUAL_INT(
478 	 "result",
479 	 result,
480 	 -1 );
481 
482 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
483 	 "error",
484 	 error );
485 
486 	libcerror_error_free(
487 	 &error );
488 
489 	result = libfsapfs_name_compare_with_utf16_string(
490 	          (uint8_t *) "equal",
491 	          6,
492 	          NULL,
493 	          5,
494 	          0,
495 	          &error );
496 
497 	FSAPFS_TEST_ASSERT_EQUAL_INT(
498 	 "result",
499 	 result,
500 	 -1 );
501 
502 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
503 	 "error",
504 	 error );
505 
506 	libcerror_error_free(
507 	 &error );
508 
509 	result = libfsapfs_name_compare_with_utf16_string(
510 	          (uint8_t *) "equal",
511 	          6,
512 	          utf16_string_equal,
513 	          (size_t) SSIZE_MAX + 1,
514 	          0,
515 	          &error );
516 
517 	FSAPFS_TEST_ASSERT_EQUAL_INT(
518 	 "result",
519 	 result,
520 	 -1 );
521 
522 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
523 	 "error",
524 	 error );
525 
526 	libcerror_error_free(
527 	 &error );
528 
529 	return( 1 );
530 
531 on_error:
532 	if( error != NULL )
533 	{
534 		libcerror_error_free(
535 		 &error );
536 	}
537 	return( 0 );
538 }
539 
540 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
541 
542 /* The main program
543  */
544 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSAPFS_TEST_ATTRIBUTE_UNUSED)545 int wmain(
546      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
547      wchar_t * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
548 #else
549 int main(
550      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
551      char * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
552 #endif
553 {
554 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argc )
555 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argv )
556 
557 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
558 
559 	FSAPFS_TEST_RUN(
560 	 "libfsapfs_name_case_folding_mappings",
561 	 fsapfs_test_name_case_folding_mappings );
562 
563 	FSAPFS_TEST_RUN(
564 	 "libfsapfs_name_decomposition_mappings",
565 	 fsapfs_test_name_decomposition_mappings );
566 
567 	FSAPFS_TEST_RUN(
568 	 "libfsapfs_name_compare_with_utf8_string",
569 	 fsapfs_test_name_compare_with_utf8_string );
570 
571 	FSAPFS_TEST_RUN(
572 	 "libfsapfs_name_compare_with_utf16_string",
573 	 fsapfs_test_name_compare_with_utf16_string );
574 
575 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
576 
577 	return( EXIT_SUCCESS );
578 
579 on_error:
580 	return( EXIT_FAILURE );
581 }
582 
583