diff options
author | Diego Biurrun <diego@biurrun.de> | 2016-03-17 21:13:17 +0300 |
---|---|---|
committer | Diego Biurrun <diego@biurrun.de> | 2016-04-07 17:14:42 +0300 |
commit | d12b5b2f135aade4099f4b26b0fe678656158c13 (patch) | |
tree | d5b44fd428a1c68213fe51aca21b5819bce3d33a | |
parent | 330177b508420a553083df94f22cbd5142de0f4a (diff) |
build: Split test programs off into separate files
This avoids spurious library rebuilds when only the test program
code is changed and simplifies the build system.
54 files changed, 2636 insertions, 2173 deletions
diff --git a/libavcodec/iirfilter-test.c b/libavcodec/iirfilter-test.c new file mode 100644 index 0000000000..139a35d505 --- /dev/null +++ b/libavcodec/iirfilter-test.c @@ -0,0 +1,52 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <math.h> +#include <stdint.h> +#include <stdio.h> + +#include "iirfilter.h" + +#define FILT_ORDER 4 +#define SIZE 1024 + +int main(void) +{ + struct FFIIRFilterCoeffs *fcoeffs = NULL; + struct FFIIRFilterState *fstate = NULL; + float cutoff_coeff = 0.4; + int16_t x[SIZE], y[SIZE]; + int i; + + fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH, + FF_FILTER_MODE_LOWPASS, FILT_ORDER, + cutoff_coeff, 0.0, 0.0); + fstate = ff_iir_filter_init_state(FILT_ORDER); + + for (i = 0; i < SIZE; i++) + x[i] = lrint(0.75 * INT16_MAX * sin(0.5 * M_PI * i * i / SIZE)); + + ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1); + + for (i = 0; i < SIZE; i++) + printf("%6d %6d\n", x[i], y[i]); + + ff_iir_filter_free_coeffs(fcoeffs); + ff_iir_filter_free_state(fstate); + return 0; +} diff --git a/libavcodec/iirfilter.c b/libavcodec/iirfilter.c index fba4ac800a..442c837338 100644 --- a/libavcodec/iirfilter.c +++ b/libavcodec/iirfilter.c @@ -315,35 +315,3 @@ av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs) } av_free(coeffs); } - -#ifdef TEST -#include <stdio.h> - -#define FILT_ORDER 4 -#define SIZE 1024 -int main(void) -{ - struct FFIIRFilterCoeffs *fcoeffs = NULL; - struct FFIIRFilterState *fstate = NULL; - float cutoff_coeff = 0.4; - int16_t x[SIZE], y[SIZE]; - int i; - - fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH, - FF_FILTER_MODE_LOWPASS, FILT_ORDER, - cutoff_coeff, 0.0, 0.0); - fstate = ff_iir_filter_init_state(FILT_ORDER); - - for (i = 0; i < SIZE; i++) - x[i] = lrint(0.75 * INT16_MAX * sin(0.5 * M_PI * i * i / SIZE)); - - ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1); - - for (i = 0; i < SIZE; i++) - printf("%6d %6d\n", x[i], y[i]); - - ff_iir_filter_free_coeffs(fcoeffs); - ff_iir_filter_free_state(fstate); - return 0; -} -#endif /* TEST */ diff --git a/libavcodec/rangecoder-test.c b/libavcodec/rangecoder-test.c new file mode 100644 index 0000000000..f4c76c01f0 --- /dev/null +++ b/libavcodec/rangecoder-test.c @@ -0,0 +1,64 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <string.h> + +#include "libavutil/lfg.h" +#include "libavutil/log.h" + +#include "rangecoder.h" + +#define SIZE 10240 + +int main(void) +{ + RangeCoder c; + uint8_t b[9 * SIZE]; + uint8_t r[9 * SIZE]; + int i; + uint8_t state[10]; + AVLFG prng; + + av_lfg_init(&prng, 1); + + ff_init_range_encoder(&c, b, SIZE); + ff_build_rac_states(&c, 0.05 * (1LL << 32), 128 + 64 + 32 + 16); + + memset(state, 128, sizeof(state)); + + for (i = 0; i < SIZE; i++) + r[i] = av_lfg_get(&prng) % 7; + + for (i = 0; i < SIZE; i++) + put_rac(&c, state, r[i] & 1); + + ff_rac_terminate(&c); + + ff_init_range_decoder(&c, b, SIZE); + + memset(state, 128, sizeof(state)); + + for (i = 0; i < SIZE; i++) + if ((r[i] & 1) != get_rac(&c, state)) { + av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i); + return 1; + } + + return 0; +} diff --git a/libavcodec/rangecoder.c b/libavcodec/rangecoder.c index 86bc362e48..4c4731dd4e 100644 --- a/libavcodec/rangecoder.c +++ b/libavcodec/rangecoder.c @@ -113,47 +113,3 @@ int ff_rac_terminate(RangeCoder *c) return c->bytestream - c->bytestream_start; } - -#ifdef TEST -#define SIZE 10240 - -#include "libavutil/lfg.h" -#include "libavutil/log.h" - -int main(void) -{ - RangeCoder c; - uint8_t b[9 * SIZE]; - uint8_t r[9 * SIZE]; - int i; - uint8_t state[10]; - AVLFG prng; - - av_lfg_init(&prng, 1); - - ff_init_range_encoder(&c, b, SIZE); - ff_build_rac_states(&c, 0.05 * (1LL << 32), 128 + 64 + 32 + 16); - - memset(state, 128, sizeof(state)); - - for (i = 0; i < SIZE; i++) - r[i] = av_lfg_get(&prng) % 7; - - for (i = 0; i < SIZE; i++) - put_rac(&c, state, r[i] & 1); - - ff_rac_terminate(&c); - - ff_init_range_decoder(&c, b, SIZE); - - memset(state, 128, sizeof(state)); - - for (i = 0; i < SIZE; i++) - if ((r[i] & 1) != get_rac(&c, state)) { - av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i); - return 1; - } - - return 0; -} -#endif /* TEST */ diff --git a/libavdevice/timefilter-test.c b/libavdevice/timefilter-test.c new file mode 100644 index 0000000000..5e93f3c8d3 --- /dev/null +++ b/libavdevice/timefilter-test.c @@ -0,0 +1,92 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "libavutil/common.h" +#include "libavutil/lfg.h" + +#include "timefilter.h" + +#define LFG_MAX ((1LL << 32) - 1) + +int main(void) +{ + AVLFG prng; + double n0, n1; +#define SAMPLES 1000 + double ideal[SAMPLES]; + double samples[SAMPLES]; + for (n0 = 0; n0 < 40; n0 = 2 * n0 + 1) { + for (n1 = 0; n1 < 10; n1 = 2 * n1 + 1) { + double best_error = 1000000000; + double bestpar0 = 1; + double bestpar1 = 0.001; + int better, i; + + av_lfg_init(&prng, 123); + for (i = 0; i < SAMPLES; i++) { + ideal[i] = 10 + i + n1 * i / (1000); + samples[i] = ideal[i] + n0 * (av_lfg_get(&prng) - LFG_MAX / 2) / (LFG_MAX * 10LL); + } + + do { + double par0, par1; + better = 0; + for (par0 = bestpar0 * 0.8; par0 <= bestpar0 * 1.21; par0 += bestpar0 * 0.05) { + for (par1 = bestpar1 * 0.8; par1 <= bestpar1 * 1.21; par1 += bestpar1 * 0.05) { + double error = 0; + TimeFilter *tf = ff_timefilter_new(1, par0, par1); + if (!tf) { + printf("Could not allocate memory for timefilter.\n"); + exit(1); + } + for (i = 0; i < SAMPLES; i++) { + double filtered; + filtered = ff_timefilter_update(tf, samples[i], 1); + error += (filtered - ideal[i]) * (filtered - ideal[i]); + } + ff_timefilter_destroy(tf); + if (error < best_error) { + best_error = error; + bestpar0 = par0; + bestpar1 = par1; + better = 1; + } + } + } + } while (better); +#if 0 + double lastfil = 9; + TimeFilter *tf = ff_timefilter_new(1, bestpar0, bestpar1); + for (i = 0; i < SAMPLES; i++) { + double filtered; + filtered = ff_timefilter_update(tf, samples[i], 1); + printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i], + samples[FFMAX(i, 1)] - samples[FFMAX(i - 1, 0)], filtered - lastfil); + lastfil = filtered; + } + ff_timefilter_destroy(tf); +#else + printf(" [%f %f %9f]", bestpar0, bestpar1, best_error); +#endif + } + printf("\n"); + } + return 0; +} diff --git a/libavdevice/timefilter.c b/libavdevice/timefilter.c index ee25d7a4ac..4e0d5006d7 100644 --- a/libavdevice/timefilter.c +++ b/libavdevice/timefilter.c @@ -77,74 +77,3 @@ double ff_timefilter_update(TimeFilter *self, double system_time, double period) } return self->cycle_time; } - -#ifdef TEST -#include "libavutil/lfg.h" -#define LFG_MAX ((1LL << 32) - 1) - -int main(void) -{ - AVLFG prng; - double n0, n1; -#define SAMPLES 1000 - double ideal[SAMPLES]; - double samples[SAMPLES]; - for (n0 = 0; n0 < 40; n0 = 2 * n0 + 1) { - for (n1 = 0; n1 < 10; n1 = 2 * n1 + 1) { - double best_error = 1000000000; - double bestpar0 = 1; - double bestpar1 = 0.001; - int better, i; - - av_lfg_init(&prng, 123); - for (i = 0; i < SAMPLES; i++) { - ideal[i] = 10 + i + n1 * i / (1000); - samples[i] = ideal[i] + n0 * (av_lfg_get(&prng) - LFG_MAX / 2) / (LFG_MAX * 10LL); - } - - do { - double par0, par1; - better = 0; - for (par0 = bestpar0 * 0.8; par0 <= bestpar0 * 1.21; par0 += bestpar0 * 0.05) { - for (par1 = bestpar1 * 0.8; par1 <= bestpar1 * 1.21; par1 += bestpar1 * 0.05) { - double error = 0; - TimeFilter *tf = ff_timefilter_new(1, par0, par1); - if (!tf) { - printf("Could not allocate memory for timefilter.\n"); - exit(1); - } - for (i = 0; i < SAMPLES; i++) { - double filtered; - filtered = ff_timefilter_update(tf, samples[i], 1); - error += (filtered - ideal[i]) * (filtered - ideal[i]); - } - ff_timefilter_destroy(tf); - if (error < best_error) { - best_error = error; - bestpar0 = par0; - bestpar1 = par1; - better = 1; - } - } - } - } while (better); -#if 0 - double lastfil = 9; - TimeFilter *tf = ff_timefilter_new(1, bestpar0, bestpar1); - for (i = 0; i < SAMPLES; i++) { - double filtered; - filtered = ff_timefilter_update(tf, samples[i], 1); - printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i], - samples[FFMAX(i, 1)] - samples[FFMAX(i - 1, 0)], filtered - lastfil); - lastfil = filtered; - } - ff_timefilter_destroy(tf); -#else - printf(" [%f %f %9f]", bestpar0, bestpar1, best_error); -#endif - } - printf("\n"); - } - return 0; -} -#endif diff --git a/libavfilter/filtfmts.c b/libavfilter/filtfmts-test.c index cc04654222..cc04654222 100644 --- a/libavfilter/filtfmts.c +++ b/libavfilter/filtfmts-test.c diff --git a/libavformat/rtmpdh-test.c b/libavformat/rtmpdh-test.c new file mode 100644 index 0000000000..603bb79ebc --- /dev/null +++ b/libavformat/rtmpdh-test.c @@ -0,0 +1,158 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "rtmpdh.c" + +#include <stdio.h> + +static int test_random_shared_secret(void) +{ + FF_DH *peer1 = NULL, *peer2 = NULL; + int ret; + uint8_t pubkey1[128], pubkey2[128]; + uint8_t sharedkey1[128], sharedkey2[128]; + + peer1 = ff_dh_init(1024); + peer2 = ff_dh_init(1024); + if (!peer1 || !peer2) { + ret = AVERROR(ENOMEM); + goto fail; + } + if ((ret = ff_dh_generate_public_key(peer1)) < 0) + goto fail; + if ((ret = ff_dh_generate_public_key(peer2)) < 0) + goto fail; + if ((ret = ff_dh_write_public_key(peer1, pubkey1, sizeof(pubkey1))) < 0) + goto fail; + if ((ret = ff_dh_write_public_key(peer2, pubkey2, sizeof(pubkey2))) < 0) + goto fail; + if ((ret = ff_dh_compute_shared_secret_key(peer1, pubkey2, sizeof(pubkey2), + sharedkey1, sizeof(sharedkey1))) < 0) + goto fail; + if ((ret = ff_dh_compute_shared_secret_key(peer2, pubkey1, sizeof(pubkey1), + sharedkey2, sizeof(sharedkey2))) < 0) + goto fail; + if (memcmp(sharedkey1, sharedkey2, sizeof(sharedkey1))) { + printf("Mismatched generated shared key\n"); + ret = AVERROR_INVALIDDATA; + } else { + printf("Generated shared key ok\n"); + } +fail: + ff_dh_free(peer1); + ff_dh_free(peer2); + return ret; +} + +static const char *private_key = + "976C18FCADC255B456564F74F3EEDA59D28AF6B744D743F2357BFD2404797EF896EF1A" + "7C1CBEAAA3AB60AF3192D189CFF3F991C9CBBFD78119FCA2181384B94011943B6D6F28" + "9E1B708E2D1A0C7771169293F03DA27E561F15F16F0AC9BC858C77A80FA98FD088A232" + "19D08BE6F165DE0B02034B18705829FAD0ACB26A5B75EF"; +static const char *public_key = + "F272ECF8362257C5D2C3CC2229CF9C0A03225BC109B1DBC76A68C394F256ACA3EF5F64" + "FC270C26382BF315C19E97A76104A716FC998A651E8610A3AE6CF65D8FAE5D3F32EEA0" + "0B32CB9609B494116A825D7142D17B88E3D20EDD98743DE29CF37A23A9F6A58B960591" + "3157D5965FCB46DDA73A1F08DD897BAE88DFE6FC937CBA"; +static const uint8_t public_key_bin[] = { + 0xf2, 0x72, 0xec, 0xf8, 0x36, 0x22, 0x57, 0xc5, 0xd2, 0xc3, 0xcc, 0x22, + 0x29, 0xcf, 0x9c, 0x0a, 0x03, 0x22, 0x5b, 0xc1, 0x09, 0xb1, 0xdb, 0xc7, + 0x6a, 0x68, 0xc3, 0x94, 0xf2, 0x56, 0xac, 0xa3, 0xef, 0x5f, 0x64, 0xfc, + 0x27, 0x0c, 0x26, 0x38, 0x2b, 0xf3, 0x15, 0xc1, 0x9e, 0x97, 0xa7, 0x61, + 0x04, 0xa7, 0x16, 0xfc, 0x99, 0x8a, 0x65, 0x1e, 0x86, 0x10, 0xa3, 0xae, + 0x6c, 0xf6, 0x5d, 0x8f, 0xae, 0x5d, 0x3f, 0x32, 0xee, 0xa0, 0x0b, 0x32, + 0xcb, 0x96, 0x09, 0xb4, 0x94, 0x11, 0x6a, 0x82, 0x5d, 0x71, 0x42, 0xd1, + 0x7b, 0x88, 0xe3, 0xd2, 0x0e, 0xdd, 0x98, 0x74, 0x3d, 0xe2, 0x9c, 0xf3, + 0x7a, 0x23, 0xa9, 0xf6, 0xa5, 0x8b, 0x96, 0x05, 0x91, 0x31, 0x57, 0xd5, + 0x96, 0x5f, 0xcb, 0x46, 0xdd, 0xa7, 0x3a, 0x1f, 0x08, 0xdd, 0x89, 0x7b, + 0xae, 0x88, 0xdf, 0xe6, 0xfc, 0x93, 0x7c, 0xba +}; +static const uint8_t peer_public_key[] = { + 0x58, 0x66, 0x05, 0x49, 0x94, 0x23, 0x2b, 0x66, 0x52, 0x13, 0xff, 0x46, + 0xf2, 0xb3, 0x79, 0xa9, 0xee, 0xae, 0x1a, 0x13, 0xf0, 0x71, 0x52, 0xfb, + 0x93, 0x4e, 0xee, 0x97, 0x05, 0x73, 0x50, 0x7d, 0xaf, 0x02, 0x07, 0x72, + 0xac, 0xdc, 0xa3, 0x95, 0x78, 0xee, 0x9a, 0x19, 0x71, 0x7e, 0x99, 0x9f, + 0x2a, 0xd4, 0xb3, 0xe2, 0x0c, 0x1d, 0x1a, 0x78, 0x4c, 0xde, 0xf1, 0xad, + 0xb4, 0x60, 0xa8, 0x51, 0xac, 0x71, 0xec, 0x86, 0x70, 0xa2, 0x63, 0x36, + 0x92, 0x7c, 0xe3, 0x87, 0xee, 0xe4, 0xf1, 0x62, 0x24, 0x74, 0xb4, 0x04, + 0xfa, 0x5c, 0xdf, 0xba, 0xfa, 0xa3, 0xc2, 0xbb, 0x62, 0x27, 0xd0, 0xf4, + 0xe4, 0x43, 0xda, 0x8a, 0x88, 0x69, 0x60, 0xe2, 0xdb, 0x75, 0x2a, 0x98, + 0x9d, 0xb5, 0x50, 0xe3, 0x99, 0xda, 0xe0, 0xa6, 0x14, 0xc9, 0x80, 0x12, + 0xf9, 0x3c, 0xac, 0x06, 0x02, 0x7a, 0xde, 0x74 +}; +static const uint8_t shared_secret[] = { + 0xb2, 0xeb, 0xcb, 0x71, 0xf3, 0x61, 0xfb, 0x5b, 0x4e, 0x5c, 0x4c, 0xcf, + 0x5c, 0x08, 0x5f, 0x96, 0x26, 0x77, 0x1d, 0x31, 0xf1, 0xe1, 0xf7, 0x4b, + 0x92, 0xac, 0x82, 0x2a, 0x88, 0xc7, 0x83, 0xe1, 0xc7, 0xf3, 0xd3, 0x1a, + 0x7d, 0xc8, 0x31, 0xe3, 0x97, 0xe4, 0xec, 0x31, 0x0e, 0x8f, 0x73, 0x1a, + 0xe4, 0xf6, 0xd8, 0xc8, 0x94, 0xff, 0xa0, 0x03, 0x84, 0x03, 0x0f, 0xa5, + 0x30, 0x5d, 0x67, 0xe0, 0x7a, 0x3b, 0x5f, 0xed, 0x4c, 0xf5, 0xbc, 0x18, + 0xea, 0xd4, 0x77, 0xa9, 0x07, 0xb3, 0x54, 0x0b, 0x02, 0xd9, 0xc6, 0xb8, + 0x66, 0x5e, 0xec, 0xa4, 0xcd, 0x47, 0xed, 0xc9, 0x38, 0xc6, 0x91, 0x08, + 0xf3, 0x85, 0x9b, 0x69, 0x16, 0x78, 0x0d, 0xb7, 0x74, 0x51, 0xaa, 0x5b, + 0x4d, 0x74, 0xe4, 0x29, 0x2e, 0x9e, 0x8e, 0xf7, 0xe5, 0x42, 0x83, 0xb0, + 0x65, 0xb0, 0xce, 0xc6, 0xb2, 0x8f, 0x5b, 0xb0 +}; + +static int test_ref_data(void) +{ + FF_DH *dh; + int ret = AVERROR(ENOMEM); + uint8_t pubkey_test[128]; + uint8_t sharedkey_test[128]; + + dh = ff_dh_init(1024); + if (!dh) + goto fail; + bn_hex2bn(dh->priv_key, private_key, ret); + if (!ret) + goto fail; + bn_hex2bn(dh->pub_key, public_key, ret); + if (!ret) + goto fail; + if ((ret = ff_dh_write_public_key(dh, pubkey_test, sizeof(pubkey_test))) < 0) + goto fail; + if (memcmp(pubkey_test, public_key_bin, sizeof(pubkey_test))) { + printf("Mismatched generated public key\n"); + ret = AVERROR_INVALIDDATA; + goto fail; + } else { + printf("Generated public key ok\n"); + } + if ((ret = ff_dh_compute_shared_secret_key(dh, peer_public_key, sizeof(peer_public_key), + sharedkey_test, sizeof(sharedkey_test))) < 0) + goto fail; + if (memcmp(shared_secret, sharedkey_test, sizeof(sharedkey_test))) { + printf("Mismatched generated shared key\n"); + ret = AVERROR_INVALIDDATA; + } else { + printf("Generated shared key ok\n"); + } +fail: + ff_dh_free(dh); + return ret; +} + +int main(void) +{ + if (test_random_shared_secret() < 0) + return 1; + if (test_ref_data() < 0) + return 1; + return 0; +} diff --git a/libavformat/rtmpdh.c b/libavformat/rtmpdh.c index 9f3a43fdfd..e7a83e173f 100644 --- a/libavformat/rtmpdh.c +++ b/libavformat/rtmpdh.c @@ -360,145 +360,3 @@ fail: return ret; } - -#ifdef TEST - -#include <stdio.h> - -static int test_random_shared_secret(void) -{ - FF_DH *peer1 = NULL, *peer2 = NULL; - int ret; - uint8_t pubkey1[128], pubkey2[128]; - uint8_t sharedkey1[128], sharedkey2[128]; - - peer1 = ff_dh_init(1024); - peer2 = ff_dh_init(1024); - if (!peer1 || !peer2) { - ret = AVERROR(ENOMEM); - goto fail; - } - if ((ret = ff_dh_generate_public_key(peer1)) < 0) - goto fail; - if ((ret = ff_dh_generate_public_key(peer2)) < 0) - goto fail; - if ((ret = ff_dh_write_public_key(peer1, pubkey1, sizeof(pubkey1))) < 0) - goto fail; - if ((ret = ff_dh_write_public_key(peer2, pubkey2, sizeof(pubkey2))) < 0) - goto fail; - if ((ret = ff_dh_compute_shared_secret_key(peer1, pubkey2, sizeof(pubkey2), - sharedkey1, sizeof(sharedkey1))) < 0) - goto fail; - if ((ret = ff_dh_compute_shared_secret_key(peer2, pubkey1, sizeof(pubkey1), - sharedkey2, sizeof(sharedkey2))) < 0) - goto fail; - if (memcmp(sharedkey1, sharedkey2, sizeof(sharedkey1))) { - printf("Mismatched generated shared key\n"); - ret = AVERROR_INVALIDDATA; - } else { - printf("Generated shared key ok\n"); - } -fail: - ff_dh_free(peer1); - ff_dh_free(peer2); - return ret; -} - -static const char *private_key = - "976C18FCADC255B456564F74F3EEDA59D28AF6B744D743F2357BFD2404797EF896EF1A" - "7C1CBEAAA3AB60AF3192D189CFF3F991C9CBBFD78119FCA2181384B94011943B6D6F28" - "9E1B708E2D1A0C7771169293F03DA27E561F15F16F0AC9BC858C77A80FA98FD088A232" - "19D08BE6F165DE0B02034B18705829FAD0ACB26A5B75EF"; -static const char *public_key = - "F272ECF8362257C5D2C3CC2229CF9C0A03225BC109B1DBC76A68C394F256ACA3EF5F64" - "FC270C26382BF315C19E97A76104A716FC998A651E8610A3AE6CF65D8FAE5D3F32EEA0" - "0B32CB9609B494116A825D7142D17B88E3D20EDD98743DE29CF37A23A9F6A58B960591" - "3157D5965FCB46DDA73A1F08DD897BAE88DFE6FC937CBA"; -static const uint8_t public_key_bin[] = { - 0xf2, 0x72, 0xec, 0xf8, 0x36, 0x22, 0x57, 0xc5, 0xd2, 0xc3, 0xcc, 0x22, - 0x29, 0xcf, 0x9c, 0x0a, 0x03, 0x22, 0x5b, 0xc1, 0x09, 0xb1, 0xdb, 0xc7, - 0x6a, 0x68, 0xc3, 0x94, 0xf2, 0x56, 0xac, 0xa3, 0xef, 0x5f, 0x64, 0xfc, - 0x27, 0x0c, 0x26, 0x38, 0x2b, 0xf3, 0x15, 0xc1, 0x9e, 0x97, 0xa7, 0x61, - 0x04, 0xa7, 0x16, 0xfc, 0x99, 0x8a, 0x65, 0x1e, 0x86, 0x10, 0xa3, 0xae, - 0x6c, 0xf6, 0x5d, 0x8f, 0xae, 0x5d, 0x3f, 0x32, 0xee, 0xa0, 0x0b, 0x32, - 0xcb, 0x96, 0x09, 0xb4, 0x94, 0x11, 0x6a, 0x82, 0x5d, 0x71, 0x42, 0xd1, - 0x7b, 0x88, 0xe3, 0xd2, 0x0e, 0xdd, 0x98, 0x74, 0x3d, 0xe2, 0x9c, 0xf3, - 0x7a, 0x23, 0xa9, 0xf6, 0xa5, 0x8b, 0x96, 0x05, 0x91, 0x31, 0x57, 0xd5, - 0x96, 0x5f, 0xcb, 0x46, 0xdd, 0xa7, 0x3a, 0x1f, 0x08, 0xdd, 0x89, 0x7b, - 0xae, 0x88, 0xdf, 0xe6, 0xfc, 0x93, 0x7c, 0xba -}; -static const uint8_t peer_public_key[] = { - 0x58, 0x66, 0x05, 0x49, 0x94, 0x23, 0x2b, 0x66, 0x52, 0x13, 0xff, 0x46, - 0xf2, 0xb3, 0x79, 0xa9, 0xee, 0xae, 0x1a, 0x13, 0xf0, 0x71, 0x52, 0xfb, - 0x93, 0x4e, 0xee, 0x97, 0x05, 0x73, 0x50, 0x7d, 0xaf, 0x02, 0x07, 0x72, - 0xac, 0xdc, 0xa3, 0x95, 0x78, 0xee, 0x9a, 0x19, 0x71, 0x7e, 0x99, 0x9f, - 0x2a, 0xd4, 0xb3, 0xe2, 0x0c, 0x1d, 0x1a, 0x78, 0x4c, 0xde, 0xf1, 0xad, - 0xb4, 0x60, 0xa8, 0x51, 0xac, 0x71, 0xec, 0x86, 0x70, 0xa2, 0x63, 0x36, - 0x92, 0x7c, 0xe3, 0x87, 0xee, 0xe4, 0xf1, 0x62, 0x24, 0x74, 0xb4, 0x04, - 0xfa, 0x5c, 0xdf, 0xba, 0xfa, 0xa3, 0xc2, 0xbb, 0x62, 0x27, 0xd0, 0xf4, - 0xe4, 0x43, 0xda, 0x8a, 0x88, 0x69, 0x60, 0xe2, 0xdb, 0x75, 0x2a, 0x98, - 0x9d, 0xb5, 0x50, 0xe3, 0x99, 0xda, 0xe0, 0xa6, 0x14, 0xc9, 0x80, 0x12, - 0xf9, 0x3c, 0xac, 0x06, 0x02, 0x7a, 0xde, 0x74 -}; -static const uint8_t shared_secret[] = { - 0xb2, 0xeb, 0xcb, 0x71, 0xf3, 0x61, 0xfb, 0x5b, 0x4e, 0x5c, 0x4c, 0xcf, - 0x5c, 0x08, 0x5f, 0x96, 0x26, 0x77, 0x1d, 0x31, 0xf1, 0xe1, 0xf7, 0x4b, - 0x92, 0xac, 0x82, 0x2a, 0x88, 0xc7, 0x83, 0xe1, 0xc7, 0xf3, 0xd3, 0x1a, - 0x7d, 0xc8, 0x31, 0xe3, 0x97, 0xe4, 0xec, 0x31, 0x0e, 0x8f, 0x73, 0x1a, - 0xe4, 0xf6, 0xd8, 0xc8, 0x94, 0xff, 0xa0, 0x03, 0x84, 0x03, 0x0f, 0xa5, - 0x30, 0x5d, 0x67, 0xe0, 0x7a, 0x3b, 0x5f, 0xed, 0x4c, 0xf5, 0xbc, 0x18, - 0xea, 0xd4, 0x77, 0xa9, 0x07, 0xb3, 0x54, 0x0b, 0x02, 0xd9, 0xc6, 0xb8, - 0x66, 0x5e, 0xec, 0xa4, 0xcd, 0x47, 0xed, 0xc9, 0x38, 0xc6, 0x91, 0x08, - 0xf3, 0x85, 0x9b, 0x69, 0x16, 0x78, 0x0d, 0xb7, 0x74, 0x51, 0xaa, 0x5b, - 0x4d, 0x74, 0xe4, 0x29, 0x2e, 0x9e, 0x8e, 0xf7, 0xe5, 0x42, 0x83, 0xb0, - 0x65, 0xb0, 0xce, 0xc6, 0xb2, 0x8f, 0x5b, 0xb0 -}; - -static int test_ref_data(void) -{ - FF_DH *dh; - int ret = AVERROR(ENOMEM); - uint8_t pubkey_test[128]; - uint8_t sharedkey_test[128]; - - dh = ff_dh_init(1024); - if (!dh) - goto fail; - bn_hex2bn(dh->priv_key, private_key, ret); - if (!ret) - goto fail; - bn_hex2bn(dh->pub_key, public_key, ret); - if (!ret) - goto fail; - if ((ret = ff_dh_write_public_key(dh, pubkey_test, sizeof(pubkey_test))) < 0) - goto fail; - if (memcmp(pubkey_test, public_key_bin, sizeof(pubkey_test))) { - printf("Mismatched generated public key\n"); - ret = AVERROR_INVALIDDATA; - goto fail; - } else { - printf("Generated public key ok\n"); - } - if ((ret = ff_dh_compute_shared_secret_key(dh, peer_public_key, sizeof(peer_public_key), - sharedkey_test, sizeof(sharedkey_test))) < 0) - goto fail; - if (memcmp(shared_secret, sharedkey_test, sizeof(sharedkey_test))) { - printf("Mismatched generated shared key\n"); - ret = AVERROR_INVALIDDATA; - } else { - printf("Generated shared key ok\n"); - } -fail: - ff_dh_free(dh); - return ret; -} - -int main(void) -{ - if (test_random_shared_secret() < 0) - return 1; - if (test_ref_data() < 0) - return 1; - return 0; -} -#endif diff --git a/libavformat/srtp-test.c b/libavformat/srtp-test.c new file mode 100644 index 0000000000..fe8749cb66 --- /dev/null +++ b/libavformat/srtp-test.c @@ -0,0 +1,167 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "rtpdec.h" +#include "srtp.h" + +static const char *aes128_80_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; + +static const uint8_t rtp_aes128_80[] = { + // RTP header + 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0x62, 0x69, 0x76, 0xca, 0xc5, + // HMAC + 0xa1, 0xac, 0x1b, 0xb4, 0xa0, 0x1c, 0xd5, 0x49, 0x28, 0x99, +}; + +static const uint8_t rtcp_aes128_80[] = { + // RTCP header + 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0x8a, 0xac, 0xdc, 0xa5, 0x4c, 0xf6, 0x78, 0xa6, 0x62, 0x8f, 0x24, 0xda, + 0x6c, 0x09, 0x3f, 0xa9, 0x28, 0x7a, 0xb5, 0x7f, 0x1f, 0x0f, 0xc9, 0x35, + // RTCP index + 0x80, 0x00, 0x00, 0x03, + // HMAC + 0xe9, 0x3b, 0xc0, 0x5c, 0x0c, 0x06, 0x9f, 0xab, 0xc0, 0xde, +}; + +static const char *aes128_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; + +static const uint8_t rtp_aes128_32[] = { + // RTP header + 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0x62, 0x69, 0x76, 0xca, 0xc5, + // HMAC + 0xa1, 0xac, 0x1b, 0xb4, +}; + +static const uint8_t rtcp_aes128_32[] = { + // RTCP header + 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0x35, 0xe9, 0xb5, 0xff, 0x0d, 0xd1, 0xde, 0x70, 0x74, 0x10, 0xaa, 0x1b, + 0xb2, 0x8d, 0xf0, 0x20, 0x02, 0x99, 0x6b, 0x1b, 0x0b, 0xd0, 0x47, 0x34, + // RTCP index + 0x80, 0x00, 0x00, 0x04, + // HMAC + 0x5b, 0xd2, 0xa9, 0x9d, +}; + +static const char *aes128_80_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; + +static const uint8_t rtp_aes128_80_32[] = { + // RTP header + 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0x62, 0x69, 0x76, 0xca, 0xc5, + // HMAC + 0xa1, 0xac, 0x1b, 0xb4, +}; + +static const uint8_t rtcp_aes128_80_32[] = { + // RTCP header + 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, + // encrypted payload + 0xd6, 0xae, 0xc1, 0x58, 0x63, 0x70, 0xc9, 0x88, 0x66, 0x26, 0x1c, 0x53, + 0xff, 0x5d, 0x5d, 0x2b, 0x0f, 0x8c, 0x72, 0x3e, 0xc9, 0x1d, 0x43, 0xf9, + // RTCP index + 0x80, 0x00, 0x00, 0x05, + // HMAC + 0x09, 0x16, 0xb4, 0x27, 0x9a, 0xe9, 0x92, 0x26, 0x4e, 0x10, +}; + +static void print_data(const uint8_t *buf, int len) +{ + int i; + for (i = 0; i < len; i++) + printf("%02x", buf[i]); + printf("\n"); +} + +static int test_decrypt(struct SRTPContext *srtp, const uint8_t *in, int len, + uint8_t *out) +{ + memcpy(out, in, len); + if (!ff_srtp_decrypt(srtp, out, &len)) { + print_data(out, len); + return len; + } else + return -1; +} + +static void test_encrypt(const uint8_t *data, int in_len, const char *suite, + const char *key) +{ + struct SRTPContext enc = { 0 }, dec = { 0 }; + int len; + char buf[RTP_MAX_PACKET_LENGTH]; + ff_srtp_set_crypto(&enc, suite, key); + ff_srtp_set_crypto(&dec, suite, key); + len = ff_srtp_encrypt(&enc, data, in_len, buf, sizeof(buf)); + if (!ff_srtp_decrypt(&dec, buf, &len)) { + if (len == in_len && !memcmp(buf, data, len)) + printf("Decrypted content matches input\n"); + else + printf("Decrypted content doesn't match input\n"); + } else { + printf("Decryption failed\n"); + } + ff_srtp_free(&enc); + ff_srtp_free(&dec); +} + +int main(void) +{ + static const char *aes128_80_suite = "AES_CM_128_HMAC_SHA1_80"; + static const char *aes128_32_suite = "AES_CM_128_HMAC_SHA1_32"; + static const char *aes128_80_32_suite = "SRTP_AES128_CM_HMAC_SHA1_32"; + static const char *test_key = "abcdefghijklmnopqrstuvwxyz1234567890ABCD"; + uint8_t buf[RTP_MAX_PACKET_LENGTH]; + struct SRTPContext srtp = { 0 }; + int len; + ff_srtp_set_crypto(&srtp, aes128_80_suite, aes128_80_key); + len = test_decrypt(&srtp, rtp_aes128_80, sizeof(rtp_aes128_80), buf); + test_encrypt(buf, len, aes128_80_suite, test_key); + test_encrypt(buf, len, aes128_32_suite, test_key); + test_encrypt(buf, len, aes128_80_32_suite, test_key); + test_decrypt(&srtp, rtcp_aes128_80, sizeof(rtcp_aes128_80), buf); + test_encrypt(buf, len, aes128_80_suite, test_key); + test_encrypt(buf, len, aes128_32_suite, test_key); + test_encrypt(buf, len, aes128_80_32_suite, test_key); + ff_srtp_free(&srtp); + + memset(&srtp, 0, sizeof(srtp)); // Clear the context + ff_srtp_set_crypto(&srtp, aes128_32_suite, aes128_32_key); + test_decrypt(&srtp, rtp_aes128_32, sizeof(rtp_aes128_32), buf); + test_decrypt(&srtp, rtcp_aes128_32, sizeof(rtcp_aes128_32), buf); + ff_srtp_free(&srtp); + + memset(&srtp, 0, sizeof(srtp)); // Clear the context + ff_srtp_set_crypto(&srtp, aes128_80_32_suite, aes128_80_32_key); + test_decrypt(&srtp, rtp_aes128_80_32, sizeof(rtp_aes128_80_32), buf); + test_decrypt(&srtp, rtcp_aes128_80_32, sizeof(rtcp_aes128_80_32), buf); + ff_srtp_free(&srtp); + return 0; +} diff --git a/libavformat/srtp.c b/libavformat/srtp.c index 6659bfc5c9..e054fcc9b6 100644 --- a/libavformat/srtp.c +++ b/libavformat/srtp.c @@ -323,150 +323,3 @@ int ff_srtp_encrypt(struct SRTPContext *s, const uint8_t *in, int len, len += hmac_size; return buf + len - out; } - -#ifdef TEST -#include <stdio.h> - -static const char *aes128_80_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; - -static const uint8_t rtp_aes128_80[] = { - // RTP header - 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0x62, 0x69, 0x76, 0xca, 0xc5, - // HMAC - 0xa1, 0xac, 0x1b, 0xb4, 0xa0, 0x1c, 0xd5, 0x49, 0x28, 0x99, -}; - -static const uint8_t rtcp_aes128_80[] = { - // RTCP header - 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0x8a, 0xac, 0xdc, 0xa5, 0x4c, 0xf6, 0x78, 0xa6, 0x62, 0x8f, 0x24, 0xda, - 0x6c, 0x09, 0x3f, 0xa9, 0x28, 0x7a, 0xb5, 0x7f, 0x1f, 0x0f, 0xc9, 0x35, - // RTCP index - 0x80, 0x00, 0x00, 0x03, - // HMAC - 0xe9, 0x3b, 0xc0, 0x5c, 0x0c, 0x06, 0x9f, 0xab, 0xc0, 0xde, -}; - -static const char *aes128_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; - -static const uint8_t rtp_aes128_32[] = { - // RTP header - 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0x62, 0x69, 0x76, 0xca, 0xc5, - // HMAC - 0xa1, 0xac, 0x1b, 0xb4, -}; - -static const uint8_t rtcp_aes128_32[] = { - // RTCP header - 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0x35, 0xe9, 0xb5, 0xff, 0x0d, 0xd1, 0xde, 0x70, 0x74, 0x10, 0xaa, 0x1b, - 0xb2, 0x8d, 0xf0, 0x20, 0x02, 0x99, 0x6b, 0x1b, 0x0b, 0xd0, 0x47, 0x34, - // RTCP index - 0x80, 0x00, 0x00, 0x04, - // HMAC - 0x5b, 0xd2, 0xa9, 0x9d, -}; - -static const char *aes128_80_32_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn"; - -static const uint8_t rtp_aes128_80_32[] = { - // RTP header - 0x80, 0xe0, 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0x62, 0x69, 0x76, 0xca, 0xc5, - // HMAC - 0xa1, 0xac, 0x1b, 0xb4, -}; - -static const uint8_t rtcp_aes128_80_32[] = { - // RTCP header - 0x81, 0xc9, 0x00, 0x07, 0x12, 0x34, 0x56, 0x78, - // encrypted payload - 0xd6, 0xae, 0xc1, 0x58, 0x63, 0x70, 0xc9, 0x88, 0x66, 0x26, 0x1c, 0x53, - 0xff, 0x5d, 0x5d, 0x2b, 0x0f, 0x8c, 0x72, 0x3e, 0xc9, 0x1d, 0x43, 0xf9, - // RTCP index - 0x80, 0x00, 0x00, 0x05, - // HMAC - 0x09, 0x16, 0xb4, 0x27, 0x9a, 0xe9, 0x92, 0x26, 0x4e, 0x10, -}; - -static void print_data(const uint8_t *buf, int len) -{ - int i; - for (i = 0; i < len; i++) - printf("%02x", buf[i]); - printf("\n"); -} - -static int test_decrypt(struct SRTPContext *srtp, const uint8_t *in, int len, - uint8_t *out) -{ - memcpy(out, in, len); - if (!ff_srtp_decrypt(srtp, out, &len)) { - print_data(out, len); - return len; - } else - return -1; -} - -static void test_encrypt(const uint8_t *data, int in_len, const char *suite, - const char *key) -{ - struct SRTPContext enc = { 0 }, dec = { 0 }; - int len; - char buf[RTP_MAX_PACKET_LENGTH]; - ff_srtp_set_crypto(&enc, suite, key); - ff_srtp_set_crypto(&dec, suite, key); - len = ff_srtp_encrypt(&enc, data, in_len, buf, sizeof(buf)); - if (!ff_srtp_decrypt(&dec, buf, &len)) { - if (len == in_len && !memcmp(buf, data, len)) - printf("Decrypted content matches input\n"); - else - printf("Decrypted content doesn't match input\n"); - } else { - printf("Decryption failed\n"); - } - ff_srtp_free(&enc); - ff_srtp_free(&dec); -} - -int main(void) -{ - static const char *aes128_80_suite = "AES_CM_128_HMAC_SHA1_80"; - static const char *aes128_32_suite = "AES_CM_128_HMAC_SHA1_32"; - static const char *aes128_80_32_suite = "SRTP_AES128_CM_HMAC_SHA1_32"; - static const char *test_key = "abcdefghijklmnopqrstuvwxyz1234567890ABCD"; - uint8_t buf[RTP_MAX_PACKET_LENGTH]; - struct SRTPContext srtp = { 0 }; - int len; - ff_srtp_set_crypto(&srtp, aes128_80_suite, aes128_80_key); - len = test_decrypt(&srtp, rtp_aes128_80, sizeof(rtp_aes128_80), buf); - test_encrypt(buf, len, aes128_80_suite, test_key); - test_encrypt(buf, len, aes128_32_suite, test_key); - test_encrypt(buf, len, aes128_80_32_suite, test_key); - test_decrypt(&srtp, rtcp_aes128_80, sizeof(rtcp_aes128_80), buf); - test_encrypt(buf, len, aes128_80_suite, test_key); - test_encrypt(buf, len, aes128_32_suite, test_key); - test_encrypt(buf, len, aes128_80_32_suite, test_key); - ff_srtp_free(&srtp); - - memset(&srtp, 0, sizeof(srtp)); // Clear the context - ff_srtp_set_crypto(&srtp, aes128_32_suite, aes128_32_key); - test_decrypt(&srtp, rtp_aes128_32, sizeof(rtp_aes128_32), buf); - test_decrypt(&srtp, rtcp_aes128_32, sizeof(rtcp_aes128_32), buf); - ff_srtp_free(&srtp); - - memset(&srtp, 0, sizeof(srtp)); // Clear the context - ff_srtp_set_crypto(&srtp, aes128_80_32_suite, aes128_80_32_key); - test_decrypt(&srtp, rtp_aes128_80_32, sizeof(rtp_aes128_80_32), buf); - test_decrypt(&srtp, rtcp_aes128_80_32, sizeof(rtcp_aes128_80_32), buf); - ff_srtp_free(&srtp); - return 0; -} -#endif /* TEST */ diff --git a/libavutil/adler32-test.c b/libavutil/adler32-test.c new file mode 100644 index 0000000000..ab109f9175 --- /dev/null +++ b/libavutil/adler32-test.c @@ -0,0 +1,51 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <string.h> + +#include "log.h" +#include "timer.h" +#include "adler32.h" + +#define LEN 7001 + +static volatile int checksum; + +int main(int argc, char **argv) +{ + int i; + char data[LEN]; + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < LEN; i++) + data[i] = ((i * i) >> 3) + 123 * i; + + if (argc > 1 && !strcmp(argv[1], "-t")) { + for (i = 0; i < 1000; i++) { + START_TIMER; + checksum = av_adler32_update(1, data, LEN); + STOP_TIMER("adler"); + } + } else { + checksum = av_adler32_update(1, data, LEN); + } + + av_log(NULL, AV_LOG_DEBUG, "%X (expected 50E6E508)\n", checksum); + return checksum == 0x50e6e508 ? 0 : 1; +} diff --git a/libavutil/adler32.c b/libavutil/adler32.c index 8a8065c4a3..8dfe2ce451 100644 --- a/libavutil/adler32.c +++ b/libavutil/adler32.c @@ -63,36 +63,3 @@ unsigned long av_adler32_update(unsigned long adler, const uint8_t * buf, } return (s2 << 16) | s1; } - -#ifdef TEST -#include <string.h> -#include "log.h" -#include "timer.h" -#define LEN 7001 - -static volatile int checksum; - -int main(int argc, char **argv) -{ - int i; - char data[LEN]; - - av_log_set_level(AV_LOG_DEBUG); - - for (i = 0; i < LEN; i++) - data[i] = ((i * i) >> 3) + 123 * i; - - if (argc > 1 && !strcmp(argv[1], "-t")) { - for (i = 0; i < 1000; i++) { - START_TIMER; - checksum = av_adler32_update(1, data, LEN); - STOP_TIMER("adler"); - } - } else { - checksum = av_adler32_update(1, data, LEN); - } - - av_log(NULL, AV_LOG_DEBUG, "%X (expected 50E6E508)\n", checksum); - return checksum == 0x50e6e508 ? 0 : 1; -} -#endif diff --git a/libavutil/aes-test.c b/libavutil/aes-test.c new file mode 100644 index 0000000000..92404e4cc2 --- /dev/null +++ b/libavutil/aes-test.c @@ -0,0 +1,92 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "aes.c" + +#include <string.h> + +#include "lfg.h" +#include "log.h" + +int main(int argc, char **argv) +{ + int i, j; + AVAES b; + uint8_t rkey[2][16] = { + { 0 }, + { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, + 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 } + }; + uint8_t pt[16], rpt[2][16] = { + { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, + 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 }, + { 0 } + }; + uint8_t rct[2][16] = { + { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, + 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf }, + { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, + 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 } + }; + uint8_t temp[16]; + int err = 0; + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < 2; i++) { + av_aes_init(&b, rkey[i], 128, 1); + av_aes_crypt(&b, temp, rct[i], 1, NULL, 1); + for (j = 0; j < 16; j++) { + if (rpt[i][j] != temp[j]) { + av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", + j, rpt[i][j], temp[j]); + err = 1; + } + } + } + + if (argc > 1 && !strcmp(argv[1], "-t")) { + AVAES ae, ad; + AVLFG prng; + + av_aes_init(&ae, "PI=3.141592654..", 128, 0); + av_aes_init(&ad, "PI=3.141592654..", 128, 1); + av_lfg_init(&prng, 1); + + for (i = 0; i < 10000; i++) { + for (j = 0; j < 16; j++) + pt[j] = av_lfg_get(&prng); + { + START_TIMER; + av_aes_crypt(&ae, temp, pt, 1, NULL, 0); + if (!(i & (i - 1))) + av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", + temp[0], temp[5], temp[10], temp[15]); + av_aes_crypt(&ad, temp, temp, 1, NULL, 1); + STOP_TIMER("aes"); + } + for (j = 0; j < 16; j++) { + if (pt[j] != temp[j]) { + av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", + i, j, pt[j], temp[j]); + } + } + } + } + return err; +} diff --git a/libavutil/aes.c b/libavutil/aes.c index d534bc78a7..e93fb0565c 100644 --- a/libavutil/aes.c +++ b/libavutil/aes.c @@ -262,78 +262,3 @@ int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt) return 0; } - -#ifdef TEST -#include <string.h> - -#include "lfg.h" -#include "log.h" - -int main(int argc, char **argv) -{ - int i, j; - AVAES b; - uint8_t rkey[2][16] = { - { 0 }, - { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, - 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 } - }; - uint8_t pt[16], rpt[2][16] = { - { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, - 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 }, - { 0 } - }; - uint8_t rct[2][16] = { - { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, - 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf }, - { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, - 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 } - }; - uint8_t temp[16]; - int err = 0; - - av_log_set_level(AV_LOG_DEBUG); - - for (i = 0; i < 2; i++) { - av_aes_init(&b, rkey[i], 128, 1); - av_aes_crypt(&b, temp, rct[i], 1, NULL, 1); - for (j = 0; j < 16; j++) { - if (rpt[i][j] != temp[j]) { - av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", - j, rpt[i][j], temp[j]); - err = 1; - } - } - } - - if (argc > 1 && !strcmp(argv[1], "-t")) { - AVAES ae, ad; - AVLFG prng; - - av_aes_init(&ae, "PI=3.141592654..", 128, 0); - av_aes_init(&ad, "PI=3.141592654..", 128, 1); - av_lfg_init(&prng, 1); - - for (i = 0; i < 10000; i++) { - for (j = 0; j < 16; j++) - pt[j] = av_lfg_get(&prng); - { - START_TIMER; - av_aes_crypt(&ae, temp, pt, 1, NULL, 0); - if (!(i & (i - 1))) - av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", - temp[0], temp[5], temp[10], temp[15]); - av_aes_crypt(&ad, temp, temp, 1, NULL, 1); - STOP_TIMER("aes"); - } - for (j = 0; j < 16; j++) { - if (pt[j] != temp[j]) { - av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", - i, j, pt[j], temp[j]); - } - } - } - } - return err; -} -#endif diff --git a/libavutil/atomic-test.c b/libavutil/atomic-test.c new file mode 100644 index 0000000000..c0a89a461e --- /dev/null +++ b/libavutil/atomic-test.c @@ -0,0 +1,35 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <assert.h> + +#include "atomic.h" + +int main(void) +{ + volatile int val = 1; + int res; + + res = avpriv_atomic_int_add_and_fetch(&val, 1); + assert(res == 2); + avpriv_atomic_int_set(&val, 3); + res = avpriv_atomic_int_get(&val); + assert(res == 3); + + return 0; +} diff --git a/libavutil/atomic.c b/libavutil/atomic.c index 83740396d4..e175a9f8ed 100644 --- a/libavutil/atomic.c +++ b/libavutil/atomic.c @@ -107,21 +107,3 @@ void *avpriv_atomic_ptr_cas(void * volatile *ptr, void *oldval, void *newval) #endif /* HAVE_PTHREADS */ #endif /* !HAVE_ATOMICS_NATIVE */ - -#ifdef TEST -#include <assert.h> - -int main(void) -{ - volatile int val = 1; - int res; - - res = avpriv_atomic_int_add_and_fetch(&val, 1); - assert(res == 2); - avpriv_atomic_int_set(&val, 3); - res = avpriv_atomic_int_get(&val); - assert(res == 3); - - return 0; -} -#endif diff --git a/libavutil/avstring-test.c b/libavutil/avstring-test.c new file mode 100644 index 0000000000..7732c22f0e --- /dev/null +++ b/libavutil/avstring-test.c @@ -0,0 +1,69 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "common.h" +#include "mem.h" +#include "avstring.h" + +int main(void) +{ + int i; + const char *strings[] = { + "''", + "", + ":", + "\\", + "'", + " '' :", + " '' '' :", + "foo '' :", + "'foo'", + "foo ", + " ' foo ' ", + "foo\\", + "foo': blah:blah", + "foo\\: blah:blah", + "foo\'", + "'foo : ' :blahblah", + "\\ :blah", + " foo", + " foo ", + " foo \\ ", + "foo ':blah", + " foo bar : blahblah", + "\\f\\o\\o", + "'foo : \\ \\ ' : blahblah", + "'\\fo\\o:': blahblah", + "\\'fo\\o\\:': foo ' :blahblah" + }; + + printf("Testing av_get_token()\n"); + for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) { + const char *p = strings[i]; + char *q; + printf("|%s|", p); + q = av_get_token(&p, ":"); + printf(" -> |%s|", q); + printf(" + |%s|\n", p); + av_free(q); + } + + return 0; +} diff --git a/libavutil/avstring.c b/libavutil/avstring.c index 5a443ab11b..bc7d0bcb98 100644 --- a/libavutil/avstring.c +++ b/libavutil/avstring.c @@ -229,55 +229,3 @@ int av_match_name(const char *name, const char *names) } return !av_strcasecmp(name, names); } - - - -#ifdef TEST - -int main(void) -{ - int i; - const char *strings[] = { - "''", - "", - ":", - "\\", - "'", - " '' :", - " '' '' :", - "foo '' :", - "'foo'", - "foo ", - " ' foo ' ", - "foo\\", - "foo': blah:blah", - "foo\\: blah:blah", - "foo\'", - "'foo : ' :blahblah", - "\\ :blah", - " foo", - " foo ", - " foo \\ ", - "foo ':blah", - " foo bar : blahblah", - "\\f\\o\\o", - "'foo : \\ \\ ' : blahblah", - "'\\fo\\o:': blahblah", - "\\'fo\\o\\:': foo ' :blahblah" - }; - - printf("Testing av_get_token()\n"); - for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) { - const char *p = strings[i]; - char *q; - printf("|%s|", p); - q = av_get_token(&p, ":"); - printf(" -> |%s|", q); - printf(" + |%s|\n", p); - av_free(q); - } - - return 0; -} - -#endif /* TEST */ diff --git a/libavutil/base64-test.c b/libavutil/base64-test.c new file mode 100644 index 0000000000..3ce0518a84 --- /dev/null +++ b/libavutil/base64-test.c @@ -0,0 +1,84 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "common.h" +#include "base64.h" + +#define MAX_DATA_SIZE 1024 +#define MAX_ENCODED_SIZE 2048 + +static int test_encode_decode(const uint8_t *data, unsigned int data_size, + const char *encoded_ref) +{ + char encoded[MAX_ENCODED_SIZE]; + uint8_t data2[MAX_DATA_SIZE]; + int data2_size, max_data2_size = MAX_DATA_SIZE; + + if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) { + printf("Failed: cannot encode the input data\n"); + return 1; + } + if (encoded_ref && strcmp(encoded, encoded_ref)) { + printf("Failed: encoded string differs from reference\n" + "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref); + return 1; + } + + if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) { + printf("Failed: cannot decode the encoded string\n" + "Encoded:\n%s\n", encoded); + return 1; + } + if (memcmp(data2, data, data_size)) { + printf("Failed: encoded/decoded data differs from original data\n"); + return 1; + } + + printf("Passed!\n"); + return 0; +} + +int main(void) +{ + int i, error_count = 0; + struct test { + const uint8_t *data; + const char *encoded_ref; + } tests[] = { + { "", ""}, + { "1", "MQ=="}, + { "22", "MjI="}, + { "333", "MzMz"}, + { "4444", "NDQ0NA=="}, + { "55555", "NTU1NTU="}, + { "666666", "NjY2NjY2"}, + { "abc:def", "YWJjOmRlZg=="}, + }; + + printf("Encoding/decoding tests\n"); + for (i = 0; i < FF_ARRAY_ELEMS(tests); i++) + error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref); + + if (error_count) + printf("Error Count: %d.\n", error_count); + + return !!error_count; +} diff --git a/libavutil/base64.c b/libavutil/base64.c index 725b03504d..29677a646b 100644 --- a/libavutil/base64.c +++ b/libavutil/base64.c @@ -98,68 +98,3 @@ char *av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size) return ret; } - -#ifdef TEST - -#define MAX_DATA_SIZE 1024 -#define MAX_ENCODED_SIZE 2048 - -static int test_encode_decode(const uint8_t *data, unsigned int data_size, - const char *encoded_ref) -{ - char encoded[MAX_ENCODED_SIZE]; - uint8_t data2[MAX_DATA_SIZE]; - int data2_size, max_data2_size = MAX_DATA_SIZE; - - if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) { - printf("Failed: cannot encode the input data\n"); - return 1; - } - if (encoded_ref && strcmp(encoded, encoded_ref)) { - printf("Failed: encoded string differs from reference\n" - "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref); - return 1; - } - - if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) { - printf("Failed: cannot decode the encoded string\n" - "Encoded:\n%s\n", encoded); - return 1; - } - if (memcmp(data2, data, data_size)) { - printf("Failed: encoded/decoded data differs from original data\n"); - return 1; - } - - printf("Passed!\n"); - return 0; -} - -int main(void) -{ - int i, error_count = 0; - struct test { - const uint8_t *data; - const char *encoded_ref; - } tests[] = { - { "", ""}, - { "1", "MQ=="}, - { "22", "MjI="}, - { "333", "MzMz"}, - { "4444", "NDQ0NA=="}, - { "55555", "NTU1NTU="}, - { "666666", "NjY2NjY2"}, - { "abc:def", "YWJjOmRlZg=="}, - }; - - printf("Encoding/decoding tests\n"); - for (i = 0; i < FF_ARRAY_ELEMS(tests); i++) - error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref); - - if (error_count) - printf("Error Count: %d.\n", error_count); - - return !!error_count; -} - -#endif diff --git a/libavutil/blowfish-test.c b/libavutil/blowfish-test.c new file mode 100644 index 0000000000..4281ca29bd --- /dev/null +++ b/libavutil/blowfish-test.c @@ -0,0 +1,190 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "blowfish.h" + +#define NUM_VARIABLE_KEY_TESTS 34 + +/* plaintext bytes -- left halves */ +static const uint32_t plaintext_l[NUM_VARIABLE_KEY_TESTS] = { + 0x00000000, 0xFFFFFFFF, 0x10000000, 0x11111111, 0x11111111, + 0x01234567, 0x00000000, 0x01234567, 0x01A1D6D0, 0x5CD54CA8, + 0x0248D438, 0x51454B58, 0x42FD4430, 0x059B5E08, 0x0756D8E0, + 0x762514B8, 0x3BDD1190, 0x26955F68, 0x164D5E40, 0x6B056E18, + 0x004BD6EF, 0x480D3900, 0x437540C8, 0x072D43A0, 0x02FE5577, + 0x1D9D5C50, 0x30553228, 0x01234567, 0x01234567, 0x01234567, + 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF +}; + +/* plaintext bytes -- right halves */ +static const uint32_t plaintext_r[NUM_VARIABLE_KEY_TESTS] = { + 0x00000000, 0xFFFFFFFF, 0x00000001, 0x11111111, 0x11111111, + 0x89ABCDEF, 0x00000000, 0x89ABCDEF, 0x39776742, 0x3DEF57DA, + 0x06F67172, 0x2DDF440A, 0x59577FA2, 0x51CF143A, 0x774761D2, + 0x29BF486A, 0x49372802, 0x35AF609A, 0x4F275232, 0x759F5CCA, + 0x09176062, 0x6EE762F2, 0x698F3CFA, 0x77075292, 0x8117F12A, + 0x18F728C2, 0x6D6F295A, 0x89ABCDEF, 0x89ABCDEF, 0x89ABCDEF, + 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF +}; + +/* key bytes for variable key tests */ +static const uint8_t variable_key[NUM_VARIABLE_KEY_TESTS][8] = { + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, + { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, + { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }, + { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 }, + { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E }, + { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 }, + { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E }, + { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 }, + { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE }, + { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 }, + { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE }, + { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 }, + { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F }, + { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 }, + { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E }, + { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 }, + { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 }, + { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F }, + { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 }, + { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF }, + { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 }, + { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF }, + { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, + { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }, + { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, + { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, + { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 } +}; + +/* ciphertext bytes -- left halves */ +static const uint32_t ciphertext_l[NUM_VARIABLE_KEY_TESTS] = { + 0x4EF99745, 0x51866FD5, 0x7D856F9A, 0x2466DD87, 0x61F9C380, + 0x7D0CC630, 0x4EF99745, 0x0ACEAB0F, 0x59C68245, 0xB1B8CC0B, + 0x1730E577, 0xA25E7856, 0x353882B1, 0x48F4D088, 0x432193B7, + 0x13F04154, 0x2EEDDA93, 0xD887E039, 0x5F99D04F, 0x4A057A3B, + 0x452031C1, 0x7555AE39, 0x53C55F9C, 0x7A8E7BFA, 0xCF9C5D7A, + 0xD1ABB290, 0x55CB3774, 0xFA34EC48, 0xA7907951, 0xC39E072D, + 0x014933E0, 0xF21E9A77, 0x24594688, 0x6B5C5A9C +}; + +/* ciphertext bytes -- right halves */ +static const uint32_t ciphertext_r[NUM_VARIABLE_KEY_TESTS] = { + 0x6198DD78, 0xB85ECB8A, 0x613063F2, 0x8B963C9D, 0x2281B096, + 0xAFDA1EC7, 0x6198DD78, 0xC6A0A28D, 0xEB05282B, 0x250F09A0, + 0x8BEA1DA4, 0xCF2651EB, 0x09CE8F1A, 0x4C379918, 0x8951FC98, + 0xD69D1AE5, 0xFFD39C79, 0x3C2DA6E3, 0x5B163969, 0x24D3977B, + 0xE4FADA8E, 0xF59B87BD, 0xB49FC019, 0x937E89A3, 0x4986ADB5, + 0x658BC778, 0xD13EF201, 0x47B268B2, 0x08EA3CAE, 0x9FAC631D, + 0xCDAFF6E4, 0xB71C49BC, 0x5754369A, 0x5D9E0A5A +}; + +/* plaintext bytes */ +static const uint8_t plaintext[8] = "BLOWFISH"; + +static const uint8_t plaintext2[16] = "BLOWFISHBLOWFISH"; + +/* ciphertext bytes */ +static const uint8_t ciphertext[8] = { + 0x32, 0x4E, 0xD0, 0xFE, 0xF4, 0x13, 0xA2, 0x03 +}; + +static const uint8_t ciphertext2[16] = { + 0x53, 0x00, 0x40, 0x06, 0x63, 0xf2, 0x1d, 0x99, + 0x3b, 0x9b, 0x27, 0x64, 0x46, 0xfd, 0x20, 0xc1, +}; + +#define IV "blowfish" + +static void test_blowfish(AVBlowfish *ctx, uint8_t *dst, const uint8_t *src, + const uint8_t *ref, int len, uint8_t *iv, int dir, + const char *test) +{ + av_blowfish_crypt(ctx, dst, src, len, iv, dir); + if (memcmp(dst, ref, 8*len)) { + int i; + printf("%s failed\ngot ", test); + for (i = 0; i < 8*len; i++) + printf("%02x ", dst[i]); + printf("\nexpected "); + for (i = 0; i < 8*len; i++) + printf("%02x ", ref[i]); + printf("\n"); + exit(1); + } +} + +int main(void) +{ + AVBlowfish ctx; + uint32_t tmptext_l[NUM_VARIABLE_KEY_TESTS]; + uint32_t tmptext_r[NUM_VARIABLE_KEY_TESTS]; + uint8_t tmp[16], iv[8]; + int i; + + av_blowfish_init(&ctx, "abcdefghijklmnopqrstuvwxyz", 26); + + test_blowfish(&ctx, tmp, plaintext, ciphertext, 1, NULL, 0, "encryption"); + test_blowfish(&ctx, tmp, ciphertext, plaintext, 1, NULL, 1, "decryption"); + test_blowfish(&ctx, tmp, tmp, ciphertext, 1, NULL, 0, "Inplace encryption"); + test_blowfish(&ctx, tmp, tmp, plaintext, 1, NULL, 1, "Inplace decryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, plaintext2, ciphertext2, 2, iv, 0, "CBC encryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, ciphertext2, plaintext2, 2, iv, 1, "CBC decryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, tmp, ciphertext2, 2, iv, 0, "Inplace CBC encryption"); + memcpy(iv, IV, 8); + test_blowfish(&ctx, tmp, tmp, plaintext2, 2, iv, 1, "Inplace CBC decryption"); + + memcpy(tmptext_l, plaintext_l, sizeof(*plaintext_l) * NUM_VARIABLE_KEY_TESTS); + memcpy(tmptext_r, plaintext_r, sizeof(*plaintext_r) * NUM_VARIABLE_KEY_TESTS); + + for (i = 0; i < NUM_VARIABLE_KEY_TESTS; i++) { + av_blowfish_init(&ctx, variable_key[i], 8); + + av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 0); + if (tmptext_l[i] != ciphertext_l[i] || tmptext_r[i] != ciphertext_r[i]) { + printf("Test encryption failed.\n"); + return 1; + } + + av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 1); + if (tmptext_l[i] != plaintext_l[i] || tmptext_r[i] != plaintext_r[i]) { + printf("Test decryption failed.\n"); + return 1; + } + } + printf("Test encryption/decryption success.\n"); + + return 0; +} + diff --git a/libavutil/blowfish.c b/libavutil/blowfish.c index a392459397..87315766d7 100644 --- a/libavutil/blowfish.c +++ b/libavutil/blowfish.c @@ -439,173 +439,3 @@ void av_blowfish_crypt(AVBlowfish *ctx, uint8_t *dst, const uint8_t *src, } } } - -#ifdef TEST -#include <stdio.h> - -#define NUM_VARIABLE_KEY_TESTS 34 - -/* plaintext bytes -- left halves */ -static const uint32_t plaintext_l[NUM_VARIABLE_KEY_TESTS] = { - 0x00000000, 0xFFFFFFFF, 0x10000000, 0x11111111, 0x11111111, - 0x01234567, 0x00000000, 0x01234567, 0x01A1D6D0, 0x5CD54CA8, - 0x0248D438, 0x51454B58, 0x42FD4430, 0x059B5E08, 0x0756D8E0, - 0x762514B8, 0x3BDD1190, 0x26955F68, 0x164D5E40, 0x6B056E18, - 0x004BD6EF, 0x480D3900, 0x437540C8, 0x072D43A0, 0x02FE5577, - 0x1D9D5C50, 0x30553228, 0x01234567, 0x01234567, 0x01234567, - 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF -}; - -/* plaintext bytes -- right halves */ -static const uint32_t plaintext_r[NUM_VARIABLE_KEY_TESTS] = { - 0x00000000, 0xFFFFFFFF, 0x00000001, 0x11111111, 0x11111111, - 0x89ABCDEF, 0x00000000, 0x89ABCDEF, 0x39776742, 0x3DEF57DA, - 0x06F67172, 0x2DDF440A, 0x59577FA2, 0x51CF143A, 0x774761D2, - 0x29BF486A, 0x49372802, 0x35AF609A, 0x4F275232, 0x759F5CCA, - 0x09176062, 0x6EE762F2, 0x698F3CFA, 0x77075292, 0x8117F12A, - 0x18F728C2, 0x6D6F295A, 0x89ABCDEF, 0x89ABCDEF, 0x89ABCDEF, - 0xFFFFFFFF, 0x00000000, 0x00000000, 0xFFFFFFFF -}; - -/* key bytes for variable key tests */ -static const uint8_t variable_key[NUM_VARIABLE_KEY_TESTS][8] = { - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, - { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, - { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, - { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }, - { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 }, - { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E }, - { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 }, - { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E }, - { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 }, - { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE }, - { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 }, - { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE }, - { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 }, - { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F }, - { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 }, - { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E }, - { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 }, - { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 }, - { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F }, - { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 }, - { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF }, - { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 }, - { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF }, - { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 }, - { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }, - { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, - { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, - { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 } -}; - -/* ciphertext bytes -- left halves */ -static const uint32_t ciphertext_l[NUM_VARIABLE_KEY_TESTS] = { - 0x4EF99745, 0x51866FD5, 0x7D856F9A, 0x2466DD87, 0x61F9C380, - 0x7D0CC630, 0x4EF99745, 0x0ACEAB0F, 0x59C68245, 0xB1B8CC0B, - 0x1730E577, 0xA25E7856, 0x353882B1, 0x48F4D088, 0x432193B7, - 0x13F04154, 0x2EEDDA93, 0xD887E039, 0x5F99D04F, 0x4A057A3B, - 0x452031C1, 0x7555AE39, 0x53C55F9C, 0x7A8E7BFA, 0xCF9C5D7A, - 0xD1ABB290, 0x55CB3774, 0xFA34EC48, 0xA7907951, 0xC39E072D, - 0x014933E0, 0xF21E9A77, 0x24594688, 0x6B5C5A9C -}; - -/* ciphertext bytes -- right halves */ -static const uint32_t ciphertext_r[NUM_VARIABLE_KEY_TESTS] = { - 0x6198DD78, 0xB85ECB8A, 0x613063F2, 0x8B963C9D, 0x2281B096, - 0xAFDA1EC7, 0x6198DD78, 0xC6A0A28D, 0xEB05282B, 0x250F09A0, - 0x8BEA1DA4, 0xCF2651EB, 0x09CE8F1A, 0x4C379918, 0x8951FC98, - 0xD69D1AE5, 0xFFD39C79, 0x3C2DA6E3, 0x5B163969, 0x24D3977B, - 0xE4FADA8E, 0xF59B87BD, 0xB49FC019, 0x937E89A3, 0x4986ADB5, - 0x658BC778, 0xD13EF201, 0x47B268B2, 0x08EA3CAE, 0x9FAC631D, - 0xCDAFF6E4, 0xB71C49BC, 0x5754369A, 0x5D9E0A5A -}; - -/* plaintext bytes */ -static const uint8_t plaintext[8] = "BLOWFISH"; - -static const uint8_t plaintext2[16] = "BLOWFISHBLOWFISH"; - -/* ciphertext bytes */ -static const uint8_t ciphertext[8] = { - 0x32, 0x4E, 0xD0, 0xFE, 0xF4, 0x13, 0xA2, 0x03 -}; - -static const uint8_t ciphertext2[16] = { - 0x53, 0x00, 0x40, 0x06, 0x63, 0xf2, 0x1d, 0x99, - 0x3b, 0x9b, 0x27, 0x64, 0x46, 0xfd, 0x20, 0xc1, -}; - -#define IV "blowfish" - -static void test_blowfish(AVBlowfish *ctx, uint8_t *dst, const uint8_t *src, - const uint8_t *ref, int len, uint8_t *iv, int dir, - const char *test) -{ - av_blowfish_crypt(ctx, dst, src, len, iv, dir); - if (memcmp(dst, ref, 8*len)) { - int i; - printf("%s failed\ngot ", test); - for (i = 0; i < 8*len; i++) - printf("%02x ", dst[i]); - printf("\nexpected "); - for (i = 0; i < 8*len; i++) - printf("%02x ", ref[i]); - printf("\n"); - exit(1); - } -} - -int main(void) -{ - AVBlowfish ctx; - uint32_t tmptext_l[NUM_VARIABLE_KEY_TESTS]; - uint32_t tmptext_r[NUM_VARIABLE_KEY_TESTS]; - uint8_t tmp[16], iv[8]; - int i; - - av_blowfish_init(&ctx, "abcdefghijklmnopqrstuvwxyz", 26); - - test_blowfish(&ctx, tmp, plaintext, ciphertext, 1, NULL, 0, "encryption"); - test_blowfish(&ctx, tmp, ciphertext, plaintext, 1, NULL, 1, "decryption"); - test_blowfish(&ctx, tmp, tmp, ciphertext, 1, NULL, 0, "Inplace encryption"); - test_blowfish(&ctx, tmp, tmp, plaintext, 1, NULL, 1, "Inplace decryption"); - memcpy(iv, IV, 8); - test_blowfish(&ctx, tmp, plaintext2, ciphertext2, 2, iv, 0, "CBC encryption"); - memcpy(iv, IV, 8); - test_blowfish(&ctx, tmp, ciphertext2, plaintext2, 2, iv, 1, "CBC decryption"); - memcpy(iv, IV, 8); - test_blowfish(&ctx, tmp, tmp, ciphertext2, 2, iv, 0, "Inplace CBC encryption"); - memcpy(iv, IV, 8); - test_blowfish(&ctx, tmp, tmp, plaintext2, 2, iv, 1, "Inplace CBC decryption"); - - memcpy(tmptext_l, plaintext_l, sizeof(*plaintext_l) * NUM_VARIABLE_KEY_TESTS); - memcpy(tmptext_r, plaintext_r, sizeof(*plaintext_r) * NUM_VARIABLE_KEY_TESTS); - - for (i = 0; i < NUM_VARIABLE_KEY_TESTS; i++) { - av_blowfish_init(&ctx, variable_key[i], 8); - - av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 0); - if (tmptext_l[i] != ciphertext_l[i] || tmptext_r[i] != ciphertext_r[i]) { - printf("Test encryption failed.\n"); - return 1; - } - - av_blowfish_crypt_ecb(&ctx, &tmptext_l[i], &tmptext_r[i], 1); - if (tmptext_l[i] != plaintext_l[i] || tmptext_r[i] != plaintext_r[i]) { - printf("Test decryption failed.\n"); - return 1; - } - } - printf("Test encryption/decryption success.\n"); - - return 0; -} - -#endif diff --git a/libavutil/cpu-test.c b/libavutil/cpu-test.c new file mode 100644 index 0000000000..a2c3b38c0d --- /dev/null +++ b/libavutil/cpu-test.c @@ -0,0 +1,136 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#if HAVE_UNISTD_H +#include <unistd.h> +#elif !HAVE_GETOPT +#include "compat/getopt.c" +#endif + +#include <stdint.h> +#include <stdio.h> + +#include "avstring.h" +#include "common.h" +#include "cpu.h" + +static const struct { + int flag; + const char *name; +} cpu_flag_tab[] = { +#if ARCH_AARCH64 + { AV_CPU_FLAG_ARMV8, "armv8" }, + { AV_CPU_FLAG_NEON, "neon" }, + { AV_CPU_FLAG_VFP, "vfp" }, +#elif ARCH_ARM + { AV_CPU_FLAG_ARMV5TE, "armv5te" }, + { AV_CPU_FLAG_ARMV6, "armv6" }, + { AV_CPU_FLAG_ARMV6T2, "armv6t2" }, + { AV_CPU_FLAG_VFP, "vfp" }, + { AV_CPU_FLAG_VFP_VM, "vfp_vm" }, + { AV_CPU_FLAG_VFPV3, "vfpv3" }, + { AV_CPU_FLAG_NEON, "neon" }, +#elif ARCH_PPC + { AV_CPU_FLAG_ALTIVEC, "altivec" }, +#elif ARCH_X86 + { AV_CPU_FLAG_MMX, "mmx" }, + { AV_CPU_FLAG_MMXEXT, "mmxext" }, + { AV_CPU_FLAG_SSE, "sse" }, + { AV_CPU_FLAG_SSE2, "sse2" }, + { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" }, + { AV_CPU_FLAG_SSE3, "sse3" }, + { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" }, + { AV_CPU_FLAG_SSSE3, "ssse3" }, + { AV_CPU_FLAG_ATOM, "atom" }, + { AV_CPU_FLAG_SSE4, "sse4.1" }, + { AV_CPU_FLAG_SSE42, "sse4.2" }, + { AV_CPU_FLAG_AVX, "avx" }, + { AV_CPU_FLAG_AVXSLOW, "avxslow" }, + { AV_CPU_FLAG_XOP, "xop" }, + { AV_CPU_FLAG_FMA3, "fma3" }, + { AV_CPU_FLAG_FMA4, "fma4" }, + { AV_CPU_FLAG_3DNOW, "3dnow" }, + { AV_CPU_FLAG_3DNOWEXT, "3dnowext" }, + { AV_CPU_FLAG_CMOV, "cmov" }, + { AV_CPU_FLAG_AVX2, "avx2" }, + { AV_CPU_FLAG_BMI1, "bmi1" }, + { AV_CPU_FLAG_BMI2, "bmi2" }, +#endif + { 0 } +}; + +static void print_cpu_flags(int cpu_flags, const char *type) +{ + int i; + + fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags); + fprintf(stderr, "cpu_flags_str(%s) =", type); + for (i = 0; cpu_flag_tab[i].flag; i++) + if (cpu_flags & cpu_flag_tab[i].flag) + fprintf(stderr, " %s", cpu_flag_tab[i].name); + fprintf(stderr, "\n"); +} + + +int main(int argc, char **argv) +{ + int cpu_flags_raw = av_get_cpu_flags(); + int cpu_flags_eff; + int cpu_count = av_cpu_count(); + char threads[5] = "auto"; + + if (cpu_flags_raw < 0) + return 1; + + for (;;) { + int c = getopt(argc, argv, "c:t:"); + if (c == -1) + break; + switch (c) { + case 'c': + { + int cpuflags = av_parse_cpu_flags(optarg); + if (cpuflags < 0) + return 2; + av_set_cpu_flags_mask(cpuflags); + break; + } + case 't': + { + int len = av_strlcpy(threads, optarg, sizeof(threads)); + if (len >= sizeof(threads)) { + fprintf(stderr, "Invalid thread count '%s'\n", optarg); + return 2; + } + } + } + } + + cpu_flags_eff = av_get_cpu_flags(); + + if (cpu_flags_eff < 0) + return 3; + + print_cpu_flags(cpu_flags_raw, "raw"); + print_cpu_flags(cpu_flags_eff, "effective"); + fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count); + + return 0; +} diff --git a/libavutil/cpu.c b/libavutil/cpu.c index 5f04461f6b..7d7390a756 100644 --- a/libavutil/cpu.c +++ b/libavutil/cpu.c @@ -178,122 +178,3 @@ int av_cpu_count(void) return nb_cpus; } - -#ifdef TEST - -#include <stdio.h> -#if HAVE_UNISTD_H -#include <unistd.h> -#endif - -#include "avstring.h" - -#if !HAVE_GETOPT -#include "compat/getopt.c" -#endif - -static const struct { - int flag; - const char *name; -} cpu_flag_tab[] = { -#if ARCH_AARCH64 - { AV_CPU_FLAG_ARMV8, "armv8" }, - { AV_CPU_FLAG_NEON, "neon" }, - { AV_CPU_FLAG_VFP, "vfp" }, -#elif ARCH_ARM - { AV_CPU_FLAG_ARMV5TE, "armv5te" }, - { AV_CPU_FLAG_ARMV6, "armv6" }, - { AV_CPU_FLAG_ARMV6T2, "armv6t2" }, - { AV_CPU_FLAG_VFP, "vfp" }, - { AV_CPU_FLAG_VFP_VM, "vfp_vm" }, - { AV_CPU_FLAG_VFPV3, "vfpv3" }, - { AV_CPU_FLAG_NEON, "neon" }, -#elif ARCH_PPC - { AV_CPU_FLAG_ALTIVEC, "altivec" }, -#elif ARCH_X86 - { AV_CPU_FLAG_MMX, "mmx" }, - { AV_CPU_FLAG_MMXEXT, "mmxext" }, - { AV_CPU_FLAG_SSE, "sse" }, - { AV_CPU_FLAG_SSE2, "sse2" }, - { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" }, - { AV_CPU_FLAG_SSE3, "sse3" }, - { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" }, - { AV_CPU_FLAG_SSSE3, "ssse3" }, - { AV_CPU_FLAG_ATOM, "atom" }, - { AV_CPU_FLAG_SSE4, "sse4.1" }, - { AV_CPU_FLAG_SSE42, "sse4.2" }, - { AV_CPU_FLAG_AVX, "avx" }, - { AV_CPU_FLAG_AVXSLOW, "avxslow" }, - { AV_CPU_FLAG_XOP, "xop" }, - { AV_CPU_FLAG_FMA3, "fma3" }, - { AV_CPU_FLAG_FMA4, "fma4" }, - { AV_CPU_FLAG_3DNOW, "3dnow" }, - { AV_CPU_FLAG_3DNOWEXT, "3dnowext" }, - { AV_CPU_FLAG_CMOV, "cmov" }, - { AV_CPU_FLAG_AVX2, "avx2" }, - { AV_CPU_FLAG_BMI1, "bmi1" }, - { AV_CPU_FLAG_BMI2, "bmi2" }, -#endif - { 0 } -}; - -static void print_cpu_flags(int cpu_flags, const char *type) -{ - int i; - - fprintf(stderr, "cpu_flags(%s) = 0x%08X\n", type, cpu_flags); - fprintf(stderr, "cpu_flags_str(%s) =", type); - for (i = 0; cpu_flag_tab[i].flag; i++) - if (cpu_flags & cpu_flag_tab[i].flag) - fprintf(stderr, " %s", cpu_flag_tab[i].name); - fprintf(stderr, "\n"); -} - - -int main(int argc, char **argv) -{ - int cpu_flags_raw = av_get_cpu_flags(); - int cpu_flags_eff; - int cpu_count = av_cpu_count(); - char threads[5] = "auto"; - - if (cpu_flags_raw < 0) - return 1; - - for (;;) { - int c = getopt(argc, argv, "c:t:"); - if (c == -1) - break; - switch (c) { - case 'c': - { - int cpuflags = av_parse_cpu_flags(optarg); - if (cpuflags < 0) - return 2; - av_set_cpu_flags_mask(cpuflags); - break; - } - case 't': - { - int len = av_strlcpy(threads, optarg, sizeof(threads)); - if (len >= sizeof(threads)) { - fprintf(stderr, "Invalid thread count '%s'\n", optarg); - return 2; - } - } - } - } - - cpu_flags_eff = av_get_cpu_flags(); - - if (cpu_flags_eff < 0) - return 3; - - print_cpu_flags(cpu_flags_raw, "raw"); - print_cpu_flags(cpu_flags_eff, "effective"); - fprintf(stderr, "threads = %s (cpu_count = %d)\n", threads, cpu_count); - - return 0; -} - -#endif diff --git a/libavutil/crc-test.c b/libavutil/crc-test.c new file mode 100644 index 0000000000..41601c5ed8 --- /dev/null +++ b/libavutil/crc-test.c @@ -0,0 +1,45 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "crc.h" + +int main(void) +{ + uint8_t buf[1999]; + int i; + int p[5][3] = { + { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 }, + { AV_CRC_32_IEEE, 0x04C11DB7, 0xC0F5BAE0 }, + { AV_CRC_16_ANSI_LE, 0xA001, 0xBFD8 }, + { AV_CRC_16_ANSI, 0x8005, 0x1FBB }, + { AV_CRC_8_ATM, 0x07, 0xE3 } + }; + const AVCRC *ctx; + + for (i = 0; i < sizeof(buf); i++) + buf[i] = i + i * i; + + for (i = 0; i < 5; i++) { + ctx = av_crc_get_table(p[i][0]); + printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf))); + } + return 0; +} diff --git a/libavutil/crc.c b/libavutil/crc.c index ad43c73ec1..5428f8c0b6 100644 --- a/libavutil/crc.c +++ b/libavutil/crc.c @@ -333,28 +333,3 @@ uint32_t av_crc(const AVCRC *ctx, uint32_t crc, return crc; } - -#ifdef TEST -int main(void) -{ - uint8_t buf[1999]; - int i; - int p[5][3] = { - { AV_CRC_32_IEEE_LE, 0xEDB88320, 0x3D5CDD04 }, - { AV_CRC_32_IEEE, 0x04C11DB7, 0xC0F5BAE0 }, - { AV_CRC_16_ANSI_LE, 0xA001, 0xBFD8 }, - { AV_CRC_16_ANSI, 0x8005, 0x1FBB }, - { AV_CRC_8_ATM, 0x07, 0xE3 } - }; - const AVCRC *ctx; - - for (i = 0; i < sizeof(buf); i++) - buf[i] = i + i * i; - - for (i = 0; i < 5; i++) { - ctx = av_crc_get_table(p[i][0]); - printf("crc %08X = %X\n", p[i][1], av_crc(ctx, 0, buf, sizeof(buf))); - } - return 0; -} -#endif diff --git a/libavutil/des-test.c b/libavutil/des-test.c new file mode 100644 index 0000000000..a372035a54 --- /dev/null +++ b/libavutil/des-test.c @@ -0,0 +1,128 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "des.c" + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "time.h" + +static uint64_t rand64(void) +{ + uint64_t r = rand(); + r = (r << 32) | rand(); + return r; +} + +static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; +static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; +static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 }; +static DECLARE_ALIGNED(8, uint8_t, tmp)[8]; +static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8]; +static const uint8_t cbc_key[] = { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, + 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, + 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 +}; + +static int run_test(int cbc, int decrypt) +{ + AVDES d; + int delay = cbc && !decrypt ? 2 : 1; + uint64_t res; + AV_WB64(large_buffer[0], 0x4e6f772069732074ULL); + AV_WB64(large_buffer[1], 0x1234567890abcdefULL); + AV_WB64(tmp, 0x1234567890abcdefULL); + av_des_init(&d, cbc_key, 192, decrypt); + av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt); + res = AV_RB64(large_buffer[9999 + delay]); + if (cbc) { + if (decrypt) + return res == 0xc5cecf63ecec514cULL; + else + return res == 0xcb191f85d1ed8439ULL; + } else { + if (decrypt) + return res == 0x8325397644091a0aULL; + else + return res == 0xdd17e8b8b437d232ULL; + } +} + +int main(void) +{ + AVDES d; + int i; + uint64_t key[3]; + uint64_t data; + uint64_t ct; + uint64_t roundkeys[16]; + srand(av_gettime()); + key[0] = AV_RB64(test_key); + data = AV_RB64(plain); + gen_roundkeys(roundkeys, key[0]); + if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) { + printf("Test 1 failed\n"); + return 1; + } + av_des_init(&d, test_key, 64, 0); + av_des_crypt(&d, tmp, plain, 1, NULL, 0); + if (memcmp(tmp, crypt, sizeof(crypt))) { + printf("Public API decryption failed\n"); + return 1; + } + if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) { + printf("Partial Monte-Carlo test failed\n"); + return 1; + } + for (i = 0; i < 1000; i++) { + key[0] = rand64(); + key[1] = rand64(); + key[2] = rand64(); + data = rand64(); + av_des_init(&d, key, 192, 0); + av_des_crypt(&d, &ct, &data, 1, NULL, 0); + av_des_init(&d, key, 192, 1); + av_des_crypt(&d, &ct, &ct, 1, NULL, 1); + if (ct != data) { + printf("Test 2 failed\n"); + return 1; + } + } +#ifdef GENTABLES + printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n"); + for (i = 0; i < 8; i++) { + int j; + printf(" {"); + for (j = 0; j < 64; j++) { + uint32_t v = S_boxes[i][j >> 1]; + v = j & 1 ? v >> 4 : v & 0xf; + v <<= 28 - 4 * i; + v = shuffle(v, P_shuffle, sizeof(P_shuffle)); + printf((j & 7) == 0 ? "\n " : " "); + printf("0x%08X,", v); + } + printf("\n },\n"); + } + printf("};\n"); +#endif + return 0; +} diff --git a/libavutil/des.c b/libavutil/des.c index 7d9458c81f..1dbedd92cd 100644 --- a/libavutil/des.c +++ b/libavutil/des.c @@ -111,7 +111,7 @@ static const uint8_t S_boxes[8][32] = { #else /** * This table contains the results of applying both the S-box and P-shuffle. - * It can be regenerated by compiling this file with -DCONFIG_SMALL -DTEST -DGENTABLES + * It can be regenerated by compiling des-test.c with "-DCONFIG_SMALL -DGENTABLES". */ static const uint32_t S_boxes_P_shuffle[8][64] = { { 0x00808200, 0x00000000, 0x00008000, 0x00808202, 0x00808002, 0x00008202, 0x00000002, 0x00008000, @@ -337,112 +337,3 @@ void av_des_mac(AVDES *d, uint8_t *dst, const uint8_t *src, int count) { av_des_crypt_mac(d, dst, src, count, (uint8_t[8]) { 0 }, 0, 1); } - -#ifdef TEST -#include <stdlib.h> -#include <stdio.h> - -#include "time.h" - -static uint64_t rand64(void) -{ - uint64_t r = rand(); - r = (r << 32) | rand(); - return r; -} - -static const uint8_t test_key[] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }; -static const DECLARE_ALIGNED(8, uint8_t, plain)[] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; -static const DECLARE_ALIGNED(8, uint8_t, crypt)[] = { 0x4a, 0xb6, 0x5b, 0x3d, 0x4b, 0x06, 0x15, 0x18 }; -static DECLARE_ALIGNED(8, uint8_t, tmp)[8]; -static DECLARE_ALIGNED(8, uint8_t, large_buffer)[10002][8]; -static const uint8_t cbc_key[] = { - 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, - 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, - 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23 -}; - -static int run_test(int cbc, int decrypt) -{ - AVDES d; - int delay = cbc && !decrypt ? 2 : 1; - uint64_t res; - AV_WB64(large_buffer[0], 0x4e6f772069732074ULL); - AV_WB64(large_buffer[1], 0x1234567890abcdefULL); - AV_WB64(tmp, 0x1234567890abcdefULL); - av_des_init(&d, cbc_key, 192, decrypt); - av_des_crypt(&d, large_buffer[delay], large_buffer[0], 10000, cbc ? tmp : NULL, decrypt); - res = AV_RB64(large_buffer[9999 + delay]); - if (cbc) { - if (decrypt) - return res == 0xc5cecf63ecec514cULL; - else - return res == 0xcb191f85d1ed8439ULL; - } else { - if (decrypt) - return res == 0x8325397644091a0aULL; - else - return res == 0xdd17e8b8b437d232ULL; - } -} - -int main(void) -{ - AVDES d; - int i; - uint64_t key[3]; - uint64_t data; - uint64_t ct; - uint64_t roundkeys[16]; - srand(av_gettime()); - key[0] = AV_RB64(test_key); - data = AV_RB64(plain); - gen_roundkeys(roundkeys, key[0]); - if (des_encdec(data, roundkeys, 0) != AV_RB64(crypt)) { - printf("Test 1 failed\n"); - return 1; - } - av_des_init(&d, test_key, 64, 0); - av_des_crypt(&d, tmp, plain, 1, NULL, 0); - if (memcmp(tmp, crypt, sizeof(crypt))) { - printf("Public API decryption failed\n"); - return 1; - } - if (!run_test(0, 0) || !run_test(0, 1) || !run_test(1, 0) || !run_test(1, 1)) { - printf("Partial Monte-Carlo test failed\n"); - return 1; - } - for (i = 0; i < 1000; i++) { - key[0] = rand64(); - key[1] = rand64(); - key[2] = rand64(); - data = rand64(); - av_des_init(&d, key, 192, 0); - av_des_crypt(&d, &ct, &data, 1, NULL, 0); - av_des_init(&d, key, 192, 1); - av_des_crypt(&d, &ct, &ct, 1, NULL, 1); - if (ct != data) { - printf("Test 2 failed\n"); - return 1; - } - } -#ifdef GENTABLES - printf("static const uint32_t S_boxes_P_shuffle[8][64] = {\n"); - for (i = 0; i < 8; i++) { - int j; - printf(" {"); - for (j = 0; j < 64; j++) { - uint32_t v = S_boxes[i][j >> 1]; - v = j & 1 ? v >> 4 : v & 0xf; - v <<= 28 - 4 * i; - v = shuffle(v, P_shuffle, sizeof(P_shuffle)); - printf((j & 7) == 0 ? "\n " : " "); - printf("0x%08X,", v); - } - printf("\n },\n"); - } - printf("};\n"); -#endif - return 0; -} -#endif diff --git a/libavutil/eval-test.c b/libavutil/eval-test.c new file mode 100644 index 0000000000..da8721de7c --- /dev/null +++ b/libavutil/eval-test.c @@ -0,0 +1,144 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <math.h> +#include <stdio.h> +#include <string.h> + +#include "timer.h" +#include "eval.h" + +static const double const_values[] = { + M_PI, + M_E, + 0 +}; + +static const char *const const_names[] = { + "PI", + "E", + 0 +}; + +int main(int argc, char **argv) +{ + int i; + double d; + const char *const *expr; + static const char *const exprs[] = { + "", + "1;2", + "-20", + "-PI", + "+PI", + "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + "80G/80Gi", + "1k", + "1Gi", + "1gi", + "1GiFoo", + "1k+1k", + "1Gi*3foo", + "foo", + "foo(", + "foo()", + "foo)", + "sin", + "sin(", + "sin()", + "sin)", + "sin 10", + "sin(1,2,3)", + "sin(1 )", + "1", + "1foo", + "bar + PI + E + 100f*2 + foo", + "13k + 12f - foo(1, 2)", + "1gi", + "1Gi", + "st(0, 123)", + "st(1, 123); ld(1)", + "lte(0, 1)", + "lte(1, 1)", + "lte(1, 0)", + "lt(0, 1)", + "lt(1, 1)", + "gt(1, 0)", + "gt(2, 7)", + "gte(122, 122)", + /* compute 1+2+...+N */ + "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)", + /* compute Fib(N) */ + "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)", + "while(0, 10)", + "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))", + "isnan(1)", + "isnan(NAN)", + "isnan(INF)", + "isinf(1)", + "isinf(NAN)", + "isinf(INF)", + "floor(NAN)", + "floor(123.123)", + "floor(-123.123)", + "trunc(123.123)", + "trunc(-123.123)", + "ceil(123.123)", + "ceil(-123.123)", + "sqrt(1764)", + "isnan(sqrt(-1))", + "not(1)", + "not(NAN)", + "not(0)", + "6.0206dB", + "-3.0103dB", + NULL + }; + + for (expr = exprs; *expr; expr++) { + printf("Evaluating '%s'\n", *expr); + av_expr_parse_and_eval(&d, *expr, + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + if (isnan(d)) + printf("'%s' -> nan\n\n", *expr); + else + printf("'%s' -> %f\n\n", *expr, d); + } + + av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + printf("%f == 12.7\n", d); + av_expr_parse_and_eval(&d, "80G/80Gi", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + printf("%f == 0.931322575\n", d); + + if (argc > 1 && !strcmp(argv[1], "-t")) { + for (i = 0; i < 1050; i++) { + START_TIMER; + av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", + const_names, const_values, + NULL, NULL, NULL, NULL, NULL, 0, NULL); + STOP_TIMER("av_expr_parse_and_eval"); + } + } + + return 0; +} diff --git a/libavutil/eval.c b/libavutil/eval.c index 31e9ebbc9c..b5d71c6000 100644 --- a/libavutil/eval.c +++ b/libavutil/eval.c @@ -565,127 +565,3 @@ int av_expr_parse_and_eval(double *d, const char *s, av_expr_free(e); return isnan(*d) ? AVERROR(EINVAL) : 0; } - -#ifdef TEST -#include <string.h> - -static const double const_values[] = { - M_PI, - M_E, - 0 -}; - -static const char *const const_names[] = { - "PI", - "E", - 0 -}; - -int main(int argc, char **argv) -{ - int i; - double d; - const char *const *expr; - static const char *const exprs[] = { - "", - "1;2", - "-20", - "-PI", - "+PI", - "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", - "80G/80Gi", - "1k", - "1Gi", - "1gi", - "1GiFoo", - "1k+1k", - "1Gi*3foo", - "foo", - "foo(", - "foo()", - "foo)", - "sin", - "sin(", - "sin()", - "sin)", - "sin 10", - "sin(1,2,3)", - "sin(1 )", - "1", - "1foo", - "bar + PI + E + 100f*2 + foo", - "13k + 12f - foo(1, 2)", - "1gi", - "1Gi", - "st(0, 123)", - "st(1, 123); ld(1)", - "lte(0, 1)", - "lte(1, 1)", - "lte(1, 0)", - "lt(0, 1)", - "lt(1, 1)", - "gt(1, 0)", - "gt(2, 7)", - "gte(122, 122)", - /* compute 1+2+...+N */ - "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)", - /* compute Fib(N) */ - "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)", - "while(0, 10)", - "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))", - "isnan(1)", - "isnan(NAN)", - "isnan(INF)", - "isinf(1)", - "isinf(NAN)", - "isinf(INF)", - "floor(NAN)", - "floor(123.123)", - "floor(-123.123)", - "trunc(123.123)", - "trunc(-123.123)", - "ceil(123.123)", - "ceil(-123.123)", - "sqrt(1764)", - "isnan(sqrt(-1))", - "not(1)", - "not(NAN)", - "not(0)", - "6.0206dB", - "-3.0103dB", - NULL - }; - - for (expr = exprs; *expr; expr++) { - printf("Evaluating '%s'\n", *expr); - av_expr_parse_and_eval(&d, *expr, - const_names, const_values, - NULL, NULL, NULL, NULL, NULL, 0, NULL); - if (isnan(d)) - printf("'%s' -> nan\n\n", *expr); - else - printf("'%s' -> %f\n\n", *expr, d); - } - - av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", - const_names, const_values, - NULL, NULL, NULL, NULL, NULL, 0, NULL); - printf("%f == 12.7\n", d); - av_expr_parse_and_eval(&d, "80G/80Gi", - const_names, const_values, - NULL, NULL, NULL, NULL, NULL, 0, NULL); - printf("%f == 0.931322575\n", d); - - if (argc > 1 && !strcmp(argv[1], "-t")) { - for (i = 0; i < 1050; i++) { - START_TIMER; - av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", - const_names, const_values, - NULL, NULL, NULL, NULL, NULL, 0, NULL); - STOP_TIMER("av_expr_parse_and_eval"); - } - } - - return 0; -} -#endif diff --git a/libavutil/fifo-test.c b/libavutil/fifo-test.c new file mode 100644 index 0000000000..f6615b1034 --- /dev/null +++ b/libavutil/fifo-test.c @@ -0,0 +1,51 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> + +#include "fifo.h" + +int main(void) +{ + /* create a FIFO buffer */ + AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); + int i, j, n; + + /* fill data */ + for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) + av_fifo_generic_write(fifo, &i, sizeof(int), NULL); + + /* peek at FIFO */ + n = av_fifo_size(fifo) / sizeof(int); + for (i = -n + 1; i < n; i++) { + int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int)); + printf("%d: %d\n", i, *v); + } + printf("\n"); + + /* read data */ + for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { + av_fifo_generic_read(fifo, &j, sizeof(int), NULL); + printf("%d ", j); + } + printf("\n"); + + av_fifo_free(fifo); + + return 0; +} diff --git a/libavutil/fifo.c b/libavutil/fifo.c index dffaf54533..a42899c2b3 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -131,37 +131,3 @@ void av_fifo_drain(AVFifoBuffer *f, int size) f->rptr -= f->end - f->buffer; f->rndx += size; } - -#ifdef TEST - -int main(void) -{ - /* create a FIFO buffer */ - AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int)); - int i, j, n; - - /* fill data */ - for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++) - av_fifo_generic_write(fifo, &i, sizeof(int), NULL); - - /* peek at FIFO */ - n = av_fifo_size(fifo) / sizeof(int); - for (i = -n + 1; i < n; i++) { - int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int)); - printf("%d: %d\n", i, *v); - } - printf("\n"); - - /* read data */ - for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) { - av_fifo_generic_read(fifo, &j, sizeof(int), NULL); - printf("%d ", j); - } - printf("\n"); - - av_fifo_free(fifo); - - return 0; -} - -#endif diff --git a/libavutil/float_dsp-test.c b/libavutil/float_dsp-test.c new file mode 100644 index 0000000000..e24018c07d --- /dev/null +++ b/libavutil/float_dsp-test.c @@ -0,0 +1,296 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <float.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "cpu.h" +#include "internal.h" +#include "lfg.h" +#include "log.h" +#include "random_seed.h" +#include "float_dsp.h" + +#define LEN 240 + +static void fill_float_array(AVLFG *lfg, float *a, int len) +{ + int i; + double bmg[2], stddev = 10.0, mean = 0.0; + + for (i = 0; i < len; i += 2) { + av_bmg_get(lfg, bmg); + a[i] = bmg[0] * stddev + mean; + a[i + 1] = bmg[1] * stddev + mean; + } +} +static int compare_floats(const float *a, const float *b, int len, + float max_diff) +{ + int i; + for (i = 0; i < len; i++) { + if (fabsf(a[i] - b[i]) > max_diff) { + av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", + i, a[i], b[i], a[i] - b[i]); + return -1; + } + } + return 0; +} + +static void fill_double_array(AVLFG *lfg, double *a, int len) +{ + int i; + double bmg[2], stddev = 10.0, mean = 0.0; + + for (i = 0; i < len; i += 2) { + av_bmg_get(lfg, bmg); + a[i] = bmg[0] * stddev + mean; + a[i + 1] = bmg[1] * stddev + mean; + } +} + +static int compare_doubles(const double *a, const double *b, int len, + double max_diff) +{ + int i; + + for (i = 0; i < len; i++) { + if (fabs(a[i] - b[i]) > max_diff) { + av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", + i, a[i], b[i], a[i] - b[i]); + return -1; + } + } + return 0; +} + +static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul(cdst, v1, v2, LEN); + fdsp->vector_fmul(odst, v1, v2, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n"); + + return ret; +} + +#define ARBITRARY_FMAC_SCALAR_CONST 0.005 +static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *src0, float scale) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + memcpy(cdst, v1, LEN * sizeof(*v1)); + memcpy(odst, v1, LEN * sizeof(*v1)); + + cdsp->vector_fmac_scalar(cdst, src0, scale, LEN); + fdsp->vector_fmac_scalar(odst, src0, scale, LEN); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n"); + + return ret; +} + +static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, float scale) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_scalar(cdst, v1, scale, LEN); + fdsp->vector_fmul_scalar(odst, v1, scale, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n"); + + return ret; +} + +static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const double *v1, double scale) +{ + LOCAL_ALIGNED(32, double, cdst, [LEN]); + LOCAL_ALIGNED(32, double, odst, [LEN]); + int ret; + + cdsp->vector_dmul_scalar(cdst, v1, scale, LEN); + fdsp->vector_dmul_scalar(odst, v1, scale, LEN); + + if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n"); + + return ret; +} + +#define ARBITRARY_FMUL_WINDOW_CONST 0.008 +static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2, const float *v3) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2); + fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n"); + + return ret; +} + +#define ARBITRARY_FMUL_ADD_CONST 0.005 +static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2, const float *v3) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN); + fdsp->vector_fmul_add(odst, v1, v2, v3, LEN); + + if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n"); + + return ret; +} + +static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cdst, [LEN]); + LOCAL_ALIGNED(32, float, odst, [LEN]); + int ret; + + cdsp->vector_fmul_reverse(cdst, v1, v2, LEN); + fdsp->vector_fmul_reverse(odst, v1, v2, LEN); + + if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) + av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n"); + + return ret; +} + +static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + LOCAL_ALIGNED(32, float, cv1, [LEN]); + LOCAL_ALIGNED(32, float, cv2, [LEN]); + LOCAL_ALIGNED(32, float, ov1, [LEN]); + LOCAL_ALIGNED(32, float, ov2, [LEN]); + int ret; + + memcpy(cv1, v1, LEN * sizeof(*v1)); + memcpy(cv2, v2, LEN * sizeof(*v2)); + memcpy(ov1, v1, LEN * sizeof(*v1)); + memcpy(ov2, v2, LEN * sizeof(*v2)); + + cdsp->butterflies_float(cv1, cv2, LEN); + fdsp->butterflies_float(ov1, ov2, LEN); + + if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) || + (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON))) + av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n"); + + return ret; +} + +#define ARBITRARY_SCALARPRODUCT_CONST 0.2 +static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, + const float *v1, const float *v2) +{ + float cprod, oprod; + int ret; + + cprod = cdsp->scalarproduct_float(v1, v2, LEN); + oprod = fdsp->scalarproduct_float(v1, v2, LEN); + + if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST)) + av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n"); + + return ret; +} + +int main(int argc, char **argv) +{ + int ret = 0; + uint32_t seed; + AVFloatDSPContext fdsp, cdsp; + AVLFG lfg; + + LOCAL_ALIGNED(32, float, src0, [LEN]); + LOCAL_ALIGNED(32, float, src1, [LEN]); + LOCAL_ALIGNED(32, float, src2, [LEN]); + LOCAL_ALIGNED(32, double, dbl_src0, [LEN]); + LOCAL_ALIGNED(32, double, dbl_src1, [LEN]); + + if (argc > 2 && !strcmp(argv[1], "-s")) + seed = strtoul(argv[2], NULL, 10); + else + seed = av_get_random_seed(); + + av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed); + + av_lfg_init(&lfg, seed); + + fill_float_array(&lfg, src0, LEN); + fill_float_array(&lfg, src1, LEN); + fill_float_array(&lfg, src2, LEN); + + fill_double_array(&lfg, dbl_src0, LEN); + fill_double_array(&lfg, dbl_src1, LEN); + + avpriv_float_dsp_init(&fdsp, 1); + av_set_cpu_flags_mask(0); + avpriv_float_dsp_init(&cdsp, 1); + + if (test_vector_fmul(&fdsp, &cdsp, src0, src1)) + ret -= 1 << 0; + if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0])) + ret -= 1 << 1; + if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0])) + ret -= 1 << 2; + if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2)) + ret -= 1 << 3; + if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2)) + ret -= 1 << 4; + if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1)) + ret -= 1 << 5; + if (test_butterflies_float(&fdsp, &cdsp, src0, src1)) + ret -= 1 << 6; + if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1)) + ret -= 1 << 7; + if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0])) + ret -= 1 << 8; + + return ret; +} diff --git a/libavutil/float_dsp.c b/libavutil/float_dsp.c index aabc800db6..d31fc3d166 100644 --- a/libavutil/float_dsp.c +++ b/libavutil/float_dsp.c @@ -133,288 +133,3 @@ av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact) if (ARCH_X86) ff_float_dsp_init_x86(fdsp); } - -#ifdef TEST - -#include <float.h> -#include <math.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#include "common.h" -#include "cpu.h" -#include "internal.h" -#include "lfg.h" -#include "log.h" -#include "mem.h" -#include "random_seed.h" - -#define LEN 240 - -static void fill_float_array(AVLFG *lfg, float *a, int len) -{ - int i; - double bmg[2], stddev = 10.0, mean = 0.0; - - for (i = 0; i < len; i += 2) { - av_bmg_get(lfg, bmg); - a[i] = bmg[0] * stddev + mean; - a[i + 1] = bmg[1] * stddev + mean; - } -} -static int compare_floats(const float *a, const float *b, int len, - float max_diff) -{ - int i; - for (i = 0; i < len; i++) { - if (fabsf(a[i] - b[i]) > max_diff) { - av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", - i, a[i], b[i], a[i] - b[i]); - return -1; - } - } - return 0; -} - -static void fill_double_array(AVLFG *lfg, double *a, int len) -{ - int i; - double bmg[2], stddev = 10.0, mean = 0.0; - - for (i = 0; i < len; i += 2) { - av_bmg_get(lfg, bmg); - a[i] = bmg[0] * stddev + mean; - a[i + 1] = bmg[1] * stddev + mean; - } -} - -static int compare_doubles(const double *a, const double *b, int len, - double max_diff) -{ - int i; - - for (i = 0; i < len; i++) { - if (fabs(a[i] - b[i]) > max_diff) { - av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n", - i, a[i], b[i], a[i] - b[i]); - return -1; - } - } - return 0; -} - -static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - cdsp->vector_fmul(cdst, v1, v2, LEN); - fdsp->vector_fmul(odst, v1, v2, LEN); - - if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) - av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n"); - - return ret; -} - -#define ARBITRARY_FMAC_SCALAR_CONST 0.005 -static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *src0, float scale) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - memcpy(cdst, v1, LEN * sizeof(*v1)); - memcpy(odst, v1, LEN * sizeof(*v1)); - - cdsp->vector_fmac_scalar(cdst, src0, scale, LEN); - fdsp->vector_fmac_scalar(odst, src0, scale, LEN); - - if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST)) - av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n"); - - return ret; -} - -static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, float scale) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - cdsp->vector_fmul_scalar(cdst, v1, scale, LEN); - fdsp->vector_fmul_scalar(odst, v1, scale, LEN); - - if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) - av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n"); - - return ret; -} - -static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const double *v1, double scale) -{ - LOCAL_ALIGNED(32, double, cdst, [LEN]); - LOCAL_ALIGNED(32, double, odst, [LEN]); - int ret; - - cdsp->vector_dmul_scalar(cdst, v1, scale, LEN); - fdsp->vector_dmul_scalar(odst, v1, scale, LEN); - - if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON)) - av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n"); - - return ret; -} - -#define ARBITRARY_FMUL_WINDOW_CONST 0.008 -static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2, const float *v3) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2); - fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2); - - if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST)) - av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n"); - - return ret; -} - -#define ARBITRARY_FMUL_ADD_CONST 0.005 -static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2, const float *v3) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN); - fdsp->vector_fmul_add(odst, v1, v2, v3, LEN); - - if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST)) - av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n"); - - return ret; -} - -static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2) -{ - LOCAL_ALIGNED(32, float, cdst, [LEN]); - LOCAL_ALIGNED(32, float, odst, [LEN]); - int ret; - - cdsp->vector_fmul_reverse(cdst, v1, v2, LEN); - fdsp->vector_fmul_reverse(odst, v1, v2, LEN); - - if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON)) - av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n"); - - return ret; -} - -static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2) -{ - LOCAL_ALIGNED(32, float, cv1, [LEN]); - LOCAL_ALIGNED(32, float, cv2, [LEN]); - LOCAL_ALIGNED(32, float, ov1, [LEN]); - LOCAL_ALIGNED(32, float, ov2, [LEN]); - int ret; - - memcpy(cv1, v1, LEN * sizeof(*v1)); - memcpy(cv2, v2, LEN * sizeof(*v2)); - memcpy(ov1, v1, LEN * sizeof(*v1)); - memcpy(ov2, v2, LEN * sizeof(*v2)); - - cdsp->butterflies_float(cv1, cv2, LEN); - fdsp->butterflies_float(ov1, ov2, LEN); - - if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) || - (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON))) - av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n"); - - return ret; -} - -#define ARBITRARY_SCALARPRODUCT_CONST 0.2 -static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp, - const float *v1, const float *v2) -{ - float cprod, oprod; - int ret; - - cprod = cdsp->scalarproduct_float(v1, v2, LEN); - oprod = fdsp->scalarproduct_float(v1, v2, LEN); - - if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST)) - av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n"); - - return ret; -} - -int main(int argc, char **argv) -{ - int ret = 0; - uint32_t seed; - AVFloatDSPContext fdsp, cdsp; - AVLFG lfg; - - LOCAL_ALIGNED(32, float, src0, [LEN]); - LOCAL_ALIGNED(32, float, src1, [LEN]); - LOCAL_ALIGNED(32, float, src2, [LEN]); - LOCAL_ALIGNED(32, double, dbl_src0, [LEN]); - LOCAL_ALIGNED(32, double, dbl_src1, [LEN]); - - if (argc > 2 && !strcmp(argv[1], "-s")) - seed = strtoul(argv[2], NULL, 10); - else - seed = av_get_random_seed(); - - av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed); - - av_lfg_init(&lfg, seed); - - fill_float_array(&lfg, src0, LEN); - fill_float_array(&lfg, src1, LEN); - fill_float_array(&lfg, src2, LEN); - - fill_double_array(&lfg, dbl_src0, LEN); - fill_double_array(&lfg, dbl_src1, LEN); - - avpriv_float_dsp_init(&fdsp, 1); - av_set_cpu_flags_mask(0); - avpriv_float_dsp_init(&cdsp, 1); - - if (test_vector_fmul(&fdsp, &cdsp, src0, src1)) - ret -= 1 << 0; - if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0])) - ret -= 1 << 1; - if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0])) - ret -= 1 << 2; - if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2)) - ret -= 1 << 3; - if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2)) - ret -= 1 << 4; - if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1)) - ret -= 1 << 5; - if (test_butterflies_float(&fdsp, &cdsp, src0, src1)) - ret -= 1 << 6; - if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1)) - ret -= 1 << 7; - if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0])) - ret -= 1 << 8; - - return ret; -} - -#endif /* TEST */ diff --git a/libavutil/hmac-test.c b/libavutil/hmac-test.c new file mode 100644 index 0000000000..fb6bf87cff --- /dev/null +++ b/libavutil/hmac-test.c @@ -0,0 +1,92 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "hmac.c" + +#include <stdio.h> +#include <string.h> + +static void test(AVHMAC *hmac, const uint8_t *key, int keylen, + const uint8_t *data, int datalen) +{ + uint8_t buf[MAX_HASHLEN]; + int out, i; + // Some of the test vectors are strings, where sizeof() includes the + // trailing null byte - remove that. + if (!key[keylen - 1]) + keylen--; + if (!data[datalen - 1]) + datalen--; + out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf)); + for (i = 0; i < out; i++) + printf("%02x", buf[i]); + printf("\n"); +} + +int main(void) +{ + uint8_t key1[20], key3[131], data3[50]; + enum AVHMACType i = AV_HMAC_SHA224; + const uint8_t key2[] = "Jefe"; + const uint8_t data1[] = "Hi There"; + const uint8_t data2[] = "what do ya want for nothing?"; + const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First"; + const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; + const uint8_t data6[] = "This is a test using a larger than block-size key and a larger " + "than block-size data. The key needs to be hashed before being used" + " by the HMAC algorithm."; + AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5); + if (!hmac) + return 1; + memset(key1, 0x0b, sizeof(key1)); + memset(key3, 0xaa, sizeof(key3)); + memset(data3, 0xdd, sizeof(data3)); + // RFC 2202 test vectors + test(hmac, key1, 16, data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 16, data3, sizeof(data3)); + test(hmac, key3, 80, data4, sizeof(data4)); + test(hmac, key3, 80, data5, sizeof(data5)); + av_hmac_free(hmac); + + /* SHA-1 */ + hmac = av_hmac_alloc(AV_HMAC_SHA1); + if (!hmac) + return 1; + // RFC 2202 test vectors + test(hmac, key1, sizeof(key1), data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, 80, data4, sizeof(data4)); + test(hmac, key3, 80, data5, sizeof(data5)); + av_hmac_free(hmac); + + /* SHA-2 */ + while (i <= AV_HMAC_SHA256) { + hmac = av_hmac_alloc(i); + // RFC 4231 test vectors + test(hmac, key1, sizeof(key1), data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, sizeof(key3), data4, sizeof(data4)); + test(hmac, key3, sizeof(key3), data6, sizeof(data6)); + av_hmac_free(hmac); + i++; + } + return 0; +} diff --git a/libavutil/hmac.c b/libavutil/hmac.c index abbee368b0..378be62fd3 100644 --- a/libavutil/hmac.c +++ b/libavutil/hmac.c @@ -158,77 +158,3 @@ int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len, av_hmac_update(c, data, len); return av_hmac_final(c, out, outlen); } - -#ifdef TEST -#include <stdio.h> - -static void test(AVHMAC *hmac, const uint8_t *key, int keylen, - const uint8_t *data, int datalen) -{ - uint8_t buf[MAX_HASHLEN]; - int out, i; - // Some of the test vectors are strings, where sizeof() includes the - // trailing null byte - remove that. - if (!key[keylen - 1]) - keylen--; - if (!data[datalen - 1]) - datalen--; - out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf)); - for (i = 0; i < out; i++) - printf("%02x", buf[i]); - printf("\n"); -} - -int main(void) -{ - uint8_t key1[20], key3[131], data3[50]; - enum AVHMACType i = AV_HMAC_SHA224; - const uint8_t key2[] = "Jefe"; - const uint8_t data1[] = "Hi There"; - const uint8_t data2[] = "what do ya want for nothing?"; - const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First"; - const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; - const uint8_t data6[] = "This is a test using a larger than block-size key and a larger " - "than block-size data. The key needs to be hashed before being used" - " by the HMAC algorithm."; - AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5); - if (!hmac) - return 1; - memset(key1, 0x0b, sizeof(key1)); - memset(key3, 0xaa, sizeof(key3)); - memset(data3, 0xdd, sizeof(data3)); - // RFC 2202 test vectors - test(hmac, key1, 16, data1, sizeof(data1)); - test(hmac, key2, sizeof(key2), data2, sizeof(data2)); - test(hmac, key3, 16, data3, sizeof(data3)); - test(hmac, key3, 80, data4, sizeof(data4)); - test(hmac, key3, 80, data5, sizeof(data5)); - av_hmac_free(hmac); - - /* SHA-1 */ - hmac = av_hmac_alloc(AV_HMAC_SHA1); - if (!hmac) - return 1; - // RFC 2202 test vectors - test(hmac, key1, sizeof(key1), data1, sizeof(data1)); - test(hmac, key2, sizeof(key2), data2, sizeof(data2)); - test(hmac, key3, 20, data3, sizeof(data3)); - test(hmac, key3, 80, data4, sizeof(data4)); - test(hmac, key3, 80, data5, sizeof(data5)); - av_hmac_free(hmac); - - /* SHA-2 */ - while (i <= AV_HMAC_SHA256) { - hmac = av_hmac_alloc(i); - // RFC 4231 test vectors - test(hmac, key1, sizeof(key1), data1, sizeof(data1)); - test(hmac, key2, sizeof(key2), data2, sizeof(data2)); - test(hmac, key3, 20, data3, sizeof(data3)); - test(hmac, key3, sizeof(key3), data4, sizeof(data4)); - test(hmac, key3, sizeof(key3), data6, sizeof(data6)); - av_hmac_free(hmac); - i++; - } - return 0; -} -#endif /* TEST */ diff --git a/libavutil/lfg-test.c b/libavutil/lfg-test.c new file mode 100644 index 0000000000..92b4259a3c --- /dev/null +++ b/libavutil/lfg-test.c @@ -0,0 +1,56 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "log.h" +#include "timer.h" +#include "lfg.h" + +int main(void) +{ + int x = 0; + int i, j; + AVLFG state; + + av_lfg_init(&state, 0xdeadbeef); + for (j = 0; j < 10000; j++) { + START_TIMER + for (i = 0; i < 624; i++) + x += av_lfg_get(&state); + STOP_TIMER("624 calls of av_lfg_get"); + } + av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); + + /* BMG usage example */ + { + double mean = 1000; + double stddev = 53; + + av_lfg_init(&state, 42); + + for (i = 0; i < 1000; i += 2) { + double bmg_out[2]; + av_bmg_get(&state, bmg_out); + av_log(NULL, AV_LOG_INFO, + "%f\n%f\n", + bmg_out[0] * stddev + mean, + bmg_out[1] * stddev + mean); + } + } + + return 0; +} diff --git a/libavutil/lfg.c b/libavutil/lfg.c index 4221e6228b..20900570a2 100644 --- a/libavutil/lfg.c +++ b/libavutil/lfg.c @@ -58,43 +58,3 @@ void av_bmg_get(AVLFG *lfg, double out[2]) out[0] = x1 * w; out[1] = x2 * w; } - -#ifdef TEST -#include "log.h" -#include "timer.h" - -int main(void) -{ - int x = 0; - int i, j; - AVLFG state; - - av_lfg_init(&state, 0xdeadbeef); - for (j = 0; j < 10000; j++) { - START_TIMER - for (i = 0; i < 624; i++) - x += av_lfg_get(&state); - STOP_TIMER("624 calls of av_lfg_get"); - } - av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); - - /* BMG usage example */ - { - double mean = 1000; - double stddev = 53; - - av_lfg_init(&state, 42); - - for (i = 0; i < 1000; i += 2) { - double bmg_out[2]; - av_bmg_get(&state, bmg_out); - av_log(NULL, AV_LOG_INFO, - "%f\n%f\n", - bmg_out[0] * stddev + mean, - bmg_out[1] * stddev + mean); - } - } - - return 0; -} -#endif diff --git a/libavutil/lls-test.c b/libavutil/lls-test.c new file mode 100644 index 0000000000..f9814b3863 --- /dev/null +++ b/libavutil/lls-test.c @@ -0,0 +1,54 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <limits.h> +#include <stdio.h> + +#include "internal.h" +#include "lfg.h" +#include "lls.h" + +int main(void) +{ + LLSModel m; + int i, order; + AVLFG lfg; + + av_lfg_init(&lfg, 1); + avpriv_init_lls(&m, 3); + + for (i = 0; i < 100; i++) { + LOCAL_ALIGNED(32, double, var, [4]); + double eval; + + var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2; + var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; + m.update_lls(&m, var); + avpriv_solve_lls(&m, 0.001, 0); + for (order = 0; order < 3; order++) { + eval = m.evaluate_lls(&m, var + 1, order); + printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n", + var[0], order, eval, sqrt(m.variance[order] / (i + 1)), + m.coeff[order][0], m.coeff[order][1], + m.coeff[order][2]); + } + } + return 0; +} diff --git a/libavutil/lls.c b/libavutil/lls.c index 37bfc4e679..97bffb4018 100644 --- a/libavutil/lls.c +++ b/libavutil/lls.c @@ -121,41 +121,3 @@ av_cold void avpriv_init_lls(LLSModel *m, int indep_count) if (ARCH_X86) ff_init_lls_x86(m); } - -#ifdef TEST - -#include <stdio.h> -#include <limits.h> -#include "lfg.h" - -int main(void) -{ - LLSModel m; - int i, order; - AVLFG lfg; - - av_lfg_init(&lfg, 1); - avpriv_init_lls(&m, 3); - - for (i = 0; i < 100; i++) { - LOCAL_ALIGNED(32, double, var, [4]); - double eval; - - var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2; - var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; - var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; - var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5; - m.update_lls(&m, var); - avpriv_solve_lls(&m, 0.001, 0); - for (order = 0; order < 3; order++) { - eval = m.evaluate_lls(&m, var + 1, order); - printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n", - var[0], order, eval, sqrt(m.variance[order] / (i + 1)), - m.coeff[order][0], m.coeff[order][1], - m.coeff[order][2]); - } - } - return 0; -} - -#endif diff --git a/libavutil/md5-test.c b/libavutil/md5-test.c new file mode 100644 index 0000000000..8d5dd1e104 --- /dev/null +++ b/libavutil/md5-test.c @@ -0,0 +1,54 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "md5.h" + +static void print_md5(uint8_t *md5) +{ + int i; + for (i = 0; i < 16; i++) + printf("%02x", md5[i]); + printf("\n"); +} + +int main(void) +{ + uint8_t md5val[16]; + int i; + uint8_t in[1000]; + + for (i = 0; i < 1000; i++) + in[i] = i * i; + av_md5_sum(md5val, in, 1000); + print_md5(md5val); + av_md5_sum(md5val, in, 63); + print_md5(md5val); + av_md5_sum(md5val, in, 64); + print_md5(md5val); + av_md5_sum(md5val, in, 65); + print_md5(md5val); + for (i = 0; i < 1000; i++) + in[i] = i % 127; + av_md5_sum(md5val, in, 999); + print_md5(md5val); + + return 0; +} diff --git a/libavutil/md5.c b/libavutil/md5.c index 79224ed7b2..e02a9ba0bf 100644 --- a/libavutil/md5.c +++ b/libavutil/md5.c @@ -185,39 +185,3 @@ void av_md5_sum(uint8_t *dst, const uint8_t *src, const int len) av_md5_update(&ctx, src, len); av_md5_final(&ctx, dst); } - -#ifdef TEST -#include <stdio.h> - -static void print_md5(uint8_t *md5) -{ - int i; - for (i = 0; i < 16; i++) - printf("%02x", md5[i]); - printf("\n"); -} - -int main(void) -{ - uint8_t md5val[16]; - int i; - uint8_t in[1000]; - - for (i = 0; i < 1000; i++) - in[i] = i * i; - av_md5_sum(md5val, in, 1000); - print_md5(md5val); - av_md5_sum(md5val, in, 63); - print_md5(md5val); - av_md5_sum(md5val, in, 64); - print_md5(md5val); - av_md5_sum(md5val, in, 65); - print_md5(md5val); - for (i = 0; i < 1000; i++) - in[i] = i % 127; - av_md5_sum(md5val, in, 999); - print_md5(md5val); - - return 0; -} -#endif diff --git a/libavutil/opt-test.c b/libavutil/opt-test.c new file mode 100644 index 0000000000..14a144cb86 --- /dev/null +++ b/libavutil/opt-test.c @@ -0,0 +1,110 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <limits.h> +#include <stdio.h> + +#include "common.h" +#include "error.h" +#include "log.h" +#include "mem.h" +#include "rational.h" +#include "opt.h" + +typedef struct TestContext { + const AVClass *class; + int num; + int toggle; + char *string; + int flags; + AVRational rational; +} TestContext; + +#define OFFSET(x) offsetof(TestContext, x) + +#define TEST_FLAG_COOL 01 +#define TEST_FLAG_LAME 02 +#define TEST_FLAG_MU 04 + +static const AVOption test_options[] = { + { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 }, + { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 }, + { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 }, + { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX }, + { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"}, + { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"}, + { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"}, + { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"}, + { NULL }, +}; + +static const char *test_get_name(void *ctx) +{ + return "test"; +} + +static const AVClass test_class = { + "TestContext", + test_get_name, + test_options +}; + +int main(void) +{ + int i; + TestContext test_ctx = { .class = &test_class }; + const char *options[] = { + "", + ":", + "=", + "foo=:", + ":=foo", + "=foo", + "foo=", + "foo", + "foo=val", + "foo==val", + "toggle=:", + "string=:", + "toggle=1 : foo", + "toggle=100", + "toggle==1", + "flags=+mu-lame : num=42: toggle=0", + "num=42 : string=blahblah", + "rational=0 : rational=1/2 : rational=1/-1", + "rational=-1/0", + }; + + printf("\nTesting av_set_options_string()\n"); + + av_opt_set_defaults(&test_ctx); + test_ctx.string = av_strdup("default"); + if (!test_ctx.string) + return AVERROR(ENOMEM); + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < FF_ARRAY_ELEMS(options); i++) { + av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); + if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0) + av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]); + printf("\n"); + } + + return 0; +} diff --git a/libavutil/opt.c b/libavutil/opt.c index df3cc0884f..7cb3d66557 100644 --- a/libavutil/opt.c +++ b/libavutil/opt.c @@ -885,90 +885,3 @@ int av_opt_copy(void *dst, const void *src) } return ret; } - -#ifdef TEST - -typedef struct TestContext { - const AVClass *class; - int num; - int toggle; - char *string; - int flags; - AVRational rational; -} TestContext; - -#define OFFSET(x) offsetof(TestContext, x) - -#define TEST_FLAG_COOL 01 -#define TEST_FLAG_LAME 02 -#define TEST_FLAG_MU 04 - -static const AVOption test_options[] = { - { "num", "set num", OFFSET(num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 100 }, - { "toggle", "set toggle", OFFSET(toggle), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 }, - { "rational", "set rational", OFFSET(rational), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, 10 }, - { "string", "set string", OFFSET(string), AV_OPT_TYPE_STRING, { 0 }, CHAR_MIN, CHAR_MAX }, - { "flags", "set flags", OFFSET(flags), AV_OPT_TYPE_FLAGS, { .i64 = 0 }, 0, INT_MAX, 0, "flags"}, - { "cool", "set cool flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_COOL }, INT_MIN, INT_MAX, 0, "flags"}, - { "lame", "set lame flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_LAME }, INT_MIN, INT_MAX, 0, "flags"}, - { "mu", "set mu flag ", 0, AV_OPT_TYPE_CONST, { .i64 = TEST_FLAG_MU }, INT_MIN, INT_MAX, 0, "flags"}, - { NULL }, -}; - -static const char *test_get_name(void *ctx) -{ - return "test"; -} - -static const AVClass test_class = { - "TestContext", - test_get_name, - test_options -}; - -int main(void) -{ - int i; - TestContext test_ctx = { .class = &test_class }; - const char *options[] = { - "", - ":", - "=", - "foo=:", - ":=foo", - "=foo", - "foo=", - "foo", - "foo=val", - "foo==val", - "toggle=:", - "string=:", - "toggle=1 : foo", - "toggle=100", - "toggle==1", - "flags=+mu-lame : num=42: toggle=0", - "num=42 : string=blahblah", - "rational=0 : rational=1/2 : rational=1/-1", - "rational=-1/0", - }; - - printf("\nTesting av_set_options_string()\n"); - - av_opt_set_defaults(&test_ctx); - test_ctx.string = av_strdup("default"); - if (!test_ctx.string) - return AVERROR(ENOMEM); - - av_log_set_level(AV_LOG_DEBUG); - - for (i = 0; i < FF_ARRAY_ELEMS(options); i++) { - av_log(&test_ctx, AV_LOG_DEBUG, "Setting options string '%s'\n", options[i]); - if (av_set_options_string(&test_ctx, options[i], "=", ":") < 0) - av_log(&test_ctx, AV_LOG_ERROR, "Error setting options string: '%s'\n", options[i]); - printf("\n"); - } - - return 0; -} - -#endif diff --git a/libavutil/parseutils-test.c b/libavutil/parseutils-test.c new file mode 100644 index 0000000000..149861f1d5 --- /dev/null +++ b/libavutil/parseutils-test.c @@ -0,0 +1,116 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> + +#include "common.h" +#include "log.h" +#include "rational.h" +#include "parseutils.h" + +int main(void) +{ + int i; + uint8_t rgba[4]; + static const char *const rates[] = { + "-inf", + "inf", + "nan", + "123/0", + "-123 / 0", + "", + "/", + " 123 / 321", + "foo/foo", + "foo/1", + "1/foo", + "0/0", + "/0", + "1/", + "1", + "0", + "-123/123", + "-foo", + "123.23", + ".23", + "-.23", + "-0.234", + "-0.0000001", + " 21332.2324 ", + " -21332.2324 ", + }; + static const char *const color_names[] = { + "foo", + "red", + "Red ", + "RED", + "Violet", + "Yellow", + "Red", + "0x000000", + "0x0000000", + "0xff000000", + "0x3e34ff", + "0x3e34ffaa", + "0xffXXee", + "0xfoobar", + "0xffffeeeeeeee", + "#ff0000", + "#ffXX00", + "ff0000", + "ffXX00", + "red@foo", + "random@10", + "0xff0000@1.0", + "red@", + "red@0xfff", + "red@0xf", + "red@2", + "red@0.1", + "red@-1", + "red@0.5", + "red@1.0", + "red@256", + "red@10foo", + "red@-1.0", + "red@-0.0", + }; + + printf("Testing av_parse_video_rate()\n"); + + for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) { + int ret; + AVRational q = { 0, 0 }; + ret = av_parse_video_rate(&q, rates[i]); + printf("'%s' -> %d/%d %s\n", + rates[i], q.num, q.den, ret ? "ERROR" : "OK"); + } + + printf("\nTesting av_parse_color()\n"); + + av_log_set_level(AV_LOG_DEBUG); + + for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { + if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) + printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", + color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); + } + + return 0; +} diff --git a/libavutil/parseutils.c b/libavutil/parseutils.c index ba21a2ee5b..f4248114b5 100644 --- a/libavutil/parseutils.c +++ b/libavutil/parseutils.c @@ -654,98 +654,3 @@ int av_find_info_tag(char *arg, int arg_size, const char *tag1, const char *info } return 0; } - -#ifdef TEST - -int main(void) -{ - int i; - uint8_t rgba[4]; - static const char *const rates[] = { - "-inf", - "inf", - "nan", - "123/0", - "-123 / 0", - "", - "/", - " 123 / 321", - "foo/foo", - "foo/1", - "1/foo", - "0/0", - "/0", - "1/", - "1", - "0", - "-123/123", - "-foo", - "123.23", - ".23", - "-.23", - "-0.234", - "-0.0000001", - " 21332.2324 ", - " -21332.2324 ", - }; - static const char *const color_names[] = { - "foo", - "red", - "Red ", - "RED", - "Violet", - "Yellow", - "Red", - "0x000000", - "0x0000000", - "0xff000000", - "0x3e34ff", - "0x3e34ffaa", - "0xffXXee", - "0xfoobar", - "0xffffeeeeeeee", - "#ff0000", - "#ffXX00", - "ff0000", - "ffXX00", - "red@foo", - "random@10", - "0xff0000@1.0", - "red@", - "red@0xfff", - "red@0xf", - "red@2", - "red@0.1", - "red@-1", - "red@0.5", - "red@1.0", - "red@256", - "red@10foo", - "red@-1.0", - "red@-0.0", - }; - - printf("Testing av_parse_video_rate()\n"); - - for (i = 0; i < FF_ARRAY_ELEMS(rates); i++) { - int ret; - AVRational q = { 0, 0 }; - ret = av_parse_video_rate(&q, rates[i]); - printf("'%s' -> %d/%d %s\n", - rates[i], q.num, q.den, ret ? "ERROR" : "OK"); - } - - printf("\nTesting av_parse_color()\n"); - - av_log_set_level(AV_LOG_DEBUG); - - for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { - if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) - printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", - color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); - } - - return 0; -} - -#endif /* TEST */ diff --git a/libavutil/sha-test.c b/libavutil/sha-test.c new file mode 100644 index 0000000000..e43a5a53d5 --- /dev/null +++ b/libavutil/sha-test.c @@ -0,0 +1,69 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "sha.c" + +#include <stdio.h> + +int main(void) +{ + int i, j, k; + AVSHA ctx; + unsigned char digest[32]; + const int lengths[3] = { 160, 224, 256 }; + + for (j = 0; j < 3; j++) { + printf("Testing SHA-%d\n", lengths[j]); + for (k = 0; k < 3; k++) { + av_sha_init(&ctx, lengths[j]); + if (k == 0) + av_sha_update(&ctx, "abc", 3); + else if (k == 1) + av_sha_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); + else + for (i = 0; i < 1000*1000; i++) + av_sha_update(&ctx, "a", 1); + av_sha_final(&ctx, digest); + for (i = 0; i < lengths[j] >> 3; i++) + printf("%02X", digest[i]); + putchar('\n'); + } + switch (j) { + case 0: + //test vectors (from FIPS PUB 180-1) + printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n" + "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n" + "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n"); + break; + case 1: + //test vectors (from FIPS PUB 180-2 Appendix A) + printf("23097d22 3405d822 8642a477 bda255b3 2aadbce4 bda0b3f7 e36c9da7\n" + "75388b16 512776cc 5dba5da1 fd890150 b0c6455c b4f58b19 52522525\n" + "20794655 980c91d8 bbb4c1ea 97618a4b f03f4258 1948b2ee 4ee7ad67\n"); + break; + case 2: + //test vectors (from FIPS PUB 180-2) + printf("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad\n" + "248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1\n" + "cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0\n"); + break; + } + } + + return 0; +} diff --git a/libavutil/sha.c b/libavutil/sha.c index 9e78d19d83..404effade2 100644 --- a/libavutil/sha.c +++ b/libavutil/sha.c @@ -329,55 +329,3 @@ void av_sha_final(AVSHA* ctx, uint8_t *digest) for (i = 0; i < ctx->digest_len; i++) AV_WB32(digest + i*4, ctx->state[i]); } - -#ifdef TEST -#include <stdio.h> - -int main(void) -{ - int i, j, k; - AVSHA ctx; - unsigned char digest[32]; - const int lengths[3] = { 160, 224, 256 }; - - for (j = 0; j < 3; j++) { - printf("Testing SHA-%d\n", lengths[j]); - for (k = 0; k < 3; k++) { - av_sha_init(&ctx, lengths[j]); - if (k == 0) - av_sha_update(&ctx, "abc", 3); - else if (k == 1) - av_sha_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); - else - for (i = 0; i < 1000*1000; i++) - av_sha_update(&ctx, "a", 1); - av_sha_final(&ctx, digest); - for (i = 0; i < lengths[j] >> 3; i++) - printf("%02X", digest[i]); - putchar('\n'); - } - switch (j) { - case 0: - //test vectors (from FIPS PUB 180-1) - printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n" - "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n" - "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n"); - break; - case 1: - //test vectors (from FIPS PUB 180-2 Appendix A) - printf("23097d22 3405d822 8642a477 bda255b3 2aadbce4 bda0b3f7 e36c9da7\n" - "75388b16 512776cc 5dba5da1 fd890150 b0c6455c b4f58b19 52522525\n" - "20794655 980c91d8 bbb4c1ea 97618a4b f03f4258 1948b2ee 4ee7ad67\n"); - break; - case 2: - //test vectors (from FIPS PUB 180-2) - printf("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad\n" - "248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1\n" - "cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0\n"); - break; - } - } - - return 0; -} -#endif diff --git a/libavutil/tree-test.c b/libavutil/tree-test.c new file mode 100644 index 0000000000..f93e53409d --- /dev/null +++ b/libavutil/tree-test.c @@ -0,0 +1,110 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "tree.c" + +#include <stdint.h> + +#include "common.h" +#include "lfg.h" +#include "log.h" + +static int check(AVTreeNode *t) +{ + if (t) { + int left = check(t->child[0]); + int right = check(t->child[1]); + + if (left > 999 || right > 999) + return 1000; + if (right - left != t->state) + return 1000; + if (t->state > 1 || t->state < -1) + return 1000; + return FFMAX(left, right) + 1; + } + return 0; +} + +static void print(AVTreeNode *t, int depth) +{ + int i; + for (i = 0; i < depth * 4; i++) + av_log(NULL, AV_LOG_ERROR, " "); + if (t) { + av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem); + print(t->child[0], depth + 1); + print(t->child[1], depth + 1); + } else + av_log(NULL, AV_LOG_ERROR, "NULL\n"); +} + +static int cmp(void *a, const void *b) +{ + return (uint8_t *) a - (const uint8_t *) b; +} + +int main(void) +{ + int i; + AVTreeNode *root = NULL, *node = NULL; + AVLFG prng; + + av_lfg_init(&prng, 1); + + for (i = 0; i < 10000; i++) { + AVTreeNode *node2 = NULL; + intptr_t j = av_lfg_get(&prng) % 86294; + void *ret, *jj = (void *)(j + 1); + + while (ret = av_tree_find(root, jj, cmp, NULL)) { + j = av_lfg_get(&prng) % 86294; + jj = (void *)(j + 1); + } + + if (check(root) > 999) { + av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); + print(root, 0); + return 1; + } + + if (!node) + node = av_tree_node_alloc(); + if (!node) { + av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n"); + return 1; + } + av_tree_insert(&root, jj, cmp, &node); + + while (ret = av_tree_find(root, jj, cmp, NULL)) { + j = av_lfg_get(&prng) % 86294; + jj = (void *)(j + 1); + } + + ret = av_tree_insert(&root, jj, cmp, &node2); + if (ret != jj) + av_tree_destroy(node2); + ret = av_tree_find(root, jj, cmp, NULL); + if (ret) + av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i); + } + + av_tree_destroy(root); + + return 0; +} diff --git a/libavutil/tree.c b/libavutil/tree.c index 998851f7f2..a65d728f86 100644 --- a/libavutil/tree.c +++ b/libavutil/tree.c @@ -19,7 +19,6 @@ */ #include "error.h" -#include "log.h" #include "mem.h" #include "tree.h" @@ -164,94 +163,3 @@ void av_tree_enumerate(AVTreeNode *t, void *opaque, av_tree_enumerate(t->child[1], opaque, cmp, enu); } } - -#ifdef TEST - -#include "common.h" -#include "lfg.h" - -static int check(AVTreeNode *t) -{ - if (t) { - int left = check(t->child[0]); - int right = check(t->child[1]); - - if (left > 999 || right > 999) - return 1000; - if (right - left != t->state) - return 1000; - if (t->state > 1 || t->state < -1) - return 1000; - return FFMAX(left, right) + 1; - } - return 0; -} - -static void print(AVTreeNode *t, int depth) -{ - int i; - for (i = 0; i < depth * 4; i++) - av_log(NULL, AV_LOG_ERROR, " "); - if (t) { - av_log(NULL, AV_LOG_ERROR, "Node %p %2d %p\n", t, t->state, t->elem); - print(t->child[0], depth + 1); - print(t->child[1], depth + 1); - } else - av_log(NULL, AV_LOG_ERROR, "NULL\n"); -} - -static int cmp(void *a, const void *b) -{ - return (uint8_t *) a - (const uint8_t *) b; -} - -int main(void) -{ - int i; - AVTreeNode *root = NULL, *node = NULL; - AVLFG prng; - - av_lfg_init(&prng, 1); - - for (i = 0; i < 10000; i++) { - AVTreeNode *node2 = NULL; - intptr_t j = av_lfg_get(&prng) % 86294; - void *ret, *jj = (void *)(j + 1); - - while (ret = av_tree_find(root, jj, cmp, NULL)) { - j = av_lfg_get(&prng) % 86294; - jj = (void *)(j + 1); - } - - if (check(root) > 999) { - av_log(NULL, AV_LOG_ERROR, "FATAL error %d\n", i); - print(root, 0); - return 1; - } - - if (!node) - node = av_tree_node_alloc(); - if (!node) { - av_log(NULL, AV_LOG_ERROR, "Memory allocation failure.\n"); - return 1; - } - av_tree_insert(&root, jj, cmp, &node); - - while (ret = av_tree_find(root, jj, cmp, NULL)) { - j = av_lfg_get(&prng) % 86294; - jj = (void *)(j + 1); - } - - ret = av_tree_insert(&root, jj, cmp, &node2); - if (ret != jj) - av_tree_destroy(node2); - ret = av_tree_find(root, jj, cmp, NULL); - if (ret) - av_log(NULL, AV_LOG_ERROR, "removal failure %d\n", i); - } - - av_tree_destroy(root); - - return 0; -} -#endif diff --git a/libavutil/xtea-test.c b/libavutil/xtea-test.c new file mode 100644 index 0000000000..f81cfdc3e8 --- /dev/null +++ b/libavutil/xtea-test.c @@ -0,0 +1,120 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "intreadwrite.h" +#include "xtea.h" + +#define XTEA_NUM_TESTS 6 + +static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = { + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } +}; + +static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = { + { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, + { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } +}; + +static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = { + { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, + { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, + { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, + { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, + { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } +}; + +static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, + const uint8_t *ref, int len, uint8_t *iv, int dir, + const char *test, + void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int)) +{ + crypt(ctx, dst, src, len, iv, dir); + if (memcmp(dst, ref, 8*len)) { + int i; + printf("%s failed\ngot ", test); + for (i = 0; i < 8*len; i++) + printf("%02x ", dst[i]); + printf("\nexpected "); + for (i = 0; i < 8*len; i++) + printf("%02x ", ref[i]); + printf("\n"); + exit(1); + } +} + +int main(void) +{ + AVXTEA ctx; + uint8_t buf[16], iv[8]; + int i, j; + const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld"; + uint8_t ct[32]; + uint8_t pl[32]; + + for (i = 0; i < XTEA_NUM_TESTS; i++) { + av_xtea_init(&ctx, xtea_test_key[i]); + + test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt); + test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt); + + for (j = 0; j < 4; j++) + AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j])); + av_xtea_le_init(&ctx, buf); + for (j = 0; j < 2; j++) { + AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j])); + AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j])); + } + test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt); + test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt); + + /* encrypt */ + memcpy(iv, "HALLO123", 8); + av_xtea_crypt(&ctx, ct, src, 4, iv, 0); + + /* decrypt into pl */ + memcpy(iv, "HALLO123", 8); + test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt); + + memcpy(iv, "HALLO123", 8); + test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt); + } + printf("Test encryption/decryption success.\n"); + + return 0; +} diff --git a/libavutil/xtea.c b/libavutil/xtea.c index 25fd2f07b1..3e222365b3 100644 --- a/libavutil/xtea.c +++ b/libavutil/xtea.c @@ -173,103 +173,3 @@ void av_xtea_le_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count, { xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_le_crypt_ecb); } - -#ifdef TEST -#include <stdio.h> - -#define XTEA_NUM_TESTS 6 - -static const uint8_t xtea_test_key[XTEA_NUM_TESTS][16] = { - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, - { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, - { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } -}; - -static const uint8_t xtea_test_pt[XTEA_NUM_TESTS][8] = { - { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, - { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, - { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, - { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, - { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, - { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } -}; - -static const uint8_t xtea_test_ct[XTEA_NUM_TESTS][8] = { - { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, - { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, - { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, - { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, - { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, - { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } -}; - -static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, - const uint8_t *ref, int len, uint8_t *iv, int dir, - const char *test, - void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *, int)) -{ - crypt(ctx, dst, src, len, iv, dir); - if (memcmp(dst, ref, 8*len)) { - int i; - printf("%s failed\ngot ", test); - for (i = 0; i < 8*len; i++) - printf("%02x ", dst[i]); - printf("\nexpected "); - for (i = 0; i < 8*len; i++) - printf("%02x ", ref[i]); - printf("\n"); - exit(1); - } -} - -int main(void) -{ - AVXTEA ctx; - uint8_t buf[16], iv[8]; - int i, j; - const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld"; - uint8_t ct[32]; - uint8_t pl[32]; - - for (i = 0; i < XTEA_NUM_TESTS; i++) { - av_xtea_init(&ctx, xtea_test_key[i]); - - test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption", av_xtea_crypt); - test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption", av_xtea_crypt); - - for (j = 0; j < 4; j++) - AV_WL32(&buf[4*j], AV_RB32(&xtea_test_key[i][4*j])); - av_xtea_le_init(&ctx, buf); - for (j = 0; j < 2; j++) { - AV_WL32(&ct[4*j], AV_RB32(&xtea_test_ct[i][4*j])); - AV_WL32(&pl[4*j], AV_RB32(&xtea_test_pt[i][4*j])); - } - test_xtea(&ctx, buf, pl, ct, 1, NULL, 0, "encryption", av_xtea_le_crypt); - test_xtea(&ctx, buf, ct, pl, 1, NULL, 1, "decryption", av_xtea_le_crypt); - - /* encrypt */ - memcpy(iv, "HALLO123", 8); - av_xtea_crypt(&ctx, ct, src, 4, iv, 0); - - /* decrypt into pl */ - memcpy(iv, "HALLO123", 8); - test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption", av_xtea_crypt); - - memcpy(iv, "HALLO123", 8); - test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption", av_xtea_crypt); - } - printf("Test encryption/decryption success.\n"); - - return 0; -} - -#endif diff --git a/library.mak b/library.mak index 3da14b6abb..c980c3dbe3 100644 --- a/library.mak +++ b/library.mak @@ -10,18 +10,6 @@ INSTHEADERS := $(INSTHEADERS) $(HEADERS:%=$(SUBDIR)%) all-$(CONFIG_STATIC): $(SUBDIR)$(LIBNAME) all-$(CONFIG_SHARED): $(SUBDIR)$(SLIBNAME) -$(SUBDIR)%-test.o: $(SUBDIR)%-test.c - $(COMPILE_C) - -$(SUBDIR)%-test.o: $(SUBDIR)%.c - $(COMPILE_C) - -$(SUBDIR)%-test.i: $(SUBDIR)%-test.c - $(CC) $(CCFLAGS) $(CC_E) $< - -$(SUBDIR)%-test.i: $(SUBDIR)%.c - $(CC) $(CCFLAGS) $(CC_E) $< - $(SUBDIR)x86/%.o: $(SUBDIR)x86/%.asm $(DEPYASM) $(YASMFLAGS) -I $(<D)/ -M -o $@ $< > $(@:.o=.d) $(YASM) $(YASMFLAGS) -I $(<D)/ -o $@ $< @@ -29,7 +17,6 @@ $(SUBDIR)x86/%.o: $(SUBDIR)x86/%.asm LIBOBJS := $(OBJS) $(SUBDIR)%.h.o $(TESTOBJS) $(LIBOBJS) $(LIBOBJS:.o=.i): CPPFLAGS += -DHAVE_AV_CONFIG_H -$(TESTOBJS) $(TESTOBJS:.o=.i): CPPFLAGS += -DTEST $(SUBDIR)$(LIBNAME): $(OBJS) $(RM) $@ |