File: bioprinttest.c

package info (click to toggle)
openssl 3.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,104 kB
  • sloc: ansic: 612,658; perl: 248,939; asm: 6,332; sh: 1,755; pascal: 997; python: 648; makefile: 551; lisp: 35; ruby: 16; cpp: 10; sed: 6
file content (1028 lines) | stat: -rw-r--r-- 39,040 bytes parent folder | download
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/*
 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#define TESTUTIL_NO_size_t_COMPARISON

#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include "internal/nelem.h"
#include "internal/numbers.h"
#include "testutil.h"
#include "testutil/output.h"

static int justprint = 0;

static const char * const fpexpected[][11][5] = {
    {
        /*  0.00 */ { "0.0000e+00", "0.0000", "0", "0.0000E+00", "0" },
        /*  0.01 */ { "6.7000e-01", "0.6700", "0.67", "6.7000E-01", "0.67" },
        /*  0.02 */ { "6.6667e-01", "0.6667", "0.6667", "6.6667E-01", "0.6667" },
        /*  0.03 */ { "6.6667e-04", "0.0007", "0.0006667", "6.6667E-04", "0.0006667" },
        /*  0.04 */ { "6.6667e-05", "0.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
        /*  0.05 */ { "6.6667e+00", "6.6667", "6.667", "6.6667E+00", "6.667" },
        /*  0.06 */ { "6.6667e+01", "66.6667", "66.67", "6.6667E+01", "66.67" },
        /*  0.07 */ { "6.6667e+02", "666.6667", "666.7", "6.6667E+02", "666.7" },
        /*  0.08 */ { "6.6667e+03", "6666.6667", "6667", "6.6667E+03", "6667" },
        /*  0.09 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
        /*  0.10 */ { "-6.6667e+04", "-66666.6667", "-6.667e+04", "-6.6667E+04", "-6.667E+04" },
    },
    {
        /*  1.00 */ { "0.00000e+00", "0.00000", "0", "0.00000E+00", "0" },
        /*  1.01 */ { "6.70000e-01", "0.67000", "0.67", "6.70000E-01", "0.67" },
        /*  1.02 */ { "6.66667e-01", "0.66667", "0.66667", "6.66667E-01", "0.66667" },
        /*  1.03 */ { "6.66667e-04", "0.00067", "0.00066667", "6.66667E-04", "0.00066667" },
        /*  1.04 */ { "6.66667e-05", "0.00007", "6.6667e-05", "6.66667E-05", "6.6667E-05" },
        /*  1.05 */ { "6.66667e+00", "6.66667", "6.6667", "6.66667E+00", "6.6667" },
        /*  1.06 */ { "6.66667e+01", "66.66667", "66.667", "6.66667E+01", "66.667" },
        /*  1.07 */ { "6.66667e+02", "666.66667", "666.67", "6.66667E+02", "666.67" },
        /*  1.08 */ { "6.66667e+03", "6666.66667", "6666.7", "6.66667E+03", "6666.7" },
        /*  1.09 */ { "6.66667e+04", "66666.66667", "66667", "6.66667E+04", "66667" },
        /*  1.10 */ { "-6.66667e+04", "-66666.66667", "-66667", "-6.66667E+04", "-66667" },
    },
    {
        /*  2.00 */ { "  0.0000e+00", "      0.0000", "           0", "  0.0000E+00", "           0" },
        /*  2.01 */ { "  6.7000e-01", "      0.6700", "        0.67", "  6.7000E-01", "        0.67" },
        /*  2.02 */ { "  6.6667e-01", "      0.6667", "      0.6667", "  6.6667E-01", "      0.6667" },
        /*  2.03 */ { "  6.6667e-04", "      0.0007", "   0.0006667", "  6.6667E-04", "   0.0006667" },
        /*  2.04 */ { "  6.6667e-05", "      0.0001", "   6.667e-05", "  6.6667E-05", "   6.667E-05" },
        /*  2.05 */ { "  6.6667e+00", "      6.6667", "       6.667", "  6.6667E+00", "       6.667" },
        /*  2.06 */ { "  6.6667e+01", "     66.6667", "       66.67", "  6.6667E+01", "       66.67" },
        /*  2.07 */ { "  6.6667e+02", "    666.6667", "       666.7", "  6.6667E+02", "       666.7" },
        /*  2.08 */ { "  6.6667e+03", "   6666.6667", "        6667", "  6.6667E+03", "        6667" },
        /*  2.09 */ { "  6.6667e+04", "  66666.6667", "   6.667e+04", "  6.6667E+04", "   6.667E+04" },
        /*  2.10 */ { " -6.6667e+04", " -66666.6667", "  -6.667e+04", " -6.6667E+04", "  -6.667E+04" },
    },
    {
        /*  3.00 */ { " 0.00000e+00", "     0.00000", "           0", " 0.00000E+00", "           0" },
        /*  3.01 */ { " 6.70000e-01", "     0.67000", "        0.67", " 6.70000E-01", "        0.67" },
        /*  3.02 */ { " 6.66667e-01", "     0.66667", "     0.66667", " 6.66667E-01", "     0.66667" },
        /*  3.03 */ { " 6.66667e-04", "     0.00067", "  0.00066667", " 6.66667E-04", "  0.00066667" },
        /*  3.04 */ { " 6.66667e-05", "     0.00007", "  6.6667e-05", " 6.66667E-05", "  6.6667E-05" },
        /*  3.05 */ { " 6.66667e+00", "     6.66667", "      6.6667", " 6.66667E+00", "      6.6667" },
        /*  3.06 */ { " 6.66667e+01", "    66.66667", "      66.667", " 6.66667E+01", "      66.667" },
        /*  3.07 */ { " 6.66667e+02", "   666.66667", "      666.67", " 6.66667E+02", "      666.67" },
        /*  3.08 */ { " 6.66667e+03", "  6666.66667", "      6666.7", " 6.66667E+03", "      6666.7" },
        /*  3.09 */ { " 6.66667e+04", " 66666.66667", "       66667", " 6.66667E+04", "       66667" },
        /*  3.10 */ { "-6.66667e+04", "-66666.66667", "      -66667", "-6.66667E+04", "      -66667" },
    },
    {
        /*  4.00 */ { "0e+00", "0", "0", "0E+00", "0" },
        /*  4.01 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
        /*  4.02 */ { "7e-01", "1", "0.7", "7E-01", "0.7" },
        /*  4.03 */ { "7e-04", "0", "0.0007", "7E-04", "0.0007" },
        /*  4.04 */ { "7e-05", "0", "7e-05", "7E-05", "7E-05" },
        /*  4.05 */ { "7e+00", "7", "7", "7E+00", "7" },
        /*  4.06 */ { "7e+01", "67", "7e+01", "7E+01", "7E+01" },
        /*  4.07 */ { "7e+02", "667", "7e+02", "7E+02", "7E+02" },
        /*  4.08 */ { "7e+03", "6667", "7e+03", "7E+03", "7E+03" },
        /*  4.09 */ { "7e+04", "66667", "7e+04", "7E+04", "7E+04" },
        /*  4.10 */ { "-7e+04", "-66667", "-7e+04", "-7E+04", "-7E+04" },
    },
    {
        /*  5.00 */ { "0.000000e+00", "0.000000", "0", "0.000000E+00", "0" },
        /*  5.01 */ { "6.700000e-01", "0.670000", "0.67", "6.700000E-01", "0.67" },
        /*  5.02 */ { "6.666667e-01", "0.666667", "0.666667", "6.666667E-01", "0.666667" },
        /*  5.03 */ { "6.666667e-04", "0.000667", "0.000666667", "6.666667E-04", "0.000666667" },
        /*  5.04 */ { "6.666667e-05", "0.000067", "6.66667e-05", "6.666667E-05", "6.66667E-05" },
        /*  5.05 */ { "6.666667e+00", "6.666667", "6.66667", "6.666667E+00", "6.66667" },
        /*  5.06 */ { "6.666667e+01", "66.666667", "66.6667", "6.666667E+01", "66.6667" },
        /*  5.07 */ { "6.666667e+02", "666.666667", "666.667", "6.666667E+02", "666.667" },
        /*  5.08 */ { "6.666667e+03", "6666.666667", "6666.67", "6.666667E+03", "6666.67" },
        /*  5.09 */ { "6.666667e+04", "66666.666667", "66666.7", "6.666667E+04", "66666.7" },
        /*  5.10 */ { "-6.666667e+04", "-66666.666667", "-66666.7", "-6.666667E+04", "-66666.7" },
    },
    {
        /*  6.00 */ { "0.0000e+00", "000.0000", "00000000", "0.0000E+00", "00000000" },
        /*  6.01 */ { "6.7000e-01", "000.6700", "00000.67", "6.7000E-01", "00000.67" },
        /*  6.02 */ { "6.6667e-01", "000.6667", "000.6667", "6.6667E-01", "000.6667" },
        /*  6.03 */ { "6.6667e-04", "000.0007", "0.0006667", "6.6667E-04", "0.0006667" },
        /*  6.04 */ { "6.6667e-05", "000.0001", "6.667e-05", "6.6667E-05", "6.667E-05" },
        /*  6.05 */ { "6.6667e+00", "006.6667", "0006.667", "6.6667E+00", "0006.667" },
        /*  6.06 */ { "6.6667e+01", "066.6667", "00066.67", "6.6667E+01", "00066.67" },
        /*  6.07 */ { "6.6667e+02", "666.6667", "000666.7", "6.6667E+02", "000666.7" },
        /*  6.08 */ { "6.6667e+03", "6666.6667", "00006667", "6.6667E+03", "00006667" },
        /*  6.09 */ { "6.6667e+04", "66666.6667", "6.667e+04", "6.6667E+04", "6.667E+04" },
        /*  6.10 */ { "-6.6667e+04", "-66666.6667", "-6.667e+04", "-6.6667E+04", "-6.667E+04" },
    },
};

