summaryrefslogtreecommitdiff
path: root/src/tests/krb5_utils-tests.c
blob: bcd9acb193ec9ba5187f9775002adc7bbe970c23 (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
/*
    SSSD

    Kerberos 5 Backend Module -- Utilities tests

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2009 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <popt.h>
#include <check.h>

#include "providers/krb5/krb5_utils.h"
#include "providers/krb5/krb5_auth.h"
#include "tests/common.h"

#define TESTS_PATH "tests_krb5_utils"

#define BASE "/abc/def"
#define FILENAME "ghi"

#define USERNAME "testuser"
#define UID "12345"
#define PRINCIPAL_NAME "testuser@EXAMPLE.COM"
#define REALM "REALM.ORG"
#define HOME_DIRECTORY "/home/testuser"
#define CCACHE_DIR "/var/tmp"
#define PID "4321"

extern struct dp_option default_krb5_opts[];

TALLOC_CTX *tmp_ctx = NULL;
struct krb5child_req *kr;

#define RMDIR(__dir__) do { \
    ret = rmdir(__dir__); \
    fail_unless(ret == EOK, "rmdir [%s] failed, [%d][%s].", __dir__, \
                errno, strerror(errno)); \
} while(0)

void setup_create_dir(void)
{
    fail_unless(tmp_ctx == NULL, "Talloc context already initialized.");
    tmp_ctx = talloc_new(NULL);
    fail_unless(tmp_ctx != NULL, "Cannot create talloc context.");
}

void teardown_create_dir(void)
{
    int ret;
    fail_unless(tmp_ctx != NULL, "Talloc context already freed.");
    ret = talloc_free(tmp_ctx);
    tmp_ctx = NULL;
    fail_unless(ret == 0, "Connot free talloc context.");
}

static void check_dir(const char *dirname, uid_t uid, gid_t gid, mode_t mode)
{
    struct stat stat_buf;
    int ret;

    ret = stat(dirname, &stat_buf);
    fail_unless(ret == EOK, "stat failed [%d][%s].", errno, strerror(errno));

    fail_unless(S_ISDIR(stat_buf.st_mode), "[%s] is not a directory.", dirname);
    fail_unless(stat_buf.st_uid == uid, "uid does not match, "
                                        "expected [%d], got [%d].",
                                        uid, stat_buf.st_uid);
    fail_unless(stat_buf.st_gid == gid, "gid does not match, "
                                        "expected [%d], got [%d].",
                                        gid, stat_buf.st_gid);
    fail_unless((stat_buf.st_mode & ~S_IFMT) == mode,
                                           "mode of [%s] does not match, "
                                           "expected [%o], got [%o].", dirname,
                                            mode, (stat_buf.st_mode & ~S_IFMT));
}

START_TEST(test_pub_ccache_dir)
{
    int ret;
    char *cwd;
    char *testpath;
    char *dirname;
    char *subdirname;
    char *filename;

    fail_unless(getuid() == 0, "This test must be run as root.");

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    testpath = talloc_asprintf(tmp_ctx, "%s/%s", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(testpath != NULL, "talloc_asprintf failed.");
    dirname = talloc_asprintf(tmp_ctx, "%s/pub_ccdir", testpath);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");
    subdirname = talloc_asprintf(tmp_ctx, "%s/subdir", dirname);
    fail_unless(subdirname != NULL, "talloc_asprintf failed.");
    filename = talloc_asprintf(tmp_ctx, "%s/ccfile", subdirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");

    ret = chmod(testpath, 0754);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, 12345, 12345, false);
    fail_unless(ret == EINVAL, "cc_file_create does not return EINVAL "
                               "while x-bit is missing.");

    ret = chmod(testpath, 0755);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, 12345, 12345, false);
    fail_unless(ret == EOK, "cc_file_create failed.");

    check_dir(subdirname, 0, 0, 01777);
    RMDIR(subdirname);
    check_dir(dirname, 0, 0, 0755);
    RMDIR(dirname);
}
END_TEST

START_TEST(test_pub_ccache_dir_in_user_dir)
{
    int ret;
    char *cwd;
    char *dirname;
    char *subdirname;
    char *filename;

    fail_unless(getuid() == 0, "This test must be run as root.");

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    dirname = talloc_asprintf(tmp_ctx, "%s/%s/pub_ccdir", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");
    ret = mkdir(dirname, 0700);
    fail_unless(ret == EOK, "mkdir failed.\n");
    ret = chown(dirname, 12345, 12345);
    fail_unless(ret == EOK, "chown failed.\n");
    subdirname = talloc_asprintf(tmp_ctx, "%s/subdir", dirname);
    fail_unless(subdirname != NULL, "talloc_asprintf failed.");
    filename = talloc_asprintf(tmp_ctx, "%s/ccfile", subdirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");

    ret = cc_file_create(filename, NULL, 12345, 12345, false);
    fail_unless(ret == EINVAL, "Creating public ccache dir in user dir "
                               "does not failed with EINVAL.");

    RMDIR(dirname);
}
END_TEST

START_TEST(test_priv_ccache_dir)
{
    int ret;
    char *cwd;
    char *testpath;
    char *dirname;
    char *subdir;
    char *filename;
    uid_t uid = 12345;
    gid_t gid = 12345;

    fail_unless(getuid() == 0, "This test must be run as root.");

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    testpath = talloc_asprintf(tmp_ctx, "%s/%s", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(testpath != NULL, "talloc_asprintf failed.");
    dirname = talloc_asprintf(tmp_ctx, "%s/base", testpath);
    subdir = talloc_asprintf(tmp_ctx, "%s/priv_ccdir", dirname);
    fail_unless(subdir != NULL, "talloc_asprintf failed.");
    filename = talloc_asprintf(tmp_ctx, "%s/ccfile", subdir);
    fail_unless(filename != NULL, "talloc_asprintf failed.");

    ret = chmod(testpath, 0754);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, uid, gid, true);
    fail_unless(ret == EINVAL, "cc_file_create does not return EINVAL "
                               "while x-bit is missing.");

    ret = chmod(testpath, 0755);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, uid, gid, true);
    fail_unless(ret == EOK, "cc_file_create failed.");

    check_dir(subdir, uid, gid, 0700);
    RMDIR(subdir);
    check_dir(dirname, 0, 0, 0755);
    RMDIR(dirname);
}
END_TEST

START_TEST(test_private_ccache_dir_in_user_dir)
{
    int ret;
    char *cwd;
    char *user_dir;
    char *dn1;
    char *dn2;
    char *dn3;
    char *filename;
    uid_t uid = getuid();
    gid_t gid = getgid();

    if (uid == 0) {
        uid = 12345;
        gid = 12345;
    }

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    user_dir = talloc_asprintf(tmp_ctx, "%s/%s/user", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(user_dir != NULL, "talloc_asprintf failed.");
    ret = mkdir(user_dir, 0700);
    fail_unless(ret == EOK, "mkdir failed.");
    ret = chown(user_dir, uid, gid);
    fail_unless(ret == EOK, "chown failed.");

    dn1 = talloc_asprintf(tmp_ctx, "%s/a", user_dir);
    fail_unless(dn1 != NULL, "talloc_asprintf failed.");
    dn2 = talloc_asprintf(tmp_ctx, "%s/b", dn1);
    fail_unless(dn2 != NULL, "talloc_asprintf failed.");
    dn3 = talloc_asprintf(tmp_ctx, "%s/c", dn2);
    fail_unless(dn3 != NULL, "talloc_asprintf failed.");
    filename = talloc_asprintf(tmp_ctx, "%s/ccfile", dn3);
    fail_unless(filename != NULL, "talloc_asprintf failed.");

    ret = chmod(user_dir, 0600);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, uid, gid, true);
    fail_unless(ret == EINVAL, "cc_file_create does not return EINVAL "
                               "while x-bit is missing.");

    ret = chmod(user_dir, 0700);
    fail_unless(ret == EOK, "chmod failed.");
    ret = cc_file_create(filename, NULL, uid, gid, true);
    fail_unless(ret == EOK, "cc_file_create failed.");

    check_dir(dn3, uid, gid, 0700);
    RMDIR(dn3);
    check_dir(dn2, uid, gid, 0700);
    RMDIR(dn2);
    check_dir(dn1, uid, gid, 0700);
    RMDIR(dn1);
    RMDIR(user_dir);
}
END_TEST

START_TEST(test_private_ccache_dir_in_wrong_user_dir)
{
    int ret;
    char *cwd;
    char *dirname;
    char *subdirname;
    char *filename;

    fail_unless(getuid() == 0, "This test must be run as root.");

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    dirname = talloc_asprintf(tmp_ctx, "%s/%s/priv_ccdir", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");
    ret = mkdir(dirname, 0700);
    fail_unless(ret == EOK, "mkdir failed.\n");
    ret = chown(dirname, 12346, 12346);
    fail_unless(ret == EOK, "chown failed.\n");
    subdirname = talloc_asprintf(tmp_ctx, "%s/subdir", dirname);
    fail_unless(subdirname != NULL, "talloc_asprintf failed.");
    filename = talloc_asprintf(tmp_ctx, "%s/ccfile", subdirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");

    ret = cc_file_create(filename, NULL, 12345, 12345, true);
    fail_unless(ret == EINVAL, "Creating private ccache dir in wrong user "
                               "dir does not failed with EINVAL.");

    RMDIR(dirname);
}
END_TEST

START_TEST(test_illegal_patterns)
{
    int ret;
    char *cwd;
    char *dirname;
    char *filename;
    uid_t uid = getuid();
    gid_t gid = getgid();
    pcre *illegal_re;
    const char *errstr;
    int errval;
    int errpos;

    illegal_re = pcre_compile2(ILLEGAL_PATH_PATTERN, 0,
                               &errval, &errstr, &errpos, NULL);
    fail_unless(illegal_re != NULL, "Invalid Regular Expression pattern at "
                                    " position %d. (Error: %d [%s])\n",
                                    errpos, errval, errstr);

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    dirname = talloc_asprintf(tmp_ctx, "%s/%s/priv_ccdir", cwd, TESTS_PATH);
    free(cwd);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");


    filename = talloc_asprintf(tmp_ctx, "abc/./ccfile");
    fail_unless(filename != NULL, "talloc_asprintf failed.");
    ret = create_ccache_dir(filename, illegal_re, uid, gid, true);
    fail_unless(ret == EINVAL, "create_ccache_dir allowed relative path [%s].",
                               filename);

    filename = talloc_asprintf(tmp_ctx, "%s/abc/./ccfile", dirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");
    ret = create_ccache_dir(filename, illegal_re, uid, gid, true);
    fail_unless(ret == EINVAL, "create_ccache_dir allowed "
                               "illegal pattern '/./' in filename [%s].",
                               filename);

    filename = talloc_asprintf(tmp_ctx, "%s/abc/../ccfile", dirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");
    ret = create_ccache_dir(filename, illegal_re, uid, gid, true);
    fail_unless(ret == EINVAL, "create_ccache_dir allowed "
                               "illegal pattern '/../' in filename [%s].",
                               filename);

    filename = talloc_asprintf(tmp_ctx, "%s/abc//ccfile", dirname);
    fail_unless(filename != NULL, "talloc_asprintf failed.");
    ret = create_ccache_dir(filename, illegal_re, uid, gid, true);
    fail_unless(ret == EINVAL, "create_ccache_dir allowed "
                               "illegal pattern '//' in filename [%s].",
                               filename);

}
END_TEST

START_TEST(test_cc_dir_create)
{
    char *residual;
    char *dirname;
    char *cwd;
    uid_t uid = getuid();
    gid_t gid = getgid();
    pcre *illegal_re;
    errno_t ret;
    const char *errstr;
    int errval;
    int errpos;

    illegal_re = pcre_compile2(ILLEGAL_PATH_PATTERN, 0,
                               &errval, &errstr, &errpos, NULL);
    fail_unless(illegal_re != NULL, "Invalid Regular Expression pattern at "
                                    " position %d. (Error: %d [%s])\n",
                                    errpos, errval, errstr);

    cwd = getcwd(NULL, 0);
    fail_unless(cwd != NULL, "getcwd failed.");

    dirname = talloc_asprintf(tmp_ctx, "%s/%s/user_dir",
                              cwd, TESTS_PATH);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");
    residual = talloc_asprintf(tmp_ctx, "DIR:%s/%s", dirname, "ccdir");
    fail_unless(residual != NULL, "talloc_asprintf failed.");

    ret = cc_dir_create(residual, illegal_re, uid, gid, true);
    fail_unless(ret == EOK, "cc_dir_create failed\n");
    ret = rmdir(dirname);
    fail_unless(ret == 0, "Cannot remove %s: %s\n", dirname, strerror(ret));
    talloc_free(residual);

    dirname = talloc_asprintf(tmp_ctx, "%s/%s/user_dir2",
                              cwd, TESTS_PATH);
    fail_unless(dirname != NULL, "talloc_asprintf failed.");
    residual = talloc_asprintf(tmp_ctx, "DIR:%s/%s", dirname, "ccdir/");
    fail_unless(residual != NULL, "talloc_asprintf failed.");

    ret = cc_dir_create(residual, illegal_re, uid, gid, true);
    fail_unless(ret == EOK, "cc_dir_create failed\n");
    ret = rmdir(dirname);
    fail_unless(ret == 0, "Cannot remove %s: %s\n", dirname, strerror(ret));
    talloc_free(residual);
    free(cwd);
}
END_TEST

void setup_talloc_context(void)
{
    int ret;
    int i;

    struct pam_data *pd;
    struct krb5_ctx *krb5_ctx;
    fail_unless(tmp_ctx == NULL, "Talloc context already initialized.");
    tmp_ctx = talloc_new(NULL);
    fail_unless(tmp_ctx != NULL, "Cannot create talloc context.");

    kr = talloc_zero(tmp_ctx, struct krb5child_req);
    fail_unless(kr != NULL, "Cannot create krb5child_req structure.");

    pd = talloc_zero(tmp_ctx, struct pam_data);
    fail_unless(pd != NULL, "Cannot create pam_data structure.");

    krb5_ctx = talloc_zero(tmp_ctx, struct krb5_ctx);
    fail_unless(pd != NULL, "Cannot create krb5_ctx structure.");

    pd->user = discard_const(USERNAME);
    kr->uid = atoi(UID);
    kr->upn = PRINCIPAL_NAME;
    pd->cli_pid = atoi(PID);

    krb5_ctx->opts = talloc_zero_array(tmp_ctx, struct dp_option, KRB5_OPTS);
    fail_unless(krb5_ctx->opts != NULL, "Cannot created options.");
    for (i = 0; i < KRB5_OPTS; i++) {
        krb5_ctx->opts[i].opt_name = default_krb5_opts[i].opt_name;
        krb5_ctx->opts[i].type = default_krb5_opts[i].type;
        krb5_ctx->opts[i].def_val = default_krb5_opts[i].def_val;
    }
    ret = dp_opt_set_string(krb5_ctx->opts, KRB5_REALM, REALM);
    fail_unless(ret == EOK, "Failed to set Realm");
    ret = dp_opt_set_string(krb5_ctx->opts, KRB5_CCACHEDIR, CCACHE_DIR);
    fail_unless(ret == EOK, "Failed to set Ccache dir");

    kr->homedir = HOME_DIRECTORY;

    kr->pd = pd;
    kr->krb5_ctx = krb5_ctx;

}

void free_talloc_context(void)
{
    int ret;
    fail_unless(tmp_ctx != NULL, "Talloc context already freed.");
    ret = talloc_free(tmp_ctx);
    tmp_ctx = NULL;
    fail_unless(ret == 0, "Connot free talloc context.");
}

static void do_test(const char *file_template, const char *dir_template,
                    const char *expected, const bool expected_private_path)
{
    char *result;
    int ret;
    bool private_path = false;

    ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCACHEDIR, dir_template);
    fail_unless(ret == EOK, "Failed to set Ccache dir");

    result = expand_ccname_template(tmp_ctx, kr, file_template, true,
                                    true, &private_path);

    fail_unless(result != NULL, "Cannot expand template [%s].", file_template);
    fail_unless(strcmp(result, expected) == 0,
                "Expansion failed, result [%s], expected [%s].",
                result, expected);
    fail_unless(private_path == expected_private_path,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false",
                expected_private_path ? "true" : "false");
}

START_TEST(test_multiple_substitutions)
{
    do_test(BASE"_%u_%U_%u", CCACHE_DIR, BASE"_"USERNAME"_"UID"_"USERNAME, false);
    do_test("%d/"FILENAME, BASE"_%u_%U_%u",
            BASE"_"USERNAME"_"UID"_"USERNAME"/"FILENAME, true);
}
END_TEST

START_TEST(test_username)
{
    do_test(BASE"_%u", CCACHE_DIR, BASE"_"USERNAME, false);
    do_test("%d/"FILENAME, BASE"_%u", BASE"_"USERNAME"/"FILENAME, true);
}
END_TEST

START_TEST(test_case_sensitive)
{
    char *result;
    int ret;
    bool private_path = false;
    const char *file_template = BASE"_%u";
    const char *expected_cs = BASE"_TestUser";
    const char *expected_ci = BASE"_testuser";

    kr->pd->user = discard_const("TestUser");
    ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCACHEDIR, CCACHE_DIR);
    fail_unless(ret == EOK, "Failed to set Ccache dir");

    result = expand_ccname_template(tmp_ctx, kr, file_template, true,
                                    true, &private_path);

    fail_unless(result != NULL, "Cannot expand template [%s].", file_template);
    fail_unless(strcmp(result, expected_cs) == 0,
                "Expansion failed, result [%s], expected [%s].",
                result, expected_cs);

    result = expand_ccname_template(tmp_ctx, kr, file_template, true,
                                    false, &private_path);

    fail_unless(result != NULL, "Cannot expand template [%s].", file_template);
    fail_unless(strcmp(result, expected_ci) == 0,
                "Expansion failed, result [%s], expected [%s].",
                result, expected_ci);
}
END_TEST

START_TEST(test_uid)
{
    do_test(BASE"_%U", CCACHE_DIR, BASE"_"UID, false);
    do_test("%d/"FILENAME, BASE"_%U", BASE"_"UID"/"FILENAME, true);
}
END_TEST

START_TEST(test_upn)
{
    do_test(BASE"_%p", CCACHE_DIR, BASE"_"PRINCIPAL_NAME, false);
    do_test("%d/"FILENAME, BASE"_%p", BASE"_"PRINCIPAL_NAME"/"FILENAME, true);
}
END_TEST

START_TEST(test_realm)
{
    do_test(BASE"_%r", CCACHE_DIR, BASE"_"REALM, false);
    do_test("%d/"FILENAME, BASE"_%r", BASE"_"REALM"/"FILENAME, false);
}
END_TEST

START_TEST(test_home)
{
    do_test(BASE"_%h", CCACHE_DIR, BASE"_"HOME_DIRECTORY, false);
    do_test("%d/"FILENAME, BASE"_%h", BASE"_"HOME_DIRECTORY"/"FILENAME, true);
}
END_TEST

START_TEST(test_ccache_dir)
{
    char *result;
    int ret;
    bool private_path = false;

    do_test(BASE"_%d", CCACHE_DIR, BASE"_"CCACHE_DIR, false);

    ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCACHEDIR, BASE"_%d");
    fail_unless(ret == EOK, "Failed to set Ccache dir");

    result = expand_ccname_template(tmp_ctx, kr, "%d/"FILENAME, true,
                                    true, &private_path);

    fail_unless(result == NULL, "Using %%d in ccache dir should fail.");
    fail_unless(private_path == false,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false", "false");
}
END_TEST

START_TEST(test_pid)
{
    char *result;
    int ret;
    bool private_path = false;

    do_test(BASE"_%P", CCACHE_DIR, BASE"_"PID, false);

    ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCACHEDIR, BASE"_%P");
    fail_unless(ret == EOK, "Failed to set Ccache dir");

    result = expand_ccname_template(tmp_ctx, kr, "%d/"FILENAME, true,
                                    true, &private_path);

    fail_unless(result == NULL, "Using %%P in ccache dir should fail.");
    fail_unless(private_path == false,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false", "false");
}
END_TEST

START_TEST(test_percent)
{
    do_test(BASE"_%%", CCACHE_DIR, BASE"_%", false);
    do_test("%d/"FILENAME, BASE"_%%", BASE"_%/"FILENAME, false);
}
END_TEST

START_TEST(test_unknow_template)
{
    const char *test_template = BASE"_%X";
    char *result;
    int ret;
    bool private_path = false;

    result = expand_ccname_template(tmp_ctx, kr, test_template, true,
                                    true, &private_path);

    fail_unless(result == NULL, "Unknown template [%s] should fail.",
                test_template);

    ret = dp_opt_set_string(kr->krb5_ctx->opts, KRB5_CCACHEDIR, BASE"_%X");
    fail_unless(ret == EOK, "Failed to set Ccache dir");
    test_template = "%d/"FILENAME;
    result = expand_ccname_template(tmp_ctx, kr, test_template, true,
                                    true, &private_path);

    fail_unless(result == NULL, "Unknown template [%s] should fail.",
                test_template);
    fail_unless(private_path == false,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false", "false");
}
END_TEST

START_TEST(test_NULL)
{
    char *test_template = NULL;
    char *result;
    bool private_path = false;

    result = expand_ccname_template(tmp_ctx, kr, test_template, true,
                                    true, &private_path);

    fail_unless(result == NULL, "Expected NULL as a result for an empty input.",
                test_template);
    fail_unless(private_path == false,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false", "false");
}
END_TEST

START_TEST(test_no_substitution)
{
    const char *test_template = BASE;
    char *result;
    bool private_path = false;

    result = expand_ccname_template(tmp_ctx, kr, test_template, true,
                                    true, &private_path);

    fail_unless(result != NULL, "Cannot expand template [%s].", test_template);
    fail_unless(strcmp(result, test_template) == 0,
                "Expansion failed, result [%s], expected [%s].",
                result, test_template);
    fail_unless(private_path == false,
                "Unexprected private path, get [%s], expected [%s].",
                private_path ? "true" : "false", "false");
}
END_TEST

Suite *krb5_utils_suite (void)
{
    Suite *s = suite_create ("krb5_utils");

    TCase *tc_ccname_template = tcase_create ("ccname_template");
    tcase_add_checked_fixture (tc_ccname_template, setup_talloc_context,
                               free_talloc_context);
    tcase_add_test (tc_ccname_template, test_no_substitution);
    tcase_add_test (tc_ccname_template, test_NULL);
    tcase_add_test (tc_ccname_template, test_unknow_template);
    tcase_add_test (tc_ccname_template, test_username);
    tcase_add_test (tc_ccname_template, test_case_sensitive);
    tcase_add_test (tc_ccname_template, test_uid);
    tcase_add_test (tc_ccname_template, test_upn);
    tcase_add_test (tc_ccname_template, test_realm);
    tcase_add_test (tc_ccname_template, test_home);
    tcase_add_test (tc_ccname_template, test_ccache_dir);
    tcase_add_test (tc_ccname_template, test_pid);
    tcase_add_test (tc_ccname_template, test_percent);
    tcase_add_test (tc_ccname_template, test_multiple_substitutions);
    suite_add_tcase (s, tc_ccname_template);

    TCase *tc_create_dir = tcase_create("create_dir");
    tcase_add_checked_fixture (tc_create_dir, setup_create_dir,
                               teardown_create_dir);
    tcase_add_test (tc_create_dir, test_illegal_patterns);
    tcase_add_test (tc_create_dir, test_cc_dir_create);
    if (getuid() == 0) {
        tcase_add_test (tc_create_dir, test_priv_ccache_dir);
        tcase_add_test (tc_create_dir, test_private_ccache_dir_in_user_dir);
        tcase_add_test (tc_create_dir, test_pub_ccache_dir);
        tcase_add_test (tc_create_dir, test_pub_ccache_dir_in_user_dir);
        tcase_add_test (tc_create_dir, test_private_ccache_dir_in_wrong_user_dir);
    } else {
        printf("Run as root to enable more tests.\n");
    }
    suite_add_tcase (s, tc_create_dir);

    return s;
}

int main(int argc, const char *argv[])
{
    int ret;
    int opt;
    poptContext pc;
    int number_failed;

    tests_set_cwd();

    struct poptOption long_options[] = {
        POPT_AUTOHELP
        SSSD_MAIN_OPTS
        POPT_TABLEEND
    };

    /* Set debug level to invalid value so we can deside if -d 0 was used. */
    debug_level = SSSDBG_INVALID;

    pc = poptGetContext(argv[0], argc, argv, long_options, 0);
    while((opt = poptGetNextOpt(pc)) != -1) {
        switch(opt) {
        default:
            fprintf(stderr, "\nInvalid option %s: %s\n\n",
                    poptBadOption(pc, 0), poptStrerror(opt));
            poptPrintUsage(pc, stderr, 0);
            return 1;
        }
    }
    poptFreeContext(pc);

    CONVERT_AND_SET_DEBUG_LEVEL(debug_level);

    ret = mkdir(TESTS_PATH, 0775);
    if (ret != EOK) {
        fprintf(stderr, "Could not create empty directory [%s]. ", TESTS_PATH);
        if (errno == EEXIST) {
            fprintf(stderr, "Please remove [%s].\n", TESTS_PATH);
        } else {
            fprintf(stderr, "[%d][%s].\n", errno, strerror(errno));
        }

        return 1;
    }

    Suite *s = krb5_utils_suite ();
    SRunner *sr = srunner_create (s);
    /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
    srunner_run_all(sr, CK_ENV);
    number_failed = srunner_ntests_failed (sr);
    srunner_free (sr);
    if (number_failed == 0) {
        ret = rmdir(TESTS_PATH);
        if (ret != EOK) {
            fprintf(stderr, "Cannot remove [%s]: [%d][%s].\n", TESTS_PATH,
                            errno, strerror(errno));
            return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
    }

    return EXIT_FAILURE;
}