Commit 275f684e authored by Josh Coalson's avatar Josh Coalson
Browse files

add more tests (but still many left)

parent b7c25063
......@@ -16,6 +16,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "metadata_utils.h"
#include "FLAC/assert.h"
#include "FLAC/file_decoder.h"
#include "FLAC/metadata.h"
......@@ -23,6 +24,14 @@
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcmp() */
#if defined _MSC_VER || defined __MINGW32__
#include <io.h> /* for chmod(), unlink */
#endif
#include <sys/stat.h> /* for stat(), chmod() */
#if defined _WIN32 && !defined __CYGWIN__
#else
#include <unistd.h> /* for unlink() */
#endif
typedef struct {
FLAC__bool error_occurred;
......@@ -32,12 +41,91 @@ typedef struct {
FILE *file;
} encoder_client_struct;
static FLAC__bool die(const char *msg)
typedef struct {
FLAC__StreamMetaData *blocks[64];
unsigned num_blocks;
} our_metadata_struct;
static const char *flacfile_ = "metadata.flac";
static our_metadata_struct our_metadata_;
static unsigned mc_our_blocknumber_ = 0;
static FLAC__bool die_(const char *msg)
{
printf("ERROR: %s\n", msg);
return false;
}
static FLAC__bool die_ss_(const char *msg, FLAC__MetaData_SimpleIterator *siterator)
{
printf("ERROR: %s\n", msg);
printf(" status=%s\n", FLAC__MetaData_SimpleIteratorStatusString[FLAC__metadata_simple_iterator_status(siterator)]);
return false;
}
static FLAC__bool replace_in_our_metadata_(FLAC__StreamMetaData *block, unsigned position, FLAC__bool copy)
{
unsigned i;
FLAC__StreamMetaData *obj = block;
FLAC__ASSERT(position < our_metadata_.num_blocks);
if(copy) {
if(0 == (obj = FLAC__metadata_object_copy(block)))
return die_("during FLAC__metadata_object_copy()");
}
FLAC__metadata_object_delete(our_metadata_.blocks[position]);
our_metadata_.blocks[position] = obj;
/* set the is_last flags */
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
return true;
}
static FLAC__bool insert_to_our_metadata_(FLAC__StreamMetaData *block, unsigned position, FLAC__bool copy)
{
unsigned i;
FLAC__StreamMetaData *obj = block;
if(copy) {
if(0 == (obj = FLAC__metadata_object_copy(block)))
return die_("during FLAC__metadata_object_copy()");
}
if(position > our_metadata_.num_blocks) {
position = our_metadata_.num_blocks;
}
else {
for(i = our_metadata_.num_blocks; i > position; i--)
our_metadata_.blocks[i] = our_metadata_.blocks[i-1];
}
our_metadata_.blocks[position] = obj;
our_metadata_.num_blocks++;
/* set the is_last flags */
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
return true;
}
static void delete_from_our_metadata_(unsigned position)
{
unsigned i;
FLAC__ASSERT(position < our_metadata_.num_blocks);
FLAC__metadata_object_delete(our_metadata_.blocks[position]);
for(i = position; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i] = our_metadata_.blocks[i+1];
our_metadata_.num_blocks--;
/* set the is_last flags */
if(our_metadata_.num_blocks > 0) {
for(i = 0; i < our_metadata_.num_blocks - 1; i++)
our_metadata_.blocks[i]->is_last = false;
our_metadata_.blocks[i]->is_last = true;
}
}
static FLAC__StreamDecoderWriteStatus decoder_write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
{
(void)decoder, (void)frame, (void)buffer, (void)client_data;
......@@ -71,16 +159,34 @@ static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const
(void)encoder, (void)metadata, (void)client_data;
}
static FLAC__bool generate_file_(const char *filename)
static FLAC__bool generate_file_(const char *input_filename)
{
FLAC__StreamEncoder *encoder;
encoder_client_struct encoder_client_data;
FILE *file;
FLAC__byte buffer[4096];
FLAC__int32 samples[4096];
unsigned i, n;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != input_filename);
our_metadata_.num_blocks = 0;
printf("generating FLAC file for test\n");
if(0 == (file = fopen(input_filename, "rb")))
return die_("opening input file");
if(0 == (encoder_client_data.file = fopen(flacfile_, "wb"))) {
fclose(file);
return die_("opening output file");
}
encoder = FLAC__stream_encoder_new();
if(0 == encoder)
return die("creating the encoder instance");
if(0 == encoder) {
fclose(file);
fclose(encoder_client_data.file);
return die_("creating the encoder instance");
}
FLAC__stream_encoder_set_streamable_subset(encoder, true);
FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
......@@ -105,8 +211,38 @@ static FLAC__bool generate_file_(const char *filename)
FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
FLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
return die("initializing encoder");
if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK) {
fclose(file);
fclose(encoder_client_data.file);
return die_("initializing encoder");
}
while(!feof(file)) {
n = fread(buffer, 1, sizeof(buffer), file);
if(n > 0) {
for(i = 0; i < n; i++)
samples[i] = (FLAC__int32)((signed char)buffer[i]);
/* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */
if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
fclose(file);
fclose(encoder_client_data.file);
return die_("during encoding");
}
}
else if(!feof(file)) {
fclose(file);
fclose(encoder_client_data.file);
return die_("reading input file");
}
}
fclose(file);
fclose(encoder_client_data.file);
if(FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK)
FLAC__stream_encoder_finish(encoder);
FLAC__stream_encoder_delete(encoder);
return true;
}
......@@ -119,10 +255,13 @@ static FLAC__bool test_file_(const char *filename, void (*metadata_callback)(con
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != metadata_callback);
mc_our_blocknumber_ = 0;
decoder_client_data.error_occurred = false;
printf("testing '%s'... ", filename);
if(0 == (decoder = FLAC__file_decoder_new()))
return die("couldn't allocate memory");
return die_("couldn't allocate memory");
FLAC__file_decoder_set_md5_checking(decoder, true);
FLAC__file_decoder_set_filename(decoder, filename);
......@@ -134,23 +273,819 @@ static FLAC__bool test_file_(const char *filename, void (*metadata_callback)(con
if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
FLAC__file_decoder_finish(decoder);
FLAC__file_decoder_delete(decoder);
return die("initializing decoder\n");
return die_("initializing decoder\n");
}
if(!FLAC__file_decoder_process_metadata(decoder)) {
FLAC__file_decoder_finish(decoder);
FLAC__file_decoder_delete(decoder);
return die("decoding file\n");
return die_("decoding file\n");
}
FLAC__file_decoder_finish(decoder);
FLAC__file_decoder_delete(decoder);
if(!decoder_client_data.error_occurred)
printf("PASSED\n");
return !decoder_client_data.error_occurred;
}
int test_metadata_file_manipulation()
static FLAC__bool change_stats_(const char *filename, FLAC__bool read_only)
{
struct stat stats;
if(0 == stat(filename, &stats)) {
if(read_only) {
stats.st_mode &= ~S_IWUSR;
stats.st_mode &= ~S_IWGRP;
stats.st_mode &= ~S_IWOTH;
}
else {
stats.st_mode |= S_IWUSR;
stats.st_mode |= S_IWGRP;
stats.st_mode |= S_IWOTH;
}
if(0 != chmod(filename, stats.st_mode))
return die_("during chmod()");
}
else
return die_("during stat()");
return true;
}
static FLAC__bool remove_file_(const char *filename)
{
if(!change_stats_(filename, /*read_only=*/false) || 0 != unlink(filename))
return die_("removing file");
return true;
}
static void mc_null_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
{
(void)decoder, (void)metadata, (void)client_data;
}
static void mc_ours_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
{
decoder_client_struct *dcd = (decoder_client_struct*)client_data;
(void)decoder;
/* don't bother checking if we've already hit an error */
if(dcd->error_occurred)
return;
printf("%d... ", mc_our_blocknumber_);
if(mc_our_blocknumber_ >= our_metadata_.num_blocks) {
(void)die_("got more metadata blocks than expected");
dcd->error_occurred = true;
}
else {
if(!compare_block_(metadata, our_metadata_.blocks[mc_our_blocknumber_])) {
(void)die_("metadata block mismatch");
dcd->error_occurred = true;
}
}
mc_our_blocknumber_++;
}
static FLAC__bool test_level_0_(const char *progname)
{
FLAC__StreamMetaData_StreamInfo streaminfo;
printf("\n\n++++++ testing level 0 interface\n");
if(!generate_file_(progname))
return false;
if(!test_file_(flacfile_, mc_null_))
return false;
if(!FLAC__metadata_get_streaminfo(flacfile_, &streaminfo))
return die_("during FLAC__metadata_get_streaminfo()");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(streaminfo.channels != 1)
return die_("mismatch in streaminfo.channels");
if(streaminfo.bits_per_sample != 8)
return die_("mismatch in streaminfo.bits_per_sample");
if(streaminfo.sample_rate != 44100)
return die_("mismatch in streaminfo.sample_rate");
if(streaminfo.min_blocksize != 576)
return die_("mismatch in streaminfo.min_blocksize");
if(streaminfo.max_blocksize != 576)
return die_("mismatch in streaminfo.max_blocksize");
if(!remove_file_(flacfile_))
return false;
return true;
}
static FLAC__bool test_level_1_(const char *progname)
{
FLAC__MetaData_SimpleIterator *siterator;
FLAC__MetaData_Iterator *iterator;
FLAC__MetaData_Chain *chain;
FLAC__StreamMetaData *block, *app, *padding;
FLAC__byte data[1000];
unsigned i = 0, our_current_position = 0;
printf("\n\n++++++ testing level 1 interface\n");
/************************************************************/
printf("simple iterator on read-only file\n");
if(!generate_file_(progname))
return false;
if(!change_stats_(flacfile_, /*read_only=*/true))
return false;
if(!test_file_(flacfile_, mc_null_))
return false;
if(0 == (siterator = FLAC__metadata_simple_iterator_new()))
return die_("FLAC__metadata_simple_iterator_new()");
if(!FLAC__metadata_simple_iterator_init(siterator, flacfile_, false))
return die_("ERROR: FLAC__metadata_simple_iterator_init()\n");
printf("is writable = %u\n", (unsigned)FLAC__metadata_simple_iterator_is_writable(siterator));
if(FLAC__metadata_simple_iterator_is_writable(siterator))
return die_("iterator claims file is writable when it should not be\n");
printf("iterate forwards\n");
if(FLAC__metadata_simple_iterator_get_block_type(siterator) != FLAC__METADATA_TYPE_STREAMINFO)
return die_("expected STREAMINFO type from FLAC__metadata_simple_iterator_get_block_type()");
if(0 == (block = FLAC__metadata_simple_iterator_get_block(siterator)))
return die_("getting block 0");
if(block->type != FLAC__METADATA_TYPE_STREAMINFO)
return die_("expected STREAMINFO type");
if(block->is_last)
return die_("expected is_last to be false");
if(block->length != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
return die_("bad STREAMINFO length");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(block->data.stream_info.channels != 1)
return die_("mismatch in channels");
if(block->data.stream_info.bits_per_sample != 8)
return die_("mismatch in bits_per_sample");
if(block->data.stream_info.sample_rate != 44100)
return die_("mismatch in sample_rate");
if(block->data.stream_info.min_blocksize != 576)
return die_("mismatch in min_blocksize");
if(block->data.stream_info.max_blocksize != 576)
return die_("mismatch in max_blocksize");
(void)insert_to_our_metadata_(block, our_current_position, /*copy=*/false);
if(!FLAC__metadata_simple_iterator_next(siterator))
return die_("forward iterator ended early");
our_current_position++;
if(FLAC__metadata_simple_iterator_get_block_type(siterator) != FLAC__METADATA_TYPE_PADDING)
return die_("expected PADDING type from FLAC__metadata_simple_iterator_get_block_type()");
if(0 == (block = FLAC__metadata_simple_iterator_get_block(siterator)))
return die_("getting block 1");
if(block->type != FLAC__METADATA_TYPE_PADDING)
return die_("expected PADDING type");
if(!block->is_last)
return die_("expected is_last to be true");
/* check to see if some basic data matches (c.f. generate_file_()) */
if(block->length != 12345)
return die_("bad STREAMINFO length");
(void)insert_to_our_metadata_(block, our_current_position, /*copy=*/false);
if(FLAC__metadata_simple_iterator_next(siterator))
return die_("forward iterator returned true but should have returned false");
printf("iterate backwards\n");
if(!FLAC__metadata_simple_iterator_prev(siterator))
return die_("reverse iterator ended early");
if(FLAC__metadata_simple_iterator_prev(siterator))
return die_("reverse iterator returned true but should have returned false");
printf("testing FLAC__metadata_simple_iterator_set_block() on read-only file...\n");
if(!FLAC__metadata_simple_iterator_set_block(siterator, (void*)99, false)) {
printf("PASSED. FLAC__metadata_simple_iterator_set_block() returned false like it should\n");
printf(" status=%s\n", FLAC__MetaData_SimpleIteratorStatusString[FLAC__metadata_simple_iterator_status(siterator)]);
}
else
return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have");
FLAC__metadata_simple_iterator_delete(siterator);
/************************************************************/
printf("simple iterator on writable file\n");
if(!change_stats_(flacfile_, /*read-only=*/false))
return false;
printf("creating APPLICATION block\n");
if(0 == (app = FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)))
return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_APPLICATION)");
memcpy(app->data.application.id, "duh", 4);
printf("creating PADDING block\n");
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
return die_("FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)");
padding->length = 20;
if(0 == (siterator = FLAC__metadata_simple_iterator_new()))
return die_("FLAC__metadata_simple_iterator_new()");
if(!FLAC__metadata_simple_iterator_init(siterator, flacfile_, /*preserve_file_stats=*/false))
return die_("ERROR: FLAC__metadata_simple_iterator_init()\n");
our_current_position = 0;
printf("is writable = %u\n", (unsigned)FLAC__metadata_simple_iterator_is_writable(siterator));
printf("[S]P try to write over STREAMINFO block...\n");
if(!FLAC__metadata_simple_iterator_set_block(siterator, app, false)) {
printf("FLAC__metadata_simple_iterator_set_block() returned false like it should\n");
printf(" status=%s\n", FLAC__MetaData_SimpleIteratorStatusString[FLAC__metadata_simple_iterator_status(siterator)]);
}
else
return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have");
printf("[S]P insert PADDING after, don't expand into padding\n");
if(!FLAC__metadata_simple_iterator_insert_block_after(siterator, padding, false))
return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(siterator, app, false)", siterator);
if(!insert_to_our_metadata_(padding, ++our_current_position, /*copy=*/true))
return false;
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("S[P]P prev\n");
if(!FLAC__metadata_simple_iterator_prev(siterator))
return die_("iterator ended early\n");
our_current_position--;
printf("[S]PP delete (STREAMINFO block), must fail\n");
if(FLAC__metadata_simple_iterator_delete_block(siterator, false))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(siterator, false) should have returned false", siterator);
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("[S]PP next\n");
if(!FLAC__metadata_simple_iterator_next(siterator))
return die_("iterator ended early\n");
our_current_position++;
printf("S[P]P delete (middle block), don't replace with padding\n");
if(!FLAC__metadata_simple_iterator_delete_block(siterator, false))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(siterator, false)", siterator);
delete_from_our_metadata_(our_current_position--);
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("[S]P next\n");
if(!FLAC__metadata_simple_iterator_next(siterator))
return die_("iterator ended early\n");
our_current_position++;
printf("S[P] delete (last block), don't replace with padding\n");
if(!FLAC__metadata_simple_iterator_delete_block(siterator, false))
return die_ss_("FLAC__metadata_simple_iterator_delete_block(siterator, false)", siterator);
delete_from_our_metadata_(our_current_position--);
if(!test_file_(flacfile_, mc_ours_))
return false;
return true;
printf("S[P]P insert APPLICATION after, don't expand into padding\n");
if(!FLAC__metadata_simple_iterator_insert_block_after(siterator, app, false))
return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(siterator, app, false)", siterator);
if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true))
return false;
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("SP[A]P insert APPLICATION after, expand into padding of exceeding size\n");
app->data.application.id[0] = 'e'; /* twiddle the id so that our comparison doesn't miss transposition */
if(!FLAC__metadata_simple_iterator_insert_block_after(siterator, app, true))
return die_ss_("FLAC__metadata_simple_iterator_insert_block_after(siterator, app, true)", siterator);
if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true))
return false;
our_metadata_.blocks[our_current_position+1]->length -= 4 + app->length;
if(!test_file_(flacfile_, mc_ours_))
return false;
if(!FLAC__metadata_simple_iterator_next(siterator))
return die_("iterator ended early\n");
printf("SPA[A]P set APPLICATION, expand into padding of exceeding size\n");
app->data.application.id[0] = 'f'; /* twiddle the id */
if(!FLAC__metadata_simple_iterator_set_block(siterator, app, true))
return die_ss_("FLAC__metadata_simple_iterator_set_block(siterator, app, true)", siterator);
if(!insert_to_our_metadata_(app, ++our_current_position, /*copy=*/true))
return false;
our_metadata_.blocks[our_current_position+1]->length -= 4 + app->length;
if(!test_file_(flacfile_, mc_ours_))
return false;
if(!FLAC__metadata_simple_iterator_next(siterator))
return die_("iterator ended early\n");
our_current_position++;
if(FLAC__metadata_simple_iterator_next(siterator))
return die_("iterator should have ended but didn't\n");
printf("[S]PAAAP set STREAMINFO (change sample rate)\n");
while(FLAC__metadata_simple_iterator_prev(siterator))
our_current_position--;
block = FLAC__metadata_simple_iterator_get_block(siterator);
FLAC__ASSERT(our_current_position == 0);
block->data.stream_info.sample_rate = 32000;
if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true))
return die_("copying object");
if(!FLAC__metadata_simple_iterator_set_block(siterator, block, false))
return die_ss_("FLAC__metadata_simple_iterator_set_block(siterator, block, false)", siterator);
FLAC__metadata_object_delete(block);
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("SPAA[A]P set block (grow), don't expand into padding\n");
while(FLAC__metadata_simple_iterator_next(siterator))
our_current_position++;
if(!FLAC__metadata_simple_iterator_prev(siterator))
return die_("iterator ended early\n");
our_current_position--;
app->data.application.id[0] = 'g'; /* twiddle the id */
if(!FLAC__metadata_object_application_set_data(app, data, sizeof(data), true))
return die_("setting APPLICATION data");
if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true))
return die_("copying object");
if(!FLAC__metadata_simple_iterator_set_block(siterator, app, false))
return die_ss_("FLAC__metadata_simple_iterator_set_block(siterator, app, false)", siterator);
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("SPAA[A]P set block (shrink), don't fill in with padding\n");
app->data.application.id[0] = 'h'; /* twiddle the id */
if(!FLAC__metadata_object_application_set_data(app, data, 12, true))
return die_("setting APPLICATION data");
if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true))
return die_("copying object");
if(!FLAC__metadata_simple_iterator_set_block(siterator, app, false))
return die_ss_("FLAC__metadata_simple_iterator_set_block(siterator, app, false)", siterator);
if(!test_file_(flacfile_, mc_ours_))
return false;
printf("SPAA[A]P set block (grow), expand into padding\n");
app->data.application.id[0] = 'i'; /* twiddle the id */
if(!FLAC__metadata_object_application_set_data(app, data, sizeof(data), true))
return die_("setting APPLICATION data");
if(!replace_in_our_metadata_(app, our_current_position, /*copy=*/true))
return die_("copying object");
our_metadata_.blocks[our_current_position+1]->length -= (sizeof(data) - 12);
if(!FLAC__metadata_simple_iterator_set_block(siterator, app, true))
return die_ss_("FLAC__metadata_simple_iterator_set_block(siterator, app, true)", siterator);
//@@@need to test cases when all follow padding is used up exactly
if(!test_file_(flacfile_, mc_ours_))
return false;