aboutsummaryrefslogtreecommitdiff
path: root/tests/unittests/torture_config.c
blob: beb1ce7965207bdd9109809abb245fe8ddc4f5b7 (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
#include "config.h"

#define LIBSSH_STATIC

#include "torture.h"
#include "libssh/options.h"
#include "libssh/session.h"
#include "libssh/config_parser.h"

extern LIBSSH_THREAD int ssh_log_level;

#define LIBSSH_TESTCONFIG1 "libssh_testconfig1.tmp"
#define LIBSSH_TESTCONFIG2 "libssh_testconfig2.tmp"
#define LIBSSH_TESTCONFIG3 "libssh_testconfig3.tmp"
#define LIBSSH_TESTCONFIG4 "libssh_testconfig4.tmp"
#define LIBSSH_TESTCONFIG5 "libssh_testconfig5.tmp"
#define LIBSSH_TESTCONFIG6 "libssh_testconfig6.tmp"
#define LIBSSH_TESTCONFIG7 "libssh_testconfig7.tmp"
#define LIBSSH_TESTCONFIG8 "libssh_testconfig8.tmp"
#define LIBSSH_TESTCONFIG9 "libssh_testconfig9.tmp"
#define LIBSSH_TESTCONFIG10 "libssh_testconfig10.tmp"
#define LIBSSH_TESTCONFIG11 "libssh_testconfig11.tmp"
#define LIBSSH_TESTCONFIG12 "libssh_testconfig12.tmp"
#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp"
#define LIBSSH_TEST_PUBKEYACCEPTEDKEYTYPES "libssh_test_PubkeyAcceptedKeyTypes.tmp"

#define USERNAME "testuser"
#define PROXYCMD "ssh -q -W %h:%p gateway.example.com"
#define ID_FILE "/etc/xxx"
#define KEXALGORITHMS "ecdh-sha2-nistp521,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1"
#define HOSTKEYALGORITHMS "ssh-ed25519,ecdsa-sha2-nistp521,ssh-rsa"
#define PUBKEYACCEPTEDTYPES "rsa-sha2-512,ssh-rsa,ecdsa-sha2-nistp521"
#define MACS "hmac-sha1,hmac-sha2-256,hmac-sha2-512,hmac-sha1-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com"
#define USER_KNOWN_HOSTS "%d/my_known_hosts"
#define GLOBAL_KNOWN_HOSTS "/etc/ssh/my_ssh_known_hosts"
#define BIND_ADDRESS "::1"



static int setup_config_files(void **state)
{
    ssh_session session;
    int verbosity;

    unlink(LIBSSH_TESTCONFIG1);
    unlink(LIBSSH_TESTCONFIG2);
    unlink(LIBSSH_TESTCONFIG3);
    unlink(LIBSSH_TESTCONFIG4);
    unlink(LIBSSH_TESTCONFIG5);
    unlink(LIBSSH_TESTCONFIG6);
    unlink(LIBSSH_TESTCONFIG7);
    unlink(LIBSSH_TESTCONFIG8);
    unlink(LIBSSH_TESTCONFIG9);
    unlink(LIBSSH_TESTCONFIG10);
    unlink(LIBSSH_TESTCONFIG11);
    unlink(LIBSSH_TESTCONFIG12);
    unlink(LIBSSH_TEST_PUBKEYACCEPTEDKEYTYPES);

    torture_write_file(LIBSSH_TESTCONFIG1,
                       "User "USERNAME"\nInclude "LIBSSH_TESTCONFIG2"\n\n");
    torture_write_file(LIBSSH_TESTCONFIG2,
                       "Include "LIBSSH_TESTCONFIG3"\n"
                       "ProxyCommand "PROXYCMD"\n\n");
    torture_write_file(LIBSSH_TESTCONFIG3,
                       "\n\nIdentityFile "ID_FILE"\n"
                       "\n\nKexAlgorithms "KEXALGORITHMS"\n"
                       "\n\nHostKeyAlgorithms "HOSTKEYALGORITHMS"\n"
                       "\n\nPubkeyAcceptedTypes "PUBKEYACCEPTEDTYPES"\n"
                       "\n\nMACs "MACS"\n");

    /* Multiple Port settings -> parsing returns early. */
    torture_write_file(LIBSSH_TESTCONFIG4,
                       "Port 123\nPort 456\n");

    /* Testing glob include */
    torture_write_file(LIBSSH_TESTCONFIG5,
                        "User "USERNAME"\nInclude "LIBSSH_TESTCONFIGGLOB"\n\n");

    torture_write_file(LIBSSH_TESTCONFIG6,
                        "ProxyCommand "PROXYCMD"\n\n");

    /* new options */
    torture_write_file(LIBSSH_TESTCONFIG7,
                        "\tBindAddress "BIND_ADDRESS"\n"
                        "\tConnectTimeout 30\n"
                        "\tLogLevel DEBUG3\n"
                        "\tGlobalKnownHostsFile "GLOBAL_KNOWN_HOSTS"\n"
                        "\tCompression yes\n"
                        "\tStrictHostkeyChecking no\n"
                        "\tGSSAPIDelegateCredentials yes\n"
                        "\tGSSAPIServerIdentity example.com\n"
                        "\tGSSAPIClientIdentity home.sweet\n"
                        "\tUserKnownHostsFile "USER_KNOWN_HOSTS"\n");

    /* authentication methods */
    torture_write_file(LIBSSH_TESTCONFIG8,
                        "Host gss\n"
                        "\tGSSAPIAuthentication yes\n"
                        "Host kbd\n"
                        "\tKbdInteractiveAuthentication yes\n"
                        "Host pass\n"
                        "\tPasswordAuthentication yes\n"
                        "Host pubkey\n"
                        "\tPubkeyAuthentication yes\n"
                        "Host nogss\n"
                        "\tGSSAPIAuthentication no\n"
                        "Host nokbd\n"
                        "\tKbdInteractiveAuthentication no\n"
                        "Host nopass\n"
                        "\tPasswordAuthentication no\n"
                        "Host nopubkey\n"
                        "\tPubkeyAuthentication no\n");

    /* unsupported options and corner cases */
    torture_write_file(LIBSSH_TESTCONFIG9,
                        "\n" /* empty line */
                        "# comment line\n"
                        "  # comment line not starting with hash\n"
                        "UnknownConfigurationOption yes\n"
                        "GSSAPIKexAlgorithms yes\n"
                        "ControlMaster auto\n" /* SOC_NA */
                        "VisualHostkey yes\n" /* SOC_UNSUPPORTED */
                        "");

    /* Match keyword */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match host example\n"
                       "\tHostName example.com\n"
                       "Match host example1,example2\n"
                       "\tHostName exampleN\n"
                       "Match user guest\n"
                       "\tHostName guest.com\n"
                       "Match user tester host testhost\n"
                       "\tHostName testhost.com\n"
                       "Match !user tester host testhost\n"
                       "\tHostName nonuser-testhost.com\n"
                       "Match all\n"
                       "\tHostName all-matched.com\n"
                       /* Unsupported options */
                       "Match originalhost example\n"
                       "\tHostName original-example.com\n"
                       "Match localuser guest\n"
                       "\tHostName local-guest.com\n"
                       "");

    /* ProxyJump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host simple\n"
                       "\tProxyJump jumpbox\n"
                       "Host user\n"
                       "\tProxyJump user@jumpbox\n"
                       "Host port\n"
                       "\tProxyJump jumpbox:2222\n"
                       "Host two-step\n"
                       "\tProxyJump u1@first:222,u2@second:33\n"
                       "Host none\n"
                       "\tProxyJump none\n"
                       "Host only-command\n"
                       "\tProxyCommand "PROXYCMD"\n"
                       "\tProxyJump jumpbox\n"
                       "Host only-jump\n"
                       "\tProxyJump jumpbox\n"
                       "\tProxyCommand "PROXYCMD"\n"
                       "Host ipv6\n"
                       "\tProxyJump [2620:52:0::fed]\n"
                       "");

    /* RekeyLimit combinations */
    torture_write_file(LIBSSH_TESTCONFIG12,
                       "Host default\n"
                       "\tRekeyLimit default none\n"
                       "Host data1\n"
                       "\tRekeyLimit 42G\n"
                       "Host data2\n"
                       "\tRekeyLimit 31M\n"
                       "Host data3\n"
                       "\tRekeyLimit 521K\n"
                       "Host time1\n"
                       "\tRekeyLimit default 3D\n"
                       "Host time2\n"
                       "\tRekeyLimit default 2h\n"
                       "Host time3\n"
                       "\tRekeyLimit default 160m\n"
                       "Host time4\n"
                       "\tRekeyLimit default 9600\n"
                       "");

    torture_write_file(LIBSSH_TEST_PUBKEYACCEPTEDKEYTYPES,
                       "PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES"\n");

    session = ssh_new();

    verbosity = torture_libssh_verbosity();
    ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

    *state = session;

    return 0;
}

static int teardown(void **state)
{
    unlink(LIBSSH_TESTCONFIG1);
    unlink(LIBSSH_TESTCONFIG2);
    unlink(LIBSSH_TESTCONFIG3);
    unlink(LIBSSH_TESTCONFIG4);
    unlink(LIBSSH_TESTCONFIG5);
    unlink(LIBSSH_TESTCONFIG6);
    unlink(LIBSSH_TESTCONFIG7);
    unlink(LIBSSH_TESTCONFIG8);
    unlink(LIBSSH_TESTCONFIG9);
    unlink(LIBSSH_TESTCONFIG10);
    unlink(LIBSSH_TESTCONFIG11);
    unlink(LIBSSH_TESTCONFIG12);
    unlink(LIBSSH_TEST_PUBKEYACCEPTEDKEYTYPES);

    ssh_free(*state);

    return 0;
}

/**
 * @brief tests ssh_config_parse_file with Include directives
 */
static void torture_config_from_file(void **state) {
    ssh_session session = *state;
    int ret;
    char *v = NULL;
    char *fips_algos = NULL;

    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG1);
    assert_true(ret == 0);

    /* Test the variable presence */

    ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v);
    assert_true(ret == 0);
    assert_non_null(v);

    assert_string_equal(v, PROXYCMD);
    SSH_STRING_FREE_CHAR(v);

    ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v);
    assert_true(ret == 0);
    assert_non_null(v);

    assert_string_equal(v, ID_FILE);
    SSH_STRING_FREE_CHAR(v);

    ret = ssh_options_get(session, SSH_OPTIONS_USER, &v);
    assert_true(ret == 0);
    assert_non_null(v);

    assert_string_equal(v, USERNAME);
    SSH_STRING_FREE_CHAR(v);

    if (ssh_fips_mode()) {
        fips_algos = ssh_keep_fips_algos(SSH_KEX, KEXALGORITHMS);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.wanted_methods[SSH_KEX], fips_algos);
        SAFE_FREE(fips_algos);
        fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, HOSTKEYALGORITHMS);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], fips_algos);
        SAFE_FREE(fips_algos);
        fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, PUBKEYACCEPTEDTYPES);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.pubkey_accepted_types, fips_algos);
        SAFE_FREE(fips_algos);
        fips_algos = ssh_keep_fips_algos(SSH_MAC_C_S, MACS);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], fips_algos);
        SAFE_FREE(fips_algos);
        fips_algos = ssh_keep_fips_algos(SSH_MAC_S_C, MACS);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], fips_algos);
        SAFE_FREE(fips_algos);
    } else {
        assert_non_null(session->opts.wanted_methods[SSH_KEX]);
        assert_string_equal(session->opts.wanted_methods[SSH_KEX], KEXALGORITHMS);
        assert_non_null(session->opts.wanted_methods[SSH_HOSTKEYS]);
        assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], HOSTKEYALGORITHMS);
        assert_non_null(session->opts.pubkey_accepted_types);
        assert_string_equal(session->opts.pubkey_accepted_types, PUBKEYACCEPTEDTYPES);
        assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]);
        assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], MACS);
        assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]);
        assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], MACS);
    }
}

