xref: /aosp_15_r20/external/flac/src/test_libFLAC/md5.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1 /* test_libFLAC - Unit tester for libFLAC
2  * Copyright (C) 2014-2023  Xiph.Org Foundation
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "FLAC/assert.h"
27 #include "share/compat.h"
28 #include "private/md5.h"
29 #include "md5.h"
30 
31 
32 static FLAC__bool test_md5_clear_context(void);
33 static FLAC__bool test_md5_codec(void);
34 static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal,uint32_t channels, uint32_t samples, uint32_t bytes_per_sample, const FLAC__byte target_digest [16]);
35 
test_md5(void)36 FLAC__bool test_md5(void)
37 {
38 	printf("\n+++ libFLAC unit test: md5\n\n");
39 
40 	if (! test_md5_clear_context())
41 		return false;
42 
43 	if (! test_md5_codec())
44 		return false;
45 
46 	printf("\nPASSED!\n");
47 	return true;
48 }
49 
50 /*----------------------------------------------------------------------------*/
51 
test_md5_clear_context(void)52 static FLAC__bool test_md5_clear_context(void)
53 {
54 	FLAC__MD5Context ctx;
55 	FLAC__byte digest[16];
56 	FLAC__byte target[16] = { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e };
57 	uint32_t k ;
58 	char * cptr;
59 
60 	printf("testing FLAC__MD5Init ... ");
61 	FLAC__MD5Init (&ctx);
62 	if (ctx.buf[0] != 0x67452301) {
63 		printf("FAILED!\n");
64 		return false;
65 	}
66 	printf("OK\n");
67 
68 	printf("testing that FLAC__MD5Final clears the MD5Context ... ");
69 	FLAC__MD5Final(digest, &ctx);
70 	cptr = (char*) &ctx ;
71 	for (k = 0 ; k < sizeof (ctx) ; k++) {
72 		if (cptr [k]) {
73 			printf("FAILED, MD5 ctx has not been cleared after FLAC__MD5Final\n");
74 			return false;
75 		}
76 	}
77 	printf("OK\n");
78 
79 	printf("testing digest correct for zero data ... ");
80 	if (memcmp(digest, target, sizeof (digest))) {
81 		printf("\nFAILED, expected MD5 sum ");
82 		for (k = 0 ; k < 16 ; k++)
83 			printf("%02x", (target [k] & 0xff));
84 		printf (" but got ");
85 		for (k = 0 ; k < 16 ; k++)
86 			printf("%02x", (digest [k] & 0xff));
87 		puts("\n");
88 		return false;
89 	}
90 	puts("OK");
91 
92 	return true;
93 }
94 
95 static FLAC__byte target_digests [8][4][16] =
96 {	/* 1 channel */
97 	{ 	/* 1 byte per sample */
98 		{ 0xc1, 0x9a, 0x5b, 0xeb, 0x57, 0x8f, 0x26, 0xeb, 0xfb, 0x34, 0x7c, 0xef, 0x04, 0x31, 0x6d, 0x7d },
99 		/* 2 bytes per sample */
100 		{ 0xd4, 0x78, 0x90, 0xd3, 0xa9, 0x17, 0x4e, 0x76, 0xca, 0x4d, 0x27, 0x20, 0x98, 0x36, 0x8b, 0x2e },
101 		/* 3 bytes per sample */
102 		{ 0x5a, 0x4b, 0xd6, 0xac, 0xa1, 0x70, 0x84, 0x19, 0x7c, 0x0d, 0xfb, 0x5b, 0xa9, 0x7b, 0xcb, 0x54 },
103 		/* 4 bytes per sample */
104 		{ 0x79, 0xd5, 0x7a, 0x32, 0x06, 0x0b, 0xfe, 0x46, 0xa3, 0xe7, 0xba, 0xc5, 0xf7, 0x48, 0x6f, 0x50 }
105 	},
106 
107 	/* 2 channels */
108 	{
109 		{ 0x89, 0xac, 0xcf, 0x91, 0xf1, 0x8c, 0xea, 0xab, 0x46, 0x12, 0x74, 0xbc, 0x4e, 0x82, 0xbe, 0x7d },
110 		{ 0xb9, 0x17, 0x16, 0x5b, 0xd8, 0x1c, 0xc8, 0x4e, 0x5a, 0x28, 0xfb, 0xba, 0x87, 0x74, 0x76, 0x44 },
111 		{ 0xec, 0x63, 0x92, 0xca, 0x4f, 0x6b, 0x9e, 0xb1, 0x9f, 0xec, 0x3b, 0x2c, 0x15, 0x30, 0xfd, 0x2a },
112 		{ 0x05, 0x4d, 0xfd, 0xb8, 0x9d, 0x8a, 0xa2, 0xdd, 0x26, 0x47, 0xc6, 0xfb, 0x4f, 0x23, 0x67, 0x6d }
113 	},
114 
115 	/* 3 channels */
116 	{
117 		{ 0xad, 0x05, 0xda, 0xf3, 0x7a, 0xa1, 0x94, 0xdb, 0x0c, 0x61, 0x06, 0xb2, 0x94, 0x39, 0x6c, 0xa9 },
118 		{ 0x8b, 0xcc, 0x41, 0x4d, 0xe9, 0xe3, 0xc2, 0x61, 0x61, 0x8a, 0x8b, 0x22, 0xc6, 0x4e, 0xac, 0xa7 },
119 		{ 0x8a, 0xce, 0x97, 0xc1, 0x86, 0xae, 0xbc, 0x73, 0x88, 0x8b, 0x35, 0x5a, 0x37, 0x33, 0xf9, 0xcf },
120 		{ 0x69, 0x59, 0xe8, 0x38, 0x29, 0x80, 0x80, 0x21, 0xb1, 0xd2, 0xba, 0xf6, 0x28, 0xd6, 0x6a, 0x83 }
121 	},
122 
123 	/* 4 channels */
124 	{
125 		{ 0x61, 0x40, 0x75, 0xef, 0x22, 0xf1, 0x0f, 0xa6, 0x08, 0x6c, 0x88, 0xff, 0x2c, 0x4e, 0x98, 0x0b },
126 		{ 0xa0, 0x77, 0x3a, 0x59, 0x4a, 0xbf, 0xd0, 0x5c, 0xcc, 0xe3, 0xb9, 0x83, 0x2b, 0xf3, 0xdf, 0x1a },
127 		{ 0xdb, 0xd7, 0xf1, 0x82, 0x13, 0x60, 0x42, 0x7c, 0x84, 0xe6, 0xcf, 0x30, 0xab, 0xa2, 0x64, 0xf1 },
128 		{ 0x4a, 0x9a, 0xad, 0x53, 0x05, 0x74, 0xb1, 0x1c, 0xb8, 0xd4, 0xae, 0x78, 0x13, 0xf6, 0x2a, 0x11 }
129 	},
130 
131 	/* 5 channels */
132 	{
133 		{ 0xcc, 0xca, 0x44, 0xc0, 0x54, 0xe2, 0xc9, 0xba, 0x99, 0x32, 0xc9, 0x65, 0xf3, 0x3e, 0x44, 0x34},
134 		{ 0x40, 0x38, 0x6a, 0xdd, 0xde, 0x89, 0x10, 0x3c, 0x8e, 0xec, 0xdf, 0x15, 0x53, 0x4c, 0x2c, 0x92 },
135 		{ 0xc8, 0x95, 0x0a, 0x7c, 0x17, 0x30, 0xc0, 0xac, 0x8e, 0x34, 0xdb, 0x79, 0x76, 0x64, 0x7c, 0x6e },
136 		{ 0x3f, 0x06, 0x11, 0x8a, 0x8d, 0x80, 0xb5, 0x4f, 0x8b, 0xb5, 0x8e, 0xb3, 0x27, 0x3e, 0x41, 0xe8 }
137 	},
138 
139 	/* 6 channels */
140 	{
141 		{ 0x61, 0xe4, 0xbd, 0xb1, 0xc0, 0x2f, 0xf4, 0x4c, 0x6e, 0x09, 0x5a, 0xbd, 0x90, 0x18, 0x8b, 0x62 },
142 		{ 0x47, 0xe7, 0x6e, 0x3b, 0x18, 0x86, 0x60, 0x1b, 0x09, 0x62, 0xc6, 0xc9, 0x7c, 0x4c, 0x03, 0xb5 },
143 		{ 0x70, 0x57, 0xbf, 0x67, 0x66, 0x0f, 0xe3, 0x0a, 0x6c, 0xd2, 0x97, 0x66, 0xa2, 0xd2, 0xe4, 0x79 },
144 		{ 0xaa, 0x3f, 0xc7, 0xf5, 0x7a, 0xa5, 0x46, 0xf7, 0xea, 0xe3, 0xd5, 0x1a, 0xa4, 0x62, 0xbe, 0xfa }
145 	},
146 
147 	/* 7 channels */
148 	{
149 		{ 0x7c, 0x8d, 0xd2, 0x8c, 0xfd, 0x91, 0xbb, 0x77, 0x6f, 0x0e, 0xf0, 0x39, 0x1f, 0x39, 0xc4, 0xac },
150 		{ 0xfb, 0xab, 0x18, 0x3f, 0x1e, 0x1d, 0xa5, 0x77, 0xe0, 0x5c, 0xea, 0x45, 0x6f, 0x64, 0xa4, 0x64 },
151 		{ 0xe3, 0xac, 0x33, 0x50, 0xc1, 0xb1, 0x93, 0xfb, 0xca, 0x4b, 0x15, 0xcb, 0x2d, 0xcd, 0xd5, 0xef },
152 		{ 0x10, 0xfb, 0x02, 0x83, 0x76, 0x0d, 0xe5, 0xd2, 0x3b, 0xb1, 0x4c, 0x78, 0x3b, 0x73, 0xf7, 0x1a }
153 	},
154 
155 	/* 8 channels */
156 	{
157 		{ 0x65, 0x7b, 0xe5, 0x92, 0xe2, 0x1c, 0x95, 0x3e, 0xd7, 0x2f, 0x64, 0xa0, 0x86, 0xec, 0x1a, 0xed },
158 		{ 0x9d, 0x04, 0x8f, 0xa4, 0xea, 0x10, 0xec, 0xb8, 0xa3, 0x88, 0xe2, 0x5d, 0x3c, 0xe2, 0xfb, 0x94 },
159 		{ 0x5a, 0xd3, 0xd2, 0x75, 0x6a, 0xfa, 0xa7, 0x42, 0xf3, 0xbf, 0x0e, 0xbc, 0x90, 0x2a, 0xf8, 0x5f },
160 		{ 0x76, 0xe1, 0xe5, 0xf6, 0xe3, 0x44, 0x08, 0x29, 0xae, 0x79, 0x19, 0xeb, 0xa8, 0x57, 0x16, 0x2a }
161 	}
162 };
163 
164 #define MAX_CHANNEL_COUNT	8
165 #define MD5_SAMPLE_COUNT	64
166 
test_md5_codec(void)167 static FLAC__bool test_md5_codec(void)
168 {
169 	FLAC__int32 arrays[MAX_CHANNEL_COUNT][MD5_SAMPLE_COUNT], *pointer[MAX_CHANNEL_COUNT], **signal;
170 	uint32_t chan, byte_size, seed = 0x12345679;
171 
172 	/* Set up signal data using a trivial Linear Congruent PRNG. */
173 	signal = &pointer[0];
174 	for (chan = 0 ; chan < MAX_CHANNEL_COUNT ; chan ++) {
175 		uint32_t k;
176 		pointer[chan] = arrays [chan];
177 		for (k = 0 ; k < MD5_SAMPLE_COUNT ; k++) {
178 			seed = seed * 1103515245 + 12345;
179 			arrays[chan][k] = seed;
180 		}
181 	}
182 
183 	for (chan = 1 ; chan <= MAX_CHANNEL_COUNT ; chan ++) {
184 		for (byte_size = 1 ; byte_size <= 4 ; byte_size ++) {
185 			if (! test_md5_accumulate((const FLAC__int32 * const *) signal, chan, MD5_SAMPLE_COUNT, byte_size, target_digests[chan-1][byte_size-1]))
186 				return false;
187 		}
188 	}
189 
190 	return true;
191 }
192 
test_md5_accumulate(const FLAC__int32 * const * signal,uint32_t channels,uint32_t samples,uint32_t bytes_per_sample,const FLAC__byte target_digest[16])193 static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal, uint32_t channels, uint32_t samples, uint32_t bytes_per_sample, const FLAC__byte target_digest [16])
194 {
195 	FLAC__MD5Context ctx;
196 	FLAC__byte digest[16];
197 
198 	memset(&ctx, 0, sizeof (ctx));
199 
200 	printf("testing FLAC__MD5Accumulate (samples=%u, channels=%u, bytes_per_sample=%u) ... ", samples, channels, bytes_per_sample);
201 
202 	FLAC__MD5Init(&ctx);
203 	FLAC__MD5Accumulate(&ctx, signal, channels, samples, bytes_per_sample);
204 	FLAC__MD5Final(digest, &ctx);
205 
206 	if (memcmp(digest, target_digest, sizeof (digest))) {
207 		int k ;
208 
209 		printf("\nFAILED, expected MD5 sum ");
210 		for (k = 0 ; k < 16 ; k++)
211 			printf("%02x", (target_digest [k] & 0xff));
212 		printf (" but got ");
213 		for (k = 0 ; k < 16 ; k++)
214 			printf("%02x", (digest [k] & 0xff));
215 		puts("\n");
216 		return false;
217 	}
218 
219 	printf("OK\n");
220 	return true;
221 }
222