aboutsummaryrefslogtreecommitdiffhomepage
path: root/src-cslul/unittest/test_mhparse.c
blob: ad45d3abe0dc87809ecc0a6312e69ecd7b9718af (plain)
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
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356

/*

  Unit tests of mhparse.c

  Copyright © 2021-2024 Samuel Lidén Borell <samuel@kodafritt.se>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

#include "../mhparse.c"
#define MHPARSE
#include "parsecommon.h"
#include "unittest.h"
#include "alltests.h"

#include <stdlib.h>
#include <string.h>

#define BASE_ASSERT_DEP(_dep, _name, _flags) do { \
        tassert((_dep) != NULL); \
        tsoftassert((_dep)->node.length == sizeof(_name)-1); \
        tassert(node_nameptr(&(_dep)->node) != NULL); \
        tsoftassert(!memcmp(node_nameptr(&(_dep)->node), (_name), sizeof(_name)-1)); \
        tsoftassert((_dep)->flags == (_flags)); \
    } while (0)
/* These are separate macros to avoid "NULL argument" warnings
   from GCC depending on compiler version and flags. */
#define ASSERT_DEP(_dep, _name, _ver, _flags) do { \
        BASE_ASSERT_DEP(_dep, _name, _flags); \
        tsoftassert((_dep)->minverlen == sizeof(_ver)-1); \
        tassert((_dep)->min_version != NULL); \
        tsoftassert(!memcmp((_dep)->min_version, (_ver), sizeof(_ver)-1)); \
    } while (0)
#define ASSERT_DEP_NOVERSION(_dep, _name, _flags) do { \
        BASE_ASSERT_DEP(_dep, _name, _flags); \
        tsoftassert((_dep)->minverlen == 0); \
        tsoftassert((_dep)->min_version == NULL); \
    } while (0)

#define ASSERT_IDEP(_idep, _dep, _modname, _minver, _sincever) do { \
        tassert((_idep) != NULL); \
        if (tsoftassert((_idep)->minverlen == sizeof(_minver)-1)) { \
            tassert((_idep)->min_version != NULL); \
            tsoftassert(!memcmp((_idep)->min_version, _minver, sizeof(_minver)-1)); \
        } \
        if (tsoftassert((_idep)->node.length == sizeof(_sincever)-1)) { \
            tassert(node_nameptr(&(_idep)->node) != NULL); \
            tsoftassert(!memcmp(node_nameptr(&(_idep)->node), _sincever, sizeof(_sincever)-1)); \
        } \
        tsoftassert((_idep)->dep == (_dep)); \
    } while (0)

#define ASSERT_APIDEF_WITHOUT_HASH(_apidef, _ind, _verstr, _flags) do { \
        tassert(_apidef != NULL); \
        tsoftassert((_apidef)->index == (_ind)); \
        if (tsoftassert((_apidef)->verstr.length == sizeof(_verstr)-1)) { \
            tassert(node_nameptr(&(_apidef)->verstr) != NULL); \
            tsoftassert(!memcmp(node_nameptr(&(_apidef)->verstr), _verstr, sizeof(_verstr)-1)); \
        } \
        tsoftassert((_apidef)->flags == (_flags)); \
    } while (0)

#define ASSERT_APIDEF_WITH_HASH(_apidef, _ind, _verstr, _apihash, _flags) do { \
        ASSERT_APIDEF_WITHOUT_HASH(_apidef, _ind, _verstr, _flags); \
        tsoftassert(!memcmp((_apidef)->apihash, (_apihash), CSLUL_APIHASHBYTES)); \
    } while (0)

/** Runs before each test case */
static void before_test(void)
{
    set_mhparse(1); /* Set mode. */
}

/** Runs after each test case */
static void after_test(void)
{
    verify_errors();
}

static void test_mh_attrs_matchattr_attr2str(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    size_t left = (sizeof(attr2str)/sizeof(attr2str[0]));
    size_t i = 1;
    while (--left) {
        const char *attrname = attr2str[i];
        cslul_ll_set_input_buffer(ctx, attrname, strlen(attrname), 0);
        sourceline = __LINE__;
        tsoftassert(cslul_ll_next_mh_token(ctx) == CSLUL_MHT_NEEDDATA);

        cslul_ll_set_input_buffer(ctx, "\n", 1, 0);
        sourceline = __LINE__;
        tsoftassert(cslul_ll_next_mh_token(ctx) == CSLUL_MHT_AttrName);
        tsoftassert(cslul_ll_next_mh_token(ctx) == CSLUL_MHT_NEEDDATA);

        tsoftassert(match_attr(ctx) == i);
        i++;
    }
    ctx->tokval = "notfound";
    ctx->toklen = 8;
    tsoftassert(match_attr(ctx) == MANone);
    free_ctx(ctx);
}

static void check_mh_values(struct CSlul *ctx)
{
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == 0);
    tsoftassert(ctx->module.type == CSLUL_MT_LIBRARY);
    /* TODO implement and check the other fields */
}

static void test_mh_attrs_good(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name x\n"
                "\\type library\n"
                "\\version 0.1\n"
                "\\repo git://example/test\n"
                "\\repo.mirror http://example2/git\n"
                "\\website https://example/\n"
                "\\license MIT\n"
                "\\license.text LICENSE.txt\n"
                "\\license.list doc/licenses.txt\n"
                "\\api_def 0.1\n", 0);
    tsoftassert(ctx->parser.mh.attr == MAApiDef);
    check_mh_values(ctx);
    free_ctx(ctx);
}