/**
 * @brief tests ssh_config_parse_file with multiple Port settings.
 */
static void torture_config_double_ports(void **state) {
    ssh_session session = *state;
    int ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG4);
    assert_true(ret == 0);
}

static void torture_config_glob(void **state) {
    ssh_session session = *state;
    int ret;
#if defined(HAVE_GLOB) && defined(HAVE_GLOB_GL_FLAGS_MEMBER)
    char *v;
#endif /* HAVE_GLOB && HAVE_GLOB_GL_FLAGS_MEMBER */

    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG5);
    assert_true(ret == 0); /* non-existing files should not error */

    /* Test the variable presence */

#if defined(HAVE_GLOB) && defined(HAVE_GLOB_GL_FLAGS_MEMBER)
    ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v);
    assert_true(ret == 0);
    assert_non_null(v);

    assert_string_equal(v, PROXYCMD);
    SSH_STRING_FREE_CHAR(v);

    ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v);
    assert_true(ret == 0);
    assert_non_null(v);

    assert_string_equal(v, ID_FILE);
    SSH_STRING_FREE_CHAR(v);
#endif /* HAVE_GLOB && HAVE_GLOB_GL_FLAGS_MEMBER */
}

/**
 * @brief Verify the new options are passed from configuration
 */
static void torture_config_new(void **state)
{
    ssh_session session = *state;
    int ret = 0;

    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG7);
    assert_true(ret == 0);

    assert_string_equal(session->opts.knownhosts, USER_KNOWN_HOSTS);
    assert_string_equal(session->opts.global_knownhosts, GLOBAL_KNOWN_HOSTS);
    assert_int_equal(session->opts.timeout, 30);
    assert_string_equal(session->opts.bindaddr, BIND_ADDRESS);
