1 //  This file is part of par2cmdline (a PAR 2.0 compatible file verification and
2 //  repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
3 //
4 //  Copyright (c) 2019 Michael D. Nahas
5 //
6 //  par2cmdline is free software; you can redistribute it and/or modify
7 //  it under the terms of the GNU General Public License as published by
8 //  the Free Software Foundation; either version 2 of the License, or
9 //  (at your option) any later version.
10 //
11 //  par2cmdline is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 
20 
21 #include <iostream>
22 #include <fstream>
23 #include <stdlib.h>
24 
25 using namespace std;
26 
27 #include "libpar2.h"
28 
29 
30 // ComputeRecoveryFileCount
31 // check when it returns false.
test1()32 int test1() {
33   u32 recoveryfilecount = 0;
34   bool success = ComputeRecoveryFileCount(cout, cerr,
35 					  &recoveryfilecount,
36 						       scUnknown,
37 						       1,
38 						       1,
39 						       1);
40   if (success) {
41     cerr << "ComputeRecoveryFileCount worked for unknown scheme" << endl;
42     return 1;
43   }
44 
45   recoveryfilecount = 10;
46   success = ComputeRecoveryFileCount(cout, cerr,
47 					  &recoveryfilecount,
48 						       scVariable,
49 						       4,
50 						       4,
51 						       4);
52   if (success) {
53     cerr << "ComputeRecoveryFileCount worked with more files than blocks!" << endl;
54     return 1;
55   }
56 
57   return 0;
58 }
59 
60 
61 // ComputeRecoveryFileCount
62 // scVariable
test2()63 int test2() {
64   u32 recoveryfilecount = 0;
65   bool success = ComputeRecoveryFileCount(cout, cerr,
66 					  &recoveryfilecount,
67 						       scVariable,
68 						       0,
69 						       4,
70 						       4);
71   if (!success) {
72     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
73     return 1;
74   }
75   if (recoveryfilecount != 0) {
76     cerr << "ComputeRecoveryFileCount for 0 blocks should return 0" << endl;
77     cerr << "   it returned " << recoveryfilecount << endl;
78     return 1;
79   }
80 
81   recoveryfilecount = 0;
82   success = ComputeRecoveryFileCount(cout, cerr,
83 					  &recoveryfilecount,
84 						       scVariable,
85 						       8,
86 						       4,
87 						       4);
88 
89   if (!success) {
90     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
91     return 1;
92   }
93   if (recoveryfilecount != 4) {
94     cerr << "ComputeRecoveryFileCount for 8 blocks should return 4" << endl;
95     cerr << "   it returned " << recoveryfilecount << endl;
96     return 1;
97   }
98 
99   recoveryfilecount = 0;
100   success = ComputeRecoveryFileCount(cout, cerr,
101 					  &recoveryfilecount,
102 						       scVariable,
103 						       15,
104 						       4,
105 						       4);
106 
107   if (!success) {
108     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
109     return 1;
110   }
111   if (recoveryfilecount != 4) {
112     cerr << "ComputeRecoveryFileCount for 15 blocks should return 4" << endl;
113     cerr << "   it returned " << recoveryfilecount << endl;
114     return 1;
115   }
116 
117 
118   recoveryfilecount = 0;
119   success = ComputeRecoveryFileCount(cout, cerr,
120 					  &recoveryfilecount,
121 						       scVariable,
122 						       64,
123 						       4,
124 						       4);
125 
126   if (!success) {
127     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
128     return 1;
129   }
130   if (recoveryfilecount != 7) {
131     cerr << "ComputeRecoveryFileCount for 64 blocks should return 7" << endl;
132     cerr << "   it returned " << recoveryfilecount << endl;
133     return 1;
134   }
135 
136   recoveryfilecount = 0;
137   success = ComputeRecoveryFileCount(cout, cerr,
138 					  &recoveryfilecount,
139 						       scVariable,
140 						       127,
141 						       4,
142 						       4);
143 
144   if (!success) {
145     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
146     return 1;
147   }
148   if (recoveryfilecount != 7) {
149     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
150     cerr << "   it returned " << recoveryfilecount << endl;
151     return 1;
152   }
153 
154   return 0;
155 }
156 
157 // ComputeRecoveryFileCount
158 // scUniform
159 // Doesn't matter the value - long as it's zero at zero and positive after.
test3()160 int test3() {
161   u32 recoveryfilecount = 0;
162   bool success = ComputeRecoveryFileCount(cout, cerr,
163 					  &recoveryfilecount,
164 						       scUniform,
165 						       0,
166 						       4,
167 						       4);
168   if (!success) {
169     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
170     return 1;
171   }
172   if (recoveryfilecount != 0) {
173     cerr << "ComputeRecoveryFileCount for 0 blocks should return 0" << endl;
174     cerr << "   it returned " << recoveryfilecount << endl;
175     return 1;
176   }
177 
178   recoveryfilecount = 0;
179   success = ComputeRecoveryFileCount(cout, cerr,
180 					  &recoveryfilecount,
181 						       scUniform,
182 						       1,
183 						       4,
184 						       4);
185 
186   if (!success) {
187     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
188     return 1;
189   }
190   if (recoveryfilecount == 0) {
191     cerr << "ComputeRecoveryFileCount for 1 block should a positive value" << endl;
192     cerr << "   it returned " << recoveryfilecount << endl;
193     return 1;
194   }
195 
196 
197   recoveryfilecount = 0;
198   success = ComputeRecoveryFileCount(cout, cerr,
199 					  &recoveryfilecount,
200 						       scUniform,
201 						       8,
202 						       4,
203 						       4);
204 
205   if (!success) {
206     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
207     return 1;
208   }
209   if (recoveryfilecount == 0) {
210     cerr << "ComputeRecoveryFileCount for 8 blocks should a positive value" << endl;
211     cerr << "   it returned " << recoveryfilecount << endl;
212     return 1;
213   }
214 
215   return 0;
216 }
217 
218 
219 // ComputeRecoveryFileCount
220 // scLimited
221 // Same as variable with big files
222 // But differs for smaller ones.
test4()223 int test4() {
224   u32 recoveryfilecount = 0;
225   bool success = ComputeRecoveryFileCount(cout, cerr,
226 					  &recoveryfilecount,
227 						       scLimited,
228 						       0,
229 						       4096,
230 						       4);
231   if (!success) {
232     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
233     return 1;
234   }
235   if (recoveryfilecount != 0) {
236     cerr << "ComputeRecoveryFileCount for 0 blocks should return 0" << endl;
237     cerr << "   it returned " << recoveryfilecount << endl;
238     return 1;
239   }
240 
241   recoveryfilecount = 0;
242   success = ComputeRecoveryFileCount(cout, cerr,
243 					  &recoveryfilecount,
244 						       scLimited,
245 						       8,
246 						       4096,
247 						       4);
248 
249   if (!success) {
250     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
251     return 1;
252   }
253   if (recoveryfilecount != 4) {
254     cerr << "ComputeRecoveryFileCount for 8 blocks should return 4" << endl;
255     cerr << "   it returned " << recoveryfilecount << endl;
256     return 1;
257   }
258 
259   recoveryfilecount = 0;
260   success = ComputeRecoveryFileCount(cout, cerr,
261 					  &recoveryfilecount,
262 						       scLimited,
263 						       15,
264 						       4096,
265 						       4);
266 
267   if (!success) {
268     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
269     return 1;
270   }
271   if (recoveryfilecount != 4) {
272     cerr << "ComputeRecoveryFileCount for 15 blocks should return 4" << endl;
273     cerr << "   it returned " << recoveryfilecount << endl;
274     return 1;
275   }
276 
277 
278   recoveryfilecount = 0;
279   success = ComputeRecoveryFileCount(cout, cerr,
280 					  &recoveryfilecount,
281 						       scLimited,
282 						       64,
283 						       4096,
284 						       4);
285 
286   if (!success) {
287     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
288     return 1;
289   }
290   if (recoveryfilecount != 7) {
291     cerr << "ComputeRecoveryFileCount for 64 blocks should return 7" << endl;
292     cerr << "   it returned " << recoveryfilecount << endl;
293     return 1;
294   }
295 
296   recoveryfilecount = 0;
297   success = ComputeRecoveryFileCount(cout, cerr,
298 					  &recoveryfilecount,
299 						       scLimited,
300 						       127,
301 						       4096,
302 						       4);
303 
304   if (!success) {
305     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
306     return 1;
307   }
308   if (recoveryfilecount != 7) {
309     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
310     cerr << "   it returned " << recoveryfilecount << endl;
311     return 1;
312   }
313 
314 
315   // smaller largest files
316   // 1 2 4 8 10 10 10...
317   recoveryfilecount = 0;
318   success = ComputeRecoveryFileCount(cout, cerr,
319 					  &recoveryfilecount,
320 						       scLimited,
321 						       8,
322 						       40,
323 						       4);
324 
325   if (!success) {
326     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
327     return 1;
328   }
329   if (recoveryfilecount != 4) {
330     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
331     cerr << "   it returned " << recoveryfilecount << endl;
332     return 1;
333   }
334 
335   recoveryfilecount = 0;
336   success = ComputeRecoveryFileCount(cout, cerr,
337 					  &recoveryfilecount,
338 						       scLimited,
339 						       15,
340 						       40,
341 						       4);
342 
343   if (!success) {
344     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
345     return 1;
346   }
347   if (recoveryfilecount != 4) {
348     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
349     cerr << "   it returned " << recoveryfilecount << endl;
350     return 1;
351   }
352 
353   recoveryfilecount = 0;
354   success = ComputeRecoveryFileCount(cout, cerr,
355 					  &recoveryfilecount,
356 						       scLimited,
357 						       16,
358 						       40,
359 						       4);
360 
361   if (!success) {
362     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
363     return 1;
364   }
365   if (recoveryfilecount != 5) {
366     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
367     cerr << "   it returned " << recoveryfilecount << endl;
368     return 1;
369   }
370 
371   recoveryfilecount = 0;
372   success = ComputeRecoveryFileCount(cout, cerr,
373 					  &recoveryfilecount,
374 						       scLimited,
375 						       25,
376 						       40,
377 						       4);
378 
379   if (!success) {
380     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
381     return 1;
382   }
383   if (recoveryfilecount != 5) {
384     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
385     cerr << "   it returned " << recoveryfilecount << endl;
386     return 1;
387   }
388 
389   recoveryfilecount = 0;
390   success = ComputeRecoveryFileCount(cout, cerr,
391 					  &recoveryfilecount,
392 						       scLimited,
393 						       26,
394 						       40,
395 						       4);
396 
397   if (!success) {
398     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
399     return 1;
400   }
401   if (recoveryfilecount != 6) {
402     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
403     cerr << "   it returned " << recoveryfilecount << endl;
404     return 1;
405   }
406 
407   recoveryfilecount = 0;
408   success = ComputeRecoveryFileCount(cout, cerr,
409 					  &recoveryfilecount,
410 						       scLimited,
411 						       35,
412 						       40,
413 						       4);
414 
415   if (!success) {
416     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
417     return 1;
418   }
419   if (recoveryfilecount != 6) {
420     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
421     cerr << "   it returned " << recoveryfilecount << endl;
422     return 1;
423   }
424 
425 
426   recoveryfilecount = 0;
427   success = ComputeRecoveryFileCount(cout, cerr,
428 					  &recoveryfilecount,
429 						       scLimited,
430 						       35 + 100,
431 						       40,
432 						       4);
433 
434   if (!success) {
435     cerr << "ComputeRecoveryFileCount failed test2.1" << endl;
436     return 1;
437   }
438   if (recoveryfilecount != 6 + 10) {
439     cerr << "ComputeRecoveryFileCount for 127 blocks should return 7" << endl;
440     cerr << "   it returned " << recoveryfilecount << endl;
441     return 1;
442   }
443 
444 
445   return 0;
446 }
447 
448 
main()449 int main() {
450   if (test1()) {
451     cerr << "FAILED: test1" << endl;
452     return 1;
453   }
454   if (test2()) {
455     cerr << "FAILED: test2" << endl;
456     return 1;
457   }
458   if (test3()) {
459     cerr << "FAILED: test3" << endl;
460     return 1;
461   }
462   if (test4()) {
463     cerr << "FAILED: test4" << endl;
464     return 1;
465   }
466 
467   cout << "SUCCESS: libpar2_test complete." << endl;
468 
469   return 0;
470 }
471