1 /*
2  * Library checksum 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_macros.h"
33 #include "fsapfs_test_unused.h"
34 
35 #include "../libfsapfs/libfsapfs_checksum.h"
36 
37 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
38 
39 /* Tests the libfsapfs_checksum_initialize_crc32_table function
40  * Returns 1 if successful or 0 if not
41  */
fsapfs_test_checksum_initialize_crc32_table(void)42 int fsapfs_test_checksum_initialize_crc32_table(
43      void )
44 {
45 	/* Test invocation of function only
46 	 */
47 	libfsapfs_checksum_initialize_crc32_table(
48 	 0x82f63b78UL );
49 
50 	return( 1 );
51 }
52 
53 /* Tests the libfsapfs_checksum_calculate_weak_crc32 function
54  * Returns 1 if successful or 0 if not
55  */
fsapfs_test_checksum_calculate_weak_crc32(void)56 int fsapfs_test_checksum_calculate_weak_crc32(
57      void )
58 {
59 	uint8_t data[ 16 ] = {
60 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
61 
62 	libcerror_error_t *error = NULL;
63 	uint32_t checksum        = 0;
64 	int result               = 0;
65 
66 	libfsapfs_checksum_crc32_table_computed = 0;
67 
68 	/* Test regular cases
69 	 */
70 	result = libfsapfs_checksum_calculate_weak_crc32(
71 	          &checksum,
72 	          data,
73 	          16,
74 	          0,
75 	          &error );
76 
77 	FSAPFS_TEST_ASSERT_EQUAL_INT(
78 	 "result",
79 	 result,
80 	 1 );
81 
82 	FSAPFS_TEST_ASSERT_EQUAL_UINT32(
83 	 "checksum",
84 	 checksum,
85 	 (uint32_t) 0x9bb99201UL );
86 
87 	FSAPFS_TEST_ASSERT_IS_NULL(
88 	 "error",
89 	 error );
90 
91 	/* Test error cases
92 	 */
93 	result = libfsapfs_checksum_calculate_weak_crc32(
94 	          NULL,
95 	          data,
96 	          16,
97 	          0,
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 	result = libfsapfs_checksum_calculate_weak_crc32(
113 	          &checksum,
114 	          NULL,
115 	          16,
116 	          0,
117 	          &error );
118 
119 	FSAPFS_TEST_ASSERT_EQUAL_INT(
120 	 "result",
121 	 result,
122 	 -1 );
123 
124 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
125 	 "error",
126 	 error );
127 
128 	libcerror_error_free(
129 	 &error );
130 
131 	result = libfsapfs_checksum_calculate_weak_crc32(
132 	          &checksum,
133 	          data,
134 	          (size_t) SSIZE_MAX + 1,
135 	          0,
136 	          &error );
137 
138 	FSAPFS_TEST_ASSERT_EQUAL_INT(
139 	 "result",
140 	 result,
141 	 -1 );
142 
143 	FSAPFS_TEST_ASSERT_IS_NOT_NULL(
144 	 "error",
145 	 error );
146 
147 	libcerror_error_free(
148 	 &error );
149 
150 	return( 1 );
151 
152 on_error:
153 	if( error != NULL )
154 	{
155 		libcerror_error_free(
156 		 &error );
157 	}
158 	return( 0 );
159 }
160 
161 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
162 
163 /* The main program
164  */
165 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
wmain(int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,wchar_t * const argv[]FSAPFS_TEST_ATTRIBUTE_UNUSED)166 int wmain(
167      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
168      wchar_t * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
169 #else
170 int main(
171      int argc FSAPFS_TEST_ATTRIBUTE_UNUSED,
172      char * const argv[] FSAPFS_TEST_ATTRIBUTE_UNUSED )
173 #endif
174 {
175 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argc )
176 	FSAPFS_TEST_UNREFERENCED_PARAMETER( argv )
177 
178 #if defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT )
179 
180 	FSAPFS_TEST_RUN(
181 	 "libfsapfs_checksum_initialize_crc32_table",
182 	 fsapfs_test_checksum_initialize_crc32_table );
183 
184 	FSAPFS_TEST_RUN(
185 	 "libfsapfs_checksum_calculate_weak_crc32",
186 	 fsapfs_test_checksum_calculate_weak_crc32 );
187 
188 #endif /* defined( __GNUC__ ) && !defined( LIBFSAPFS_DLL_IMPORT ) */
189 
190 	return( EXIT_SUCCESS );
191 
192 on_error:
193 	return( EXIT_FAILURE );
194 }
195 
196