enum arg_type {
    AT_NONE = 0,
    AT_CHAR, AT_SHORT, AT_INT, AT_LONG, AT_LLONG,
    /* The ones below are used in n_data only so far */
    AT_SIZE, AT_PTRDIFF, AT_STR,
};

static const struct int_data {
    union {
        unsigned char hh;
        unsigned short h;
        unsigned int i;
        unsigned long l;
        unsigned long long ll;
    } value;
    enum arg_type type;
    const char *format;
    const char *expected;
    bool skip_libc_check;
    /* Since OpenSSL's snprintf is non-standard on buffer overflow */
    bool skip_libc_ret_check;
    int exp_ret;
} int_data[] = {
    { { .hh = 0x42 }, AT_CHAR, "%+hhu", "66" },
    { { .hh = 0x88 }, AT_CHAR, "%hhd", "-120" },
    { { .hh = 0x0 }, AT_CHAR, "%hho", "0" },
    { { .hh = 0x0 }, AT_CHAR, "%#hho", "0" },
    { { .hh = 0x1 }, AT_CHAR, "%hho", "1" },
    { { .hh = 0x1 }, AT_CHAR, "%#hho", "01" },
    { { .hh = 0x0 }, AT_CHAR, "%+hhx", "0" },
    { { .hh = 0x0 }, AT_CHAR, "%#hhx", "0" },
    { { .hh = 0xf }, AT_CHAR, "%hhx", "f" },
    { { .hh = 0xe }, AT_CHAR, "%hhX", "E" },
    { { .hh = 0xd }, AT_CHAR, "%#hhx", "0xd" },
    { { .hh = 0xc }, AT_CHAR, "%#hhX", "0XC" },
    { { .hh = 0xb }, AT_CHAR, "%#04hhX", "0X0B" },
    { { .hh = 0xa }, AT_CHAR, "%#-015hhx", "0xa            " },
    { { .hh = 0x9 }, AT_CHAR, "%#+01hho", "011" },
    { { .hh = 0x8 }, AT_CHAR, "%#09hho", "000000010" },
    { { .hh = 0x7 }, AT_CHAR, "%#+ 9hhi", "       +7" },
    { { .hh = 0x6 }, AT_CHAR, "%# 9hhd", "        6" },
    { { .hh = 0x95 }, AT_CHAR, "%#06hhi", "-00107" },
    { { .hh = 0x4 }, AT_CHAR, "%# hhd", " 4" },
    { { .hh = 0x3 }, AT_CHAR, "%# hhu", "3" },
    { { .hh = 0x0 }, AT_CHAR, "%02hhx", "00" },
    { { .h = 0 }, AT_SHORT, "|%.0hd|", "||" },
    { { .h = 0 }, AT_SHORT, "|%.hu|", "||" },
    { { .h = 0 }, AT_SHORT, "|%#.ho|", "|0|" },
    { { .h = 1 }, AT_SHORT, "%4.2hi", "  01" },
    { { .h = 2 }, AT_SHORT, "%-4.3hu", "002 " },
    { { .h = 3 }, AT_SHORT, "%+.3hu", "003" },
    { { .h = 9 }, AT_SHORT, "%#5.2ho", "  011" },
    { { .h = 0xf }, AT_SHORT, "%#-6.2hx", "0x0f  " },
    { { .h = 0xaa }, AT_SHORT, "%#8.0hX", "    0XAA" },
    { { .h = 0xdead }, AT_SHORT, "%#hi", "-8531" },
    { { .h = 0xcafe }, AT_SHORT, "%#0.1hX", "0XCAFE" },
    { { .h = 0xbeef }, AT_SHORT, "%#012.8ho", "    00137357" },
    { { .h = 0xbeef }, AT_SHORT, "%#000000000000000000012.ho", "     0137357" },
    { { .h = 0xbeef }, AT_SHORT, "%012.ho", "      137357" },
    { { .h = 0xfade }, AT_SHORT, "%#012ho", "000000175336" },
    { { .h = 0xfaff }, AT_SHORT, "%#-012ho", "0175377     " },
    { { .h = 0xbea7 }, AT_SHORT, "%#-012.8ho", "00137247    " },
    { { .i = 0 }, AT_INT, "-%#+.0u-", "--" },
    { { .i = 0 }, AT_INT, "-%-8.u-", "-        -" },
    { { .i = 0xdeadc0de }, AT_INT, "%#+67.65i",
      " -0000000000000000000000000000000000000000000000000000000055903",
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { { .i = 0xfeedface }, AT_INT, "%#+70.10X",
      "                                                          0X00F",
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { { .i = 0xdecaffee }, AT_INT, "%76.15o",
      "                                                             00",
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { { .i = 0x5ad }, AT_INT, "%#67.x",
      "                                                              0",
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { { .i = 0x1337 }, AT_INT, "|%2147483639.x|",
      "|                                                              ",
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { { .i = 0x1337 }, AT_INT, "|%.2147483639x|",
      "|00000000000000000000000000000000000000000000000000000000000000",
#if defined(OPENSSL_SYS_WINDOWS)
      /* MS CRT can't handle this one, snprintf() causes access violation. */
      .skip_libc_check = true,
#endif
      .skip_libc_ret_check = true, .exp_ret = -1 },
    /*
     * We treat the following three format strings as errneous and bail out
     * mid-string.
     */
    { { .i = 0x1337 }, AT_INT, "|%2147483647.x|", "|",
      .skip_libc_check = true, .exp_ret = -1 },
    { { .i = 0x1337 }, AT_INT,
      "abcdefghijklmnopqrstuvwxyz0123456789ZYXWVUTSRQPONMLKJIHGFEDCBA"
      "|%4294967295.x|",
      "abcdefghijklmnopqrstuvwxyz0123456789ZYXWVUTSRQPONMLKJIHGFEDCBA|",
      .skip_libc_check = true, .exp_ret = -1 },
    { { .i = 0x1337 }, AT_INT, "%4294967302.x", "",
      .skip_libc_check = true, .exp_ret = -1 },
    { { .i = 0xbeeface }, AT_INT, "%#+-12.1d", "+200211150  " },
    { { .l = 0 }, AT_LONG, "%%%#.0lo%%", "%0%" },
    { { .l = 0 }, AT_LONG, "%%%.0lo%%", "%%" },
    { { .l = 0 }, AT_LONG, "%%%-.0lo%%", "%%" },
    { { .l = 0xfacefed }, AT_LONG, "%#-1.14ld", "00000262991853" },
    { { .l = 0xdefaced }, AT_LONG, "%#+-014.11li", "+00233811181  " },
    { { .l = 0xfacade }, AT_LONG, "%#0.14lo", "00000076545336" },
    { { .l = 0 }, AT_LONG, "%#0.14lo", "00000000000000" },
    { { .l = 0xfacade }, AT_LONG, "%#0.14lx", "0x00000000facade" },
    { { .ll = 0 }, AT_LLONG, "#%#.0llx#", "##" },
    { { .ll = 0 }, AT_LLONG, "#%.0llx#", "##" },
    { { .ll = 0xffffFFFFffffFFFFULL }, AT_LLONG, "%#-032llo",
      "01777777777777777777777         " },
    { { .ll = 0xbadc0deddeadfaceULL }, AT_LLONG, "%022lld",
      "-004982091772484257074" },
};

static int test_int(int i)
{
    char bio_buf[64];
    char std_buf[64];
    int bio_ret;
    int std_ret = 0;
    const struct int_data *data = int_data + i;
    const int exp_ret = data->exp_ret ? data->exp_ret
                                      : (int) strlen(data->expected);

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    switch (data->type) {
#define DO_PRINT(field_)                                                \
    do {                                                                \
        bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,  \
                               data->value.field_);                     \
        if (!data->skip_libc_check)                                     \
            std_ret = snprintf(std_buf, sizeof(std_buf), data->format,  \
                               data->value.field_);                     \
    } while (0)
    case AT_CHAR:
        DO_PRINT(hh);
        break;
    case AT_SHORT:
        DO_PRINT(h);
        break;
    case AT_INT:
        DO_PRINT(i);
        break;
    case AT_LONG:
        DO_PRINT(l);
        break;
    case AT_LLONG:
        DO_PRINT(ll);
        break;
    default:
        TEST_error("Unexpected arg type: %d", data->type);
        return 0;
#undef DO_PRINT
    }

    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_int_eq(bio_ret, exp_ret)) {
        TEST_note("Format: \"%s\"", data->format);
        return 0;
    }

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal.
     */
    if (!data->skip_libc_check) {
        if (!TEST_str_eq(bio_buf, std_buf)
            + !(data->skip_libc_ret_check || TEST_int_eq(bio_ret, std_ret))) {
            TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
            return 0;
#endif
        }
    }

    return 1;
}

union ptrint {
    uintptr_t i;
    const char *s;
};

static const struct wp_data {
    union ptrint value;
    const char *format;
    const char *expected;
    int num_args;
    int arg1;
    int arg2;
    bool skip_libc_check;
    /* Since OpenSSL's snprintf is non-standard on buffer overflow */
    bool skip_libc_ret_check;
    int exp_ret;
} wp_data[] = {
    /* Integer checks with width/precision provided via arguments */
    { { .i = 01234 }, "%#*" PRIoPTR, "       01234", 1, 12 },
    { { .i = 01234 }, "%#.*" PRIxPTR, "0x00000000029c", 1, 12 },

    { { .i = 0 }, "|%#*" PRIoPTR "|", "| 0|", 1, 2 },
    { { .i = 0 }, "|%#.*" PRIoPTR "|", "|00|", 1, 2 },
    { { .i = 0 }, "|%#.*" PRIoPTR "|", "|0|", 1, 1 },
    { { .i = 0 }, "|%#.*" PRIoPTR "|", "|0|", 1, 0 },
    { { .i = 0 }, "|%.*" PRIoPTR "|", "||", 1, 0 },
    { { .i = 0 }, "|%#.*" PRIoPTR "|", "|0|", 1, -12 },

    { { .i = 0 }, "|%#.*" PRIxPTR "|", "||", 1, 0 },
    { { .i = 0 }, "|%#.*" PRIxPTR "|", "|0|", 1, -12 },
    { { .i = 1 }, "|%#.*" PRIxPTR "|", "|0x1|", 1, -12 },

    { { .i = 0 }, "|%#*.*" PRIxPTR "|", "|            |", 2, 12, 0 },
    { { .i = 1234 }, "|%*.*" PRIuPTR "|", "|      001234|", 2, 12, 6 },

    /* FreeBSD's libc bails out on the following three */
    { { .i = 1337 }, "|%*" PRIuPTR "|",
      "|                                                              ",
      1, 2147483647, .skip_libc_check = true, .exp_ret = -1 },
    { { .i = 1337 }, "|%.*" PRIuPTR "|",
      "|00000000000000000000000000000000000000000000000000000000000000",
      1, 2147483647, .skip_libc_check = true, .exp_ret = -1 },
    { { .i = 1337 }, "|%#*.*" PRIoPTR "|",
      "|                                                             0",
      2, 2147483647, 2147483586, .skip_libc_check = true, .exp_ret = -1 },

    /* String width/precision checks */
    { { .s = "01234" }, "%12s", "       01234" },
    { { .s = "01234" }, "%-12s", "01234       " },
    { { .s = "01234" }, "%.12s", "01234" },
    { { .s = "01234" }, "%.2s", "01" },

    { { .s = "abc" }, "%*s", "         abc", 1, 12 },
    { { .s = "abc" }, "%*s", "abc         ", 1, -12 },
    { { .s = "abc" }, "%-*s", "abc         ", 1, 12 },
    { { .s = "abc" }, "%-*s", "abc         ", 1, -12 },

    { { .s = "ABC" }, "%*.*s", "         ABC", 2, 12, 5 },
    { { .s = "ABC" }, "%*.*s", "AB          ", 2, -12, 2 },
    { { .s = "ABC" }, "%-*.*s", "ABC         ", 2, 12, -5 },
    { { .s = "ABC" }, "%-*.*s", "ABC         ", 2, -12, -2 },

    { { .s = "def" }, "%.*s", "def", 1, 12 },
    { { .s = "%%s0123456789" }, "%.*s", "%%s01", 1, 5 },
    { { .s = "9876543210" }, "|%-61s|",
      "|9876543210                                                   |" },
    { { .s = "0123456789" }, "|%62s|",
      "|                                                    0123456789",
      .skip_libc_ret_check = true, .exp_ret = -1 },

    { { .s = "DEF" }, "%-2147483639s",
      "DEF                                                            ",
      .skip_libc_check = true, .exp_ret = -1 },
    { { .s = "DEF" }, "%-2147483640s", "",
      .skip_libc_check = true, .exp_ret = -1 },
    { { .s = "DEF" }, "%*s",
      "                                                               ",
      1, 2147483647, .skip_libc_check = true, .exp_ret = -1 },
};

static int test_width_precision(int i)
{
    char bio_buf[64];
    char std_buf[64];
    int bio_ret;
    int std_ret = 0;
    const struct wp_data *data = wp_data + i;
    const int exp_ret = data->exp_ret ? data->exp_ret
                                      : (int) strlen(data->expected);

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    switch (data->num_args) {
    case 2:
        bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,
                               data->arg1, data->arg2, data->value.i);
        if (!data->skip_libc_check)
            std_ret = snprintf(std_buf, sizeof(std_buf), data->format,
                               data->arg1, data->arg2, data->value.i);
        break;

    case 1:
        bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,
                               data->arg1, data->value.i);
        if (!data->skip_libc_check)
            std_ret = snprintf(std_buf, sizeof(std_buf), data->format,
                               data->arg1, data->value.i);
        break;

    case 0:
    default:
        bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,
                               data->value.i);
        if (!data->skip_libc_check)
            std_ret = snprintf(std_buf, sizeof(std_buf), data->format,
                               data->value.i);
    }

    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_int_eq(bio_ret, exp_ret)) {
        TEST_note("Format: \"%s\"", data->format);
        return 0;
    }

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal, unless OPENSSL_STRICT_PRINTF_COMPLIANCE_CHECK is defined.
     */
    if (!data->skip_libc_check) {
        if (!TEST_str_eq(bio_buf, std_buf)
            + !(data->skip_libc_ret_check || TEST_int_eq(bio_ret, std_ret))) {
            TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
            return 0;
#endif
        }
    }

    return 1;
}

