1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2004-2009 Josh Coalson
3 * Copyright (C) 2011-2023 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 , true , true },
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 , true , false },
69 { 655360 , true , false },
70 { 700000 , true , false },
71 { 700010 , true , false },
72 { 705600 , true , false },
73 { 768000 , true , false },
74 { 1000000, true , false },
75 { 1048575, true , false },
76 { 1100000, false, false }
77 };
78
79 static struct {
80 const char *string;
81 FLAC__bool valid;
82 } VCENTRY_NAMES[] = {
83 { "" , true },
84 { "a" , true },
85 { "=" , false },
86 { "a=" , false },
87 { "\x01", false },
88 { "\x1f", false },
89 { "\x7d", true },
90 { "\x7e", false },
91 { "\xff", false }
92 };
93
94 static struct {
95 uint32_t length;
96 const FLAC__byte *string;
97 FLAC__bool valid;
98 } VCENTRY_VALUES[] = {
99 { 0, (const FLAC__byte*)"" , true },
100 { 1, (const FLAC__byte*)"" , true },
101 { 1, (const FLAC__byte*)"\x01" , true },
102 { 1, (const FLAC__byte*)"\x7f" , true },
103 { 1, (const FLAC__byte*)"\x80" , false },
104 { 1, (const FLAC__byte*)"\x81" , false },
105 { 1, (const FLAC__byte*)"\xc0" , false },
106 { 1, (const FLAC__byte*)"\xe0" , false },
107 { 1, (const FLAC__byte*)"\xf0" , false },
108 { 2, (const FLAC__byte*)"\xc0\x41" , false },
109 { 2, (const FLAC__byte*)"\xc1\x41" , false },
110 { 2, (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */
111 { 2, (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */
112 { 2, (const FLAC__byte*)"\xc2\x85" , true },
113 { 2, (const FLAC__byte*)"\xe0\x41" , false },
114 { 2, (const FLAC__byte*)"\xe1\x41" , false },
115 { 2, (const FLAC__byte*)"\xe0\x85" , false },
116 { 2, (const FLAC__byte*)"\xe1\x85" , false },
117 { 3, (const FLAC__byte*)"\xe0\x85\x41", false },
118 { 3, (const FLAC__byte*)"\xe1\x85\x41", false },
119 { 3, (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
120 { 3, (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
121 { 3, (const FLAC__byte*)"\xe0\xa5\x80", true },
122 { 3, (const FLAC__byte*)"\xe1\x85\x80", true },
123 { 3, (const FLAC__byte*)"\xe1\x95\x80", true },
124 { 3, (const FLAC__byte*)"\xe1\xa5\x80", true }
125 };
126
127 static struct {
128 const FLAC__byte *string;
129 FLAC__bool valid;
130 } VCENTRY_VALUES_NT[] = {
131 { (const FLAC__byte*)"" , true },
132 { (const FLAC__byte*)"\x01" , true },
133 { (const FLAC__byte*)"\x7f" , true },
134 { (const FLAC__byte*)"\x80" , false },
135 { (const FLAC__byte*)"\x81" , false },
136 { (const FLAC__byte*)"\xc0" , false },
137 { (const FLAC__byte*)"\xe0" , false },
138 { (const FLAC__byte*)"\xf0" , false },
139 { (const FLAC__byte*)"\xc0\x41" , false },
140 { (const FLAC__byte*)"\xc1\x41" , false },
141 { (const FLAC__byte*)"\xc0\x85" , false }, /* non-shortest form */
142 { (const FLAC__byte*)"\xc1\x85" , false }, /* non-shortest form */
143 { (const FLAC__byte*)"\xc2\x85" , true },
144 { (const FLAC__byte*)"\xe0\x41" , false },
145 { (const FLAC__byte*)"\xe1\x41" , false },
146 { (const FLAC__byte*)"\xe0\x85" , false },
147 { (const FLAC__byte*)"\xe1\x85" , false },
148 { (const FLAC__byte*)"\xe0\x85\x41", false },
149 { (const FLAC__byte*)"\xe1\x85\x41", false },
150 { (const FLAC__byte*)"\xe0\x85\x80", false }, /* non-shortest form */
151 { (const FLAC__byte*)"\xe0\x95\x80", false }, /* non-shortest form */
152 { (const FLAC__byte*)"\xe0\xa5\x80", true },
153 { (const FLAC__byte*)"\xe1\x85\x80", true },
154 { (const FLAC__byte*)"\xe1\x95\x80", true },
155 { (const FLAC__byte*)"\xe1\xa5\x80", true }
156 };
157
158 static struct {
159 uint32_t length;
160 const FLAC__byte *string;
161 FLAC__bool valid;
162 } VCENTRIES[] = {
163 { 0, (const FLAC__byte*)"" , false },
164 { 1, (const FLAC__byte*)"a" , false },
165 { 1, (const FLAC__byte*)"=" , true },
166 { 2, (const FLAC__byte*)"a=" , true },
167 { 2, (const FLAC__byte*)"\x01=" , false },
168 { 2, (const FLAC__byte*)"\x1f=" , false },
169 { 2, (const FLAC__byte*)"\x7d=" , true },
170 { 2, (const FLAC__byte*)"\x7e=" , false },
171 { 2, (const FLAC__byte*)"\xff=" , false },
172 { 3, (const FLAC__byte*)"a=\x01" , true },
173 { 3, (const FLAC__byte*)"a=\x7f" , true },
174 { 3, (const FLAC__byte*)"a=\x80" , false },
175 { 3, (const FLAC__byte*)"a=\x81" , false },
176 { 3, (const FLAC__byte*)"a=\xc0" , false },
177 { 3, (const FLAC__byte*)"a=\xe0" , false },
178 { 3, (const FLAC__byte*)"a=\xf0" , false },
179 { 4, (const FLAC__byte*)"a=\xc0\x41" , false },
180 { 4, (const FLAC__byte*)"a=\xc1\x41" , false },
181 { 4, (const FLAC__byte*)"a=\xc0\x85" , false }, /* non-shortest form */
182 { 4, (const FLAC__byte*)"a=\xc1\x85" , false }, /* non-shortest form */
183 { 4, (const FLAC__byte*)"a=\xc2\x85" , true },
184 { 4, (const FLAC__byte*)"a=\xe0\x41" , false },
185 { 4, (const FLAC__byte*)"a=\xe1\x41" , false },
186 { 4, (const FLAC__byte*)"a=\xe0\x85" , false },
187 { 4, (const FLAC__byte*)"a=\xe1\x85" , false },
188 { 5, (const FLAC__byte*)"a=\xe0\x85\x41", false },
189 { 5, (const FLAC__byte*)"a=\xe1\x85\x41", false },
190 { 5, (const FLAC__byte*)"a=\xe0\x85\x80", false }, /* non-shortest form */
191 { 5, (const FLAC__byte*)"a=\xe0\x95\x80", false }, /* non-shortest form */
192 { 5, (const FLAC__byte*)"a=\xe0\xa5\x80", true },
193 { 5, (const FLAC__byte*)"a=\xe1\x85\x80", true },
194 { 5, (const FLAC__byte*)"a=\xe1\x95\x80", true },
195 { 5, (const FLAC__byte*)"a=\xe1\xa5\x80", true }
196 };
197
test_format(void)198 FLAC__bool test_format(void)
199 {
200 uint32_t i;
201
202 printf("\n+++ libFLAC unit test: format\n\n");
203
204 for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
205 printf("testing FLAC__format_sample_rate_is_valid(%u)... ", SAMPLE_RATES[i].rate);
206 if(FLAC__format_sample_rate_is_valid(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].valid) {
207 printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].valid], true_false_string_[!SAMPLE_RATES[i].valid]);
208 return false;
209 }
210 printf("OK\n");
211 }
212
213 for(i = 0; i < sizeof(SAMPLE_RATES)/sizeof(SAMPLE_RATES[0]); i++) {
214 printf("testing FLAC__format_sample_rate_is_subset(%u)... ", SAMPLE_RATES[i].rate);
215 if(FLAC__format_sample_rate_is_subset(SAMPLE_RATES[i].rate) != SAMPLE_RATES[i].subset) {
216 printf("FAILED, expected %s, got %s\n", true_false_string_[SAMPLE_RATES[i].subset], true_false_string_[!SAMPLE_RATES[i].subset]);
217 return false;
218 }
219 printf("OK\n");
220 }
221
222 for(i = 0; i < sizeof(VCENTRY_NAMES)/sizeof(VCENTRY_NAMES[0]); i++) {
223 printf("testing FLAC__format_vorbiscomment_entry_name_is_legal(\"%s\")... ", VCENTRY_NAMES[i].string);
224 if(FLAC__format_vorbiscomment_entry_name_is_legal(VCENTRY_NAMES[i].string) != VCENTRY_NAMES[i].valid) {
225 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_NAMES[i].valid], true_false_string_[!VCENTRY_NAMES[i].valid]);
226 return false;
227 }
228 printf("OK\n");
229 }
230
231 for(i = 0; i < sizeof(VCENTRY_VALUES)/sizeof(VCENTRY_VALUES[0]); i++) {
232 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", %u)... ", VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length);
233 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES[i].string, VCENTRY_VALUES[i].length) != VCENTRY_VALUES[i].valid) {
234 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES[i].valid], true_false_string_[!VCENTRY_VALUES[i].valid]);
235 return false;
236 }
237 printf("OK\n");
238 }
239
240 for(i = 0; i < sizeof(VCENTRY_VALUES_NT)/sizeof(VCENTRY_VALUES_NT[0]); i++) {
241 printf("testing FLAC__format_vorbiscomment_entry_value_is_legal(\"%s\", -1)... ", VCENTRY_VALUES_NT[i].string);
242 if(FLAC__format_vorbiscomment_entry_value_is_legal(VCENTRY_VALUES_NT[i].string, (uint32_t)(-1)) != VCENTRY_VALUES_NT[i].valid) {
243 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRY_VALUES_NT[i].valid], true_false_string_[!VCENTRY_VALUES_NT[i].valid]);
244 return false;
245 }
246 printf("OK\n");
247 }
248
249 for(i = 0; i < sizeof(VCENTRIES)/sizeof(VCENTRIES[0]); i++) {
250 printf("testing FLAC__format_vorbiscomment_entry_is_legal(\"%s\", %u)... ", VCENTRIES[i].string, VCENTRIES[i].length);
251 if(FLAC__format_vorbiscomment_entry_is_legal(VCENTRIES[i].string, VCENTRIES[i].length) != VCENTRIES[i].valid) {
252 printf("FAILED, expected %s, got %s\n", true_false_string_[VCENTRIES[i].valid], true_false_string_[!VCENTRIES[i].valid]);
253 return false;
254 }
255 printf("OK\n");
256 }
257
258 printf("\nPASSED!\n");
259 return true;
260 }
261