static void test_mh_attrs_good_splitted(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\", 0);
    tsoftassert(ctx->parser.mh.attr == MAName);
    TEST_SOURCE("type library\n\\version 0.1", 0);
    tsoftassert(ctx->parser.mh.attr == MAVersion);
    TEST_SOURCE("\n\\repo git://example/test\n\\repo.mirror", 0);
    tsoftassert(ctx->parser.mh.attr == MARepo);
    TEST_SOURCE(" http://example2/git\n\\website https://example/\n", 0);
    tsoftassert(ctx->parser.mh.attr == MAWebsite);
    TEST_SOURCE("\\license MIT\n\\license.text LICENSE.txt\n\\license.list ", 0);
    tsoftassert(ctx->parser.mh.attr == MALicenseList);
    TEST_SOURCE("doc/licenses.txt\n\\api", 0);
    tsoftassert(ctx->parser.mh.attr == MALicenseList);
    TEST_SOURCE("_def ", 0);
    tsoftassert(ctx->parser.mh.attr == MAApiDef);
    check_mh_values(ctx);
    free_ctx(ctx);
}

static void test_mh_attrs_unknown1(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADATTRNAME, 2, 1);
    TEST_SOURCE("\\slul 0.0.0\n\\namee\n", 0);
    free_ctx(ctx);
}

static void test_mh_attrs_unknown2(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADATTRNAME, 2, 1);
    TEST_SOURCE("\\slul 0.0.0\n\\namee test 123\n", 0);
    free_ctx(ctx);
}

static void test_mh_attrs_wrongorder(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_WRONGATTRORDER, 3, 1);
    TEST_SOURCE("\\slul 0.0.0\n\\type app\n\\name x\n", 0);
    tsoftassert(ctx->parser.mh.attr == MAName);
    free_ctx(ctx);
}

static void test_mh_attrs_duplicate(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLICATEATTR, 3, 1);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\name x\n", 0);
    tsoftassert(ctx->parser.mh.attr == MAName);
    free_ctx(ctx);
}

static void test_mh_attrs_novalue(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MISSINGATTRVALUE, 2, 6);
    TEST_SOURCE("\\slul 0.0.0\n\\name\n\\type ", 0);
    free_ctx(ctx);
}

static void test_mh_attrs_badchars(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_VERSTRCHAR, 4, 10);
    expect_error(CSLUL_E_INVALIDCHAR, 4, 12);
    expect_error(CSLUL_E_BADMODULETYPE, 3, 7);
    expect_error(CSLUL_E_INVALIDCHAR, 3, 7);
    expect_error(CSLUL_E_MODNAMECHAR, 2, 7);
    expect_error(CSLUL_E_INVALIDCHAR, 2, 8);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name x\x01\n"
                "\\type \x1Fx\n"
                "\\version 0.\x7F\n", 0);
    free_ctx(ctx);
}

static void test_mh_eof_newline(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_NOEOFNEWLINE, 2, 11);
    TEST_SOURCE("\\slul 0.0.0\n\\name test", 1);
    tsoftassert(ctx->module.type == CSLUL_MT_APP);
    tsoftassert(!ctx->in_moduleheader);
    free_ctx(ctx);
}

static void test_mh_eof_early(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    /* check mandatory attributes for libraries */
    expect_error(CSLUL_E_MISSINGSOURCE, 0, 0);
    expect_error(CSLUL_E_MISSINGATTR, 0, 0);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\type library\n", 1);
    tsoftassert(!ctx->in_moduleheader);
    free_ctx(ctx);
}

#define GOODVERSTR(s) tsoftassert(check_verstr(ctx, (s), strlen(s)))
#define BADVERSTR(s, err) do { \
        ctx->tokline = 123; \
        ctx->tokcolumn = 45; \
        ctx->tokval = "dummy"; \
        ctx->toklen = 5; \
        expect_error((err), 123, 45); \
        tsoftassert(!check_verstr(ctx, (s), strlen(s))); \
    } while (0)
static void test_mh_verstr(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    GOODVERSTR("0");
    GOODVERSTR("0.1");
    GOODVERSTR("1.23");
    GOODVERSTR("1.23.0");
    GOODVERSTR("1.23.0a");
    GOODVERSTR("1.23.b");
    GOODVERSTR("1.23~alpha1");
    GOODVERSTR("1.23.git20211218");
    GOODVERSTR("1.20003");
    GOODVERSTR("2021.01.02");
    BADVERSTR("", CSLUL_E_VERSTRLENGTH);
    BADVERSTR("a", CSLUL_E_VERSTRSTART);
    BADVERSTR("_", CSLUL_E_VERSTRSTART);
    BADVERSTR(".1", CSLUL_E_VERSTRSTART);
    BADVERSTR("~1", CSLUL_E_VERSTRSTART);
    BADVERSTR("0.", CSLUL_E_VERSTRDOTEND);
    BADVERSTR("0~", CSLUL_E_VERSTRDOTEND);
    BADVERSTR("1..0", CSLUL_E_VERSTRDOTDOT);
    BADVERSTR("1.2_3", CSLUL_E_VERSTRCHAR);
    BADVERSTR("01.2", CSLUL_E_VERSTRZEROPREFIXED);
    free_ctx(ctx);
}

