File: main.c

package info (click to toggle)
yadifa 3.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,392 kB
  • sloc: ansic: 254,102; makefile: 1,409; pascal: 971; sh: 706
file content (1163 lines) | stat: -rw-r--r-- 48,309 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
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
/*------------------------------------------------------------------------------
 *
 * Copyright (c) 2011-2025, EURid vzw. All rights reserved.
 * The YADIFA TM software product is provided under the BSD 3-clause license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *        * Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in the
 *          documentation and/or other materials provided with the distribution.
 *        * Neither the name of EURid nor the names of its contributors may be
 *          used to endorse or promote products derived from this software
 *          without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *----------------------------------------------------------------------------*/

#include "yatest.h"
#include "yatest_stream.h"
#include "dnscore/ctrl_rfc.h"
#include <dnscore/dnscore.h>
#include <dnscore/format.h>
#include <dnscore/bytearray_output_stream.h>
#include <time.h>

static void init() { dnscore_init(); }

static void finalise() { dnscore_finalize(); }

#if !__FreeBSD__
extern char *tzname[2];
extern long  timezone;
extern int   daylight;
#endif

static int osformat_test()
{
#if !__FreeBSD__
    setenv("TZ", "Europe/Brussels", 1);
    tzset();
    yatest_log("TZ: %s,%s %i %i", tzname[0], tzname[1], timezone, daylight);
#endif

    static const char expected_output[] = {
        "%;\n"
        "-11i: < -2147483648>< 2147483647>;11i: <-2147483648 ><2147483647 >;lli: -9223372036854775808 "
        "9223372036854775807;li: -2147483648 2147483647;i: -2147483648 2147483647;hi: -32768 32767;hhi: -128 127;\n"
        "-11d: <          0>< 4294967295>;11d: <0          ><4294967295 >;lld: 0 18446744073709551615;ld: 0 "
        "4294967295;d: 0 4294967295;hd: 0 65535;hhd: 0 255;\n"
        "-11o: <          0><37777777777>;11o: <0          ><37777777777>;llo: 0 1777777777777777777777;lo: 0 "
        "37777777777;o: 0 37777777777;ho: 0 177777;hho: 0 377;\n"
        "-11x: <          0><   ffffffff>;11x: <0          ><ffffffff   >;llx: 0 ffffffffffffffff;lx: 0 ffffffff;x: 0 "
        "ffffffff;hx: 0 ffff;hhx: 0 ff;\n"
        "-11X: <          0><   FFFFFFFF>;11X: <0          ><FFFFFFFF   >;llX: 0 FFFFFFFFFFFFFFFF;lX: 0 FFFFFFFF;X: 0 "
        "FFFFFFFF;hX: 0 FFFF;hhX: 0 FF;\n"
#if __SIZEOF_POINTER__ == 8
        "P: 0000000000000000;\n"
        "p: 0000000000000000;\n"
#else
        "P: 00000000;\n"
        "p: 00000000;\n"
#endif
        "f: 3.141500; Lf: 3.141500;11.8f:  3.14150000; L11.8f:  3.14150000;\n"
        "s: <Hello World >< Hello World> Hello World;\n"
        "c: .;\n"
        "{dnstype}: ANY;\n"
#if __SIZEOF_POINTER__ == 8
        "{undefined}: 0123456789abcdef;\n"
#else
        "{undefined}: 89abcdef;\n"
#endif
        "t: <\t\t>;\n"
        "S: <  >;\n"
        "T: 2038-01-19 04:14:07;lT: 2038-01-19 04:14:07;llT: 2038-01-19 04:14:07.000000;\n"
        "U: 2038-01-19 03:14:07Z;lU: 2038-01-19 03:14:07;llU: 2038-01-19 03:14:07.000000Z;\n"
        "r: SUCCESS;"};

    init();
    output_stream_t baos;
    yatest_log("char: %i, short: %i, int: %i, long: %i, long long: %i, float: %i, double: %i, long double: %i", sizeof(char), sizeof(short), sizeof(int), sizeof(long), sizeof(long long), sizeof(float), sizeof(double), sizeof(long double));
    bytearray_output_stream_init(&baos, NULL, 65536);
    uint16_t dnstype = TYPE_ANY;
    osformat(&baos,
             "%%;"
             "\n"
             "-11i: <%-11i><%-11i>;"
             "11i: <%11i><%11i>;"
             "lli: %lli %lli;"
             "li: %li %li;"
             "i: %i %i;"
             "hi: %hi %hi;"
             "hhi: %hhi %hhi;"
             "\n"
             "-11d: <%-11d><%-11d>;"
             "11d: <%11d><%11d>;"
             "lld: %lld %lld;"
             "ld: %ld %ld;"
             "d: %d %d;"
             "hd: %hd %hd;"
             "hhd: %hhd %hhd;"
             "\n"
             "-11o: <%-11o><%-11o>;"
             "11o: <%11o><%11o>;"
             "llo: %llo %llo;"
             "lo: %lo %lo;"
             "o: %o %o;"
             "ho: %ho %ho;"
             "hho: %hho %hho;"
             "\n"
             "-11x: <%-11x><%-11x>;"
             "11x: <%11x><%11x>;"
             "llx: %llx %llx;"
             "lx: %lx %lx;"
             "x: %x %x;"
             "hx: %hx %hx;"
             "hhx: %hhx %hhx;"
             "\n"
             "-11X: <%-11X><%-11X>;"
             "11X: <%11X><%11X>;"
             "llX: %llX %llX;"
             "lX: %lX %lX;"
             "X: %X %X;"
             "hX: %hX %hX;"
             "hhX: %hhX %hhX;"
             "\n"
             "P: %P;"
             "\n"
             "p: %p;"
             "\n"
             "f: %f; Lf: %Lf;"
             "11.8f: %11.8f; L11.8f: %11.8Lf;"
             "\n"
             "s: <%12s><%-12s> %s;"
             "\n"
             "c: %c;"
             "\n"
             "{dnstype}: %{dnstype};"
             "\n"
             "{undefined}: %{undefined};"
             "\n"
             "t: <%t>;"
             "\n"
             "S: <%S>;"
             "\n"
             "T: %T;"
             "lT: %lT;"
             "llT: %llT;"
             "\n"
             "U: %U;"
             "lU: %lU;"
             "llU: %llU;"
             "\n"
             "r: %r;",
             INT32_MIN, // -11i
             INT32_MAX,
             INT32_MIN, // 11i
             INT32_MAX,
             INT64_MIN, // lli
             INT64_MAX,
             INT32_MIN, // li
             INT32_MAX,
             INT32_MIN, // i
             INT32_MAX,
             INT16_MIN, // hi
             INT16_MAX,
             INT8_MIN, // hhi
             INT8_MAX,

             0, // -11d
             UINT32_MAX,
             0, // 11d
             UINT32_MAX,
             0ULL, // lld
             UINT64_MAX,
             0, // ld
             UINT32_MAX,
             0, // d
             UINT32_MAX,
             0, // hd
             UINT16_MAX,
             0, // hhd
             UINT8_MAX,

             0, // -11o
             UINT32_MAX,
             0, // 11o
             UINT32_MAX,
             0ULL, // llo
             UINT64_MAX,
             0, // lo
             UINT32_MAX,
             0, // o
             UINT32_MAX,
             0, // ho
             UINT16_MAX,
             0, // hho
             UINT8_MAX,

             0, // -11x
             UINT32_MAX,
             0, // 11x
             UINT32_MAX,
             0ULL, // llx
             UINT64_MAX,
             0, // lx
             UINT32_MAX,
             0, // x
             UINT32_MAX,
             0, // hx
             UINT16_MAX,
             0, // hhx
             UINT8_MAX,

             0, // -11X
             UINT32_MAX,
             0, // 11X
             UINT32_MAX,
             0ULL, // llX
             UINT64_MAX,
             0, // lX
             UINT32_MAX,
             0, // X
             UINT32_MAX,
             0, // hX
             UINT16_MAX,
             0, // hhX
             UINT8_MAX,

             NULL, // P

             NULL, // p

             3.1415, // f

             (long double)3.1415, // Lf

             3.1415, // f

             (long double)3.1415, // Lf

             "Hello World", // s
             "Hello World",
             "Hello World",

             '.', // c

             &dnstype, // {dnstype}

             (intptr_t)0x123456789abcdef, // {undefined}

             2, // t

             2, // S

             INT32_MAX,             // T
             1000000LL * INT32_MAX, // lT
             1000000LL * INT32_MAX, // llT

             INT32_MAX,             // U
             1000000LL * INT32_MAX, // lU
             1000000LL * INT32_MAX, // llU

             SUCCESS // r
    );
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_wrapped_test()
{
    static const char expected_output[] = {
        "....Hello World\n"
        "  Press any \n"
        "  key. 0 1 2 3 \n"
        "  4 5 6 7 8 ab \n"
        "  cd ef gh ijk \n"
        "  lmn opq rst\n"
        "        last \n"
        "  line"};
    init();
    static const char text[] = "Hello World\tPress any key. 0 1 2 3 4 5 6 7 8 ab cd ef gh ijk lmn opq rst\n\t\t\t\t\r\nlast line";
    output_stream_t   baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    output_stream_write(&baos, "....", 4);  // to go to column 4
    osprint_wrapped(&baos, text, 4, 16, 2); // column 4 on a 16 columns screen, wrapping at column 2
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int debug_osformatln_test()
{
    static const char expected_output[] = {"Hello World 00000000\n"};
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    debug_osformatln(&baos, "Hello World %08x", 0);
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    char *p = (char *)bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos) - sizeof(expected_output);
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, p, sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected: (minus the prefix)");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int debug_println_test()
{
    static const char expected_output[] = {"Hello World 00000000\n"};
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);
    debug_println("Hello World 00000000");
    output_stream_write_u8(termout, 0);
    output_stream_flush(termout);
    yatest_log((const char *)bytearray_output_stream_buffer(termout));
    char *p = (char *)bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout) - sizeof(expected_output);
    if((bytearray_output_stream_size(termout) < sizeof(expected_output)) || (memcmp(expected_output, p, sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(termout), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(termout), bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout));
        yatest_err("expected: (minus the prefix)");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static int println_test()
{
    static const char expected_output[] = {"Hello World 00000000\n"};
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);
    println("Hello World 00000000");
    output_stream_write_u8(termout, 0);
    output_stream_flush(termout);
    yatest_log((char *)bytearray_output_stream_buffer(termout));
    if((bytearray_output_stream_size(termout) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(termout), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(termout), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(termout), bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static int print_test()
{
    static const char expected_output[] = {"Hello World 00000000"};
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);
    print("Hello World 00000000");
    output_stream_write_u8(termout, 0);
    output_stream_flush(termout);
    yatest_log((char *)bytearray_output_stream_buffer(termout));
    if((bytearray_output_stream_size(termout) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(termout), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(termout), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(termout), bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static int print_char_test()
{
    static const char expected_output[] = {"Hello World"};
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);

    for(int i = 0; expected_output[i] != '\0'; ++i)
    {
        print_char(expected_output[i]);
    }
    output_stream_write_u8(termout, 0);
    output_stream_flush(termout);
    yatest_log((char *)bytearray_output_stream_buffer(termout));
    if((bytearray_output_stream_size(termout) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(termout), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(termout), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(termout), bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static int asnformat_test()
{
    static const char expected_output[] = {"Hello World 00000000"};
    init();
    char     *text = NULL;
    ya_result ret = asnformat(&text, 1024, "Hello World %08x", 0);
    if(ret < 0)
    {
        yatest_err("asnformat returned %08x", ret);
        return 1;
    }
    if(strcmp(text, expected_output) != 0)
    {
        yatest_err("output content doesn't match expectations:");
        yatest_err("got:");
        yatest_hexdump_err(text, text + strlen(text));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static void format_handler_test(const void *p, output_stream_t *os, int32_t a, char b, bool c, void *reserved_for_method_parameters)
{
    (void)a;
    (void)b;
    (void)c;
    (void)reserved_for_method_parameters;
    uintptr_t val = (uintptr_t)p;
    do
    {
        output_stream_write_u8(os, '0' + (val % 10));
        val /= 10;
    } while(val != 0);
}

static int format_grow_hash_table_test()
{
    // format_grow_hash_table() is called when the format table grows
    ya_result ret;
    init();
    char      tmp[64];
    const int fmt_count = 1117;
    for(int i = 0; i < fmt_count; ++i)
    {
        format_handler_descriptor_t *fhd = (format_handler_descriptor_t *)yatest_malloc(sizeof(format_handler_descriptor_t));
        fhd->format_handler = format_handler_test;
        snprintf(tmp, sizeof(tmp), "fmt%i", i);
        fhd->name = yatest_strdup(tmp);
        fhd->name_len = strlen(fhd->name);
        ret = format_registerclass(fhd);
        if(ret < 0)
        {
            yatest_err("format_registerclass failed for '%s' with %08x = %s", tmp, ret, error_gettext(ret));
            return 1;
        }
        if(i == 0)
        {
            ret = format_registerclass(fhd);
            if(ret != FORMAT_ALREADY_REGISTERED)
            {
                yatest_err("format_registerclass expected to return FORMAT_ALREADY_REGISTERED, returned %08x = %s", tmp, ret, error_gettext(ret));
                return 1;
            }
        }
    }
    for(int i = 0; i < fmt_count; ++i)
    {
        char txt[64];
        memset(txt, 0, sizeof(txt));
        snprintf(tmp, sizeof(tmp), "%%{fmt%i}", i);
#if __SIZEOF_POINTER__ == 8
        snformat(txt, sizeof(txt), tmp, (void *)(uintptr_t)18446744073709551615ULL);
        const char *expected = "51615590737044764481";
#else
        snformat(txt, sizeof(txt), tmp, (void *)(uintptr_t)4294967295UL);
        const char *expected = "5927694924";
#endif
        if(strcmp(txt, expected) != 0)
        {
            yatest_err("formatting failed with '%s' ('%s' != '%s')", tmp, txt, expected);
            return 1;
        }
    }
    finalise();
    return 0;
}

static int osprint_base16_test()
{
    int               ret;
    static const char expected_output[] = {
        "123456789ABCDEF0"
        "123456789ABCDEF1"
        "123456789ABCDEF2"
        "123456789ABCDEF3"
        " " // one space after 32 bytes
        "123456789ABCDEF4"};
    static const uint8_t data[40] = {
        0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf1, 0x12, 0x34, 0x56, 0x78,
        0x9a, 0xbc, 0xde, 0xf2, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf3, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf4,
    };
    init();
    output_stream_t os;
    bytearray_output_stream_init(&os, NULL, 65536);

    ret = osprint_base16(&os, data, sizeof(data));
    if(ret < 0)
    {
        yatest_err("osprint_base16 returned %08x = %s", ret, error_gettext(ret));
        return 1;
    }
    output_stream_write_u8(&os, 0);
    output_stream_flush(&os);
    yatest_log((char *)bytearray_output_stream_buffer(&os));
    if((bytearray_output_stream_size(&os) != sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&os), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&os), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&os), bytearray_output_stream_buffer(&os) + bytearray_output_stream_size(&os));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    // output_stream_close(termout);
    finalise();
    return 0;
}

static int osprint_u32_test()
{
    static const char expected_output[] = {"1234567890"};
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_u32(&baos, 1234567890);
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_u16_test()
{
    static const char expected_output[] = {"56789"};
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_u16(&baos, 56789);
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_u32_hex_test()
{
    static const char expected_output[] = {"12345678"};
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_u32_hex(&baos, 0x12345678);
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_quoted_text_escaped_test()
{
    static const char  expected_output[] = {"\"Hello World, CHR(1)=\\001, CR=\\013, LF=\\010, ESCAPE=\\\\\""};
    static const char *text = "Hello World, CHR(1)=\001, CR=\r, LF=\n, ESCAPE=\\";
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_quoted_text_escaped(&baos, (const uint8_t *)text, strlen(text));
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_char_times_test()
{
    static const char expected_output[] = {"AAAAA"};

    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_char_times(&baos, 'A', 5);
    output_stream_write_u8(&baos, 0);
    output_stream_flush(&baos);
    yatest_log((const char *)bytearray_output_stream_buffer(&baos));
    if((bytearray_output_stream_size(&baos) < sizeof(expected_output)) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), sizeof(expected_output)) != 0))
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", bytearray_output_stream_size(&baos), sizeof(expected_output));
        yatest_err("got:");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int snformat_test()
{
    int               ret;
    static const char expected_output[] = {"Hello World"};

    char              tmp[64];
    init();
    ret = snformat(tmp, sizeof(tmp), "%s %s", "Hello", "World");
    if(ret < 0)
    {
        yatest_err("snformat failed with %08x = %s", ret, error_gettext(ret));
        return 1;
    }
    yatest_log(tmp);
    if(strcmp(tmp, expected_output) != 0)
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", strlen(tmp), sizeof(expected_output) - 1);
        yatest_err("got:");
        yatest_hexdump_err(tmp, tmp + strlen(tmp));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output) - 1);
        return 1;
    }
    finalise();
    return 0;
}

static int asformat_test()
{
    int               ret;
    static const char expected_output[] = {"Hello World"};
    char             *tmp = NULL;
    init();
    ret = asformat(&tmp, "%s %s", "Hello", "World");
    if(ret < 0)
    {
        yatest_err("asformat failed with %08x = %s", ret, error_gettext(ret));
        return 1;
    }
    yatest_log(tmp);
    if(strcmp(tmp, expected_output) != 0)
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", strlen(tmp), sizeof(expected_output) - 1);
        yatest_err("got:");
        yatest_hexdump_err(tmp, tmp + strlen(tmp));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output) - 1);
        return 1;
    }
    free(tmp);
    finalise();
    return 0;
}

static int fformat_test()
{
    int               ret;
    static const char expected_output[] = {"Hello World"};
    init();
    char filename[64];
    char tmp[64];
    yatest_file_getname(11, filename, sizeof(filename));
    FILE *f = fopen(filename, "w+");
    if(f == NULL)
    {
        yatest_err("fopen(%s, \"w+\") failed with %s", strerror(errno));
        return 1;
    }
    ret = fformat(f, "%s %s", "Hello", "World");
    if(ret < 0)
    {
        yatest_err("asformat failed with %08x = %s", ret, error_gettext(ret));
        return 1;
    }
    char zero[1] = {0};
    fwrite(zero, 1, 1, f);
    fclose(f);
    f = fopen(filename, "r");
    int tmp_size = fread(tmp, 1, 64, f);
    if(tmp_size != sizeof(expected_output))
    {
        yatest_err("fread returned %i instead of %i", tmp_size, sizeof(expected_output));
        return 1;
    }
    fclose(f);
    yatest_log(tmp);
    if(strcmp(tmp, expected_output) != 0)
    {
        yatest_err("output content doesn't match expectations: (sizes: %i vs %i)", strlen(tmp), sizeof(expected_output) - 1);
        yatest_err("got:");
        yatest_hexdump_err(tmp, tmp + strlen(tmp));
        yatest_err("expected:");
        yatest_hexdump_err(expected_output, expected_output + sizeof(expected_output) - 1);
        return 1;
    }
    finalise();
    return 0;
}

/**
 * That one will be complicated:
 * I need an rdata for all types, and the expected output.
 */

struct osprint_rdata_test_entry_s
{
    uint16_t      rtype;
    const uint8_t rdata[256];
    uint16_t      rdata_size;
    const char   *expected_text;
    int           error;
};

static const struct osprint_rdata_test_entry_s osprint_rdata_test_entries[] = {
    {TYPE_A, {1, 2, 3, 4}, 4, "1.2.3.4", 0},
    {TYPE_A, {}, 5, "", INCORRECT_RDATA},
    {TYPE_AAAA, {0x26, 0x06, 0x47, 0x00, 0x30, 0x32, 0, 0, 0, 0, 0, 0, 0x68, 0x15, 0x29, 0xb8}, 16, "2606:4700:3032::6815:29b8", 0},
    {TYPE_AAAA, {}, 17, "", INCORRECT_RDATA},
    {TYPE_MX, {0x00, 0x0a, 4, 'm', 'a', 'i', 'l', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 18, "10 mail.yadifa.eu.", 0},
    {TYPE_MX, {0x00, 0x0a}, 2, "", INCORRECT_RDATA},
    {TYPE_NS, {3, 'n', 's', '1', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 15, "ns1.yadifa.eu.", 0},
    {TYPE_RP, {3, 'n', 's', '1', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 3, 'n', 's', '2', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 30, "ns1.yadifa.eu. ns2.yadifa.eu.", 0},
    {TYPE_RP, {}, 0, "", INCORRECT_RDATA},
    {TYPE_PX, {0, 10, 3, 'n', 's', '1', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 3, 'n', 's', '2', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 32, "10 ns1.yadifa.eu. ns2.yadifa.eu.", 0},
    {TYPE_PX,
     {
         0,
         10,
     },
     2,
     "",
     INCORRECT_RDATA},
    {TYPE_TALINK, {3, 'n', 's', '1', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 3, 'n', 's', '2', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 30, "ns1.yadifa.eu. ns2.yadifa.eu.", 0},
    {TYPE_TALINK, {}, 0, "", INCORRECT_RDATA},
    {TYPE_WKS, {1, 2, 3, 4, 17, 128, 128, 128}, 8, "1.2.3.4 udp 0 8 16", 0},
    {TYPE_WKS, {1, 2, 3, 4, 6, 128, 128, 128}, 8, "1.2.3.4 tcp 0 8 16", 0},
    {TYPE_WKS, {}, 5, "", INCORRECT_RDATA},
    {TYPE_GPOS, {7, '5', '0', '.', '8', '4', '6', '7', 7, '4', '.', '3', '5', '2', '5', '0', 2, '1', '3'}, 19, "50.8467 4.35250 13", 0},
    {TYPE_GPOS, {7, '5', '0', '.', '8', '4', '6', '7', 7, '4', '.', '3', '5', '2', '5', '0', 2, '1', '3'}, 18, "", INCORRECT_RDATA},
    {TYPE_LOC, {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}, 16, "591 49 34.588 S 591 49 34.588 W 69090.60m 0m 0m 0m", 0}, // need to find example
    {TYPE_LOC, {1, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}, 1, "", INCORRECT_RDATA},                                      // need to find example
    {TYPE_CSYNC, {0x00, 0x00, 0x00, 0x42, 0x00, 0x03, 0x00, 0x04, 0x60, 0x00, 0x00, 0x08}, 12, "66 3 A NS AAAA", 0},
    {TYPE_CSYNC, {1, 2, 3, 4, 0, 1, 0, 1, 1}, 9, "16909060 1 MB", 0},
    {TYPE_OPENPGPKEY, {0x94, 0x6e, 0x68, 0x8e, 0xa7, 0xf4, 0x05, 0xaa, 0x87, 0x4f, 0xef}, 11, "lG5ojqf0BaqHT+8=", 0},
    {TYPE_OPENPGPKEY, {}, 0, "", INCORRECT_RDATA},
    {TYPE_HINFO, {5, 'H', 'e', 'l', 'l', 'o', 5, 'W', 'o', 'r', 'l', 'd'}, 12, "\"Hello\" \"World\"", 0},
    {TYPE_HINFO, {2}, 1, "", INCORRECT_RDATA},
    {TYPE_HINFO, {1, 'X', 2, 'Y'}, 4, "", INCORRECT_RDATA},
    {TYPE_SOA, {3, 'n', 's', '1', 0, 4, 'm', 'a', '.', 'l', 0, 0, 0, 0, 1, 0, 0, 14, 16, 0, 0, 7, 8, 0, 54, 238, 128, 0, 0, 2, 88}, 31, "ns1. ma\\.l. 1 3600 1800 3600000 600", 0},
    {TYPE_SOA, {3, 'n', 's', '1', 0}, 5, "", INCORRECT_RDATA},
    {TYPE_RRSIG,
     {0, 6, 8, 2, 0, 0, 14, 16, 0, 0, 0, 0, 127, 255, 255, 255, 12, 34, 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 'F', 'A', 'K', 'E', ' ', 'S', 'I', 'G', 'N', 'A', 'T', 'U', 'R', 'E'},
     43,
     "SOA 8 2 3600 19700101000000 20380119031407 3106 yadifa.eu. RkFLRSBTSUdOQVRVUkU=",
     0},
    {TYPE_DNSKEY, {1, 1, 3, 8, 'F', 'A', 'K', 'E', ' ', 'K', 'E', 'Y'}, 12, "257 3 8 RkFLRSBLRVk=", 0},
    {TYPE_DS, {0x12, 0x34, 8, 1, 1, 2, 3, 4, 5, 6, 7, 8}, 12, "4660 8 1 0102030405060708", 0},
    {TYPE_NSEC,
     {4, 'n', 'e', 'x', 't', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 0, 2, 127, 255, 1, 1, 255},
     23,
     "next.yadifa.eu. A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX URI CAA AVC TYPE259 TYPE260 TYPE261 "
     "TYPE262 TYPE263",
     0},
    {TYPE_NSEC3,
     {1, 1, 0, 10, 0, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 2, 127, 255, 1, 1, 255},
     33,
     "1 1 10 - 041061050O3GG28A1C60Q3GF208H44OK A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX URI CAA AVC "
     "TYPE259 TYPE260 TYPE261 TYPE262 TYPE263",
     0},
    {TYPE_NSEC3,
     {1, 1, 0, 10, 2, 0xba, 0x11, 20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 2, 127, 255, 1, 1, 255},
     35,
     "1 1 10 ba11 041061050O3GG28A1C60Q3GF208H44OK A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX URI CAA "
     "AVC TYPE259 TYPE260 TYPE261 TYPE262 TYPE263",
     0},
    {TYPE_NSEC3PARAM, {1, 0, 0, 10, 0}, 5, "1 0 10 -", 0},
    {TYPE_NSEC3PARAM, {1, 0, 0, 10, 2, 0xba, 0x11}, 7, "1 0 10 ba11", 0},
    {TYPE_TLSA, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, "1 2 3 0405060708090A", 0},
    {TYPE_SSHFP, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 10, "1 2 030405060708090A", 0},
    {TYPE_NID, {0, 10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, 10, "10 1234:5678:9abc:def0", 0},
    {TYPE_NID, {}, 0, "", INCORRECT_RDATA},
    {TYPE_L64, {0, 10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}, 10, "10 1234:5678:9abc:def0", 0},
    {TYPE_L32, {0, 10, 172, 20, 1, 1}, 6, "10 172.20.1.1", 0},
    {TYPE_L32, {}, 0, "", INCORRECT_RDATA},
    {TYPE_EUI48, {0, 0, 127, 128, 129, 130}, 6, "00-00-7f-80-81-82", 0},
    {TYPE_EUI48, {}, 0, "", INCORRECT_RDATA},
    {TYPE_EUI64, {0, 0, 127, 128, 129, 130, 131, 132}, 8, "00-00-7f-80-81-82-83-84", 0},
    {TYPE_EUI64, {}, 0, "", INCORRECT_RDATA},
    {TYPE_SRV, {12, 34, 56, 78, 1, 80, 3, 'w', 'w', 'w', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 21, "8716 20024 20481 www.yadifa.eu.", 0},
    {TYPE_TXT, {11, 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'}, 12, "\"Hello World\"", 0},
    {TYPE_TXT, {5, 'H', 'e', 'l', 'l', 'o', 5, 'W', 'o', 'r', 'l', 'd'}, 12, "\"Hello\" \"World\"", 0},
    {TYPE_TXT, {5, 'H', 'e', 'l', 'l', 'o', 5, 'W', 'o', 'r', 'l'}, 11, "", INCORRECT_RDATA},
    {TYPE_CAA, {0, 3, 't', 'a', 'g', 'h', 'e', 'l', 'l', 'o', ';', ' ', 'w', 'o', 'r', 'l', 'd'}, 17, "0 tag \"hello; world\"", 0},
    {TYPE_CAA, {}, 0, "", INCORRECT_RDATA},
    {TYPE_CERT,
     {0, 1, 0x12, 0x34, 8, 'h', 't', 't', 'p', ':', '/', '/', 'w', 'w', 'w', '.', 'y', 'a', 'd', 'i', 'f', 'a', '.', 'e', 'u', '/', 'c', 'e', 'r', 't', '.', 'p', 'e', 'm'},
     34,
     "PKIX 4660 RSASHA256 aHR0cDovL3d3dy55YWRpZmEuZXUvY2VydC5wZW0=",
     0},
    {TYPE_CERT,
     {255, 255, 0x12, 0x34, 240, 'h', 't', 't', 'p', ':', '/', '/', 'w', 'w', 'w', '.', 'y', 'a', 'd', 'i', 'f', 'a', '.', 'e', 'u', '/', 'c', 'e', 'r', 't', '.', 'p', 'e', 'm'},
     34,
     "65535 4660 240 aHR0cDovL3d3dy55YWRpZmEuZXUvY2VydC5wZW0=",
     0},
    {TYPE_CERT, {}, 0, "", INCORRECT_RDATA},
    {TYPE_DHCID, {1, 2, 3, 4, 5, 6, 7, 8}, 8, "AQIDBAUGBwg=", 0}, // 29
    {TYPE_TSIG,
     {4, 'f', 'a', 'k', 'e', 9, 'a', 'l', 'g', 'o', 'r', 'i', 't', 'h', 'm', 0, 0, 0, 0x66, 0x5d, 0x8e, 0x47, 0, 0x2c, 0, 4, 1, 2, 3, 4, 0x01, 0x23, 0, 0, 0, 2, 3, 4},
     38,
     "fake.algorithm. 1717407303 44 4 AQIDBA== 8961 NOERROR 2 03 04",
     0},
    {TYPE_ANY, {1, 2, 3, 4}, 4, "\\# 4 01020304", 0},
    // YADIFA CTRL types
    {TYPE_ZONE_TYPE, {0}, 1, ZT_HINT_STRING, 0},
    {TYPE_ZONE_TYPE, {1}, 1, ZT_PRIMARY_STRING, 0},
    {TYPE_ZONE_TYPE, {2}, 1, ZT_SECONDARY_STRING, 0},
    {TYPE_ZONE_TYPE, {3}, 1, ZT_STUB_STRING, 0},
    {TYPE_ZONE_TYPE, {4}, 1, "undefined", 0},
    {TYPE_ZONE_PRIMARY, {0x34, 172, 20, 1, 1, 0, 53, 5, 'm', 'y', 'k', 'e', 'y', 0}, 14, "172.20.1.1 53 mykey.", 0},
    {TYPE_ZONE_PRIMARY, {0x36, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0, 53, 5, 'm', 'y', 'k', 'e', 'y', 0}, 26, "1234:5678:9abc:def0:1234:5678:9abc:def0 53 mykey.", 0},
    {TYPE_CTRL_ZONEFREEZE, {}, 0, "", 0},
    {TYPE_CTRL_ZONEFREEZE, {6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 11, "yadifa.eu.", 0},
    {TYPE_CTRL_SRVLOGLEVEL, {1}, 1, "01", 0},
    {TYPE_CTRL_SRVLOGLEVEL, {}, 0, "", INCORRECT_RDATA},
    {TYPE_CTRL_ZONESYNC, {1, 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 12, "01 yadifa.eu.", 0},
    {TYPE_CTRL_ZONESYNC, {}, 0, "", INCORRECT_RDATA},
    //{TYPE_, {}, 0, "", 0},
    {0, {}, 0, NULL, 0}};

static const struct osprint_rdata_test_entry_s osprint_rdata_escaped_test_entries[] = {
    {TYPE_MX, {0x00, 0x0a, 4, 'm', 'a', 'i', 'l', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 18, "10 mail.yadifa.eu.", 0},
    {TYPE_TALINK, {3, 'n', 's', '1', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 3, 'n', 's', '2', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 30, "ns1.yadifa.eu. ns2.yadifa.eu.", 0},
    {TYPE_SOA, {3, 'n', 's', '1', 0, 4, 'm', 'a', '.', 'l', 0, 0, 0, 0, 1, 0, 0, 14, 16, 0, 0, 7, 8, 0, 54, 238, 128, 0, 0, 2, 88}, 31, "ns1. ma\\.l. 1 3600 1800 3600000 600", 0},
    {TYPE_RRSIG,
     {0, 6, 8, 2, 0, 0, 14, 16, 0, 0, 0, 0, 127, 255, 255, 255, 12, 34, 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 'F', 'A', 'K', 'E', ' ', 'S', 'I', 'G', 'N', 'A', 'T', 'U', 'R', 'E'},
     43,
     "SOA 8 2 3600 19700101000000 20380119031407 3106 yadifa.eu. RkFLRSBTSUdOQVRVUkU=",
     0},
    {TYPE_NSEC,
     {4, 'n', 'e', 'x', 't', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0, 0, 2, 127, 255, 1, 1, 255},
     23,
     "next.yadifa.eu. A NS MD MF CNAME SOA MB MG MR NULL WKS PTR HINFO MINFO MX URI CAA AVC TYPE259 TYPE260 TYPE261 "
     "TYPE262 TYPE263",
     0},
    {TYPE_SRV, {12, 34, 56, 78, 1, 80, 3, 'w', 'w', 'w', 6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0}, 21, "8716 20024 20481 www.yadifa.eu.", 0},
    {0, {}, 0, NULL, 0}};

static int rdata_printer_test(const struct osprint_rdata_test_entry_s *entries, ya_result (*rdata_printer)(output_stream_t *, uint16_t, const uint8_t *, uint16_t))
{
    int ret;
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);

    for(int i = 0; entries[i].expected_text != NULL; ++i)
    {
        if(entries[i].error == 0)
        {
            yatest_log("[%3i] record type %i expects '%s'", i, ntohs(entries[i].rtype), entries[i].expected_text);
        }
        else
        {
            yatest_log("[%3i] record type %i expects %08x = %s", i, ntohs(entries[i].rtype), entries[i].error, error_gettext(entries[i].error));
        }
        bytearray_output_stream_reset(&baos);
        ret = rdata_printer(&baos, entries[i].rtype, entries[i].rdata, entries[i].rdata_size);
        if(ret < 0)
        {
            if(entries[i].error == 0)
            {
                yatest_err("[%3i] osprint_rdata DNS type %i failed with %08x = %s", i, ntohs(entries[i].rtype), ret, error_gettext(ret));
                return 1;
            }
            else
            {
                if(ret != entries[i].error)
                {
                    yatest_err("[%3i] osprint_rdata DNS type %i failed with %08x = %s, expected to fail with %08x = %s", i, ntohs(entries[i].rtype), ret, error_gettext(ret), entries[i].error, error_gettext(entries[i].error));
                    return 1;
                }
                else
                {
                    continue;
                }
            }
        }
        output_stream_write_u8(&baos, 0);
        output_stream_flush(&baos);
        yatest_log((const char *)bytearray_output_stream_buffer(&baos));
        const char *expected_output = entries[i].expected_text;
        size_t      expected_output_size = strlen(expected_output) + 1;
        if((bytearray_output_stream_size(&baos) != expected_output_size) || (memcmp(expected_output, bytearray_output_stream_buffer(&baos), expected_output_size) != 0))
        {
            yatest_err("output content [%3i] doesn't match expectations: (sizes: %i vs %i)", i, bytearray_output_stream_size(&baos), expected_output_size);
            yatest_err("got:");
            yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
            yatest_err("expected:");
            yatest_hexdump_err(expected_output, expected_output + expected_output_size);
            return 1;
        }
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_rdata_test() { return rdata_printer_test(osprint_rdata_test_entries, osprint_rdata); }

static int osprint_rdata_escaped_test() { return rdata_printer_test(osprint_rdata_escaped_test_entries, osprint_rdata_escaped); }

static int print_rdata_test()
{
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);
    print_rdata(osprint_rdata_test_entries[0].rtype, osprint_rdata_test_entries[0].rdata, osprint_rdata_test_entries[0].rdata_size);
    output_stream_write_u8(termout, 0);
    yatest_log("'%s'", bytearray_output_stream_buffer(termout));
    if(strcmp((const char *)bytearray_output_stream_buffer(termout), osprint_rdata_test_entries[0].expected_text) != 0)
    {
        yatest_err("got '%s', expected '%s'", bytearray_output_stream_buffer(termout), osprint_rdata_test_entries[0].expected_text);
        return 1;
    }

    finalise();
    return 0;
}

static int osprint_dump_with_base_test()
{
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    osprint_dump_with_base(&baos, yatest_lorem_ipsum, 33, 16, OSPRINT_DUMP_LAYOUT_DENSE | OSPRINT_DUMP_BUFFER, yatest_lorem_ipsum);
    output_stream_write_u8(&baos, 0);
    yatest_log("'%s'", bytearray_output_stream_buffer(&baos));
    static const char expect_dense_buffer[] =
        "0000 | 4c6f72656d20697073756d20646f6c6f |  Lorem ipsum dolo\n"
        "0010 | 722073697420616d65742c20636f6e73 |  r sit amet, cons\n"
        "0020 | 65                               |  e";
    if(strcmp((const char *)bytearray_output_stream_buffer(&baos), expect_dense_buffer) != 0)
    {
        yatest_err("OSPRINT_DUMP_LAYOUT_DENSE|OSPRINT_DUMP_BUFFER");
        yatest_err("got");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected");
        yatest_hexdump_err(expect_dense_buffer, expect_dense_buffer + sizeof(expect_dense_buffer));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_dump_with_base_squeezable_test()
{
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    static const uint8_t squeezable[] = {'Z', 'E', 'R', 'O', 'E', 'S', 0, 0, 0, 0, 0, 0, 0, 0, '<', '>', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                         0,   0,   0,   0,   0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0,   0,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    osprint_dump_with_base(&baos, squeezable, sizeof(squeezable), 16, OSPRINT_DUMP_LAYOUT_DENSE | OSPRINT_DUMP_BUFFER | OSPRINT_DUMP_SQUEEZE_ZEROES, squeezable);
    output_stream_write_u8(&baos, 0);
    yatest_log("'%s'", bytearray_output_stream_buffer(&baos));
    static const char expect_dense_buffer[] =
        "0000 | 5a45524f455300000000000000003c3e |  ZEROES........<>\n"
        "0040 | 00                               |  .";
    if(strcmp((const char *)bytearray_output_stream_buffer(&baos), expect_dense_buffer) != 0)
    {
        yatest_err("OSPRINT_DUMP_LAYOUT_DENSE|OSPRINT_DUMP_BUFFER");
        yatest_err("got");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected");
        yatest_hexdump_err(expect_dense_buffer, expect_dense_buffer + sizeof(expect_dense_buffer));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int osprint_question_test()
{
    init();
    output_stream_t baos;
    bytearray_output_stream_init(&baos, NULL, 65536);
    static const uint8_t yadifa_eu[] = {6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0};
    osprint_question(&baos, yadifa_eu, CLASS_IN, TYPE_A);
    output_stream_write_u8(&baos, 0);
    yatest_log("'%s'", bytearray_output_stream_buffer(&baos));
    static const char expected[] = ";; QUESTION SECTION:\nyadifa.eu. IN A\n\n";
    if(strcmp((const char *)bytearray_output_stream_buffer(&baos), expected) != 0)
    {
        yatest_err("got");
        yatest_hexdump_err(bytearray_output_stream_buffer(&baos), bytearray_output_stream_buffer(&baos) + bytearray_output_stream_size(&baos));
        yatest_err("expected");
        yatest_hexdump_err(expected, expected + sizeof(expected));
        return 1;
    }
    output_stream_close(&baos);
    finalise();
    return 0;
}

static int print_question_test()
{
    init();
    output_stream_close(termout);
    bytearray_output_stream_init(termout, NULL, 65536);
    static const uint8_t yadifa_eu[] = {6, 'y', 'a', 'd', 'i', 'f', 'a', 2, 'e', 'u', 0};
    print_question(yadifa_eu, CLASS_IN, TYPE_A);
    output_stream_write_u8(termout, 0);
    yatest_log("'%s'", bytearray_output_stream_buffer(termout));
    static const char expected[] = ";; QUESTION SECTION:\nyadifa.eu. IN A\n\n";
    if(strcmp((const char *)bytearray_output_stream_buffer(termout), expected) != 0)
    {
        yatest_err("got");
        yatest_hexdump_err(bytearray_output_stream_buffer(termout), bytearray_output_stream_buffer(termout) + bytearray_output_stream_size(termout));
        yatest_err("expected");
        yatest_hexdump_err(expected, expected + sizeof(expected));
        return 1;
    }
    finalise();
    return 0;
}

YATEST_TABLE_BEGIN
YATEST(osformat_test)
YATEST(osprint_wrapped_test)
YATEST(debug_osformatln_test)
YATEST(debug_println_test)
YATEST(println_test)
YATEST(print_test)
YATEST(print_char_test)
YATEST(asnformat_test)
YATEST(format_grow_hash_table_test)
YATEST(osprint_base16_test)
YATEST(osprint_u32_test)
YATEST(osprint_u16_test)
YATEST(osprint_u32_hex_test)
YATEST(osprint_quoted_text_escaped_test)
YATEST(osprint_char_times_test)
YATEST(snformat_test)
YATEST(asformat_test)
YATEST(fformat_test)
YATEST(osprint_rdata_test)
YATEST(osprint_rdata_escaped_test)
YATEST(print_rdata_test)
YATEST(osprint_dump_with_base_test)
YATEST(osprint_dump_with_base_squeezable_test)
YATEST(osprint_question_test)
YATEST(print_question_test)
YATEST_TABLE_END