static const struct n_data {
    const char *format;
    const char *expected;
    enum arg_type n_type;
    const uint64_t exp_n;
    enum arg_type arg1_type;
    union ptrint arg1;
    enum arg_type arg2_type;
    union ptrint arg2;
    bool skip_libc_check;
    /* Since OpenSSL's snprintf is non-standard on buffer overflow */
    bool skip_libc_ret_check;
    int exp_ret;
} n_data[] = {
    { "%n", "", AT_INT, 0, AT_NONE },
    { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz%n",
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
      AT_INT, 62, AT_NONE },
    { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+=%n",
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+",
      AT_INT, 64, AT_NONE, .skip_libc_ret_check = true, .exp_ret = -1 },
    { "%" PRIdPTR "%hhn", "1234567890",
      AT_CHAR, 10, AT_INT, { .i = 1234567890 } },
    { "%#.200" PRIXPTR "%hhn",
      "0X0000000000000000000000000000000000000000000000000000000000000",
      AT_CHAR, -54, AT_INT, { .i = 1234567890 },
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { "%#10000" PRIoPTR "%hhn1234567890",
      "                                                               ",
      /* XXX Should we overflow or saturate?  glibc does the former. */
      AT_CHAR, 16, AT_INT, { .i = 1234567890 },
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { "%.0s%hn0987654321", "0987654321",
      AT_SHORT, 0, AT_INT, { .s = "1234567890" } },
    { "%-123456s%hn0987654321",
      "1234567890                                                     ",
      AT_SHORT, -7616, AT_INT, { .s = "1234567890" },
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { "%1234567898.1234567890" PRIxPTR "%n",
      "        0000000000000000000000000000000000000000000000000000000",
      AT_INT, 1234567898, AT_INT, { .i = 0xbadc0ded },
#if defined(OPENSSL_SYS_WINDOWS)
      /* MS CRT can't handle this one, snprintf() causes access violation. */
      .skip_libc_check = true,
#endif
      .skip_libc_ret_check = true, .exp_ret = -1 },
    { "%s|%n",
      "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ|",
      AT_INT, 63, AT_STR, { .s =
      "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" } },
    { "|%#2147483639x|%2147483639s|0123456789abcdef|%ln",
      "|                                                              ",
      AT_LONG, sizeof(long) == 8 ? 4294967298ULL : 2,
      AT_INT, { .i = 0x1337 }, AT_STR, { .s = "1EE7" },
      /* glibc caps %n value (1 << 32) - 1 */
      .skip_libc_check = true, .exp_ret = -1 },
    { "|%-2147483638s|0123456789abcdef|%02147483637o|0123456789ABCDEF|%lln",
      "|echo test test test                                           ",
      AT_LLONG, 4294967312ULL,
      AT_STR, { .s = "echo test test test" }, AT_INT, { .i = 0xbad },
      /* glibc caps %n value (1 << 32) - 1 */
      .skip_libc_check = true, .exp_ret = -1 },
    { "|%+2147483639s|2147483639|%.2147483639u|2147483639|%zn",
      "|                                                              ",
      AT_SIZE, sizeof(size_t) == 8 ? 4294967303ULL : 7,
      AT_STR, { .s = "according to all known laws of aviation" },
      AT_INT, { .i = 0xbee },
      /* glibc caps %n value (1 << 32) - 1 */
      .skip_libc_check = true, .exp_ret = -1 },
    { "==%2147483639.2147483639s==2147483639.2147483639==%+2147483639d==%tn==",
      "==                                                             ",
      AT_PTRDIFF, sizeof(ptrdiff_t) == 8 ? 4294967307ULL : 11,
      AT_STR, { .s = "oh hi there hello" }, AT_INT, { .i = 0x1234 },
      /* glibc caps %n value (1 << 32) - 1 */
      .skip_libc_check = true, .exp_ret = -1 },
    { "=%2147483639s=%888888888X=%tn=",
      "=                                                              ",
      AT_PTRDIFF, sizeof(ptrdiff_t) == 8 ? 3036372530LL : -1258594766LL,
      AT_STR, { .s = NULL }, AT_INT, { .i = 0xdead },
      .skip_libc_check = true, .exp_ret = -1 },
};

static int test_n(int i)
{
    const struct n_data *data = n_data + i;
    const int exp_ret = data->exp_ret ? data->exp_ret
                                      : (int) strlen(data->expected);
    char bio_buf[64];
    char std_buf[64];
    int bio_ret;
    int std_ret = 0;
    bool skip_libc_check = data->skip_libc_check;
    union {
        uint64_t val;
        signed char hh;
        short h;
        int i;
        long int l;
        long long int ll;
        ossl_ssize_t z;
        ptrdiff_t t;
    } n = { 0 }, std_n = { 0 };

#if defined(OPENSSL_SYS_WINDOWS)
    /*
     * MS CRT is special and throws an exception when %n is used even
     * in non-*_s versions of printf routines, and there is a special function
     * to enable %n handling.
     */
    _set_printf_count_output(1);
    if (_get_printf_count_output() == 0) {
        TEST_note("Can't enable %%n handling for snprintf"
                  ", skipping the checks against libc");
        skip_libc_check = true;
    }
#elif defined(__OpenBSD__)
    {
        static bool note_printed;

        if (!note_printed) {
            TEST_note("OpenBSD libc unconditionally terminates a program "
                      "if %%n is used in a *printf routine"
                      ", skipping the checks against libc");
            note_printed = true;
        }

        skip_libc_check = true;
    }
#endif /* defined(OPENSSL_SYS_WINDOWS) || defined(__OpenBSD__) */

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    switch (data->n_type) {
#define DO_PRINT(field_)                                                       \
    do {                                                                       \
        if (data->arg1_type == AT_NONE) {                                      \
            bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,     \
                                   &n.field_);                                 \
            if (!skip_libc_check)                                              \
                std_ret = snprintf(std_buf, sizeof(std_buf), data->format,     \
                                   &std_n.field_);                             \
        } else if (data->arg2_type == AT_NONE) {                               \
            bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,     \
                                   data->arg1.i, &n.field_);                   \
            if (!skip_libc_check)                                              \
                std_ret = snprintf(std_buf, sizeof(std_buf), data->format,     \
                                   data->arg1.i, &std_n.field_);               \
        } else {                                                               \
            bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format,     \
                                   data->arg1.i, data->arg2.i, &n.field_);     \
            if (!skip_libc_check)                                              \
                std_ret = snprintf(std_buf, sizeof(std_buf), data->format,     \
                                   data->arg1.i, data->arg2.i, &std_n.field_); \
        }                                                                      \
        n.val = n.field_;                                                      \
        std_n.val = std_n.field_;                                              \
    } while (0)
    case AT_CHAR:
        DO_PRINT(hh);
        break;
    case AT_SHORT:
        DO_PRINT(h);
        break;
    case AT_INT:
        DO_PRINT(i);
        break;
    case AT_LONG:
        DO_PRINT(l);
        break;
    case AT_LLONG:
        DO_PRINT(ll);
        break;
    case AT_SIZE:
        DO_PRINT(z);
        break;
    case AT_PTRDIFF:
        DO_PRINT(t);
        break;
    default:
        TEST_error("Unexpected arg type: %d", data->n_type);
        return 0;
#undef DO_PRINT
    }

    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_uint64_t_eq(n.val, data->exp_n)
        + !TEST_int_eq(bio_ret, exp_ret)) {
        TEST_note("Format: \"%s\"", data->format);
        return 0;
    }

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal.
     */
    if (!data->skip_libc_check) {
        if (!TEST_str_eq(bio_buf, std_buf)
            + !TEST_uint64_t_eq(n.val, std_n.val)
            + !(data->skip_libc_ret_check || TEST_int_eq(bio_ret, std_ret))) {
            TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
            return 0;
#endif
        }
    }

    return 1;
}