#ifdef WITH_ZLIB
    assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S],
                        "zlib@openssh.com,zlib");
    assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C],
                        "zlib@openssh.com,zlib");
#else
    assert_null(session->opts.wanted_methods[SSH_COMP_C_S]);
    assert_null(session->opts.wanted_methods[SSH_COMP_S_C]);
#endif /* WITH_ZLIB */
    assert_int_equal(session->opts.StrictHostKeyChecking, 0);
    assert_int_equal(session->opts.gss_delegate_creds, 1);
    assert_string_equal(session->opts.gss_server_identity, "example.com");
    assert_string_equal(session->opts.gss_client_identity, "home.sweet");

    assert_int_equal(ssh_get_log_level(), SSH_LOG_TRACE);
    assert_int_equal(session->common.log_verbosity, SSH_LOG_TRACE);
}

/**
 * @brief Verify the authentication methods from configuration are effective
 */
static void torture_config_auth_methods(void **state) {
    ssh_session session = *state;
    int ret = 0;

    /* gradually disable all the methods based on different hosts */
    ssh_options_set(session, SSH_OPTIONS_HOST, "nogss");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_false(session->opts.flags & SSH_OPT_FLAG_GSSAPI_AUTH);
    assert_true(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "nokbd");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_false(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "nopass");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_false(session->opts.flags & SSH_OPT_FLAG_PASSWORD_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "nopubkey");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_false(session->opts.flags & SSH_OPT_FLAG_PUBKEY_AUTH);

    /* no method should be left enabled */
    assert_int_equal(session->opts.flags, 0);

    /* gradually enable them again */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "gss");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_true(session->opts.flags & SSH_OPT_FLAG_GSSAPI_AUTH);
    assert_false(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "kbd");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_true(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "pass");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_true(session->opts.flags & SSH_OPT_FLAG_PASSWORD_AUTH);

    ssh_options_set(session, SSH_OPTIONS_HOST, "pubkey");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG8);
    assert_true(ret == 0);
    assert_true(session->opts.flags & SSH_OPT_FLAG_PUBKEY_AUTH);
}

