summaryrefslogtreecommitdiffstats
path: root/server/tests/sysdb-tests.c
blob: ef49d4152bf8a08039826bb531d8d9f0dc5b7cf5 (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
/*
   SSSD

   System Database

   Copyright (C) Stephen Gallagher <sgallagh@redhat.com>	2009

   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 <check.h>
#include <talloc.h>
#include <tevent.h>
#include <popt.h>
#include "util/util.h"
#include "confdb/confdb.h"
#include "db/sysdb.h"

struct sysdb_test_ctx {
    struct sysdb_ctx *sysdb;
    struct confdb_ctx *confdb;
    struct event_context *ev;
};

static int setup_sysdb_tests(TALLOC_CTX *mem_ctx, struct sysdb_test_ctx **ctx)
{
    struct sysdb_test_ctx *test_ctx;
    int ret;

    test_ctx = talloc_zero(mem_ctx, struct sysdb_test_ctx);
    if (test_ctx == NULL) {
        fail("Could not allocate memory for test context");
        return ENOMEM;
    }

    /* Create an event context
     * It will not be used except in confdb_init and sysdb_init
     */
    test_ctx->ev = event_context_init(test_ctx);
    if (test_ctx->ev == NULL) {
        fail("Could not create event context");
        talloc_free(test_ctx);
        return EIO;
    }

    /* Connect to the conf db */
    ret = confdb_init(mem_ctx, test_ctx->ev, &test_ctx->confdb);
    if(ret != EOK) {
        fail("Could not initialize connection to the confdb");
        talloc_free(test_ctx);
        return ret;
    }

    ret = sysdb_init(test_ctx, test_ctx->ev, test_ctx->confdb, &test_ctx->sysdb);
    if(ret != EOK) {
        fail("Could not initialize connection to the sysdb");
        talloc_free(test_ctx);
        return ret;
    }

    *ctx = test_ctx;
    return EOK;
}

START_TEST (test_sysdb_store_group_posix)
{
    int ret;
    struct sysdb_test_ctx *test_ctx;
    TALLOC_CTX *mem_ctx;

    /* Setup */
    mem_ctx = talloc_new(NULL);
    ret = setup_sysdb_tests(mem_ctx, &test_ctx);
    if (ret != EOK) {
        fail("Could not set up the test");
        return;
    }

    ret = sysdb_store_group_posix(test_ctx, test_ctx->sysdb,
                            "LOCAL", "sysdbtestgroup", 67000);
    fail_if(ret != EOK, "Could not store sysdbtestgroup");

    talloc_free(mem_ctx);
}
END_TEST

START_TEST (test_sysdb_replace_group_posix)
{
    int ret;
    struct sysdb_test_ctx *test_ctx;
    TALLOC_CTX *mem_ctx;

    /* Setup */
    mem_ctx = talloc_new(NULL);
    ret = setup_sysdb_tests(mem_ctx, &test_ctx);
    if (ret != EOK) {
        fail("Could not set up the test");
        return;
    }

    ret = sysdb_store_group_posix(test_ctx, test_ctx->sysdb,
                            "LOCAL", "sysdbtestgroup", 67001);
    fail_if(ret != EOK, "Could not store sysdbtestgroup");

    talloc_free(mem_ctx);
}
END_TEST

Suite *create_sysdb_suite(void)
{
    Suite *s = suite_create("sysdb");

    /* POSIX Group test case */
    TCase *tc_posix_gr = tcase_create("\tPOSIX Groups");
    tcase_add_test(tc_posix_gr, test_sysdb_store_group_posix);
    tcase_add_test(tc_posix_gr, test_sysdb_replace_group_posix);
    suite_add_tcase(s, tc_posix_gr);

    return s;
}

int main(int argc, const char *argv[]) {
    int opt;
    poptContext pc;
    int failure_count;
    Suite *sysdb_suite;
    SRunner *sr;

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

    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);

    sysdb_suite = create_sysdb_suite();
    sr = srunner_create(sysdb_suite);
    srunner_run_all(sr, CK_VERBOSE);
    failure_count = srunner_ntests_failed(sr);
    srunner_free(sr);
    return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}