static void test_mh_langver_good1(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == 0);
    free_ctx(ctx);
}

static void test_mh_langver_good2(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0 upto 0.0.0\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_UNSET);
    tsoftassert(ctx->module.max_langver == LANGVER_0_0_0);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_good3(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0 upto 999.999.999\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_UNSET);
    tsoftassert(ctx->module.max_langver == LANGVER_BAD);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_good4(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0 impl 0.0.0\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == LANGVER_UNSET);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_good5(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0 impl 0.0.0 upto 999.999.999\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == LANGVER_BAD);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_good6(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0 impl 999.999.999 upto 999.999.999\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.0 unstable_api\n"
                "\\source someimpl.slul\n"
                "\n"
                "func ", 1);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_BAD);
    tsoftassert(ctx->module.max_langver == LANGVER_BAD);
    if (!ctx->has_fatal_errors) { /* skip when testing out-of-memory */
        tsoftassert(ctx->module.effective_minlangver == LANGVER_0_0_0);
        tsoftassert(ctx->module.effective_maxlangver == LANGVER_0_0_0);
    }
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_unsupported(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_UNSUPPORTEDLANGVER, 1, 7);
    TEST_SOURCE("\\slul 999.999.999\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_BAD);
    tsoftassert(ctx->module.max_langver == 0);
    tassert(ctx->module.name == NULL); /* parsing stops */
    free_ctx(ctx);
}

static void test_mh_langver_older(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SLULMAXVEROLDER, 1, 35);
    TEST_SOURCE("\\slul 0.0.0 impl 999.999.999 upto 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.0 unstable_api\n"
                "\\source impl.slul\n"
                "\n"
                "func ", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.impl_minlangver == LANGVER_BAD);
    tsoftassert(ctx->module.max_langver == LANGVER_0_0_0);
    free_ctx(ctx);
}

static void test_mh_langver_malformed_upto(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_VERSTRDOTDOT, 1, 18);
    TEST_SOURCE("\\slul 0.0.0 upto 0.0..1\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.0 unstable_api\n"
                "\\source impl.slul\n"
                "\n"
                "func ", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == LANGVER_BAD);
    free_ctx(ctx);
}