/**
 * @brief Verify the configuration parser does not choke on unknown
 * or unsupported configuration options
 */
static void torture_config_unknown(void **state) {
    ssh_session session = *state;
    int ret = 0;

    /* test corner cases */
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG9);
    assert_true(ret == 0);
    ret = ssh_config_parse_file(session, "/etc/ssh/ssh_config");
    assert_true(ret == 0);
    ret = ssh_config_parse_file(session, GLOBAL_CLIENT_CONFIG);
    assert_true(ret == 0);
}


/**
 * @brief Verify the configuration parser accepts Match keyword with
 * full OpenSSH syntax.
 */
static void torture_config_match(void **state)
{
    ssh_session session = *state;
    char *localuser = NULL;
    char config[1024];
    int ret = 0;

    /* Without any settings we should get all-matched.com hostname */
    ssh_options_set(session, SSH_OPTIONS_HOST, "unmatched");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "all-matched.com");

    /* Hostname example does simple hostname matching */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "example");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "example.com");

    /* We can match also both hosts from a comma separated list */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "example1");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "exampleN");

    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "example2");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "exampleN");

    /* We can match by user */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_USER, "guest");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "guest.com");

    /* We can combine two options on a single line to match both of them */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_USER, "tester");
    ssh_options_set(session, SSH_OPTIONS_HOST, "testhost");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "testhost.com");

    /* We can also negate conditions */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_USER, "not-tester");
    ssh_options_set(session, SSH_OPTIONS_HOST, "testhost");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "nonuser-testhost.com");

    /* Match final is not completely supported, but should do quite much the
     * same as "match all". The trailing "all" is not mandatory. */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match final all\n"
                       "\tHostName final-all.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "final-all.com");

    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match final\n"
                       "\tHostName final.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "final.com");

    /* Match canonical is not completely supported, but should do quite much the
     * same as "match all". The trailing "all" is not mandatory. */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match canonical all\n"
                       "\tHostName canonical-all.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "canonical-all.com");

    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match canonical all\n"
                       "\tHostName canonical.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "canonical.com");

    localuser = ssh_get_local_username();
    assert_non_null(localuser);
    snprintf(config, sizeof(config),
             "Match localuser %s\n"
             "\tHostName otherhost\n"
             "", localuser);
    free(localuser);
    torture_write_file(LIBSSH_TESTCONFIG10, config);
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.host, "otherhost");

    /* Try to create some invalid configurations */
    /* Missing argument to Match*/
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match\n"
                       "\tHost missing.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing argument to unsupported option originalhost */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match originalhost\n"
                       "\tHost originalhost.com\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing argument to option localuser */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match localuser\n"
                       "\tUser localuser2\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing argument to option user */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match user\n"
                       "\tUser user2\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing argument to option host */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match host\n"
                       "\tUser host2\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing argument to unsupported option exec */
    torture_write_file(LIBSSH_TESTCONFIG10,
                       "Match exec\n"
                       "\tUser exec\n"
                       "");
    torture_reset_config(session);
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG10);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);
}

