1 /* Copyright (c) 2023 Amazon
2 Written by Michael Klingbeil */
3 /*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #ifndef _WIN32
37 #include <unistd.h>
38 #else
39 #include <process.h>
40 #define getpid _getpid
41 #endif
42
43 /* including sources directly to test internal APIs */
44 #define CELT_C /* to make celt_assert work */
45 #include "opus.h"
46 #include "test_opus_common.h"
47
48
49
50 #define NB_RANDOM_EXTENSIONS 10000000
51 #define MAX_EXTENSION_SIZE 200
52 #define MAX_NB_EXTENSIONS 100
53
test_random_dred(void)54 void test_random_dred(void)
55 {
56 int error;
57 int i;
58 OpusDREDDecoder *dred_dec;
59 OpusDRED *dred;
60 dred_dec = opus_dred_decoder_create(&error);
61 expect_true(error == OPUS_OK, "opus_dred_decoder_create() failed");
62 dred = opus_dred_alloc(&error);
63 expect_true(error == OPUS_OK, "opus_dred_create() failed");
64 for (i=0;i<NB_RANDOM_EXTENSIONS;i++)
65 {
66 unsigned char payload[MAX_EXTENSION_SIZE];
67 int len;
68 int j;
69 int res1, res2;
70 int dred_end;
71 len = fast_rand()%(MAX_EXTENSION_SIZE+1);
72 for (j=0;j<len;j++)
73 payload[j] = fast_rand()&0xFF;
74 res1 = opus_dred_parse(dred_dec, dred, payload, len, 48000, 48000, &dred_end, fast_rand()&0x1);
75 if (res1 > 0)
76 {
77 res2 = opus_dred_process(dred_dec, dred, dred);
78 expect_true(res2 == OPUS_OK, "process should succeed if parse succeeds");
79 expect_true(res1 >= dred_end, "end before beginning");
80 }
81 }
82 opus_dred_free(dred);
83 opus_dred_decoder_destroy(dred_dec);
84 }
85
main(int argc,char ** argv)86 int main(int argc, char **argv)
87 {
88 int env_used;
89 char *env_seed;
90 env_used=0;
91 env_seed=getenv("SEED");
92 if(argc>1)iseed=atoi(argv[1]);
93 else if(env_seed)
94 {
95 iseed=atoi(env_seed);
96 env_used=1;
97 }
98 else iseed=(opus_uint32)time(NULL)^(((opus_uint32)getpid()&65535)<<16);
99 Rw=Rz=iseed;
100
101 fprintf(stderr,"Testing dred. Random seed: %u (%.4X)\n", iseed, fast_rand() % 65535);
102 if(env_used)fprintf(stderr," Random seed set from the environment (SEED=%s).\n", env_seed);
103
104 test_random_dred();
105 fprintf(stderr,"Tests completed successfully.\n");
106 return 0;
107 }
108