static void test_mh_langver_badsyntax1(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MISSINGATTRVALUE, 1, 6);
    TEST_SOURCE("\\slul\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_BAD);
    tsoftassert(ctx->module.max_langver == 0);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_badsyntax2(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADLANGVERSYNTAX, 1, 13);
    TEST_SOURCE("\\slul 0.0.0 xxx\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == 0);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_langver_badsyntax3(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADLANGVERSYNTAX, 1, 17);
    TEST_SOURCE("\\slul 0.0.0 upto\n"
                "\\name test\n", 0);
    tsoftassert(ctx->module.min_langver == LANGVER_0_0_0);
    tsoftassert(ctx->module.max_langver == 0);
    tassert(ctx->module.name != NULL);
    tsoftassert(!strcmp(ctx->module.name, "test"));
    free_ctx(ctx);
}

static void test_mh_sources_good(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source file1.slul\n"
                "\\source file2.slul\n"
                "\\source src/file2.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_bad_pattern(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADDIRNAME, 7, 10);
    expect_error(CSLUL_E_FILENAMEDOTDOT, 6, 18);
    expect_error(CSLUL_E_FILENAMEDOUBLESLASH, 5, 13);
    expect_error(CSLUL_E_FILENAMEROOT, 4, 9);
    expect_error(CSLUL_E_FILENAMEDOT, 3, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source .hidden.slul\n"
                "\\source /root.slul\n"
                "\\source src//doubleslash.slul\n"
                "\\source doubledot..slul\n"
                "\\source d.slul/stuff.slul\n", 0);
    expect_error(CSLUL_E_DIRTRAILINGDOT, 11, 12);
    expect_error(CSLUL_E_FILENAMEDOTDOT, 10, 22);
    expect_error(CSLUL_E_FILENAMEDOUBLESLASH, 9, 17);
    expect_error(CSLUL_E_FILENAMEDOT, 8, 13);
    TEST_SOURCE("\\source dir/.hidden.slul\n"
                "\\source dir/src//doubleslash.slul\n"
                "\\source dir/doubledot..slul\n"
                "\\source dir./trailingdot.slul\n", 0);
    expect_error(CSLUL_E_DIRTRAILINGDOT, 15, 12);
    expect_error(CSLUL_E_NONSLULFILE, 14, 9);
    expect_error(CSLUL_E_FILENAMEDOUBLESLASH, 13, 15);
    expect_error(CSLUL_E_FILENAMEDOT, 12, 15);
    TEST_SOURCE("\\source x/y/z/.slul\n"
                "\\source x/y/z//.slul\n"
                "\\source .slul\n"
                "\\source x/x./y.slul\n", 0);
    expect_error(CSLUL_E_BADFILENAMECHAR, 21, 9);
    expect_error(CSLUL_E_BADFILENAMECHAR, 20, 10);
    expect_error(CSLUL_E_BADFILENAMECHAR, 19, 11);
    expect_error(CSLUL_E_BADFILENAMECHAR, 18, 12);
    expect_error(CSLUL_E_BADFILENAMECHAR, 17, 10);
    expect_error(CSLUL_E_BADFILENAMECHAR, 16, 9);
    TEST_SOURCE("\\source å.slul\n"
                "\\source xå.slul\n"
                "\\source xxxåxx.slul\n"
                "\\source x/å.slul\n"
                "\\source x-x.slul\n"
                "\\source \\.slul\n", 0);
    {
        const char *windev =
                "\\source con.slul\n"
                "\\source lpt1.slul\n"
                "\\source com9.slul\n"
                "\\source nul/src.slul\n"
                "\\source dir/aux.slul\n"
                "\\source dir/lpt9.slul\n"
                "\\source dir/com1.slul\n"
                "\\source dir/prn/src.slul\n";
        int line = 22;
        while (*windev) {
            size_t len;
            int has_dir = memcmp(windev+8, "dir/", 4)==0;
            expect_error(CSLUL_E_DEVICEFILENAME, line++, has_dir ? 13 : 9);
            len = strcspn(windev, "\n")+1;
            sourceline = __LINE__;
            test_source(ctx, windev, len, 0);
            windev += len;
        }
    }
    free_ctx(ctx);
}

static void test_mh_sources_bad_fileext(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_NONSLULFILE, 3, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source somefile.c\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_mainslul1(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SOURCEMAINSLUL, 3, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source main.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_mainslul2(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SOURCEMAINSLUL, 3, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source a/main.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_duplicate_samecase(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLICATEFILE, 4, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source dupfile.slul\n"
                "\\source dupfile.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_duplicate_casediff(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SOURCECASEDUP, 4, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source dUpfIle.slul\n"
                "\\source DuPfiLE.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_duplicate_short(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SOURCECASEDUP, 4, 9);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source a.slul\n"
                "\\source A.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_toomany(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\source file1.slul\n", 0);
    ctx->num_sourcefiles = MAX_SOURCEFILES;
    expect_error(CSLUL_E_MAXSOURCEFILES, 4, 1);
    TEST_SOURCE("\\source file2.slul\n", 0);
    free_ctx(ctx);
}

static void test_mh_sources_toolong(void)
{
    static const char part1[] =
        "\\slul 0.0.0\n"
        "\\name someapp\n"
        "\\source ";
    static const char part2[] =
        "\n\\source file.slul\n";
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buff[sizeof(part1)-1+101+sizeof(part2)];
    char *sp = buff;
    memcpy(sp, part1, sizeof(part1)-1);
    sp += sizeof(part1)-1;
    memset(sp, 'a', 101);
    sp += 101;
    memcpy(sp, part2, sizeof(part2));
    expect_error(CSLUL_E_NONSLULFILE, 3, 9);
    /* FIXME fix error message. should say attribute value, not identifier */
    expect_error(CSLUL_E_IDENTTOOLONG, 3, 109);
    TEST_SOURCE(buff, 0);
    free_ctx(ctx);
}

static void test_mh_sources_toolong_eof(void)
{
    static const char part1[] =
        "\\slul 0.0.0\n"
        "\\name someapp\n"
        "\\source ";
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buff[sizeof(part1)+101];
    char *sp = buff;
    memcpy(sp, part1, sizeof(part1)-1);
    sp += sizeof(part1)-1;
    memset(sp, 'a', 101);
    sp += 101;
    *sp = '\0';
    expect_error(CSLUL_E_NOEOFNEWLINE, 3, 110);
    expect_error(CSLUL_E_NONSLULFILE, 3, 9);
    /* FIXME fix error message. should say attribute value, not identifier */
    expect_error(CSLUL_E_IDENTTOOLONG, 3, 109);
    TEST_SOURCE(buff, 1);
    free_ctx(ctx);
}

static void test_mh_deps_single(void)
{
    struct CSlulDependency *dep;
    struct Module *mod;
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    ctx->parser.mh.seen_explicit_slulrt_dep = 1; /* skip implicit slulrt dep */
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\version 0.1.23\n"
                "\\depends a 1.23.4\n"
                "\\source file1.slul\n", 1);
    tassert((mod = ctx->parsed_module) != NULL);
    tassert(mod->deps_root != NULL);
    tassert(mod->first_dep != NULL);
    tassert(mod->last_dep == mod->first_dep);
    dep = mod->first_dep;
    tassert(mod->deps_root == &dep->node);
    ASSERT_DEP(dep, "a", "1.23.4", 0);
    free_ctx(ctx);
}

static void test_mh_deps_several(void)
{
    struct CSlulDependency *dep;
    struct Module *mod;
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    ctx->parser.mh.seen_explicit_slulrt_dep = 1; /* skip implicit slulrt dep */
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\version 0.1.23\n"
                "\\depends optdep 12.3.4 optional\n"
                "\\depends onlynested 5.789bugfix nestedonly\n"
                "\\depends multiflags 9.87a unstable_api optional nestedonly\n"
                "\\depends noversion bundled\n"
                "\\depends noflags 1.23.4\n"
                "\\source file1.slul\n", 1);
    tassert((mod = ctx->parsed_module) != NULL);
    tassert(mod->deps_root != NULL);
    tassert(mod->first_dep != NULL);
    tassert(mod->last_dep != mod->first_dep);
    dep = mod->first_dep;
    ASSERT_DEP(dep, "optdep", "12.3.4", CSLUL_DF_OPTIONAL);
    dep = dep->next;
    ASSERT_DEP(dep, "onlynested", "5.789bugfix", CSLUL_DF_NESTEDONLY);
    dep = dep->next;
    ASSERT_DEP(dep, "multiflags", "9.87a",
            CSLUL_DF_UNSTABLE_API|CSLUL_DF_OPTIONAL|CSLUL_DF_NESTEDONLY);
    dep = dep->next;
    ASSERT_DEP_NOVERSION(dep, "noversion", CSLUL_DF_BUNDLED);
    dep = dep->next;
    ASSERT_DEP(dep, "noflags", "1.23.4", 0);
    tsoftassert(dep == mod->last_dep);
    tsoftassert(dep->next == NULL);
    free_ctx(ctx);
}

static void test_mh_deps_selfdepend(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SELFDEPEND, 5, 10);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n" /* case insensitive check */
                "\\version 1.0\n"
                "\n"
                "\\depends test 1.0\n", 0);
    free_ctx(ctx);
}