typedef struct z_data_st {
    size_t value;
    const char *format;
    const char *expected;
} z_data;

static const z_data zu_data[] = {
    { SIZE_MAX, "%zu", (sizeof(size_t) == 4 ? "4294967295"
                        : sizeof(size_t) == 8 ? "18446744073709551615"
                        : "") },
    /*
     * in 2-complement, the unsigned number divided by two plus one becomes the
     * smallest possible negative signed number of the corresponding type
     */
    { SIZE_MAX / 2 + 1, "%zi", (sizeof(size_t) == 4 ? "-2147483648"
                                : sizeof(size_t) == 8 ? "-9223372036854775808"
                                : "") },
    { 0, "%zu", "0" },
    { 0, "%zi", "0" },
};

static int test_zu(int i)
{
    char bio_buf[80];
    char std_buf[80];
    const z_data *data = &zu_data[i];
    const int exp_ret = (int) strlen(data->expected);
    int bio_ret;
    int std_ret;

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format, data->value);
    std_ret = snprintf(std_buf, sizeof(std_buf), data->format, data->value);
    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_int_eq(bio_ret, exp_ret))
        return 0;

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal.
     */
    if (!TEST_str_eq(bio_buf, std_buf)
        + !TEST_int_eq(bio_ret, std_ret)) {
        TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
        return 0;
#endif
    }

    return 1;
}