/**
 * @brief Verify we can parse ProxyJump configuration option
 */
static void torture_config_proxyjump(void **state) {
    ssh_session session = *state;
    int ret = 0;

    /* Simplest version with just a hostname */
    ssh_options_set(session, SSH_OPTIONS_HOST, "simple");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand, "ssh -W [%h]:%p jumpbox");

    /* With username */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "user");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand,
                        "ssh -l user -W [%h]:%p jumpbox");

    /* With port */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "port");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand,
                        "ssh -p 2222 -W [%h]:%p jumpbox");

    /* Two step jump */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "two-step");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand,
                        "ssh -l u1 -p 222 -J u2@second:33 -W [%h]:%p first");

    /* none */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "none");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_true(session->opts.ProxyCommand == NULL);

    /* If also ProxyCommand is specifed, the first is applied */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "only-command");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand, PROXYCMD);

    /* If also ProxyCommand is specifed, the first is applied */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "only-jump");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand,
                        "ssh -W [%h]:%p jumpbox");

    /* IPv6 address */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "ipv6");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code(session, ret);
    assert_string_equal(session->opts.ProxyCommand,
                        "ssh -W [%h]:%p 2620:52:0::fed");

    /* Try to create some invalid configurations */
    /* Non-numeric port */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host bad-port\n"
                       "\tProxyJump jumpbox:22bad22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "bad-port");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Too many @ */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host bad-hostname\n"
                       "\tProxyJump user@principal.com@jumpbox:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "bad-hostname");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Braces mismatch in hostname */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host mismatch\n"
                       "\tProxyJump [::1\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "mismatch");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Bad host-port separator */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host beef\n"
                       "\tProxyJump [dead::beef]::22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "beef");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing hostname */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-host\n"
                       "\tProxyJump user@:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-host");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing user */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-user\n"
                       "\tProxyJump @host:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-user");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing port */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-port\n"
                       "\tProxyJump host:\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-port");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Non-numeric port in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host bad-port-2\n"
                       "\tProxyJump localhost,jumpbox:22bad22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "bad-port-2");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Too many @ in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host bad-hostname\n"
                       "\tProxyJump localhost,user@principal.com@jumpbox:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "bad-hostname");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Braces mismatch in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host mismatch\n"
                       "\tProxyJump localhost,[::1:20\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "mismatch");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Bad host-port separator in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host beef\n"
                       "\tProxyJump localhost,[dead::beef]::22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "beef");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing hostname in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-host\n"
                       "\tProxyJump localhost,user@:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-host");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing user in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-user\n"
                       "\tProxyJump localhost,@host:22\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-user");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);

    /* Missing port in second jump */
    torture_write_file(LIBSSH_TESTCONFIG11,
                       "Host no-port\n"
                       "\tProxyJump localhost,host:\n"
                       "");
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "no-port");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG11);
    assert_ssh_return_code_equal(session, ret, SSH_ERROR);
}