static void test_mh_deps_dup(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLICATEDEP, 4, 10);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\depends module 1.0\n"
                "\\depends module 1.0\n", 0);
    free_ctx(ctx);
}

static void test_mh_deps_flag_unknown(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADDEPFLAG, 3, 21);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\depends module 1.0 bad\n", 0);
    free_ctx(ctx);
}

static void test_mh_deps_flag_dup(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLDEPFLAG, 4, 42);
    expect_error(CSLUL_E_DUPLDEPFLAG, 3, 30);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\depends module 1.0 optional optional\n"
                "\\depends module2 1.0 optional nestedonly optional\n", 0);
    free_ctx(ctx);
}

static void test_mh_deps_flag_tab(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_TAB, 3, 20);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\depends module 1.0\tnestedonly\n", 0);
    free_ctx(ctx);
}

static void test_mh_deps_flag_wrongorder(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DEPFLAGSORDER, 3, 32);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\depends module 1.0 nestedonly optional\n", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps(void)
{
    struct CSlulDependency *dep;
    struct CSlulInterfaceDep *idep;
    struct Module *mod;
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    ctx->parser.mh.seen_explicit_slulrt_dep = 1; /* skip implicit slulrt dep */
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 0.1.23\n"
                "\\depends optdep 12.3.4 optional\n"
                "\\depends onlynested 5.789bugfix nestedonly\n"
                "\\interface_depends optdep 0.9.34b since 0.1.17\n"
                "\\interface_depends optdep 11.3 since 0.1.20\n"
                "\\interface_depends onlynested 5.789bugfix nestedonly since 0.1.23\n"
                "\\interface_depends ifaceonly 0.98.7 since 0.1.23\n"
                "\\api_def 0.1.17 00101700101700101700101700101700\n"
                "\\api_def 0.1.20 00102000102000102000102000102000\n"
                "\\api_def 0.1.23 00102300102300102300102300102300\n"
                "\\source file1.slul\n", 1);
    tassert((mod = ctx->parsed_module) != NULL);
    tassert(mod->deps_root != NULL);
    tassert(mod->first_dep != NULL);
    tassert(mod->last_dep != mod->first_dep);
    dep = mod->first_dep;
    ASSERT_DEP(dep, "optdep", "12.3.4", CSLUL_DF_OPTIONAL);
    tassert((idep = dep->first_ifacever) != NULL);
    tsoftassert(dep->last_ifacever != NULL);
    ASSERT_IDEP(idep, dep, "optdep", "0.9.34b", "0.1.17");
    idep = idep->sincever_next;
    ASSERT_IDEP(idep, dep, "optdep", "11.3", "0.1.20");
    dep = dep->next;
    ASSERT_DEP(dep, "onlynested", "5.789bugfix", CSLUL_DF_NESTEDONLY);
    dep = dep->next;
    ASSERT_DEP(dep, "ifaceonly", "0.98.7", CSLUL_DF_IFACEONLY);
    tsoftassert(dep == mod->last_dep);
    tsoftassert(dep->next == NULL);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_bundled(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_IFACEDEPENDBUNDLED, 6, 20);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\depends somemodule bundled\n"
                "\\interface_depends somemodule 1.0 since 1.2.2\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_selfdepend(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SELFDEPEND, 5, 20);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\interface_depends test 1.0 since 1.2.2\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_missingversion(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_NOIFACEDEPVERSION, 5, 26);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\interface_depends module\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_missingsinceversion(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_NOSINCEVERSION, 5, 36);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\interface_depends module 1.0 since\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_badsinceversion(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_VERSTRSTART, 5, 37);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\interface_depends module 1.0 since x\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_app(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_APPWITHIFACEDEP, 4, 1);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\version 1.2.3\n"
                "\\interface_depends module 1.0 since 1.2.2\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_ifacedeps_dup(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLICATEIFACEDEP, 6, 37);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2.3\n"
                "\\interface_depends module 1.0 since 1.2.2~rc1\n"
                "\\interface_depends module 1.0 since 1.2.2~rc1\n"
                "\\interface_depends ", 0);
    free_ctx(ctx);
}