static const struct t_data {
    size_t value;
    const char *format;
    const char *expected;
} t_data[] = {
    { PTRDIFF_MAX, "%+td",
      sizeof(ptrdiff_t) == 4 ? "+2147483647" : "+9223372036854775807" },
    { PTRDIFF_MIN, "%+ti",
      sizeof(ptrdiff_t) == 4 ? "-2147483648" : "-9223372036854775808" },
    { 0, "%tu", "0" },
    { 0, "%+09ti", "+00000000" },
};

static int test_t(int i)
{
    char bio_buf[64];
    char std_buf[64];
    const struct t_data *data = &t_data[i];
    const int exp_ret = (int) strlen(data->expected);
    int bio_ret;
    int std_ret;

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format, data->value);
    std_ret = snprintf(std_buf, sizeof(std_buf), data->format, data->value);
    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_int_eq(bio_ret, exp_ret))
        return 0;

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal.
     */
    if (!TEST_str_eq(bio_buf, std_buf)
        + !TEST_int_eq(bio_ret, std_ret)) {
        TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
        return 0;
#endif
    }

    return 1;
}

typedef struct j_data_st {
    uint64_t value;
    const char *format;
    const char *expected;
} j_data;

static const j_data jf_data[] = {
    { 0xffffffffffffffffULL, "%ju", "18446744073709551615" },
    { 0xffffffffffffffffULL, "%jx", "ffffffffffffffff" },
    { 0x8000000000000000ULL, "%ju", "9223372036854775808" },
    /*
     * These tests imply two's complement, but it's the only binary
     * representation we support, see test/sanitytest.c...
     */
    { 0x8000000000000000ULL, "%ji", "-9223372036854775808" },
};

