/* Copyright (c) 2011-2013 Xiph.Org Foundation Written by Gregory Maxwell */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* This tests the API presented by the libopus system. It does not attempt to extensively exercise the codec internals. The strategy here is to simply the API interface invariants: That sane options are accepted, insane options are rejected, and that nothing blows up. In particular we don't actually test that settings are heeded by the codec (though we do check that get after set returns a sane value when it should). Other tests check the actual codec behavior. In cases where its reasonable to do so we test exhaustively, but its not reasonable to do so in all cases. Although these tests are simple they found several library bugs when they were initially developed. */ /* These tests are more sensitive if compiled with -DVALGRIND and run inside valgrind. Malloc failure testing requires glibc. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #include "arch.h" #include "opus_multistream.h" #include "opus.h" #include "test_opus_common.h" #ifdef VALGRIND #include <valgrind/memcheck.h> #define VG_UNDEF … #define VG_CHECK … #else #define VG_UNDEF(x,y) … #define VG_CHECK(x,y) … #endif #if defined(HAVE___MALLOC_HOOK) #define MALLOC_FAIL #include "os_support.h" #include <malloc.h> static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP, OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY}; void *malloc_hook(__attribute__((unused)) size_t size, __attribute__((unused)) const void *caller) { return 0; } #endif opus_int32 *null_int_ptr = …; opus_uint32 *null_uint_ptr = …; static const opus_int32 opus_rates[5] = …; opus_int32 test_dec_api(void) { … } opus_int32 test_msdec_api(void) { … } #ifdef VALGRIND #define UNDEFINE_FOR_PARSE … #else #define UNDEFINE_FOR_PARSE … #endif /* This test exercises the heck out of the libopus parser. It is much larger than the parser itself in part because it tries to hit a lot of corner cases that could never fail with the libopus code, but might be problematic for other implementations. */ opus_int32 test_parse(void) { … } /* This is a helper macro for the encoder tests. The encoder api tests all have a pattern of set-must-fail, set-must-fail, set-must-pass, get-and-compare, set-must-pass, get-and-compare. */ #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) … opus_int32 test_enc_api(void) { … } #define max_out … int test_repacketizer_api(void) { … } #ifdef MALLOC_FAIL /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning * under GCC. However, this is the cleanest way to test malloc failure * handling in our codebase, and the lack of thread safety isn't an * issue here. We therefore disable the warning for this function. */ #if OPUS_GNUC_PREREQ(4,6) /* Save the current warning settings */ #pragma GCC diagnostic push #endif #pragma GCC diagnostic ignored "-Wdeprecated-declarations" typedef void *(*mhook)(size_t __size, __const void *); #endif int test_malloc_fail(void) { … } #ifdef MALLOC_FAIL #if __GNUC_PREREQ(4,6) #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */ #endif #endif int main(int _argc, char **_argv) { … }