static void test_mh_apidef(void)
{
    struct ApiDef *apidef;
    struct Module *mod;
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.1.23\n"
                "\\api_def 0.1.0\n"
                "\\api_def 0.1.1 retracted\n"
                "\\api_def 0.2.0 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGVm retracted\n"
                "\\api_def 0.2.1 T3RoZXKqu8wwMDExMjIzMzQ0NTU2Njc3\n"
                "\\api_def 0.2.2 X/x+x//x/+x+/x++x+++XxXxXxXxXxX/\n"
                "\\source file1.slul\n", 1);
    tassert((mod = ctx->parsed_module) != NULL);
    tassert(mod->apidefs_root != NULL);
    tassert(mod->first_apidef != NULL);
    tassert(mod->last_apidef != mod->first_apidef);
    apidef = mod->first_apidef;
    ASSERT_APIDEF_WITHOUT_HASH(apidef, 0, "0.1.0", 0);
    apidef = apidef->next;
    ASSERT_APIDEF_WITHOUT_HASH(apidef, 1, "0.1.1", APIDEF_RETRACTED);
    apidef = apidef->next;
    /* Bogus data for testing. This would normally be a random binary value */
    ASSERT_APIDEF_WITH_HASH(apidef, 2, "0.2.0", "Test\0\x01\x02\x03"
            "0123456789abcdef", APIDEF_HAS_HASH|APIDEF_RETRACTED);
    apidef = apidef->next;
    ASSERT_APIDEF_WITH_HASH(apidef, 3, "0.2.1", "Other\xaa\xbb\xcc"
            "0011223344556677", APIDEF_HAS_HASH);
    apidef = apidef->next;
    ASSERT_APIDEF_WITH_HASH(apidef, 4, "0.2.2", "_\xfc~\xc7\xff\xf1\xff\xec"
            "\x7e\xff\x1f\xbe\xc7\xef\xbe\x5f\x15\xf1_\x15\xf1_\x15\xff",
            APIDEF_HAS_HASH);
    tsoftassert(apidef == mod->last_apidef);
    tsoftassert(apidef->next == NULL);
    free_ctx(ctx);
}
/*
static void test_mh_apidef_hash(void)
{
}
*/

static void test_mh_apidef_badversion(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_VERSTRSTART, 5, 10);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2\n"
                "\\api_def x1.2\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_badflag(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADAPIDEFFLAG, 6, 47);
    expect_error(CSLUL_E_BADAPIDEFFLAG, 5, 14);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2\n"
                "\\api_def 1.1 badflag\n"
                "\\api_def 1.2 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGVm badflag\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_dup(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_DUPLICATEAPIDEF, 6, 1);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.2\n"
                "\\api_def 1.2\n"
                "\\api_def 1.2\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_badhashlength(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADAPIHASHLEN, 5, 14);
    /* last character is missing in hash */
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.0\n"
                "\\api_def 1.0 TWlzc2luZyBvbmUgc2luZ2xlIGJ5dGU\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_badhashchar(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADAPIHASH, 5, 14);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\version 1.0\n"
                "\\api_def 1.0 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGV!\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_toomany(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name someapp\n"
                "\\type library\n"
                "\\version 1.1\n"
                "\\api_def 1.0 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGVm\n", 0);
    ctx->parsed_module->num_apidefs = MAX_APIDEFS;
    expect_error(CSLUL_E_MAXAPIDEFS, 6, 1);
    TEST_SOURCE("\\api_def 0.2.1 T3RoZXKqu8wwMDExMjIzMzQ0NTU2Njc3\n", 0);
    free_ctx(ctx);
}

static void test_mh_apidef_withapp(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_APPWITHAPI, 4, 1);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\version 1.0\n"
                "\\api_def 1.0 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGVm\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_apidef_withoutversion(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_APIDEFWITHOUTMODVER, 4, 1);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type library\n"
                "\\api_def 1.0 VGVzdAABAgMwMTIzNDU2Nzg5YWJjZGVm\n"
                "\\source file1.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_moduletype_good(void)
{
    static const char *const types[] = {
        "internal",
        "library",
        "plugin",
        "libraryspec",
        "pluginspec",
        "app",
        NULL
    };
    char buffer[200];
    const char *const *type = types;
    for (; *type; type++) {
        struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
        sprintf(buffer, "\\slul 0.0.0\n\\name test\n\\type %s\n", *type);
        sourceline = __LINE__;
        test_source(ctx, buffer, strlen(buffer), 0);
        free_ctx(ctx);
    }
}

static void test_mh_moduletype_bad(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_BADMODULETYPE, 3, 7);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name test\n"
                "\\type internzl\n", 0);
    free_ctx(ctx);
}

static void test_mh_nameversion_good(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n"
                "\\name a0123456789bcdefghijklmnopqrstuvwxy_z\n"
                "\\version 0.1.999923456789abcdefghijkl~m\n", 0);
    free_ctx(ctx);
}

static void test_mh_name_bad(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buffer[52];
    memset(buffer, 'z', sizeof(buffer)-1);
    buffer[51] ='\0';
    TEST_SOURCE("\\slul 0.0.0\n\\name ", 0);
    TEST_SOURCE(buffer, 0);
    expect_error(CSLUL_E_MODNAMELENGTH, 2, 7);
    TEST_SOURCE("\n", 0);
    expect_error(CSLUL_E_MODNAMESTART, 3, 7);
    expect_error(CSLUL_E_DUPLICATEATTR, 3, 1);
    TEST_SOURCE("\\name 0\n", 0);
    expect_error(CSLUL_E_MODNAMECHAR, 4, 7);
    expect_error(CSLUL_E_DUPLICATEATTR, 4, 1);
    TEST_SOURCE("\\name a?\n", 0);
    free_ctx(ctx);
}