static int test_j(int i)
{
    const j_data *data = &jf_data[i];
    char bio_buf[80];
    char std_buf[80];
    const int exp_ret = (int) strlen(data->expected);
    int bio_ret;
    int std_ret;

    memset(bio_buf, '@', sizeof(bio_buf));
    memset(std_buf, '#', sizeof(std_buf));

    bio_ret = BIO_snprintf(bio_buf, sizeof(bio_buf), data->format, data->value);
    std_ret = snprintf(std_buf, sizeof(std_buf), data->format, data->value);
    if (!TEST_str_eq(bio_buf, data->expected)
        + !TEST_int_eq(bio_ret, exp_ret))
        return 0;

    /*
     * We treat the unexpected discrepancies with libc results as notable,
     * but not fatal.
     */
    if (!TEST_str_eq(bio_buf, std_buf)
        + !TEST_int_eq(bio_ret, std_ret)) {
        TEST_note("Format: \"%s\"", data->format);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
        return 0;
#endif
    }

    return 1;
}


/* Precision and width. */
typedef struct pw_st {
    int p;
    const char *w;
} pw;

static const pw pw_params[] = {
    { 4, "" },
    { 5, "" },
    { 4, "12" },
    { 5, "12" },
    { 0, "" },
    { -1, "" },
    { 4, "08" }
};

