File: test_database.c

package info (click to toggle)
postsrsd 2.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 572 kB
  • sloc: ansic: 4,084; python: 385; sh: 99; makefile: 16
file content (104 lines) | stat: -rw-r--r-- 2,980 bytes parent folder | download
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
/* PostSRSd - Sender Rewriting Scheme daemon for Postfix
 * Copyright 2012-2022 Timo Röhling <timo@gaussglocke.de>
 * SPDX-License-Identifier: GPL-3.0-only
 *
 * 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, version 3.
 *
 * 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 "common.h"
#include "database.h"

#include <check.h>
#include <postsrsd_build_config.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

START_TEST(invalid_database)
{
    ck_assert_ptr_null(database_connect("invalid:", true));
}
END_TEST

#ifdef WITH_SQLITE
START_TEST(database_sqlite_key_value)
{
    database_t* db = database_connect("sqlite::memory:", true);
    ck_assert_ptr_nonnull(db);
    ck_assert_ptr_null(database_read(db, "mykey"));
    database_write(db, "mykey", "myvalue", 1);
    char* value = database_read(db, "mykey");
    ck_assert_str_eq(value, "myvalue");
    free(value);
    database_disconnect(db);
}
END_TEST

START_TEST(database_sqlite_expiry)
{
    database_t* db = database_connect("sqlite::memory:", true);
    ck_assert_ptr_nonnull(db);
    database_write(db, "mykey", "myvalue", 0);
    char* value = database_read(db, "mykey");
    ck_assert_str_eq(value, "myvalue");
    free(value);
    database_expire(db);
    ck_assert_ptr_null(database_read(db, "mykey"));
    database_disconnect(db);
}
END_TEST
#endif

#ifdef WITH_REDIS
START_TEST(database_redis_key_value)
{
    database_t* db = database_connect("redis:localhost:6379", true);
    if (db == NULL)
        return; /* skip test if no redis server is available */
    database_write(db, "mykey", "myvalue", 1);
    char* value = database_read(db, "mykey");
    ck_assert_str_eq(value, "myvalue");
    free(value);
    database_disconnect(db);
}
END_TEST

START_TEST(database_redis_expiry)
{
    database_t* db = database_connect("redis:localhost:6379", true);
    if (db == NULL)
        return; /* skip test if no redis server is available */
    database_write(db, "mykey", "myvalue", 1);
    char* value = database_read(db, "mykey");
    ck_assert_str_eq(value, "myvalue");
    free(value);
    sleep(2);
    database_expire(db);
    ck_assert_ptr_null(database_read(db, "mykey"));
    database_disconnect(db);
}
END_TEST
#endif

BEGIN_TEST_SUITE(database)
ADD_TEST(invalid_database)
#ifdef WITH_SQLITE
ADD_TEST(database_sqlite_key_value)
ADD_TEST(database_sqlite_expiry)
#endif
#ifdef WITH_REDIS
ADD_TEST(database_redis_key_value)
ADD_TEST(database_redis_expiry)
#endif
END_TEST_SUITE()
TEST_MAIN(database)