static void test_mh_version_bad(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buffer[52];
    memset(buffer, '1', sizeof(buffer)-1);
    buffer[51] = '\0';
    TEST_SOURCE("\\slul 0.0.0\n\\name test\n\\version ", 0);
    TEST_SOURCE(buffer, 0);
    expect_error(CSLUL_E_VERSTRLENGTH, 3, 10);
    TEST_SOURCE("\n", 0);
    expect_error(CSLUL_E_VERSTRSTART, 4, 10);
    expect_error(CSLUL_E_DUPLICATEATTR, 4, 1);
    TEST_SOURCE("\\version x\n", 0);
    expect_error(CSLUL_E_VERSTRCHAR, 5, 10);
    expect_error(CSLUL_E_DUPLICATEATTR, 5, 1);
    TEST_SOURCE("\\version 0?\n", 0);
    expect_error(CSLUL_E_VERSTRDOTDOT, 6, 10);
    expect_error(CSLUL_E_DUPLICATEATTR, 6, 1);
    TEST_SOURCE("\\version 0..1\n", 0);
    expect_error(CSLUL_E_VERSTRDOTEND, 7, 10);
    expect_error(CSLUL_E_DUPLICATEATTR, 7, 1);
    TEST_SOURCE("\\version 0.\n", 0);
    expect_error(CSLUL_E_BADVERSIONFLAG, 8, 14);
    expect_error(CSLUL_E_DUPLICATEATTR, 8, 1);
    TEST_SOURCE("\\version 0.1 unstablx\n", 0);
    free_ctx(ctx);
}

static void test_mh_specwithsource(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_SPECWITHSOURCE, 4, 1);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\type libraryspec\n\\source x.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_missingsource(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MISSINGSOURCE, 0, 0);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\type library\n", 1);
    free_ctx(ctx);
}

static void test_mh_missingattr(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MISSINGATTR, 0, 0); /* missing 'name' */
    TEST_SOURCE("\\slul 0.0.0\n\\type library\n\\source x.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_withsource_good(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\source x.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_withoutsource_good(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\type libraryspec\n", 1);
    free_ctx(ctx);
}

/** Module headers end when a line starts with a keyword without a leading.
    backslash. This test checks that the transition works correctly. */
static void test_mh_with_end(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\ntype ", 0);
    tsoftassert(ctx->parser.mh.state == MHPDone);
    tsoftassert(ctx->line == 4);
    tsoftassert(ctx->tokline == 4);
    tsoftassert(ctx->tokcolumn == 1);
    tassert(!ctx->has_fatal_errors); /* stop if testing out-of-memory */
    /* Poke the normal (non module header) parser */
    TEST_SOURCE("", 0);
    tsoftassert(ctx->parser.slul.state == PTypeIdent);
    tsoftassert(ctx->line == 4);
    tsoftassert(ctx->tokline == 4);
    tsoftassert(ctx->tokcolumn == 1);
    tassert(!ctx->has_fatal_errors); /* stop if testing out-of-memory */
    tsoftassert(ctx->typedepth == -1);
    tsoftassert(ctx->blockdepth == -1);
    tsoftassert(ctx->exprdepth == -1);
    tsoftassert(ctx->current_block == NULL);
    free_ctx(ctx);
}

/* TODO enforce a blank line between the module header and main source? */

/** Tests parsing an attribute with a missing \\ */
static void test_mh_nobackslash(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MHNOBACKSLASH, 2, 1);
    TEST_SOURCE("\\slul 0.0.0\nname abc\n\\type app\n", 0);
    /* Should recover from error */
    tassert(ctx->parsed_module != NULL);
    tassert(ctx->parsed_module->namelen == 3);
    tassert(ctx->parsed_module->name != NULL);
    tsoftassert(!memcmp(ctx->parsed_module->name, "abc", 3));
    tsoftassert(ctx->parsed_module->type == CSLUL_MT_APP);
    free_ctx(ctx);
}

/** Tests parsing a file with a missing module header */
static void test_mh_missingheader(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    expect_error(CSLUL_E_MISSINGMODULEHEADER, 1, 1);
    TEST_SOURCE("type ", 0);
    free_ctx(ctx);
}

static void test_mh_toolong(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buff[103];
    memset(buff+1, 'x', 100);
    buff[0] = '\\';
    buff[1] = 'a';
    buff[99] = 'b';
    buff[100] = 'c';
    buff[101] = 'd';
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n", 0);
    expect_error(CSLUL_E_IDENTTOOLONG, 3, 102);
    TEST_SOURCE(buff, 0);
    tsoftassert(ctx->toklen == 100);
    tsoftassert(!memcmp(ctx->tokval, buff+1, 100));
    expect_error(CSLUL_E_BADATTRNAME, 3, 1);
    TEST_SOURCE(" ", 0);
    expect_error(CSLUL_E_IDENTTOOLONG, 3, 204);
    buff[0] = 'e';
    buff[99] = 'f';
    TEST_SOURCE(buff, 0);
    tsoftassert(ctx->toklen == 100);
    tsoftassert(!memcmp(ctx->tokval, buff, 100));
    TEST_SOURCE("\n\\source a.slul\n", 1);
    free_ctx(ctx);
}