static int dofptest(int test, int sub, double val, const char *width, int prec)
{
    static const char *fspecs[] = {
        "e", "f", "g", "E", "G"
    };
    char format[80], result[80], std_result[80];
    int ret = 1, i;
    int exp_ret;
    int bio_ret;
    int std_ret;

    for (i = 0; i < (int)OSSL_NELEM(fspecs); i++) {
        const char *fspec = fspecs[i];

        memset(result, '@', sizeof(result));
        memset(std_result, '#', sizeof(std_result));

        if (prec >= 0)
            BIO_snprintf(format, sizeof(format), "%%%s.%d%s", width, prec,
                         fspec);
        else
            BIO_snprintf(format, sizeof(format), "%%%s%s", width, fspec);

        exp_ret = (int) strlen(fpexpected[test][sub][i]);
        bio_ret = BIO_snprintf(result, sizeof(result), format, val);
        std_ret = snprintf(std_result, sizeof(std_result), format, val);

        if (justprint) {
            if (i == 0)
                printf("    /*  %d.%02d */ { \"%s\"", test, sub, result);
            else
                printf(", \"%s\"", result);
        } else {
            if (!TEST_str_eq(fpexpected[test][sub][i], result)
                + !TEST_int_eq(bio_ret, exp_ret)) {
                TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|"
                          ", stdlib_ret=|%s|",
                          test, format, fpexpected[test][sub][i], result,
                          std_result);
                ret = 0;
            }

            /*
             * We treat the unexpected discrepancies with libc results as notable,
             * but not fatal.
             */
            if (!TEST_str_eq(result, std_result)
                + !TEST_int_eq(bio_ret, std_ret)) {
                TEST_info("test %d format=|%s| exp=|%s|, ret=|%s|"
                          ", stdlib_ret=|%s|",
                          test, format, fpexpected[test][sub][i], result,
                          std_result);
#if defined(OPENSSL_STRICT_LIBC_PRINTF_CHECK)
                ret = 0;
#endif
            }
        }
    }
    if (justprint)
        printf(" },\n");
    return ret;
}

