1 /* test_libFLAC++ - Unit tester for libFLAC++
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "decoders.h"
29 #include "FLAC/assert.h"
30 #include "FLAC/metadata.h" // for ::FLAC__metadata_object_is_equal()
31 #include "FLAC++/decoder.h"
32 #include "share/grabbag.h"
33 #include "share/compat.h"
34 extern "C" {
35 #include "test_libs_common/file_utils_flac.h"
36 #include "test_libs_common/metadata_utils.h"
37 }
38
39 #ifdef _MSC_VER
40 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
41 #pragma warning ( disable : 4800 )
42 #endif
43
44 typedef enum {
45 LAYER_STREAM = 0, /* FLAC__stream_decoder_init_stream() without seeking */
46 LAYER_SEEKABLE_STREAM, /* FLAC__stream_decoder_init_stream() with seeking */
47 LAYER_FILE, /* FLAC__stream_decoder_init_FILE() */
48 LAYER_FILENAME /* FLAC__stream_decoder_init_file() */
49 } Layer;
50
51 static const char * const LayerString[] = {
52 "Stream",
53 "Seekable Stream",
54 "FILE*",
55 "Filename"
56 };
57
58 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
59 static ::FLAC__StreamMetadata *expected_metadata_sequence_[9];
60 static uint32_t num_expected_;
61 static FLAC__off_t flacfilesize_;
62
flacfilename(bool is_ogg)63 static const char *flacfilename(bool is_ogg)
64 {
65 return is_ogg? "metadata.oga" : "metadata.flac";
66 }
67
die_(const char * msg)68 static bool die_(const char *msg)
69 {
70 printf("ERROR: %s\n", msg);
71 return false;
72 }
73
die_s_(const char * msg,const FLAC::Decoder::Stream * decoder)74 static FLAC__bool die_s_(const char *msg, const FLAC::Decoder::Stream *decoder)
75 {
76 FLAC::Decoder::Stream::State state = decoder->get_state();
77
78 if(msg)
79 printf("FAILED, %s", msg);
80 else
81 printf("FAILED");
82
83 printf(", state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
84
85 return false;
86 }
87
init_metadata_blocks_()88 static void init_metadata_blocks_()
89 {
90 mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
91 }
92
free_metadata_blocks_()93 static void free_metadata_blocks_()
94 {
95 mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
96 }
97
generate_file_(FLAC__bool is_ogg)98 static bool generate_file_(FLAC__bool is_ogg)
99 {
100 printf("\n\ngenerating %sFLAC file for decoder tests...\n", is_ogg? "Ogg ":"");
101
102 num_expected_ = 0;
103 expected_metadata_sequence_[num_expected_++] = &padding_;
104 expected_metadata_sequence_[num_expected_++] = &seektable_;
105 expected_metadata_sequence_[num_expected_++] = &application1_;
106 expected_metadata_sequence_[num_expected_++] = &application2_;
107 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
108 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
109 expected_metadata_sequence_[num_expected_++] = &picture_;
110 expected_metadata_sequence_[num_expected_++] = &unknown_;
111 /* WATCHOUT: for Ogg FLAC the encoder should move the VORBIS_COMMENT block to the front, right after STREAMINFO */
112
113 if(!file_utils__generate_flacfile(is_ogg, flacfilename(is_ogg), &flacfilesize_, 512 * 1024, &streaminfo_, expected_metadata_sequence_, num_expected_))
114 return die_("creating the encoded file");
115
116 return true;
117 }
118
119
120 class DecoderCommon {
121 public:
122 Layer layer_;
123 uint32_t current_metadata_number_;
124 bool ignore_errors_;
125 bool error_occurred_;
126
DecoderCommon(Layer layer)127 DecoderCommon(Layer layer): layer_(layer), current_metadata_number_(0), ignore_errors_(false), error_occurred_(false) { }
~DecoderCommon(void)128 virtual ~DecoderCommon(void) { }
129 ::FLAC__StreamDecoderWriteStatus common_write_callback_(const ::FLAC__Frame *frame);
130 void common_metadata_callback_(const ::FLAC__StreamMetadata *metadata);
131 void common_error_callback_(::FLAC__StreamDecoderErrorStatus status);
132 };
133
common_write_callback_(const::FLAC__Frame * frame)134 ::FLAC__StreamDecoderWriteStatus DecoderCommon::common_write_callback_(const ::FLAC__Frame *frame)
135 {
136 if(error_occurred_)
137 return ::FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
138
139 if(
140 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER && frame->header.number.frame_number == 0) ||
141 (frame->header.number_type == ::FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER && frame->header.number.sample_number == 0)
142 ) {
143 printf("content... ");
144 fflush(stdout);
145 }
146
147 return ::FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
148 }
149
common_metadata_callback_(const::FLAC__StreamMetadata * metadata)150 void DecoderCommon::common_metadata_callback_(const ::FLAC__StreamMetadata *metadata)
151 {
152 if(error_occurred_)
153 return;
154
155 printf("%u... ", current_metadata_number_);
156 fflush(stdout);
157
158 if(current_metadata_number_ >= num_expected_) {
159 (void)die_("got more metadata blocks than expected");
160 error_occurred_ = true;
161 }
162 else {
163 if(!::FLAC__metadata_object_is_equal(expected_metadata_sequence_[current_metadata_number_], metadata)) {
164 (void)die_("metadata block mismatch");
165 error_occurred_ = true;
166 }
167 }
168 current_metadata_number_++;
169 }
170
common_error_callback_(::FLAC__StreamDecoderErrorStatus status)171 void DecoderCommon::common_error_callback_(::FLAC__StreamDecoderErrorStatus status)
172 {
173 if(!ignore_errors_) {
174 printf("ERROR: got error callback: err = %u (%s)\n", (uint32_t)status, ::FLAC__StreamDecoderErrorStatusString[status]);
175 error_occurred_ = true;
176 }
177 }
178
179 class StreamDecoder : public FLAC::Decoder::Stream, public DecoderCommon {
180 public:
181 FILE *file_;
182
StreamDecoder(Layer layer)183 StreamDecoder(Layer layer): FLAC::Decoder::Stream(), DecoderCommon(layer), file_(0) { }
~StreamDecoder()184 ~StreamDecoder() { }
185
186 // from FLAC::Decoder::Stream
187 ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
188 ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
189 ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
190 ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length);
191 bool eof_callback();
192 ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
193 void metadata_callback(const ::FLAC__StreamMetadata *metadata);
194 void error_callback(::FLAC__StreamDecoderErrorStatus status);
195
196 bool test_respond(bool is_ogg);
197 private:
198 StreamDecoder(const StreamDecoder&);
199 StreamDecoder&operator=(const StreamDecoder&);
200 };
201
read_callback(FLAC__byte buffer[],size_t * bytes)202 ::FLAC__StreamDecoderReadStatus StreamDecoder::read_callback(FLAC__byte buffer[], size_t *bytes)
203 {
204 const size_t requested_bytes = *bytes;
205
206 if(error_occurred_)
207 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT;
208
209 if(feof(file_)) {
210 *bytes = 0;
211 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
212 }
213 else if(requested_bytes > 0) {
214 *bytes = ::fread(buffer, 1, requested_bytes, file_);
215 if(*bytes == 0) {
216 if(feof(file_))
217 return ::FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
218 else
219 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
220 }
221 else {
222 return ::FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
223 }
224 }
225 else
226 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
227 }
228
seek_callback(FLAC__uint64 absolute_byte_offset)229 ::FLAC__StreamDecoderSeekStatus StreamDecoder::seek_callback(FLAC__uint64 absolute_byte_offset)
230 {
231 if(layer_ == LAYER_STREAM)
232 return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
233
234 if(error_occurred_)
235 return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
236
237 if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0) {
238 error_occurred_ = true;
239 return ::FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
240 }
241
242 return ::FLAC__STREAM_DECODER_SEEK_STATUS_OK;
243 }
244
tell_callback(FLAC__uint64 * absolute_byte_offset)245 ::FLAC__StreamDecoderTellStatus StreamDecoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
246 {
247 if(layer_ == LAYER_STREAM)
248 return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
249
250 if(error_occurred_)
251 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
252
253 FLAC__off_t offset = ftello(file_);
254 *absolute_byte_offset = (FLAC__uint64)offset;
255
256 if(offset < 0) {
257 error_occurred_ = true;
258 return ::FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
259 }
260
261 return ::FLAC__STREAM_DECODER_TELL_STATUS_OK;
262 }
263
length_callback(FLAC__uint64 * stream_length)264 ::FLAC__StreamDecoderLengthStatus StreamDecoder::length_callback(FLAC__uint64 *stream_length)
265 {
266 if(layer_ == LAYER_STREAM)
267 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
268
269 if(error_occurred_)
270 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
271
272 *stream_length = (FLAC__uint64)flacfilesize_;
273 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
274 }
275
eof_callback()276 bool StreamDecoder::eof_callback()
277 {
278 if(layer_ == LAYER_STREAM)
279 return false;
280
281 if(error_occurred_)
282 return true;
283
284 return (bool)feof(file_);
285 }
286
write_callback(const::FLAC__Frame * frame,const FLAC__int32 * const buffer[])287 ::FLAC__StreamDecoderWriteStatus StreamDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
288 {
289 (void)buffer;
290
291 return common_write_callback_(frame);
292 }
293
metadata_callback(const::FLAC__StreamMetadata * metadata)294 void StreamDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
295 {
296 common_metadata_callback_(metadata);
297 }
298
error_callback(::FLAC__StreamDecoderErrorStatus status)299 void StreamDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
300 {
301 common_error_callback_(status);
302 }
303
test_respond(bool is_ogg)304 bool StreamDecoder::test_respond(bool is_ogg)
305 {
306 ::FLAC__StreamDecoderInitStatus init_status;
307
308 if(!set_md5_checking(true)) {
309 printf("FAILED at set_md5_checking(), returned false\n");
310 return false;
311 }
312
313 printf("testing init%s()... ", is_ogg? "_ogg":"");
314 init_status = is_ogg? init_ogg() : init();
315 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
316 return die_s_(0, this);
317 printf("OK\n");
318
319 current_metadata_number_ = 0;
320
321 if(fseeko(file_, 0, SEEK_SET) < 0) {
322 printf("FAILED rewinding input, errno = %d\n", errno);
323 return false;
324 }
325
326 printf("testing process_until_end_of_stream()... ");
327 if(!process_until_end_of_stream()) {
328 State state = get_state();
329 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
330 return false;
331 }
332 printf("OK\n");
333
334 printf("testing finish()... ");
335 if(!finish()) {
336 State state = get_state();
337 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
338 return false;
339 }
340 printf("OK\n");
341
342 return true;
343 }
344
345 class FileDecoder : public FLAC::Decoder::File, public DecoderCommon {
346 public:
FileDecoder(Layer layer)347 FileDecoder(Layer layer): FLAC::Decoder::File(), DecoderCommon(layer) { }
~FileDecoder()348 ~FileDecoder() { }
349
350 // from FLAC::Decoder::Stream
351 ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
352 void metadata_callback(const ::FLAC__StreamMetadata *metadata);
353 void error_callback(::FLAC__StreamDecoderErrorStatus status);
354
355 bool test_respond(bool is_ogg);
356 };
357
write_callback(const::FLAC__Frame * frame,const FLAC__int32 * const buffer[])358 ::FLAC__StreamDecoderWriteStatus FileDecoder::write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[])
359 {
360 (void)buffer;
361 return common_write_callback_(frame);
362 }
363
metadata_callback(const::FLAC__StreamMetadata * metadata)364 void FileDecoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
365 {
366 common_metadata_callback_(metadata);
367 }
368
error_callback(::FLAC__StreamDecoderErrorStatus status)369 void FileDecoder::error_callback(::FLAC__StreamDecoderErrorStatus status)
370 {
371 common_error_callback_(status);
372 }
373
test_respond(bool is_ogg)374 bool FileDecoder::test_respond(bool is_ogg)
375 {
376 ::FLAC__StreamDecoderInitStatus init_status;
377
378 if(!set_md5_checking(true)) {
379 printf("FAILED at set_md5_checking(), returned false\n");
380 return false;
381 }
382
383 switch(layer_) {
384 case LAYER_FILE:
385 {
386 printf("opening %sFLAC file... ", is_ogg? "Ogg ":"");
387 FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb");
388 if(0 == file) {
389 printf("ERROR (%s)\n", strerror(errno));
390 return false;
391 }
392 printf("OK\n");
393
394 printf("testing init%s()... ", is_ogg? "_ogg":"");
395 init_status = is_ogg? init_ogg(file) : init(file);
396 }
397 break;
398 case LAYER_FILENAME:
399 printf("testing init%s()... ", is_ogg? "_ogg":"");
400 init_status = is_ogg? init_ogg(flacfilename(is_ogg)) : init(flacfilename(is_ogg));
401 break;
402 default:
403 die_("internal error 001");
404 return false;
405 }
406 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
407 return die_s_(0, this);
408 printf("OK\n");
409
410 current_metadata_number_ = 0;
411
412 printf("testing process_until_end_of_stream()... ");
413 if(!process_until_end_of_stream()) {
414 State state = get_state();
415 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
416 return false;
417 }
418 printf("OK\n");
419
420 printf("testing finish()... ");
421 if(!finish()) {
422 State state = get_state();
423 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
424 return false;
425 }
426 printf("OK\n");
427
428 return true;
429 }
430
431
new_by_layer(Layer layer)432 static FLAC::Decoder::Stream *new_by_layer(Layer layer)
433 {
434 if(layer < LAYER_FILE)
435 return new StreamDecoder(layer);
436 else
437 return new FileDecoder(layer);
438 }
439
test_stream_decoder(Layer layer,bool is_ogg)440 static bool test_stream_decoder(Layer layer, bool is_ogg)
441 {
442 FLAC::Decoder::Stream *decoder;
443 ::FLAC__StreamDecoderInitStatus init_status;
444 bool expect;
445
446 printf("\n+++ libFLAC++ unit test: FLAC::Decoder::%s (layer: %s, format: %s)\n\n", layer<LAYER_FILE? "Stream":"File", LayerString[layer], is_ogg? "Ogg FLAC" : "FLAC");
447
448 //
449 // test new -> delete
450 //
451 printf("allocating decoder instance... ");
452 decoder = new_by_layer(layer);
453 if(0 == decoder) {
454 printf("FAILED, new returned NULL\n");
455 return false;
456 }
457 printf("OK\n");
458
459 printf("testing is_valid()... ");
460 if(!decoder->is_valid()) {
461 printf("FAILED, returned false\n");
462 delete decoder;
463 return false;
464 }
465 printf("OK\n");
466
467 printf("freeing decoder instance... ");
468 delete decoder;
469 printf("OK\n");
470
471 //
472 // test new -> init -> delete
473 //
474 printf("allocating decoder instance... ");
475 decoder = new_by_layer(layer);
476 if(0 == decoder) {
477 printf("FAILED, new returned NULL\n");
478 return false;
479 }
480 printf("OK\n");
481
482 printf("testing is_valid()... ");
483 if(!decoder->is_valid()) {
484 printf("FAILED, returned false\n");
485 delete decoder;
486 return false;
487 }
488 printf("OK\n");
489
490 printf("testing init%s()... ", is_ogg? "_ogg":"");
491 switch(layer) {
492 case LAYER_STREAM:
493 case LAYER_SEEKABLE_STREAM:
494 dynamic_cast<StreamDecoder*>(decoder)->file_ = stdin;
495 init_status = is_ogg? decoder->init_ogg() : decoder->init();
496 break;
497 case LAYER_FILE:
498 init_status = is_ogg?
499 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(stdin) :
500 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(stdin);
501 break;
502 case LAYER_FILENAME:
503 init_status = is_ogg?
504 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) :
505 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg));
506 break;
507 default:
508 die_("internal error 006");
509 delete decoder;
510 return false;
511 }
512 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
513 return die_s_(0, decoder);
514 printf("OK\n");
515
516 printf("freeing decoder instance... ");
517 delete decoder;
518 printf("OK\n");
519
520 //
521 // test normal usage
522 //
523 num_expected_ = 0;
524 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
525
526 printf("allocating decoder instance... ");
527 decoder = new_by_layer(layer);
528 if(0 == decoder) {
529 printf("FAILED, new returned NULL\n");
530 return false;
531 }
532 printf("OK\n");
533
534 printf("testing is_valid()... ");
535 if(!decoder->is_valid()) {
536 printf("FAILED, returned false\n");
537 delete decoder;
538 return false;
539 }
540 printf("OK\n");
541
542 if(is_ogg) {
543 printf("testing set_ogg_serial_number()... ");
544 if(!decoder->set_ogg_serial_number(file_utils__ogg_serial_number))
545 return die_s_("returned false", decoder);
546 printf("OK\n");
547 }
548
549 if(!decoder->set_md5_checking(true)) {
550 printf("FAILED at set_md5_checking(), returned false\n");
551 return false;
552 }
553
554 switch(layer) {
555 case LAYER_STREAM:
556 case LAYER_SEEKABLE_STREAM:
557 printf("opening %sFLAC file... ", is_ogg? "Ogg ":"");
558 dynamic_cast<StreamDecoder*>(decoder)->file_ = ::flac_fopen(flacfilename(is_ogg), "rb");
559 if(0 == dynamic_cast<StreamDecoder*>(decoder)->file_) {
560 printf("ERROR (%s)\n", strerror(errno));
561 return false;
562 }
563 printf("OK\n");
564
565 printf("testing init%s()... ", is_ogg? "_ogg":"");
566 init_status = is_ogg? decoder->init_ogg() : decoder->init();
567 break;
568 case LAYER_FILE:
569 {
570 printf("opening FLAC file... ");
571 FILE *file = ::flac_fopen(flacfilename(is_ogg), "rb");
572 if(0 == file) {
573 printf("ERROR (%s)\n", strerror(errno));
574 return false;
575 }
576 printf("OK\n");
577
578 printf("testing init%s()... ", is_ogg? "_ogg":"");
579 init_status = is_ogg?
580 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(file) :
581 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(file);
582 }
583 break;
584 case LAYER_FILENAME:
585 printf("testing init%s()... ", is_ogg? "_ogg":"");
586 init_status = is_ogg?
587 dynamic_cast<FLAC::Decoder::File*>(decoder)->init_ogg(flacfilename(is_ogg)) :
588 dynamic_cast<FLAC::Decoder::File*>(decoder)->init(flacfilename(is_ogg));
589 break;
590 default:
591 die_("internal error 009");
592 return false;
593 }
594 if(init_status != ::FLAC__STREAM_DECODER_INIT_STATUS_OK)
595 return die_s_(0, decoder);
596 printf("OK\n");
597
598 printf("testing get_state()... ");
599 FLAC::Decoder::Stream::State state = decoder->get_state();
600 printf("returned state = %u (%s)... OK\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
601
602 dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0;
603 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false;
604 dynamic_cast<DecoderCommon*>(decoder)->error_occurred_ = false;
605
606 printf("testing get_md5_checking()... ");
607 if(!decoder->get_md5_checking()) {
608 printf("FAILED, returned false, expected true\n");
609 return false;
610 }
611 printf("OK\n");
612
613 printf("testing process_until_end_of_metadata()... ");
614 if(!decoder->process_until_end_of_metadata())
615 return die_s_("returned false", decoder);
616 printf("OK\n");
617
618 printf("testing process_single()... ");
619 if(!decoder->process_single())
620 return die_s_("returned false", decoder);
621 printf("OK\n");
622
623 printf("testing skip_single_frame()... ");
624 if(!decoder->skip_single_frame())
625 return die_s_("returned false", decoder);
626 printf("OK\n");
627
628 if(layer < LAYER_FILE) {
629 printf("testing flush()... ");
630 if(!decoder->flush())
631 return die_s_("returned false", decoder);
632 printf("OK\n");
633
634 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = true;
635 printf("testing process_single()... ");
636 if(!decoder->process_single())
637 return die_s_("returned false", decoder);
638 printf("OK\n");
639 dynamic_cast<DecoderCommon*>(decoder)->ignore_errors_ = false;
640 }
641
642 expect = (layer != LAYER_STREAM);
643 printf("testing seek_absolute()... ");
644 if(decoder->seek_absolute(0) != expect)
645 return die_s_(expect? "returned false" : "returned true", decoder);
646 printf("OK\n");
647
648 printf("testing process_until_end_of_stream()... ");
649 if(!decoder->process_until_end_of_stream())
650 return die_s_("returned false", decoder);
651 printf("OK\n");
652
653 expect = (layer != LAYER_STREAM);
654 printf("testing seek_absolute()... ");
655 if(decoder->seek_absolute(0) != expect)
656 return die_s_(expect? "returned false" : "returned true", decoder);
657 printf("OK\n");
658
659 printf("testing get_channels()... ");
660 {
661 uint32_t channels = decoder->get_channels();
662 if(channels != streaminfo_.data.stream_info.channels) {
663 printf("FAILED, returned %u, expected %u\n", channels, streaminfo_.data.stream_info.channels);
664 return false;
665 }
666 }
667 printf("OK\n");
668
669 printf("testing get_bits_per_sample()... ");
670 {
671 uint32_t bits_per_sample = decoder->get_bits_per_sample();
672 if(bits_per_sample != streaminfo_.data.stream_info.bits_per_sample) {
673 printf("FAILED, returned %u, expected %u\n", bits_per_sample, streaminfo_.data.stream_info.bits_per_sample);
674 return false;
675 }
676 }
677 printf("OK\n");
678
679 printf("testing get_sample_rate()... ");
680 {
681 uint32_t sample_rate = decoder->get_sample_rate();
682 if(sample_rate != streaminfo_.data.stream_info.sample_rate) {
683 printf("FAILED, returned %u, expected %u\n", sample_rate, streaminfo_.data.stream_info.sample_rate);
684 return false;
685 }
686 }
687 printf("OK\n");
688
689 printf("testing get_blocksize()... ");
690 {
691 uint32_t blocksize = decoder->get_blocksize();
692 /* value could be anything since we're at the last block, so accept any reasonable answer */
693 printf("returned %u... %s\n", blocksize, blocksize>0? "OK" : "FAILED");
694 if(blocksize == 0)
695 return false;
696 }
697
698 printf("testing get_channel_assignment()... ");
699 {
700 ::FLAC__ChannelAssignment ca = decoder->get_channel_assignment();
701 printf("returned %u (%s)... OK\n", (uint32_t)ca, ::FLAC__ChannelAssignmentString[ca]);
702 }
703
704 if(layer < LAYER_FILE) {
705 printf("testing reset()... ");
706 if(!decoder->reset())
707 return die_s_("returned false", decoder);
708 printf("OK\n");
709
710 if(layer == LAYER_STREAM) {
711 /* after a reset() we have to rewind the input ourselves */
712 printf("rewinding input... ");
713 if(fseeko(dynamic_cast<StreamDecoder*>(decoder)->file_, 0, SEEK_SET) < 0) {
714 printf("FAILED, errno = %d\n", errno);
715 return false;
716 }
717 printf("OK\n");
718 }
719
720 dynamic_cast<DecoderCommon*>(decoder)->current_metadata_number_ = 0;
721
722 printf("testing process_until_end_of_stream()... ");
723 if(!decoder->process_until_end_of_stream())
724 return die_s_("returned false", decoder);
725 printf("OK\n");
726 }
727
728 printf("testing finish()... ");
729 if(!decoder->finish()) {
730 state = decoder->get_state();
731 printf("FAILED, returned false, state = %u (%s)\n", (uint32_t)((::FLAC__StreamDecoderState)state), state.as_cstring());
732 return false;
733 }
734 printf("OK\n");
735
736 /*
737 * respond all
738 */
739
740 printf("testing set_metadata_respond_all()... ");
741 if(!decoder->set_metadata_respond_all()) {
742 printf("FAILED, returned false\n");
743 return false;
744 }
745 printf("OK\n");
746
747 num_expected_ = 0;
748 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */
749 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
750 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
751 expected_metadata_sequence_[num_expected_++] = &padding_;
752 expected_metadata_sequence_[num_expected_++] = &application1_;
753 expected_metadata_sequence_[num_expected_++] = &application2_;
754 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
755 expected_metadata_sequence_[num_expected_++] = &picture_;
756 expected_metadata_sequence_[num_expected_++] = &unknown_;
757 }
758 else {
759 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
760 expected_metadata_sequence_[num_expected_++] = &padding_;
761 expected_metadata_sequence_[num_expected_++] = &seektable_;
762 expected_metadata_sequence_[num_expected_++] = &application1_;
763 expected_metadata_sequence_[num_expected_++] = &application2_;
764 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
765 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
766 expected_metadata_sequence_[num_expected_++] = &picture_;
767 expected_metadata_sequence_[num_expected_++] = &unknown_;
768 }
769
770 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
771 return false;
772
773 /*
774 * ignore all
775 */
776
777 printf("testing set_metadata_ignore_all()... ");
778 if(!decoder->set_metadata_ignore_all()) {
779 printf("FAILED, returned false\n");
780 return false;
781 }
782 printf("OK\n");
783
784 num_expected_ = 0;
785
786 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
787 return false;
788
789 /*
790 * respond all, ignore VORBIS_COMMENT
791 */
792
793 printf("testing set_metadata_respond_all()... ");
794 if(!decoder->set_metadata_respond_all()) {
795 printf("FAILED, returned false\n");
796 return false;
797 }
798 printf("OK\n");
799
800 printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
801 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
802 printf("FAILED, returned false\n");
803 return false;
804 }
805 printf("OK\n");
806
807 num_expected_ = 0;
808 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
809 expected_metadata_sequence_[num_expected_++] = &padding_;
810 if(!is_ogg) /* encoder removes seektable for ogg */
811 expected_metadata_sequence_[num_expected_++] = &seektable_;
812 expected_metadata_sequence_[num_expected_++] = &application1_;
813 expected_metadata_sequence_[num_expected_++] = &application2_;
814 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
815 expected_metadata_sequence_[num_expected_++] = &picture_;
816 expected_metadata_sequence_[num_expected_++] = &unknown_;
817
818 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
819 return false;
820
821 /*
822 * respond all, ignore APPLICATION
823 */
824
825 printf("testing set_metadata_respond_all()... ");
826 if(!decoder->set_metadata_respond_all()) {
827 printf("FAILED, returned false\n");
828 return false;
829 }
830 printf("OK\n");
831
832 printf("testing set_metadata_ignore(APPLICATION)... ");
833 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
834 printf("FAILED, returned false\n");
835 return false;
836 }
837 printf("OK\n");
838
839 num_expected_ = 0;
840 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */
841 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
842 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
843 expected_metadata_sequence_[num_expected_++] = &padding_;
844 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
845 expected_metadata_sequence_[num_expected_++] = &picture_;
846 expected_metadata_sequence_[num_expected_++] = &unknown_;
847 }
848 else {
849 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
850 expected_metadata_sequence_[num_expected_++] = &padding_;
851 expected_metadata_sequence_[num_expected_++] = &seektable_;
852 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
853 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
854 expected_metadata_sequence_[num_expected_++] = &picture_;
855 expected_metadata_sequence_[num_expected_++] = &unknown_;
856 }
857
858 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
859 return false;
860
861 /*
862 * respond all, ignore APPLICATION id of app#1
863 */
864
865 printf("testing set_metadata_respond_all()... ");
866 if(!decoder->set_metadata_respond_all()) {
867 printf("FAILED, returned false\n");
868 return false;
869 }
870 printf("OK\n");
871
872 printf("testing set_metadata_ignore_application(of app block #1)... ");
873 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
874 printf("FAILED, returned false\n");
875 return false;
876 }
877 printf("OK\n");
878
879 num_expected_ = 0;
880 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */
881 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
882 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
883 expected_metadata_sequence_[num_expected_++] = &padding_;
884 expected_metadata_sequence_[num_expected_++] = &application2_;
885 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
886 expected_metadata_sequence_[num_expected_++] = &picture_;
887 expected_metadata_sequence_[num_expected_++] = &unknown_;
888 }
889 else {
890 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
891 expected_metadata_sequence_[num_expected_++] = &padding_;
892 expected_metadata_sequence_[num_expected_++] = &seektable_;
893 expected_metadata_sequence_[num_expected_++] = &application2_;
894 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
895 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
896 expected_metadata_sequence_[num_expected_++] = &picture_;
897 expected_metadata_sequence_[num_expected_++] = &unknown_;
898 }
899
900 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
901 return false;
902
903 /*
904 * respond all, ignore APPLICATION id of app#1 & app#2
905 */
906
907 printf("testing set_metadata_respond_all()... ");
908 if(!decoder->set_metadata_respond_all()) {
909 printf("FAILED, returned false\n");
910 return false;
911 }
912 printf("OK\n");
913
914 printf("testing set_metadata_ignore_application(of app block #1)... ");
915 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
916 printf("FAILED, returned false\n");
917 return false;
918 }
919 printf("OK\n");
920
921 printf("testing set_metadata_ignore_application(of app block #2)... ");
922 if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
923 printf("FAILED, returned false\n");
924 return false;
925 }
926 printf("OK\n");
927
928 num_expected_ = 0;
929 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes seektable */
930 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
931 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
932 expected_metadata_sequence_[num_expected_++] = &padding_;
933 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
934 expected_metadata_sequence_[num_expected_++] = &picture_;
935 expected_metadata_sequence_[num_expected_++] = &unknown_;
936 }
937 else {
938 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
939 expected_metadata_sequence_[num_expected_++] = &padding_;
940 expected_metadata_sequence_[num_expected_++] = &seektable_;
941 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
942 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
943 expected_metadata_sequence_[num_expected_++] = &picture_;
944 expected_metadata_sequence_[num_expected_++] = &unknown_;
945 }
946
947 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
948 return false;
949
950 /*
951 * ignore all, respond VORBIS_COMMENT
952 */
953
954 printf("testing set_metadata_ignore_all()... ");
955 if(!decoder->set_metadata_ignore_all()) {
956 printf("FAILED, returned false\n");
957 return false;
958 }
959 printf("OK\n");
960
961 printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
962 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
963 printf("FAILED, returned false\n");
964 return false;
965 }
966 printf("OK\n");
967
968 num_expected_ = 0;
969 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
970
971 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
972 return false;
973
974 /*
975 * ignore all, respond APPLICATION
976 */
977
978 printf("testing set_metadata_ignore_all()... ");
979 if(!decoder->set_metadata_ignore_all()) {
980 printf("FAILED, returned false\n");
981 return false;
982 }
983 printf("OK\n");
984
985 printf("testing set_metadata_respond(APPLICATION)... ");
986 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
987 printf("FAILED, returned false\n");
988 return false;
989 }
990 printf("OK\n");
991
992 num_expected_ = 0;
993 expected_metadata_sequence_[num_expected_++] = &application1_;
994 expected_metadata_sequence_[num_expected_++] = &application2_;
995
996 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
997 return false;
998
999 /*
1000 * ignore all, respond APPLICATION id of app#1
1001 */
1002
1003 printf("testing set_metadata_ignore_all()... ");
1004 if(!decoder->set_metadata_ignore_all()) {
1005 printf("FAILED, returned false\n");
1006 return false;
1007 }
1008 printf("OK\n");
1009
1010 printf("testing set_metadata_respond_application(of app block #1)... ");
1011 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1012 printf("FAILED, returned false\n");
1013 return false;
1014 }
1015 printf("OK\n");
1016
1017 num_expected_ = 0;
1018 expected_metadata_sequence_[num_expected_++] = &application1_;
1019
1020 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1021 return false;
1022
1023 /*
1024 * ignore all, respond APPLICATION id of app#1 & app#2
1025 */
1026
1027 printf("testing set_metadata_ignore_all()... ");
1028 if(!decoder->set_metadata_ignore_all()) {
1029 printf("FAILED, returned false\n");
1030 return false;
1031 }
1032 printf("OK\n");
1033
1034 printf("testing set_metadata_respond_application(of app block #1)... ");
1035 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1036 printf("FAILED, returned false\n");
1037 return false;
1038 }
1039 printf("OK\n");
1040
1041 printf("testing set_metadata_respond_application(of app block #2)... ");
1042 if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
1043 printf("FAILED, returned false\n");
1044 return false;
1045 }
1046 printf("OK\n");
1047
1048 num_expected_ = 0;
1049 expected_metadata_sequence_[num_expected_++] = &application1_;
1050 expected_metadata_sequence_[num_expected_++] = &application2_;
1051
1052 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1053 return false;
1054
1055 /*
1056 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1057 */
1058
1059 printf("testing set_metadata_respond_all()... ");
1060 if(!decoder->set_metadata_respond_all()) {
1061 printf("FAILED, returned false\n");
1062 return false;
1063 }
1064 printf("OK\n");
1065
1066 printf("testing set_metadata_ignore(APPLICATION)... ");
1067 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1068 printf("FAILED, returned false\n");
1069 return false;
1070 }
1071 printf("OK\n");
1072
1073 printf("testing set_metadata_respond_application(of app block #1)... ");
1074 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1075 printf("FAILED, returned false\n");
1076 return false;
1077 }
1078 printf("OK\n");
1079
1080 num_expected_ = 0;
1081 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping. Also removes the seektable */
1082 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1083 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1084 expected_metadata_sequence_[num_expected_++] = &padding_;
1085 expected_metadata_sequence_[num_expected_++] = &application1_;
1086 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1087 expected_metadata_sequence_[num_expected_++] = &picture_;
1088 expected_metadata_sequence_[num_expected_++] = &unknown_;
1089 }
1090 else {
1091 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1092 expected_metadata_sequence_[num_expected_++] = &padding_;
1093 expected_metadata_sequence_[num_expected_++] = &seektable_;
1094 expected_metadata_sequence_[num_expected_++] = &application1_;
1095 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1096 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1097 expected_metadata_sequence_[num_expected_++] = &picture_;
1098 expected_metadata_sequence_[num_expected_++] = &unknown_;
1099 }
1100
1101 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1102 return false;
1103
1104 /*
1105 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1106 */
1107
1108 printf("testing set_metadata_ignore_all()... ");
1109 if(!decoder->set_metadata_ignore_all()) {
1110 printf("FAILED, returned false\n");
1111 return false;
1112 }
1113 printf("OK\n");
1114
1115 printf("testing set_metadata_respond(APPLICATION)... ");
1116 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1117 printf("FAILED, returned false\n");
1118 return false;
1119 }
1120 printf("OK\n");
1121
1122 printf("testing set_metadata_ignore_application(of app block #1)... ");
1123 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1124 printf("FAILED, returned false\n");
1125 return false;
1126 }
1127 printf("OK\n");
1128
1129 num_expected_ = 0;
1130 expected_metadata_sequence_[num_expected_++] = &application2_;
1131
1132 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1133 return false;
1134
1135 if(layer < LAYER_FILE) /* for LAYER_FILE, FLAC__stream_decoder_finish() closes the file */
1136 ::fclose(dynamic_cast<StreamDecoder*>(decoder)->file_);
1137
1138 printf("freeing decoder instance... ");
1139 delete decoder;
1140 printf("OK\n");
1141
1142 printf("\nPASSED!\n");
1143
1144 return true;
1145 }
1146
test_decoders()1147 bool test_decoders()
1148 {
1149 FLAC__bool is_ogg = false;
1150
1151 while(1) {
1152 init_metadata_blocks_();
1153
1154 if(!generate_file_(is_ogg))
1155 return false;
1156
1157 if(!test_stream_decoder(LAYER_STREAM, is_ogg))
1158 return false;
1159
1160 if(!test_stream_decoder(LAYER_SEEKABLE_STREAM, is_ogg))
1161 return false;
1162
1163 if(!test_stream_decoder(LAYER_FILE, is_ogg))
1164 return false;
1165
1166 if(!test_stream_decoder(LAYER_FILENAME, is_ogg))
1167 return false;
1168
1169 (void) grabbag__file_remove_file(flacfilename(is_ogg));
1170
1171 free_metadata_blocks_();
1172
1173 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
1174 break;
1175 is_ogg = true;
1176 }
1177
1178 return true;
1179 }
1180