static void test_mh_toolong_utf8(void)
{
    struct CSlul *ctx = create_ctx(CSLUL_P_MODULEHEADER);
    char buff[102];
    memset(buff, 'x', 100);
    TEST_SOURCE("\\slul 0.0.0\n\\name x\n\\version 1.23.4\n\\repo ", 0);
    expect_error(CSLUL_E_IDENTTOOLONG, 4, 107);
    buff[0] = 'y';
    buff[99] = '\xe2';
    buff[100] = '\x8c';
    buff[101] = '\xa8';
    TEST_SOURCE(buff, 0);
    tsoftassert(ctx->toklen == 100);
    tsoftassert(!memcmp(ctx->tokval, buff, 100));
    TEST_SOURCE("\n\\source a.slul\n", 1);
    free_ctx(ctx);
}

const TestFunctionInfo tests_mhparse[] = {
    TEST_BEFORE(before_test)
    TEST_AFTER(after_test)
    TEST_INFO(test_mh_attrs_matchattr_attr2str)
    TEST_INFO(test_mh_attrs_good)
    TEST_INFO(test_mh_attrs_good_splitted)
    TEST_INFO(test_mh_attrs_unknown1)
    TEST_INFO(test_mh_attrs_unknown2)
    TEST_INFO(test_mh_attrs_wrongorder)
    TEST_INFO(test_mh_attrs_duplicate)
    TEST_INFO(test_mh_attrs_novalue)
    TEST_INFO(test_mh_attrs_badchars)
    TEST_INFO(test_mh_eof_newline)
    TEST_INFO(test_mh_eof_early)
    TEST_INFO(test_mh_verstr)
    TEST_INFO(test_mh_langver_good1)
    TEST_INFO(test_mh_langver_good2)
    TEST_INFO(test_mh_langver_good3)
    TEST_INFO(test_mh_langver_good4)
    TEST_INFO(test_mh_langver_good5)
    TEST_INFO(test_mh_langver_good6)
    TEST_INFO(test_mh_langver_unsupported)
    TEST_INFO(test_mh_langver_older)
    TEST_INFO(test_mh_langver_malformed_upto)
    TEST_INFO(test_mh_langver_badsyntax1)
    TEST_INFO(test_mh_langver_badsyntax2)
    TEST_INFO(test_mh_langver_badsyntax3)
    TEST_INFO(test_mh_sources_good)
    TEST_INFO(test_mh_sources_bad_pattern)
    TEST_INFO(test_mh_sources_bad_fileext)
    TEST_INFO(test_mh_sources_mainslul1)
    TEST_INFO(test_mh_sources_mainslul2)
    TEST_INFO(test_mh_sources_duplicate_samecase)
    TEST_INFO(test_mh_sources_duplicate_casediff)
    TEST_INFO(test_mh_sources_duplicate_short)
    TEST_INFO(test_mh_sources_toomany)
    TEST_INFO(test_mh_sources_toolong)
    TEST_INFO(test_mh_sources_toolong_eof)
    TEST_INFO(test_mh_deps_single)
    TEST_INFO(test_mh_deps_several)
    TEST_INFO(test_mh_deps_selfdepend)
    TEST_INFO(test_mh_deps_dup)
    TEST_INFO(test_mh_deps_flag_unknown)
    TEST_INFO(test_mh_deps_flag_dup)
    TEST_INFO(test_mh_deps_flag_tab)
    TEST_INFO(test_mh_deps_flag_wrongorder)
    TEST_INFO(test_mh_ifacedeps)
    TEST_INFO(test_mh_ifacedeps_bundled)
    TEST_INFO(test_mh_ifacedeps_selfdepend)
    TEST_INFO(test_mh_ifacedeps_missingversion)
    TEST_INFO(test_mh_ifacedeps_missingsinceversion)
    TEST_INFO(test_mh_ifacedeps_badsinceversion)
    TEST_INFO(test_mh_ifacedeps_app)
    TEST_INFO(test_mh_ifacedeps_dup)
    TEST_INFO(test_mh_apidef)
    TEST_INFO(test_mh_apidef_badversion)
    TEST_INFO(test_mh_apidef_badflag)
    TEST_INFO(test_mh_apidef_dup)
    TEST_INFO(test_mh_apidef_badhashlength)
    TEST_INFO(test_mh_apidef_badhashchar)
    TEST_INFO(test_mh_apidef_toomany)
    TEST_INFO(test_mh_apidef_withapp)
    TEST_INFO(test_mh_apidef_withoutversion)
    TEST_INFO(test_mh_moduletype_good)
    TEST_INFO(test_mh_moduletype_bad)
    TEST_INFO(test_mh_nameversion_good)
    TEST_INFO(test_mh_name_bad)
    TEST_INFO(test_mh_version_bad)
    TEST_INFO(test_mh_specwithsource)
    TEST_INFO(test_mh_missingsource)
    TEST_INFO(test_mh_missingattr)
    TEST_INFO(test_mh_withsource_good)
    TEST_INFO(test_mh_withoutsource_good)
    TEST_INFO(test_mh_with_end)
    TEST_INFO(test_mh_nobackslash)
    TEST_INFO(test_mh_missingheader)
    TEST_INFO(test_mh_toolong)
    TEST_INFO(test_mh_toolong_utf8)
    TEST_END
};