X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Ftest_libFLAC%2Fmetadata_manip.c;h=d8e73d9378a3da8900a683877389ba970fb692d3;hb=49623ea2d08b104b5a08088d72d61717d0d057ab;hp=2c2323799f3ea76e42a5dd1d4ca308b48a22a79e;hpb=afae69f0e3491e69362f7f582ec6b3c8605ac83b;p=platform%2Fupstream%2Fflac.git diff --git a/src/test_libFLAC/metadata_manip.c b/src/test_libFLAC/metadata_manip.c index 2c23237..d8e73d9 100644 --- a/src/test_libFLAC/metadata_manip.c +++ b/src/test_libFLAC/metadata_manip.c @@ -1,5 +1,5 @@ /* test_libFLAC - Unit tester for libFLAC - * Copyright (C) 2002,2003 Josh Coalson + * Copyright (C) 2002,2003,2004 Josh Coalson * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -25,6 +25,17 @@ #include #include /* for malloc() */ +#if defined _MSC_VER || defined __MINGW32__ +#include /* for utime() */ +#include /* for chmod() */ +#else +#include /* some flavors of BSD (like OS X) require this to get time_t */ +#include /* for utime() */ +#include /* for chown(), unlink() */ +#endif +#include /* for stat(), maybe chmod() */ + + /****************************************************************************** The general strategy of these tests (for interface levels 1 and 2) is to create a dummy FLAC file with a known set of initial metadata @@ -148,6 +159,202 @@ static void delete_from_our_metadata_(unsigned position) } } +/* + * This wad of functions supports filename- and callback-based chain reading/writing. + * Everything up to set_file_stats_() is copied from libFLAC/metadata_iterators.c + */ +FLAC__bool open_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + static const char *tempfile_suffix = ".metadata_edit"; + + if(0 == (*tempfilename = (char*)malloc(strlen(filename) + strlen(tempfile_suffix) + 1))) + return false; + strcpy(*tempfilename, filename); + strcat(*tempfilename, tempfile_suffix); + + if(0 == (*tempfile = fopen(*tempfilename, "wb"))) + return false; + + return true; +} + +void cleanup_tempfile_(FILE **tempfile, char **tempfilename) +{ + if(0 != *tempfile) { + (void)fclose(*tempfile); + *tempfile = 0; + } + + if(0 != *tempfilename) { + (void)unlink(*tempfilename); + free(*tempfilename); + *tempfilename = 0; + } +} + +FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != tempfile); + FLAC__ASSERT(0 != tempfilename); + FLAC__ASSERT(0 != *tempfilename); + + if(0 != *tempfile) { + (void)fclose(*tempfile); + *tempfile = 0; + } + +#if defined _MSC_VER || defined __MINGW32__ + if(unlink(filename) < 0) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } +#endif + + if(0 != rename(*tempfilename, filename)) { + cleanup_tempfile_(tempfile, tempfilename); + return false; + } + + cleanup_tempfile_(tempfile, tempfilename); + + return true; +} + +FLAC__bool get_file_stats_(const char *filename, struct stat *stats) +{ + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + return (0 == stat(filename, stats)); +} + +void set_file_stats_(const char *filename, struct stat *stats) +{ + struct utimbuf srctime; + + FLAC__ASSERT(0 != filename); + FLAC__ASSERT(0 != stats); + + srctime.actime = stats->st_atime; + srctime.modtime = stats->st_mtime; + (void)chmod(filename, stats->st_mode); + (void)utime(filename, &srctime); +#if !defined _MSC_VER && !defined __MINGW32__ + (void)chown(filename, stats->st_uid, -1); + (void)chown(filename, -1, stats->st_gid); +#endif +} + +#ifdef FLAC__VALGRIND_TESTING +static size_t chain_write_cb_(const void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle) +{ + FILE *stream = (FILE*)handle; + size_t ret = fwrite(ptr, size, nmemb, stream); + if(!ferror(stream)) + fflush(stream); + return ret; +} +#endif + +static int chain_seek_cb_(FLAC__IOHandle handle, FLAC__int64 offset, int whence) +{ + long o = (long)offset; + FLAC__ASSERT(offset == o); + return fseek((FILE*)handle, o, whence); +} + +static FLAC__int64 chain_tell_cb_(FLAC__IOHandle handle) +{ + return ftell((FILE*)handle); +} + +static int chain_eof_cb_(FLAC__IOHandle handle) +{ + return feof((FILE*)handle); +} + +static FLAC__bool write_chain_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats, FLAC__bool filename_based, const char *filename) +{ + if(filename_based) + return FLAC__metadata_chain_write(chain, use_padding, preserve_file_stats); + else { + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.eof = chain_eof_cb_; + + if(FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) { + struct stat stats; + FILE *file, *tempfile; + char *tempfilename; + if(preserve_file_stats) { + if(!get_file_stats_(filename, &stats)) + return false; + } + if(0 == (file = fopen(filename, "rb"))) + return false; /*@@@ chain status still says OK though */ + if(!open_tempfile_(filename, &tempfile, &tempfilename)) { + fclose(file); + cleanup_tempfile_(&tempfile, &tempfilename); + return false; /*@@@ chain status still says OK though */ + } + if(!FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, use_padding, (FLAC__IOHandle)file, callbacks, (FLAC__IOHandle)tempfile, callbacks)) { + fclose(file); + fclose(tempfile); + return false; + } + fclose(file); + fclose(tempfile); + file = tempfile = 0; + if(!transport_tempfile_(filename, &tempfile, &tempfilename)) + return false; + if(preserve_file_stats) + set_file_stats_(filename, &stats); + } + else { + FILE *file = fopen(filename, "r+b"); + if(0 == file) + return false; /*@@@ chain status still says OK though */ + if(!FLAC__metadata_chain_write_with_callbacks(chain, use_padding, (FLAC__IOHandle)file, callbacks)) + return false; + fclose(file); + } + } + + return true; +} + +static FLAC__bool read_chain_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool filename_based) +{ + if(filename_based) + return FLAC__metadata_chain_read(chain, flacfile_); + else { + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + + { + FLAC__bool ret; + FILE *file = fopen(filename, "rb"); + if(0 == file) + return false; /*@@@ chain status still says OK though */ + ret = FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks); + fclose(file); + return ret; + } + } +} + /* function for comparing our metadata to a FLAC__Metadata_Chain */ static FLAC__bool compare_chain_(FLAC__Metadata_Chain *chain, unsigned current_position, FLAC__StreamMetadata *current_block) @@ -177,7 +384,7 @@ static FLAC__bool compare_chain_(FLAC__Metadata_Chain *chain, unsigned current_p return die_("getting block from iterator"); } - if(!compare_block_(our_metadata_.blocks[i], block)) { + if(!mutils__compare_block(our_metadata_.blocks[i], block)) { FLAC__metadata_iterator_delete(iterator); return die_("metadata block mismatch"); } @@ -198,7 +405,7 @@ static FLAC__bool compare_chain_(FLAC__Metadata_Chain *chain, unsigned current_p printf("CURRENT_POSITION... "); fflush(stdout); - if(!compare_block_(our_metadata_.blocks[current_position], current_block)) + if(!mutils__compare_block(our_metadata_.blocks[current_position], current_block)) return die_("metadata block mismatch"); } @@ -254,7 +461,7 @@ static void decoder_metadata_callback_compare_(const FLAC__FileDecoder *decoder, dcd->error_occurred = true; } else { - if(!compare_block_(our_metadata_.blocks[mc_our_block_number_], metadata)) { + if(!mutils__compare_block(our_metadata_.blocks[mc_our_block_number_], metadata)) { (void)die_("metadata block mismatch"); dcd->error_occurred = true; } @@ -327,7 +534,7 @@ static FLAC__bool generate_file_() return true; } -static FLAC__bool test_file_(const char *filename, void (*metadata_callback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)) +static FLAC__bool test_file_(const char *filename, FLAC__FileDecoderMetadataCallback metadata_callback) { FLAC__FileDecoder *decoder; decoder_client_struct decoder_client_data; @@ -522,7 +729,7 @@ static FLAC__bool test_level_1_() printf("testing FLAC__metadata_simple_iterator_set_block() on read-only file...\n"); if(!FLAC__metadata_simple_iterator_set_block(iterator, (FLAC__StreamMetadata*)99, false)) - printf("PASSED. FLAC__metadata_simple_iterator_set_block() returned false like it should\n"); + printf("OK: FLAC__metadata_simple_iterator_set_block() returned false like it should\n"); else return die_("FLAC__metadata_simple_iterator_set_block() returned true but shouldn't have"); @@ -1087,7 +1294,7 @@ static FLAC__bool test_level_1_() return true; } -static FLAC__bool test_level_2_() +static FLAC__bool test_level_2_(FLAC__bool filename_based) { FLAC__Metadata_Iterator *iterator; FLAC__Metadata_Chain *chain; @@ -1098,7 +1305,7 @@ static FLAC__bool test_level_2_() /* initialize 'data' to avoid Valgrind errors */ memset(data, 0, sizeof(data)); - printf("\n\n++++++ testing level 2 interface\n"); + printf("\n\n++++++ testing level 2 interface (%s-based)\n", filename_based? "filename":"callback"); printf("generate read-only file\n"); @@ -1115,7 +1322,7 @@ static FLAC__bool test_level_2_() printf("read chain\n"); - if(!FLAC__metadata_chain_read(chain, flacfile_)) + if(!read_chain_(chain, flacfile_, filename_based)) return die_c_("reading chain", FLAC__metadata_chain_status(chain)); printf("[S]VP\ttest initial metadata\n"); @@ -1149,7 +1356,7 @@ static FLAC__bool test_level_2_() if(!replace_in_our_metadata_(block, our_current_position, /*copy=*/true)) return die_("copying object"); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/true)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/true, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, true)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1179,7 +1386,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1196,7 +1403,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1213,7 +1420,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1230,7 +1437,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1247,7 +1454,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1269,7 +1476,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1287,7 +1494,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1304,7 +1511,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1322,7 +1529,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1340,7 +1547,7 @@ static FLAC__bool test_level_2_() if(!FLAC__metadata_iterator_set_block(iterator, app)) return die_c_("FLAC__metadata_iterator_set_block(iterator, app)", FLAC__metadata_chain_status(chain)); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, our_current_position, FLAC__metadata_iterator_get_block(iterator))) return false; @@ -1456,7 +1663,7 @@ static FLAC__bool test_level_2_() delete_from_our_metadata_(4); delete_from_our_metadata_(3); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, 0, 0)) return false; @@ -1468,7 +1675,7 @@ static FLAC__bool test_level_2_() our_metadata_.blocks[4]->length += (FLAC__STREAM_METADATA_HEADER_LENGTH + our_metadata_.blocks[2]->length); delete_from_our_metadata_(2); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/true, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/true, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, true, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, 0, 0)) return false; @@ -1567,7 +1774,7 @@ static FLAC__bool test_level_2_() printf("SV\tmerge padding\n"); FLAC__metadata_chain_merge_padding(chain); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, 0, 0)) return false; @@ -1577,7 +1784,7 @@ static FLAC__bool test_level_2_() printf("SV\tsort padding\n"); FLAC__metadata_chain_sort_padding(chain); - if(!FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + if(!write_chain_(chain, /*use_padding=*/false, /*preserve_file_stats=*/false, filename_based, flacfile_)) return die_c_("during FLAC__metadata_chain_write(chain, false, false)", FLAC__metadata_chain_status(chain)); if(!compare_chain_(chain, 0, 0)) return false; @@ -1594,6 +1801,168 @@ static FLAC__bool test_level_2_() return true; } +static FLAC__bool test_level_2_misc_() +{ + FLAC__Metadata_Iterator *iterator; + FLAC__Metadata_Chain *chain; + FLAC__IOCallbacks callbacks; + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.read = (FLAC__IOCallback_Read)fread; +#ifdef FLAC__VALGRIND_TESTING + callbacks.write = chain_write_cb_; +#else + callbacks.write = (FLAC__IOCallback_Write)fwrite; +#endif + callbacks.seek = chain_seek_cb_; + callbacks.tell = chain_tell_cb_; + callbacks.eof = chain_eof_cb_; + + printf("\n\n++++++ testing level 2 interface (mismatched read/write protections)\n"); + + printf("generate file\n"); + + if(!generate_file_()) + return false; + + printf("create chain\n"); + + if(0 == (chain = FLAC__metadata_chain_new())) + return die_("allocating chain"); + + printf("read chain (filename-based)\n"); + + if(!FLAC__metadata_chain_read(chain, flacfile_)) + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks(chain, /*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (filename-based)\n"); + + if(!FLAC__metadata_chain_read(chain, flacfile_)) + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks_and_tempfile()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, /*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks_and_tempfile() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = fopen(flacfile_, "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("write chain with wrong method FLAC__metadata_chain_write()\n"); + { + if(FLAC__metadata_chain_write(chain, /*use_padding=*/false, /*preserve_file_stats=*/false)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write() returned false,FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = fopen(flacfile_, "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("testing FLAC__metadata_chain_check_if_tempfile_needed()... "); + + if(!FLAC__metadata_chain_check_if_tempfile_needed(chain, /*use_padding=*/false)) + printf("OK: FLAC__metadata_chain_check_if_tempfile_needed() returned false like it should\n"); + else + return die_("FLAC__metadata_chain_check_if_tempfile_needed() returned true but shouldn't have"); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks_and_tempfile()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks_and_tempfile(chain, /*use_padding=*/false, 0, callbacks, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks_and_tempfile() returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + printf("read chain (callback-based)\n"); + { + FILE *file = fopen(flacfile_, "rb"); + if(0 == file) + return die_("opening file"); + if(!FLAC__metadata_chain_read_with_callbacks(chain, (FLAC__IOHandle)file, callbacks)) { + fclose(file); + return die_c_("reading chain", FLAC__metadata_chain_status(chain)); + } + fclose(file); + } + + printf("create iterator\n"); + if(0 == (iterator = FLAC__metadata_iterator_new())) + return die_("allocating memory for iterator"); + + FLAC__metadata_iterator_init(iterator, chain); + + printf("[S]VP\tnext\n"); + if(!FLAC__metadata_iterator_next(iterator)) + return die_("iterator ended early\n"); + + printf("S[V]P\tdelete VORBIS_COMMENT, write\n"); + if(!FLAC__metadata_iterator_delete_block(iterator, /*replace_with_padding=*/false)) + return die_c_("block delete failed\n", FLAC__metadata_chain_status(chain)); + + printf("testing FLAC__metadata_chain_check_if_tempfile_needed()... "); + + if(FLAC__metadata_chain_check_if_tempfile_needed(chain, /*use_padding=*/false)) + printf("OK: FLAC__metadata_chain_check_if_tempfile_needed() returned true like it should\n"); + else + return die_("FLAC__metadata_chain_check_if_tempfile_needed() returned false but shouldn't have"); + + printf("write chain with wrong method FLAC__metadata_chain_write_with_callbacks()\n"); + { + if(FLAC__metadata_chain_write_with_callbacks(chain, /*use_padding=*/false, 0, callbacks)) + return die_c_("mismatched write should have failed", FLAC__metadata_chain_status(chain)); + if(FLAC__metadata_chain_status(chain) != FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL) + return die_c_("expected FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL", FLAC__metadata_chain_status(chain)); + printf(" OK: FLAC__metadata_chain_write_with_callbacks() returned false,FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL like it should\n"); + } + + printf("delete iterator\n"); + + FLAC__metadata_iterator_delete(iterator); + + printf("delete chain\n"); + + FLAC__metadata_chain_delete(chain); + + if(!remove_file_(flacfile_)) + return false; + + return true; +} + FLAC__bool test_metadata_file_manipulation() { printf("\n+++ libFLAC unit test: metadata manipulation\n\n"); @@ -1606,7 +1975,11 @@ FLAC__bool test_metadata_file_manipulation() if(!test_level_1_()) return false; - if(!test_level_2_()) + if(!test_level_2_(/*filename_based=*/true)) /* filename-based */ + return false; + if(!test_level_2_(/*filename_based=*/false)) /* callback-based */ + return false; + if(!test_level_2_misc_()) return false; return true;