1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2004-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "FLAC/assert.h"
25 #include "FLAC/format.h"
26 #include "format.h"
27 #include <stdio.h>
28
29 static const char *true_false_string_[2] = { "false", "true" };
30
31 static struct {
32 uint32_t rate;
33 FLAC__bool valid;
34 FLAC__bool subset;
35 } SAMPLE_RATES[] = {
36 { 0 , false, false },
37 { 1 , true , true },
38 { 9 , true , true },
39 { 10 , true , true },
40 { 4000 , true , true },
41 { 8000 , true , true },
42 { 11025 , true , true },
43 { 12000 , true , true },
44 { 16000 , true , true },
45 { 22050 , true , true },
46 { 24000 , true , true },
47 { 32000 , true , true },
48 { 32768 , true , true },
49 { 44100 , true , true },
50 { 48000 , true , true },
51 { 65000 , true , true },
52 { 65535 , true , true },
53 { 65536 , true , false },
54 { 65540 , true , true },
55 { 65550 , true , true },
56 { 65555 , true , false },
57 { 66000 , true , true },
58 { 66001 , true , false },
59 { 96000 , true , true },
60 { 100000 , true , true },
61 { 100001 , true , false },
62 { 192000 , true , true },
63 { 500000 , true , true },
64 { 500001 , true , false },
65 { 500010 , true , true },
66 { 655349 , true , false },
67 { 655350 , true , true },
68 { 655351 , false, false },
69 { 655360 , false, false },
70 { 700000 , false, false },
71 { 700010 , false, false },
72 { 1000000, false, false },
73 { 1100000, false, false }
74 };
75
76 static struct {
77 const char *string;
78 FLAC__bool valid;
79 } VCENTRY_NAMES[] = {
80 { "" , true },
81 { "a" , true },
82 { "=" , false },
83 { "a=" , false },
84 { "\x01", false },
85 { "\x1f", false },
86 { "\x7d", true },
87 { "\x7e", false },
88 { "\xff", false }
89 };
90
91 static struct {
92 uint32_t length;
93 const FLAC__byte *string;
94 FLAC__bool valid;
95 } VCENTRY_VALUES[] = {
96 { 0, (const FLAC__byte*)"" , true },
97 { 1, (const FLAC__byte*)"" , true },
98 { 1, (const FLAC__byte*)"\x01" , true },
99 { 1, (const FLAC__byte*)"\x7f" , true },
100 { 1, (const FLAC__byte*)"\x80" , false },
101 { 1, (const FLAC__byte*)"\x81" , false },
102 { 1, (const FLAC__byte*)"\xc0" , false },
103 { 1, (const FLAC__byte*)"\xe0" , false },
104 { 1, (const FLAC__byte*)"\xf0" , false },
105 { 2, (const FLAC__byte*)"\xc0\x41" , false },
106 { 2, (const FLAC__byte*)"\xc1\x41" , false },
107 { 2, (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */
108 { 2, (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */
109 { 2, (const FLAC__byte*)"\xc2\x85" , true },
110 { 2, (const FLAC__byte*)"\xe0\x41" , false },
111 { 2, (const FLAC__byte*)"\xe1\x41" , false },
112 { 2, (const FLAC__byte*)"\xe0\x85" , false },
113 { 2, (const FLAC__byte*)"\xe1\x85" , false },
114 { 3, (const FLAC__byte*)"\xe0\x85\x41", false },
115 { 3, (const FLAC__byte*)"\xe1\x85\x41", false },
116 { 3, (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
117 { 3, (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
118 { 3, (const FLAC__byte*)"\xe0\xa5\x80", true },
119 { 3, (const FLAC__byte*)"\xe1\x85\x80", true },
120 { 3, (const FLAC__byte*)"\xe1\x95\x80", true },
121 { 3, (const FLAC__byte*)"\xe1\xa5\x80", true }
122 };
123
124 static struct {
125 const FLAC__byte *string;
126 FLAC__bool valid;
127 } VCENTRY_VALUES_NT[] = {
128 { (const FLAC__byte*)"" , true },
129 { (const FLAC__byte*)"\x01" , true },
130 { (const FLAC__byte*)"\x7f" , true },
131 { (const FLAC__byte*)"\x80" , false },
132 { (const FLAC__byte*)"\x81" , false },
133 { (const FLAC__byte*)"\xc0" , false },
134 { (const FLAC__byte*)"\xe0" , false },
135 { (const FLAC__byte*)"\xf0" , false },
136 { (const FLAC__byte*)"\xc0\x41" , false },
137 { (const FLAC__byte*)"\xc1\x41" , false },
138 { (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */
139 { (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */
140 { (const FLAC__byte*)"\xc2\x85" , true },
141 { (const FLAC__byte*)"\xe0\x41" , false },
142 { (const FLAC__byte*)"\xe1\x41" , false },
143 { (const FLAC__byte*)"\xe0\x85" , false },
144 { (const FLAC__byte*)"\xe1\x85" , false },
145 { (const FLAC__byte*)"\xe0\x85\x41", false },
146 { (const FLAC__byte*)"\xe1\x85\x41", false },
147 { (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
148 { (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
149 { (const FLAC__byte*)"\xe0\xa5\x80", true },
150 { (const FLAC__byte*)"\xe1\x85\x80", true },
151 { (const FLAC__byte*)"\xe1\x95\x80", true },
152 { (const FLAC__byte*)"\xe1\xa5\x80", true }
153 };
154
155 static struct {
156 uint32_t length;
157 const FLAC__byte *string;
158 FLAC__bool valid;
159 } VCENTRIES[] = {
160 { 0, (const FLAC__byte*)"" , false },
161 { 1, (const FLAC__byte*)"a" , false },
162 { 1, (const FLAC__byte*)"=" , true },
163 { 2, (const FLAC__byte*)"a=" , true },
164 { 2, (const FLAC__byte*)"\x01=" , false },
165 { 2, (const FLAC__byte*)"\x1f=" , false },
166 { 2, (const FLAC__byte*)"\x7d=" , true },
167 { 2, (const FLAC__byte*)"\x7e=" , false },
168 { 2, (const FLAC__byte*)"\xff=" , false },
169 { 3, (const FLAC__byte*)"a=\x01" , true },
170 { 3, (const FLAC__byte*)"a=\x7f" , true },
171 { 3, (const FLAC__byte*)"a=\x80" , false },
172 { 3, (const FLAC__byte*)"a=\x81" , false },
173 { 3, (const FLAC__byte*)"a=\xc0" , false },
174 { 3, (const FLAC__byte*)"a=\xe0" , false },
175 { 3, (const FLAC__byte*)"a=\xf0" , false },
176 { 4, (const FLAC__byte*)"a=\xc0\x41" , false },
177 { 4, (const FLAC__byte*)"a=\xc1\x41" , false },
178 { 4, (const FLAC__byte*)"a=\xc0\x85" , false }, /* non-shortest form */
179 { 4, (const FLAC__byte*)"a=\xc1\x85" , false }, /* non-shortest form */
180 { 4, (const FLAC__byte*)"a=\xc2\x85" , true },
181 { 4, (const FLAC__byte*)"a=\xe0\x41" , false },
182 { 4, (const FLAC__byte*)"a=\xe1\x41" , false },
183 { 4, (const FLAC__byte*)"a=\xe0\x85" , false },
184 { 4, (const FLAC__byte*)"a=\xe1\x85" , false },
185 { 5, (const FLAC__byte*)"a=\xe0\x85\x41", false },
186 { 5, (const FLAC__byte*)"a=\xe1\x85\x41", false },
187 { 5, (const FLAC__byte*)"a=\xe0\x85\x80", false }, /* non-shortest form */
188 { 5, (const FLAC__byte*)"a=\xe0\x95\x80", false }, /* non-shortest form */
189 { 5, (const FLAC__byte*)"a=\xe0\xa5\x80", true },
190 { 5, (const FLAC__byte*)"a=\xe1\x85\x80", true },
191 { 5, (const FLAC__byte*)"a=\xe1\x95\x80", true },
192 { 5, (const FLAC__byte*)"a=\xe1\xa5\x80", true }
193 };
194
test_format(void)195 FLAC__bool test_format(void)
196 {
197 uint32_t i;
198
199 printf("\n+++ libFLAC unit test: format\n\n");
200
201 for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
202 printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate);
203 if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) {
204 printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]);
205 return false;
206 }
207 printf("OK\n");
208 }
209
210 for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
211 printf("testing FLAC__format_sample_rate_is_subset(%u)... ", SAMPLE_RATES[i].rate);
212 if(FLAC__format_sample_rate_is_subset(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].subset) {
213 printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].subset], true_false_string_[!SAMPLE_RATES[i].subset]);
214 return false;
215 }
216 printf("OK\n");
217 }
218
219 for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) {
220 printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string);
221 if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) {
222 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]);
223 return false;
224 }
225 printf("OK\n");
226 }
227
228 for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) {
229 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length);
230 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) {
231 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]);
232 return false;
233 }
234 printf("OK\n");
235 }
236
237 for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) {
238 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string);
239 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (uint32_t)(-1)) != VCENTRY_VALUES_NT[i].valid) {
240 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]);
241 return false;
242 }
243 printf("OK\n");
244 }
245
246 for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) {
247 printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length);
248 if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) {
249 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]);
250 return false;
251 }
252 printf("OK\n");
253 }
254
255 printf("\nPASSED!\n");
256 return true;
257 }
258