1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
///////////////////////////////////////////////////////////////////////////////
//
/// \file test_info.c
/// \brief Tests functions handling the lzma_info structure
//
// Copyright (C) 2007 Lasse Collin
//
// This library 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.
//
// This library 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.
//
///////////////////////////////////////////////////////////////////////////////
#include "tests.h"
static lzma_info *info = NULL;
static lzma_info_iter iter;
static lzma_vli stream_start = 0;
static lzma_vli header_metadata_size = LZMA_VLI_VALUE_UNKNOWN;
static lzma_vli total_size = LZMA_VLI_VALUE_UNKNOWN;
static lzma_vli uncompressed_size = LZMA_VLI_VALUE_UNKNOWN;
static lzma_vli footer_metadata_size = LZMA_VLI_VALUE_UNKNOWN;
static lzma_index my_index[3] = {
{ 22, 33, my_index + 1 },
{ 44, 55, my_index + 2 },
{ 66, 77, NULL },
};
static lzma_metadata my_metadata = {
.header_metadata_size = 11,
.total_size = 22 + 44 + 66,
.uncompressed_size = 33 + 55 + 77,
.index = my_index,
.extra = NULL,
};
static void
reset(void)
{
expect(lzma_info_init(info, NULL) == info);
stream_start = 0;
header_metadata_size = LZMA_VLI_VALUE_UNKNOWN;
total_size = LZMA_VLI_VALUE_UNKNOWN;
uncompressed_size = LZMA_VLI_VALUE_UNKNOWN;
footer_metadata_size = LZMA_VLI_VALUE_UNKNOWN;
}
static void
validate(void)
{
expect(lzma_info_size_get(info, LZMA_INFO_STREAM_START)
== stream_start);
expect(lzma_info_size_get(info, LZMA_INFO_HEADER_METADATA)
== header_metadata_size);
expect(lzma_info_size_get(info, LZMA_INFO_TOTAL) == total_size);
expect(lzma_info_size_get(info, LZMA_INFO_UNCOMPRESSED)
== uncompressed_size);
expect(lzma_info_size_get(info, LZMA_INFO_FOOTER_METADATA)
== footer_metadata_size);
}
static void
test1(void)
{
// Basics
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START,
stream_start = 1234) == LZMA_OK);
validate();
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA,
header_metadata_size = 2345) == LZMA_OK);
validate();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, total_size = 3456)
== LZMA_OK);
validate();
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
uncompressed_size = 4567) == LZMA_OK);
validate();
expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA,
footer_metadata_size = 5432) == LZMA_OK);
validate();
// Not everything allow zero size
reset();
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START,
stream_start = 0) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA,
header_metadata_size = 0) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
uncompressed_size = 0) == LZMA_OK);
validate();
reset();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 0)
== LZMA_PROG_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA, 0)
== LZMA_PROG_ERROR);
// Invalid sizes
reset();
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START,
LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA,
LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL,
LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_FOOTER_METADATA,
LZMA_VLI_VALUE_MAX + 1) == LZMA_PROG_ERROR);
reset();
}
static bool
test2_helper(lzma_vli *num, lzma_info_size type)
{
expect(lzma_info_size_set(info, type, *num = 1234) == LZMA_OK);
validate();
const bool ret = lzma_info_size_set(info, type, 4321) != LZMA_OK;
reset();
return ret;
}
static void
test2(void)
{
// Excluding start offset of Stream, once a size has been set,
// trying to set some other known value fails.
expect(!test2_helper(&stream_start, LZMA_INFO_STREAM_START));
expect(test2_helper(&header_metadata_size, LZMA_INFO_HEADER_METADATA));
expect(test2_helper(&total_size, LZMA_INFO_TOTAL));
expect(test2_helper(&uncompressed_size, LZMA_INFO_UNCOMPRESSED));
expect(test2_helper(&footer_metadata_size, LZMA_INFO_FOOTER_METADATA));
}
static void
test3_init(void)
{
reset();
lzma_info_iter_begin(info, &iter);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
}
static void
test3(void)
{
// Setting the same sizes multiple times for the same Index Record
// is OK, but the values must always be the same.
test3_init();
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_set(&iter, 1111, 2345) == LZMA_DATA_ERROR);
// Cannot finish an empty Index.
test3_init();
expect(lzma_info_index_finish(info) == LZMA_DATA_ERROR);
test3_init();
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_index_finish(info) == LZMA_DATA_ERROR);
test3_init();
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_index_finish(info) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1111)
== LZMA_DATA_ERROR);
test3_init();
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_iter_set(&iter, 4321, 5432) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_index_finish(info) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234 + 4321)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345 + 5432)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 1111)
== LZMA_DATA_ERROR);
test3_init();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1234 + 4321)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2345 + 5432)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 1111)
== LZMA_DATA_ERROR);
expect(lzma_info_iter_set(&iter, 1234, 2345) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_iter_set(&iter, 4321, 5432) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_index_finish(info) == LZMA_OK);
test3_init();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, 1000) == LZMA_OK);
expect(lzma_info_iter_set(&iter, 1001, 2001) == LZMA_DATA_ERROR);
test3_init();
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED, 2000)
== LZMA_OK);
expect(lzma_info_iter_set(&iter, 1001, 2001) == LZMA_DATA_ERROR);
reset();
}
static void
test4(void)
{
// 4a
lzma_info_iter_begin(info, &iter);
expect(lzma_info_index_count_get(info) == 0);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK);
expect(iter.total_size == 22);
expect(iter.uncompressed_size == 33);
expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_offset == 33);
// 4b
reset();
lzma_info_iter_begin(info, &iter);
expect(lzma_info_index_count_get(info) == 0);
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11)
== LZMA_OK);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK);
expect(iter.total_size == 22);
expect(iter.uncompressed_size == 33);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22);
expect(iter.uncompressed_offset == 33);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_iter_set(&iter, 44, 55) == LZMA_OK);
expect(iter.total_size == 44);
expect(iter.uncompressed_size == 55);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22);
expect(iter.uncompressed_offset == 33);
expect(lzma_info_index_count_get(info) == 2);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44);
expect(iter.uncompressed_offset == 33 + 55);
expect(lzma_info_index_count_get(info) == 3);
expect(lzma_info_iter_set(&iter, 66, 77) == LZMA_OK);
expect(iter.total_size == 66);
expect(iter.uncompressed_size == 77);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44);
expect(iter.uncompressed_offset == 33 + 55);
expect(lzma_info_index_count_get(info) == 3);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66);
expect(iter.uncompressed_offset == 33 + 55 + 77);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_set(&iter, 88, 99) == LZMA_OK);
expect(iter.total_size == 88);
expect(iter.uncompressed_size == 99);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66);
expect(iter.uncompressed_offset == 33 + 55 + 77);
expect(lzma_info_index_count_get(info) == 4);
// 4c (continues from 4b)
lzma_info_iter_begin(info, &iter);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == 22);
expect(iter.uncompressed_size == 33);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_set(&iter, 22, LZMA_VLI_VALUE_UNKNOWN)
== LZMA_OK);
expect(iter.total_size == 22);
expect(iter.uncompressed_size == 33);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == 44);
expect(iter.uncompressed_size == 55);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22);
expect(iter.uncompressed_offset == 33);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN, 55)
== LZMA_OK);
expect(iter.total_size == 44);
expect(iter.uncompressed_size == 55);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22);
expect(iter.uncompressed_offset == 33);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == 66);
expect(iter.uncompressed_size == 77);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44);
expect(iter.uncompressed_offset == 33 + 55);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN,
LZMA_VLI_VALUE_UNKNOWN) == LZMA_OK);
expect(iter.total_size == 66);
expect(iter.uncompressed_size == 77);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44);
expect(iter.uncompressed_offset == 33 + 55);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == 88);
expect(iter.uncompressed_size == 99);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66);
expect(iter.uncompressed_offset == 33 + 55 + 77);
expect(lzma_info_index_count_get(info) == 4);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66 + 88);
expect(iter.uncompressed_offset == 33 + 55 + 77 + 99);
expect(lzma_info_index_count_get(info) == 5);
expect(lzma_info_iter_set(&iter, 1234, LZMA_VLI_VALUE_UNKNOWN)
== LZMA_OK);
expect(iter.total_size == 1234);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66 + 88);
expect(iter.uncompressed_offset == 33 + 55 + 77 + 99);
expect(lzma_info_index_count_get(info) == 5);
// Test 4d (continues from 4c)
lzma_info_iter_begin(info, &iter);
for (size_t i = 0; i < 4; ++i)
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(lzma_info_iter_set(&iter, 88, 99) == LZMA_OK);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.total_size == 1234);
expect(iter.uncompressed_size == LZMA_VLI_VALUE_UNKNOWN);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66 + 88);
expect(iter.uncompressed_offset == 33 + 55 + 77 + 99);
expect(lzma_info_index_count_get(info) == 5);
expect(lzma_info_iter_set(&iter, LZMA_VLI_VALUE_UNKNOWN, 4321)
== LZMA_OK);
expect(iter.total_size == 1234);
expect(iter.uncompressed_size == 4321);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE
+ 11 + 22 + 44 + 66 + 88);
expect(iter.uncompressed_offset == 33 + 55 + 77 + 99);
expect(lzma_info_index_count_get(info) == 5);
expect(lzma_info_index_finish(info) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 5);
// Test 4e (continues from 4d)
lzma_info_iter_begin(info, &iter);
for (size_t i = 0; i < 5; ++i)
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(lzma_info_iter_set(&iter, 1234, 4321) == LZMA_OK);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_DATA_ERROR);
reset();
}
static void
test5(void)
{
lzma_index *i;
expect(lzma_info_index_set(info, NULL, NULL, true)
== LZMA_PROG_ERROR);
reset();
expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK);
i = lzma_index_dup(my_index, NULL);
expect(i != NULL);
i->next->uncompressed_size = 99;
expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR);
reset();
expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK);
i = lzma_index_dup(my_index, NULL);
expect(i != NULL);
lzma_index_free(i->next->next, NULL);
i->next->next = NULL;
expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR);
reset();
expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK);
i = lzma_index_dup(my_index, NULL);
expect(i != NULL);
lzma_index_free(i->next->next, NULL);
i->next->next = lzma_index_dup(my_index, NULL);
expect(i->next->next != NULL);
expect(lzma_info_index_set(info, NULL, i, true) == LZMA_DATA_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL,
total_size = 22 + 44 + 66) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
uncompressed_size = 33 + 55 + 77) == LZMA_OK);
validate();
expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK);
validate();
reset();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL, total_size = 77)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
uncompressed_size = 33 + 55 + 77) == LZMA_OK);
validate();
expect(lzma_info_index_set(info, NULL, my_index, false)
== LZMA_DATA_ERROR);
reset();
expect(lzma_info_size_set(info, LZMA_INFO_TOTAL,
total_size = 22 + 44 + 66) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_UNCOMPRESSED,
uncompressed_size = 777777) == LZMA_OK);
validate();
expect(lzma_info_index_set(info, NULL, my_index, false)
== LZMA_DATA_ERROR);
reset();
}
static void
test6(void)
{
lzma_metadata metadata;
// Same complete Metadata in both Header and Footer
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA,
my_metadata.header_metadata_size) == LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &my_metadata, false, false)
== LZMA_OK);
// Header Metadata is not present but Size of Header Metadata is
// still present in Footer.
reset();
metadata = my_metadata;
metadata.header_metadata_size = 0;
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 0)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &metadata, true, false)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &my_metadata, false, false)
== LZMA_DATA_ERROR);
// Header Metadata is present but Size of Header Metadata is missing
// from Footer.
reset();
metadata = my_metadata;
metadata.header_metadata_size = 0;
expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false)
== LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA,
my_metadata.header_metadata_size) == LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &metadata, false, false)
== LZMA_DATA_ERROR);
// Index missing
reset();
metadata = my_metadata;
metadata.index = NULL;
expect(lzma_info_metadata_set(info, NULL, &metadata, true, false)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &metadata, false, false)
== LZMA_DATA_ERROR);
// Index in Header Metadata but not in Footer Metadata
reset();
expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &metadata, false, false)
== LZMA_OK);
// Index in Header Metadata but not in Footer Metadata but
// Total Size is missing from Footer.
reset();
metadata.total_size = LZMA_VLI_VALUE_UNKNOWN;
expect(lzma_info_metadata_set(info, NULL, &my_metadata, true, false)
== LZMA_OK);
expect(lzma_info_metadata_set(info, NULL, &metadata, false, false)
== LZMA_DATA_ERROR);
// Total Size doesn't match the Index
reset();
metadata = my_metadata;
metadata.total_size = 9999;
expect(lzma_info_metadata_set(info, NULL, &metadata, true, false)
== LZMA_DATA_ERROR);
// Uncompressed Size doesn't match the Index
reset();
metadata = my_metadata;
metadata.uncompressed_size = 9999;
expect(lzma_info_metadata_set(info, NULL, &metadata, true, false)
== LZMA_DATA_ERROR);
reset();
}
static void
test7(void)
{
// No info yet, so we cannot locate anything.
expect(lzma_info_metadata_locate(info, true)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
// Setting the Stream start offset doesn't change this situation.
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
// Setting the Size of Header Metadata known allows us to locate
// the Header Metadata Block.
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11)
== LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== 5 + LZMA_STREAM_HEADER_SIZE);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
// Adding a Data Block. As long as Index is not Finished, we cannot
// locate Footer Metadata Block.
lzma_info_iter_begin(info, &iter);
expect(lzma_info_iter_next(&iter, NULL) == LZMA_OK);
expect(iter.stream_offset == 5 + LZMA_STREAM_HEADER_SIZE + 11);
expect(iter.uncompressed_offset == 0);
expect(lzma_info_iter_set(&iter, 22, 33) == LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== 5 + LZMA_STREAM_HEADER_SIZE);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
// Once the Index is finished, we can locate Footer Metadata Block too.
expect(lzma_info_index_finish(info) == LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== 5 + LZMA_STREAM_HEADER_SIZE);
expect(lzma_info_metadata_locate(info, false)
== 5 + LZMA_STREAM_HEADER_SIZE + 11 + 22);
// A retry of most of the above but now with unknown Size of Header
// Metadata Block, which makes locating Footer Metadata Block
// impossible.
reset();
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_index_set(info, NULL, my_index, false) == LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_metadata_locate(info, false)
== LZMA_VLI_VALUE_UNKNOWN);
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11)
== LZMA_OK);
expect(lzma_info_metadata_locate(info, true)
== 5 + LZMA_STREAM_HEADER_SIZE);
expect(lzma_info_metadata_locate(info, false)
== LZMA_STREAM_HEADER_SIZE + 5 + 11 + 22 + 44 + 66);
reset();
}
static void
test8(void)
{
expect(lzma_info_size_set(info, LZMA_INFO_STREAM_START, 5) == LZMA_OK);
expect(lzma_info_size_set(info, LZMA_INFO_HEADER_METADATA, 11)
== LZMA_OK);
lzma_info_iter_begin(info, &iter);
expect(lzma_info_iter_locate(&iter, NULL, 0, false)
== LZMA_DATA_ERROR);
expect(lzma_info_index_count_get(info) == 0);
lzma_info_iter_begin(info, &iter);
expect(lzma_info_iter_locate(&iter, NULL, 0, true) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
expect(lzma_info_iter_locate(&iter, NULL, 0, false) == LZMA_OK);
expect(lzma_info_index_count_get(info) == 1);
// TODO
}
/*
static void
test9(void)
{
// TODO Various integer overflow checks
}
*/
int
main(void)
{
lzma_init();
info = lzma_info_init(NULL, NULL);
if (info == NULL)
return 1;
validate();
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
lzma_info_free(info, NULL);
return 0;
}
|