/**
 * @brief Verify the configuration parser handles all the possible
 * versions of RekeyLimit configuration option.
 */
static void torture_config_rekey(void **state)
{
    ssh_session session = *state;
    int ret = 0;

    /* Default values */
    ssh_options_set(session, SSH_OPTIONS_HOST, "default");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 0);
    assert_int_equal(session->opts.rekey_time, 0);

    /* 42 GB */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "data1");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, (uint64_t) 42 * 1024 * 1024 * 1024);
    assert_int_equal(session->opts.rekey_time, 0);

    /* 41 MB */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "data2");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 31 * 1024 * 1024);
    assert_int_equal(session->opts.rekey_time, 0);

    /* 521 KB */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "data3");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 521 * 1024);
    assert_int_equal(session->opts.rekey_time, 0);

    /* default 3D */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "time1");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 0);
    assert_int_equal(session->opts.rekey_time, 3 * 24 * 60 * 60 * 1000);

    /* default 2h */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "time2");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 0);
    assert_int_equal(session->opts.rekey_time, 2 * 60 * 60 * 1000);

    /* default 160m */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "time3");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 0);
    assert_int_equal(session->opts.rekey_time, 160 * 60 * 1000);

    /* default 9600 [s] */
    torture_reset_config(session);
    ssh_options_set(session, SSH_OPTIONS_HOST, "time4");
    ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG12);
    assert_ssh_return_code(session, ret);
    assert_int_equal(session->opts.rekey_data, 0);
    assert_int_equal(session->opts.rekey_time, 9600 * 1000);

}

/**
 * @brief test ssh_config_parse_file with PubkeyAcceptedKeyTypes
 */
static void torture_config_pubkeyacceptedkeytypes(void **state)
{
    ssh_session session = *state;
    int rc;
    char *fips_algos;

    rc = ssh_config_parse_file(session, LIBSSH_TEST_PUBKEYACCEPTEDKEYTYPES);
    assert_int_equal(rc, SSH_OK);

    if (ssh_fips_mode()) {
        fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, PUBKEYACCEPTEDTYPES);
        assert_non_null(fips_algos);
        assert_string_equal(session->opts.pubkey_accepted_types, fips_algos);
        SAFE_FREE(fips_algos);
    } else {
        assert_string_equal(session->opts.pubkey_accepted_types, PUBKEYACCEPTEDTYPES);
    }
}

/* ssh_config_get_cmd() does three things:
 *  * Strips leading whitespace
 *  * Terminate the characted on the end of next quotes-enclosed string
 *  * Terminate on the end of line
 */
