File: test_db.c

package info (click to toggle)
libpam-abl 0.6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: ansic: 4,757; sh: 47; makefile: 10
file content (312 lines) | stat: -rw-r--r-- 12,065 bytes parent folder | download | duplicates (4)
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
/*
 *   pam_abl - a PAM module and program for automatic blacklisting of hosts and users
 *
 *   Copyright (C) 2005-2012
 *
 *   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 2 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 "test.h"

#include "test.h"
#include "dbfun.h"

#include <string.h>

#define TEST_DIR "/tmp/pam-abl_dbtest-dir"

static DbEnvironment *openTestEnvironment() {
    DbEnvironment *environment = NULL;
    if (createEnvironment(NULL, TEST_DIR, &environment)) {
        printf("   Could not create the test environment.\n");
        return NULL;
    }
    if (!environment) {
        printf("   Environment creation succeeded, yet no valid pointer.\n");
        return NULL;
    }
    return environment;
}

static Database *openTestDb(DbEnvironment *environment) {
    Database *db = NULL;
    if (openDatabase(environment, "test.db", "test", &db)) {
        printf("   Could not open our test database.\n");
        return NULL;
    }
    if (!db) {
        printf("   Database creation succeeded, yet no valid pointer.\n");
        return NULL;
    }
    return db;
}

static void testOpenClose() {
    removeDir(TEST_DIR);
    makeDir(TEST_DIR);

    DbEnvironment *environment = openTestEnvironment();
    if (!environment)
        return;

    Database *db = openTestDb(environment);
    if (db)
        closeDatabase(db);
    destroyEnvironment(environment);
    removeDir(TEST_DIR);
}

static void testWriteReadOneTransaction() {
    removeDir(TEST_DIR);
    makeDir(TEST_DIR);

    DbEnvironment *environment = openTestEnvironment();
    if (!environment)
        return;

    Database *db = openTestDb(environment);
    if (db) {
        if (startTransaction(environment)) {
            printf("   Could not start a transaction.");
        } else {
            const char *key = "mykey";
            AuthState *result = NULL;
            if (getUserOrHostInfo(db, key, &result))
                printf("   Could not get a non existing key.\n");
            if (result)
                printf("   We expected that the key would not be found.\n");
            if (createEmptyState(CLEAR, &result)) {
                printf("   Creating an empty State failed.\n");
            } else {
                if (saveInfo(db, key, result))
                    printf("   Could not save the empty state.\n");

                AuthState *result2 = NULL;
                if (getUserOrHostInfo(db, key, &result2))
                    printf("   Could not get the saved key.\n");

                if (result && result2) {
                    if (result->m_usedSize != result2->m_usedSize) {
                        printf("   The size of the saved state and the retrieved state is not the same.\n");
                    } else {
                        if (memcmp(result->m_data, result2->m_data, result->m_usedSize))
                            printf("   The saved and retrieved values are not the same.\n");
                    }
                }
                if (result)
                    destroyAuthState(result);
                if (result2)
                    destroyAuthState(result2);
            }
            if (commitTransaction(environment)) {
                printf("   Transaction could not be committed\n.");
            }
        }
        closeDatabase(db);
    }
    destroyEnvironment(environment);
    removeDir(TEST_DIR);
}

static void testWriteReadDifferentTransactions() {
    removeDir(TEST_DIR);
    makeDir(TEST_DIR);

    DbEnvironment *environment = openTestEnvironment();
    if (!environment)
        return;

    Database *db = openTestDb(environment);
    if (db) {
        if (startTransaction(environment)) {
            printf("   Could not start a transaction.");
        } else {
            const char *key = "valueKey";
            AuthState *result = NULL;
            if (createEmptyState(CLEAR, &result)) {
                printf("   Creating an empty State failed.\n");
            } else {
                if (addAttempt(result, USER_BLOCKED, 2, "User1", "Service1", 0, 0))
                    printf("   Could not add an attempt.\n");
                if (saveInfo(db, key, result))
                    printf("   Could not save the empty state.\n");
                if (commitTransaction(environment))
                    printf("   Transaction of get could not be ended.\n");
                if (startTransaction(environment))
                    printf("   The get transaction could not be opened.\n");
                AuthState *result2 = NULL;
                if (getUserOrHostInfo(db, key, &result2))
                    printf("   Could not get the saved key.\n");

                if (result && result2) {
                    if (result->m_usedSize != result2->m_usedSize) {
                        printf("   The size of the saved state and the retrieved state is not the same.\n");
                    } else {
                        if (memcmp(result->m_data, result2->m_data, result->m_usedSize))
                            printf("   The saved and retrieved values are not the same.\n");
                    }
                }
                if (result)
                    destroyAuthState(result);
                if (result2)
                    destroyAuthState(result2);
            }
            if (commitTransaction(environment)) {
                printf("   Transaction could not be committed\n.");
            }
        }
        closeDatabase(db);
    }
    destroyEnvironment(environment);
    removeDir(TEST_DIR);
}

static void testCreateRetrieveUpdate() {
    removeDir(TEST_DIR);
    makeDir(TEST_DIR);

    DbEnvironment *environment = openTestEnvironment();
    if (!environment)
        return;

    Database *db = openTestDb(environment);
    if (db) {
        if (startTransaction(environment)) {
            printf("   Could not start a transaction.");
        } else {
            const char *key = "valueKey";
            AuthState *result = NULL;
            if (createEmptyState(CLEAR, &result)) {
                printf("   Creating an empty State failed.\n");
            } else {
                if (addAttempt(result, USER_BLOCKED, 2, "User1", "Service1", 0, 0))
                    printf("   Could not add an attempt.\n");
                if (saveInfo(db, key, result))
                    printf("   Could not save the empty state.\n");
                if (commitTransaction(environment))
                    printf("   Transaction of get could not be ended.\n");
                if (startTransaction(environment))
                    printf("   The get transaction could not be opened.\n");
                AuthState *result2 = NULL;
                if (getUserOrHostInfo(db, key, &result2))
                    printf("   Could not get the saved key.\n");

                if (result && result2) {
                    if (result->m_usedSize != result2->m_usedSize) {
                        printf("   The size of the saved state and the retrieved state is not the same.\n");
                    } else {
                        if (memcmp(result->m_data, result2->m_data, result->m_usedSize)) {
                            printf("   The saved and retrieved values are not the same.\n");
                        } else {
                            if (addAttempt(result2, USER_BLOCKED, 50, "User2", "Service2", 0, 0))
                                printf("   Could not add a second attempt.");
                            if (saveInfo(db, key, result2))
                                printf("   Could not save the empty state.\n");
                            AuthState *result3 = NULL;
                            if (getUserOrHostInfo(db, key, &result3))
                                printf("   Could not get the updated value.\n");
                            if (result3) {
                                if (getNofAttempts(result3) != 2
                                    || result2->m_usedSize != result3->m_usedSize
                                    || memcmp(result2->m_data, result3->m_data, result2->m_usedSize))
                                    printf("   Did not retrieve the updated value.\n");
                                destroyAuthState(result3);
                            }
                        }
                    }
                }
                if (result)
                    destroyAuthState(result);
                if (result2)
                    destroyAuthState(result2);
            }
            if (commitTransaction(environment)) {
                printf("   Transaction could not be committed\n.");
            }
        }
        closeDatabase(db);
    }
    destroyEnvironment(environment);
    removeDir(TEST_DIR);
}

static void testCreateRetrieveDelete() {
    removeDir(TEST_DIR);
    makeDir(TEST_DIR);

    DbEnvironment *environment = openTestEnvironment();
    if (!environment)
        return;

    Database *db = openTestDb(environment);
    if (db) {
        if (startTransaction(environment)) {
            printf("   Could not start a transaction.");
        } else {
            const char *key = "valueKey";
            AuthState *result = NULL;
            if (createEmptyState(CLEAR, &result)) {
                printf("   Creating an empty State failed.\n");
            } else {
                if (addAttempt(result, USER_BLOCKED, 2, "User1", "Service1", 0, 0))
                    printf("   Could not add an attempt.\n");
                if (saveInfo(db, key, result))
                    printf("   Could not save the empty state.\n");
                AuthState *result2 = NULL;
                if (getUserOrHostInfo(db, key, &result2))
                    printf("   Could not get the saved key.\n");
                if (!result2)
                    printf("   The saved value could not be retrieved\n.");
                if (commitTransaction(environment))
                    printf("   Transaction of get could not be ended.\n");
                if (startTransaction(environment))
                    printf("   The get transaction could not be opened.\n");
                if (removeInfo(db, key))
                    printf("   The saved information could not be removed\n");
                AuthState *result3 = NULL;
                if (getUserOrHostInfo(db, key, &result3))
                    printf("   An error occured while retrieving an unexisting record.\n");
                if (result3)
                    printf("   The removed information still exists.\n");
                if (result)
                    destroyAuthState(result);
                if (result2)
                    destroyAuthState(result2);
                if (result3)
                    destroyAuthState(result3);
            }
            if (commitTransaction(environment)) {
                printf("   Transaction could not be committed\n.");
            }
        }
        closeDatabase(db);
    }
    destroyEnvironment(environment);
    removeDir(TEST_DIR);
}

void runDatabaseTests() {
    printf("Db test start.\n");
    printf(" Starting testOpenClose.\n");
    testOpenClose();
    printf(" Starting testWriteReadOneTransaction.\n");
    testWriteReadOneTransaction();
    printf(" Starting testWriteReadDifferentTransactions.\n");
    testWriteReadDifferentTransactions();
    printf(" Starting testCreateRetrieveUpdate.\n");
    testCreateRetrieveUpdate();
    printf(" Starting testCreateRetrieveDelete.\n");
    testCreateRetrieveDelete();
    printf("Db test end.\n");
}