test_libFLAC/md5.c : Fix the tests.

The previous version of this test worked on amd64 and powerpc64 but
failed on armhf. Investigation show that the previous version of the
test data was generated for a case where the ARRAY_LEN macro was
exploiting undefined behaviour.
Fix was ditching the ARRAY_LEN macro and then recalculating the
target data.
This commit is contained in:
Erik de Castro Lopo
2014-07-01 20:00:07 +10:00
parent c69a20944f
commit 74d8133387

View File

@@ -53,6 +53,7 @@ static FLAC__bool test_md5_clear_context(void)
{
FLAC__MD5Context ctx;
FLAC__byte digest[16];
FLAC__byte target[16] = { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e };
unsigned k ;
char * cptr;
@@ -73,102 +74,116 @@ static FLAC__bool test_md5_clear_context(void)
return false;
}
}
printf("OK\n");
printf("testing digest correct for zero data ... ");
if (memcmp(digest, target, sizeof (digest))) {
printf("\nFAILED, expected MD5 sum ");
for (k = 0 ; k < 16 ; k++)
printf("%02x", (target [k] & 0xff));
printf (" but got ");
for (k = 0 ; k < 16 ; k++)
printf("%02x", (digest [k] & 0xff));
puts("\n");
return false;
}
puts("OK");
return true;
}
#define ARRAY_LEN(x) (sizeof (x) / sizeof (x [0]))
static FLAC__byte target_digests [8][4][16] =
{ /* 1 channel */
{ /* 1 byte per sample */
{ 0x41, 0x91, 0x23, 0x31, 0x5a, 0x22, 0x59, 0x2e, 0x31, 0x70, 0xb6, 0x7d, 0x0a, 0xa2, 0x69, 0x16 },
{ 0xc1, 0x9a, 0x5b, 0xeb, 0x57, 0x8f, 0x26, 0xeb, 0xfb, 0x34, 0x7c, 0xef, 0x04, 0x31, 0x6d, 0x7d },
/* 2 bytes per sample */
{ 0xab, 0x7d, 0x77, 0x99, 0xdc, 0x15, 0x4a, 0xbc, 0x37, 0x95, 0xe5, 0x26, 0xce, 0x3c, 0x3f, 0x25 },
{ 0xd4, 0x78, 0x90, 0xd3, 0xa9, 0x17, 0x4e, 0x76, 0xca, 0x4d, 0x27, 0x20, 0x98, 0x36, 0x8b, 0x2e },
/* 3 bytes per sample */
{ 0x03, 0x92, 0xa7, 0x6d, 0x78, 0xa8, 0x8a, 0x2b, 0x7c, 0x80, 0x8e, 0x6c, 0x09, 0xf6, 0x8b, 0x73 },
{ 0x5a, 0x4b, 0xd6, 0xac, 0xa1, 0x70, 0x84, 0x19, 0x7c, 0x0d, 0xfb, 0x5b, 0xa9, 0x7b, 0xcb, 0x54 },
/* 4 bytes per sample */
{ 0xbd, 0x04, 0x81, 0x60, 0xee, 0x96, 0xb8, 0x42, 0x18, 0x65, 0x9d, 0x40, 0x21, 0x67, 0x03, 0x87 }
{ 0x79, 0xd5, 0x7a, 0x32, 0x06, 0x0b, 0xfe, 0x46, 0xa3, 0xe7, 0xba, 0xc5, 0xf7, 0x48, 0x6f, 0x50 }
},
/* 2 channels */
{
{ 0xae, 0x46, 0xa7, 0x20, 0x76, 0xf2, 0xa3, 0x76, 0xe0, 0xaf, 0xc4, 0x33, 0x5a, 0x90, 0x6d, 0x38 },
{ 0x52, 0x43, 0x94, 0xa0, 0xe8, 0xa8, 0xa8, 0x2e, 0xfe, 0x90, 0x1c, 0x0a, 0x34, 0x89, 0xb2, 0x40 },
{ 0xf4, 0x9f, 0xa5, 0xf0, 0x8d, 0x4e, 0x3d, 0x68, 0x36, 0xdc, 0x43, 0x9a, 0x88, 0x84, 0xba, 0xd0 },
{ 0xb1, 0xcf, 0xe2, 0x11, 0x45, 0x57, 0xe8, 0x98, 0xe3, 0xdc, 0x7b, 0xcd, 0x4e, 0xe3, 0xde, 0x86 }
{ 0x89, 0xac, 0xcf, 0x91, 0xf1, 0x8c, 0xea, 0xab, 0x46, 0x12, 0x74, 0xbc, 0x4e, 0x82, 0xbe, 0x7d },
{ 0xb9, 0x17, 0x16, 0x5b, 0xd8, 0x1c, 0xc8, 0x4e, 0x5a, 0x28, 0xfb, 0xba, 0x87, 0x74, 0x76, 0x44 },
{ 0xec, 0x63, 0x92, 0xca,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e8, 0xe6, 0x39, 0x73, 0x18, 0xc2, 0xa0 },
{ 0x6a, 0x28, 0xa1, 0xd8, 0x7c, 0x2f, 0xd9, 0x79, 0x35, 0xa0, 0x6c, 0x53, 0x81, 0x7e, 0x40, 0x9b },
{ 0xdf, 0x4e, 0xe2, 0x37, 0x88, 0x3d, 0xfd, 0x94, 0xf4, 0x47, 0x00, 0x76, 0x55, 0x6f, 0xea, 0xfd }
{ 0x65, 0x7b, 0xe5, 0x92, 0xe2, 0x1c, 0x95, 0x3e, 0xd7, 0x2f, 0x64, 0xa0, 0x86, 0xec, 0x1a, 0xed },
{ 0x9d, 0x04, 0x8f, 0xa4, 0xea, 0x10, 0xec, 0xb8, 0xa3, 0x88, 0xe2, 0x5d, 0x3c, 0xe2, 0xfb, 0x94 },
{ 0x5a, 0xd3, 0xd2, 0x75, 0x6a, 0xfa, 0xa7, 0x42, 0xf3, 0xbf, 0x0e, 0xbc, 0x90, 0x2a, 0xf8, 0x5f },
{ 0x76, 0xe1, 0xe5, 0xf6, 0xe3, 0x44, 0x08, 0x29, 0xae, 0x79, 0x19, 0xeb, 0xa8, 0x57, 0x16, 0x2a }
}
};
#define MAX_CHANNEL_COUNT 8
#define MD5_SAMPLE_COUNT 64
static FLAC__bool test_md5_codec(void)
{
FLAC__int32 arrays[8][64], *pointer[8], **signal;
FLAC__int32 arrays[MAX_CHANNEL_COUNT][MD5_SAMPLE_COUNT], *pointer[MAX_CHANNEL_COUNT], **signal;
FLAC__int32 seed = 0x12345679;
unsigned chan, byte_size;
/* Set up signal data using a trival Linear Congruent PRNG. */
signal = &pointer[0];
for (chan = 0 ; chan < ARRAY_LEN (arrays) ; chan ++) {
for (chan = 0 ; chan < MAX_CHANNEL_COUNT ; chan ++) {
unsigned k;
pointer[chan] = arrays [chan];
for (k = 0 ; k < ARRAY_LEN (arrays [0]) ; k++) {
for (k = 0 ; k < MD5_SAMPLE_COUNT ; k++) {
seed = seed * 1103515245 + 12345;
arrays[chan][k] = seed;
}
}
for (chan = 1 ; chan <= ARRAY_LEN (arrays) ; chan ++) {
for (chan = 1 ; chan <= MAX_CHANNEL_COUNT ; chan ++) {
for (byte_size = 1 ; byte_size <= 4 ; byte_size ++) {
if (! test_md5_accumulate((const FLAC__int32 * const *) signal, chan, ARRAY_LEN (signal [0]), byte_size, target_digests[chan-1][byte_size-1]))
if (! test_md5_accumulate((const FLAC__int32 * const *) signal, chan, MD5_SAMPLE_COUNT, byte_size, target_digests[chan-1][byte_size-1]))
return false;
}
}
@@ -183,7 +198,7 @@ static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal, unsign
memset(&ctx, 0, sizeof (ctx));
printf("testing FLAC__MD5Accumulate (channels=%d, bytes_per_sample=%d) ... ", channels, bytes_per_sample);
printf("testing FLAC__MD5Accumulate (samples=%u, channels=%u, bytes_per_sample=%u) ... ", samples, channels, bytes_per_sample);
FLAC__MD5Init(&ctx);
FLAC__MD5Accumulate(&ctx, signal, channels, samples, bytes_per_sample);
@@ -194,10 +209,10 @@ static FLAC__bool test_md5_accumulate(const FLAC__int32 * const * signal, unsign
printf("\nFAILED, expected MD5 sum ");
for (k = 0 ; k < 16 ; k++)
printf("%02x", (digest [k] & 0xff));
printf("%02x", (target_digest [k] & 0xff));
printf (" but got ");
for (k = 0 ; k < 16 ; k++)
printf("%02x", (target_digest [k] & 0xff));
printf("%02x", (digest [k] & 0xff));
puts("\n");
return false;
}