static void torture_config_parser_get_cmd(void **state)
{
    char *p = NULL, *tok = NULL;
    char data[256];

    (void) state;

    /* Ignore leading whitespace */
    strncpy(data, "  \t\t  string\n", sizeof(data));
    p = data;
    tok = ssh_config_get_cmd(&p);
    assert_string_equal(tok, "string");
    assert_int_equal(*p, '\0');

    /* but keeps the trailing whitespace */
    strncpy(data, "string  \t\t  \n", sizeof(data));
    p = data;
    tok = ssh_config_get_cmd(&p);
    assert_string_equal(tok, "string  \t\t  ");
    assert_int_equal(*p, '\0');

    /* should drop the quotes and split them into separate arguments */
    strncpy(data, "\"multi string\" something\n", sizeof(data));
    p = data;
    tok = ssh_config_get_cmd(&p);
    assert_string_equal(tok, "multi string");
    assert_int_equal(*p, ' ');
    tok = ssh_config_get_cmd(&p);
    assert_string_equal(tok, "something");
    assert_int_equal(*p, '\0');

    /* But it does not split tokens by whitespace if they are not quoted, which is weird */
    strncpy(data, "multi string something\n", sizeof(data));
    p = data;
    tok = ssh_config_get_cmd(&p);
    assert_string_equal(tok, "multi string something");
    assert_int_equal(*p, '\0');
}

/* ssh_config_get_token() should behave as expected
 *  * Strip leading whitespace
 *  * Return first token separated by whitespace or equal sign, respecting quotes!
 */
static void torture_config_parser_get_token(void **state)
{
    char *p = NULL, *tok = NULL;
    char data[256];

    (void) state;

    /* Ignore leading whitespace (from get_cmd() already */
    strncpy(data, "  \t\t  string\n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "string");
    assert_int_equal(*p, '\0');

    /* drops trailing whitespace */
    strncpy(data, "string  \t\t  \n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "string");
    assert_int_equal(*p, ' ');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "\0");
    assert_int_equal(*p, '\0');

    /* Correctly handles tokens in quotes */
    strncpy(data, "\"multi string\" something\n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "multi string");
    assert_int_equal(*p, ' ');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "something");
    assert_int_equal(*p, '\0');

    /* Consistently splits unquoted strings */
    strncpy(data, "multi string something\n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "multi");
    assert_int_equal(*p, 's');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "string");
    assert_int_equal(*p, 's');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "something");
    assert_int_equal(*p, '\0');

    /* It is made to parse also option=value pairs as well */
    strncpy(data, "  key=value  \n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "key");
    assert_int_equal(*p, 'v');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "value");
    assert_int_equal(*p, ' ');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "\0");
    assert_int_equal(*p, '\0');

    /* correctly parses even key=value pairs with either one in quotes */
    strncpy(data, "  key=\"value with spaces\" \n", sizeof(data));
    p = data;
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "key");
    assert_int_equal(*p, '\"');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "value with spaces");
    assert_int_equal(*p, ' ');
    tok = ssh_config_get_token(&p);
    assert_string_equal(tok, "\0");
    assert_int_equal(*p, '\0');
}

int torture_run_tests(void) {
    int rc;
    struct CMUnitTest tests[] = {
        cmocka_unit_test(torture_config_from_file),
        cmocka_unit_test(torture_config_double_ports),
        cmocka_unit_test(torture_config_glob),
        cmocka_unit_test(torture_config_new),
        cmocka_unit_test(torture_config_auth_methods),
        cmocka_unit_test(torture_config_unknown),
        cmocka_unit_test(torture_config_match),
        cmocka_unit_test(torture_config_proxyjump),
        cmocka_unit_test(torture_config_rekey),
        cmocka_unit_test(torture_config_pubkeyacceptedkeytypes),
        cmocka_unit_test(torture_config_parser_get_cmd),
        cmocka_unit_test(torture_config_parser_get_token),
    };


    ssh_init();
    torture_filter_tests(tests);
    rc = cmocka_run_group_tests(tests, setup_config_files, teardown);
    ssh_finalize();
    return rc;
}