1 /* test_libFLAC++ - Unit tester for libFLAC++
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2016 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 */
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_++] = &seektable_;
753 expected_metadata_sequence_[num_expected_++] = &application1_;
754 expected_metadata_sequence_[num_expected_++] = &application2_;
755 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
756 expected_metadata_sequence_[num_expected_++] = &picture_;
757 expected_metadata_sequence_[num_expected_++] = &unknown_;
758 }
759 else {
760 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
761 expected_metadata_sequence_[num_expected_++] = &padding_;
762 expected_metadata_sequence_[num_expected_++] = &seektable_;
763 expected_metadata_sequence_[num_expected_++] = &application1_;
764 expected_metadata_sequence_[num_expected_++] = &application2_;
765 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
766 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
767 expected_metadata_sequence_[num_expected_++] = &picture_;
768 expected_metadata_sequence_[num_expected_++] = &unknown_;
769 }
770
771 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
772 return false;
773
774 /*
775 * ignore all
776 */
777
778 printf("testing set_metadata_ignore_all()... ");
779 if(!decoder->set_metadata_ignore_all()) {
780 printf("FAILED, returned false\n");
781 return false;
782 }
783 printf("OK\n");
784
785 num_expected_ = 0;
786
787 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
788 return false;
789
790 /*
791 * respond all, ignore VORBIS_COMMENT
792 */
793
794 printf("testing set_metadata_respond_all()... ");
795 if(!decoder->set_metadata_respond_all()) {
796 printf("FAILED, returned false\n");
797 return false;
798 }
799 printf("OK\n");
800
801 printf("testing set_metadata_ignore(VORBIS_COMMENT)... ");
802 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
803 printf("FAILED, returned false\n");
804 return false;
805 }
806 printf("OK\n");
807
808 num_expected_ = 0;
809 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
810 expected_metadata_sequence_[num_expected_++] = &padding_;
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 */
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_++] = &seektable_;
845 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
846 expected_metadata_sequence_[num_expected_++] = &picture_;
847 expected_metadata_sequence_[num_expected_++] = &unknown_;
848 }
849 else {
850 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
851 expected_metadata_sequence_[num_expected_++] = &padding_;
852 expected_metadata_sequence_[num_expected_++] = &seektable_;
853 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
854 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
855 expected_metadata_sequence_[num_expected_++] = &picture_;
856 expected_metadata_sequence_[num_expected_++] = &unknown_;
857 }
858
859 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
860 return false;
861
862 /*
863 * respond all, ignore APPLICATION id of app#1
864 */
865
866 printf("testing set_metadata_respond_all()... ");
867 if(!decoder->set_metadata_respond_all()) {
868 printf("FAILED, returned false\n");
869 return false;
870 }
871 printf("OK\n");
872
873 printf("testing set_metadata_ignore_application(of app block #1)... ");
874 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
875 printf("FAILED, returned false\n");
876 return false;
877 }
878 printf("OK\n");
879
880 num_expected_ = 0;
881 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
882 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
883 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
884 expected_metadata_sequence_[num_expected_++] = &padding_;
885 expected_metadata_sequence_[num_expected_++] = &seektable_;
886 expected_metadata_sequence_[num_expected_++] = &application2_;
887 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
888 expected_metadata_sequence_[num_expected_++] = &picture_;
889 expected_metadata_sequence_[num_expected_++] = &unknown_;
890 }
891 else {
892 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
893 expected_metadata_sequence_[num_expected_++] = &padding_;
894 expected_metadata_sequence_[num_expected_++] = &seektable_;
895 expected_metadata_sequence_[num_expected_++] = &application2_;
896 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
897 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
898 expected_metadata_sequence_[num_expected_++] = &picture_;
899 expected_metadata_sequence_[num_expected_++] = &unknown_;
900 }
901
902 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
903 return false;
904
905 /*
906 * respond all, ignore APPLICATION id of app#1 & app#2
907 */
908
909 printf("testing set_metadata_respond_all()... ");
910 if(!decoder->set_metadata_respond_all()) {
911 printf("FAILED, returned false\n");
912 return false;
913 }
914 printf("OK\n");
915
916 printf("testing set_metadata_ignore_application(of app block #1)... ");
917 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
918 printf("FAILED, returned false\n");
919 return false;
920 }
921 printf("OK\n");
922
923 printf("testing set_metadata_ignore_application(of app block #2)... ");
924 if(!decoder->set_metadata_ignore_application(application2_.data.application.id)) {
925 printf("FAILED, returned false\n");
926 return false;
927 }
928 printf("OK\n");
929
930 num_expected_ = 0;
931 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
932 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
933 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
934 expected_metadata_sequence_[num_expected_++] = &padding_;
935 expected_metadata_sequence_[num_expected_++] = &seektable_;
936 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
937 expected_metadata_sequence_[num_expected_++] = &picture_;
938 expected_metadata_sequence_[num_expected_++] = &unknown_;
939 }
940 else {
941 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
942 expected_metadata_sequence_[num_expected_++] = &padding_;
943 expected_metadata_sequence_[num_expected_++] = &seektable_;
944 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
945 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
946 expected_metadata_sequence_[num_expected_++] = &picture_;
947 expected_metadata_sequence_[num_expected_++] = &unknown_;
948 }
949
950 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
951 return false;
952
953 /*
954 * ignore all, respond VORBIS_COMMENT
955 */
956
957 printf("testing set_metadata_ignore_all()... ");
958 if(!decoder->set_metadata_ignore_all()) {
959 printf("FAILED, returned false\n");
960 return false;
961 }
962 printf("OK\n");
963
964 printf("testing set_metadata_respond(VORBIS_COMMENT)... ");
965 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_VORBIS_COMMENT)) {
966 printf("FAILED, returned false\n");
967 return false;
968 }
969 printf("OK\n");
970
971 num_expected_ = 0;
972 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
973
974 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
975 return false;
976
977 /*
978 * ignore all, respond APPLICATION
979 */
980
981 printf("testing set_metadata_ignore_all()... ");
982 if(!decoder->set_metadata_ignore_all()) {
983 printf("FAILED, returned false\n");
984 return false;
985 }
986 printf("OK\n");
987
988 printf("testing set_metadata_respond(APPLICATION)... ");
989 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
990 printf("FAILED, returned false\n");
991 return false;
992 }
993 printf("OK\n");
994
995 num_expected_ = 0;
996 expected_metadata_sequence_[num_expected_++] = &application1_;
997 expected_metadata_sequence_[num_expected_++] = &application2_;
998
999 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1000 return false;
1001
1002 /*
1003 * ignore all, respond APPLICATION id of app#1
1004 */
1005
1006 printf("testing set_metadata_ignore_all()... ");
1007 if(!decoder->set_metadata_ignore_all()) {
1008 printf("FAILED, returned false\n");
1009 return false;
1010 }
1011 printf("OK\n");
1012
1013 printf("testing set_metadata_respond_application(of app block #1)... ");
1014 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1015 printf("FAILED, returned false\n");
1016 return false;
1017 }
1018 printf("OK\n");
1019
1020 num_expected_ = 0;
1021 expected_metadata_sequence_[num_expected_++] = &application1_;
1022
1023 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1024 return false;
1025
1026 /*
1027 * ignore all, respond APPLICATION id of app#1 & app#2
1028 */
1029
1030 printf("testing set_metadata_ignore_all()... ");
1031 if(!decoder->set_metadata_ignore_all()) {
1032 printf("FAILED, returned false\n");
1033 return false;
1034 }
1035 printf("OK\n");
1036
1037 printf("testing set_metadata_respond_application(of app block #1)... ");
1038 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1039 printf("FAILED, returned false\n");
1040 return false;
1041 }
1042 printf("OK\n");
1043
1044 printf("testing set_metadata_respond_application(of app block #2)... ");
1045 if(!decoder->set_metadata_respond_application(application2_.data.application.id)) {
1046 printf("FAILED, returned false\n");
1047 return false;
1048 }
1049 printf("OK\n");
1050
1051 num_expected_ = 0;
1052 expected_metadata_sequence_[num_expected_++] = &application1_;
1053 expected_metadata_sequence_[num_expected_++] = &application2_;
1054
1055 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1056 return false;
1057
1058 /*
1059 * respond all, ignore APPLICATION, respond APPLICATION id of app#1
1060 */
1061
1062 printf("testing set_metadata_respond_all()... ");
1063 if(!decoder->set_metadata_respond_all()) {
1064 printf("FAILED, returned false\n");
1065 return false;
1066 }
1067 printf("OK\n");
1068
1069 printf("testing set_metadata_ignore(APPLICATION)... ");
1070 if(!decoder->set_metadata_ignore(FLAC__METADATA_TYPE_APPLICATION)) {
1071 printf("FAILED, returned false\n");
1072 return false;
1073 }
1074 printf("OK\n");
1075
1076 printf("testing set_metadata_respond_application(of app block #1)... ");
1077 if(!decoder->set_metadata_respond_application(application1_.data.application.id)) {
1078 printf("FAILED, returned false\n");
1079 return false;
1080 }
1081 printf("OK\n");
1082
1083 num_expected_ = 0;
1084 if(is_ogg) { /* encoder moves vorbis comment after streaminfo according to ogg mapping */
1085 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1086 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1087 expected_metadata_sequence_[num_expected_++] = &padding_;
1088 expected_metadata_sequence_[num_expected_++] = &seektable_;
1089 expected_metadata_sequence_[num_expected_++] = &application1_;
1090 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1091 expected_metadata_sequence_[num_expected_++] = &picture_;
1092 expected_metadata_sequence_[num_expected_++] = &unknown_;
1093 }
1094 else {
1095 expected_metadata_sequence_[num_expected_++] = &streaminfo_;
1096 expected_metadata_sequence_[num_expected_++] = &padding_;
1097 expected_metadata_sequence_[num_expected_++] = &seektable_;
1098 expected_metadata_sequence_[num_expected_++] = &application1_;
1099 expected_metadata_sequence_[num_expected_++] = &vorbiscomment_;
1100 expected_metadata_sequence_[num_expected_++] = &cuesheet_;
1101 expected_metadata_sequence_[num_expected_++] = &picture_;
1102 expected_metadata_sequence_[num_expected_++] = &unknown_;
1103 }
1104
1105 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1106 return false;
1107
1108 /*
1109 * ignore all, respond APPLICATION, ignore APPLICATION id of app#1
1110 */
1111
1112 printf("testing set_metadata_ignore_all()... ");
1113 if(!decoder->set_metadata_ignore_all()) {
1114 printf("FAILED, returned false\n");
1115 return false;
1116 }
1117 printf("OK\n");
1118
1119 printf("testing set_metadata_respond(APPLICATION)... ");
1120 if(!decoder->set_metadata_respond(FLAC__METADATA_TYPE_APPLICATION)) {
1121 printf("FAILED, returned false\n");
1122 return false;
1123 }
1124 printf("OK\n");
1125
1126 printf("testing set_metadata_ignore_application(of app block #1)... ");
1127 if(!decoder->set_metadata_ignore_application(application1_.data.application.id)) {
1128 printf("FAILED, returned false\n");
1129 return false;
1130 }
1131 printf("OK\n");
1132
1133 num_expected_ = 0;
1134 expected_metadata_sequence_[num_expected_++] = &application2_;
1135
1136 if(!(layer < LAYER_FILE? dynamic_cast<StreamDecoder*>(decoder)->test_respond(is_ogg) : dynamic_cast<FileDecoder*>(decoder)->test_respond(is_ogg)))
1137 return false;
1138
1139 if(layer < LAYER_FILE) /* for LAYER_FILE, FLAC__stream_decoder_finish() closes the file */
1140 ::fclose(dynamic_cast<StreamDecoder*>(decoder)->file_);
1141
1142 printf("freeing decoder instance... ");
1143 delete decoder;
1144 printf("OK\n");
1145
1146 printf("\nPASSED!\n");
1147
1148 return true;
1149 }
1150
test_decoders()1151 bool test_decoders()
1152 {
1153 FLAC__bool is_ogg = false;
1154
1155 while(1) {
1156 init_metadata_blocks_();
1157
1158 if(!generate_file_(is_ogg))
1159 return false;
1160
1161 if(!test_stream_decoder(LAYER_STREAM, is_ogg))
1162 return false;
1163
1164 if(!test_stream_decoder(LAYER_SEEKABLE_STREAM, is_ogg))
1165 return false;
1166
1167 if(!test_stream_decoder(LAYER_FILE, is_ogg))
1168 return false;
1169
1170 if(!test_stream_decoder(LAYER_FILENAME, is_ogg))
1171 return false;
1172
1173 (void) grabbag__file_remove_file(flacfilename(is_ogg));
1174
1175 free_metadata_blocks_();
1176
1177 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
1178 break;
1179 is_ogg = true;
1180 }
1181
1182 return true;
1183 }
1184