static int test_fp(int i)
{
    int t = 0, r;
    const double frac = 2.0 / 3.0;
    const pw *pwp = &pw_params[i];

    if (justprint)
        printf("    {\n");
    r = TEST_true(dofptest(i, t++, 0.0, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 0.67, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, frac / 1000, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, frac / 10000, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 6.0 + frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 66.0 + frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 666.0 + frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 6666.0 + frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, 66666.0 + frac, pwp->w, pwp->p))
        && TEST_true(dofptest(i, t++, -66666.0 - frac, pwp->w, pwp->p));
    if (justprint)
        printf("    },\n");
    return r;
}

static int test_big(void)
{
    char buf[80];

    /* Test excessively big number. Should fail */
    if (!TEST_int_eq(BIO_snprintf(buf, sizeof(buf),
                                  "%f\n", 2 * (double)ULONG_MAX), -1))
        return 0;

    return 1;
}

typedef enum OPTION_choice {
    OPT_ERR = -1,
    OPT_EOF = 0,
    OPT_PRINT,
    OPT_TEST_ENUM
} OPTION_CHOICE;

const OPTIONS *test_get_options(void)
{
    static const OPTIONS options[] = {
        OPT_TEST_OPTIONS_DEFAULT_USAGE,
        { "expected", OPT_PRINT, '-', "Output values" },
        { NULL }
    };
    return options;
}

int setup_tests(void)
{
    OPTION_CHOICE o;

    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_PRINT:
            justprint = 1;
            break;
        case OPT_TEST_CASES:
            break;
        default:
            return 0;
        }
    }

    ADD_TEST(test_big);
    ADD_ALL_TESTS(test_fp, OSSL_NELEM(pw_params));
    ADD_ALL_TESTS(test_int, OSSL_NELEM(int_data));
    ADD_ALL_TESTS(test_width_precision, OSSL_NELEM(wp_data));
    ADD_ALL_TESTS(test_n, OSSL_NELEM(n_data));
    ADD_ALL_TESTS(test_zu, OSSL_NELEM(zu_data));
    ADD_ALL_TESTS(test_t, OSSL_NELEM(t_data));
    ADD_ALL_TESTS(test_j, OSSL_NELEM(jf_data));
    return 1;
}

/*
 * Replace testutil output routines.  We do this to eliminate possible sources
 * of BIO error
 */
BIO *bio_out = NULL;
BIO *bio_err = NULL;

static int tap_level = 0;

void test_open_streams(void)
{
}

void test_adjust_streams_tap_level(int level)
{
    tap_level = level;
}

void test_close_streams(void)
{
}

/*
 * This works out as long as caller doesn't use any "fancy" formats.
 * But we are caller's caller, and test_str_eq is the only one called,
 * and it uses only "%s", which is not "fancy"...
 */
int test_vprintf_stdout(const char *fmt, va_list ap)
{
    return fprintf(stdout, "%*s# ", tap_level, "") + vfprintf(stdout, fmt, ap);
}

int test_vprintf_stderr(const char *fmt, va_list ap)
{
    return fprintf(stderr, "%*s# ", tap_level, "") + vfprintf(stderr, fmt, ap);
}

int test_flush_stdout(void)
{
    return fflush(stdout);
}

int test_flush_stderr(void)
{
    return fflush(stderr);
}

int test_vprintf_tapout(const char *fmt, va_list ap)
{
    return fprintf(stdout, "%*s", tap_level, "") + vfprintf(stdout, fmt, ap);
}

int test_vprintf_taperr(const char *fmt, va_list ap)
{
    return fprintf(stderr, "%*s", tap_level, "") + vfprintf(stderr, fmt, ap);
}

int test_flush_tapout(void)
{
    return fflush(stdout);
}

int test_flush_taperr(void)
{
    return fflush(stderr);
}