| 1 |
/* SPDX-License-Identifier: BSD-2-Clause |
| 2 |
|
| 3 |
Copyright (c) 2023, 2025 Thorsten Kukuk <[email protected]> |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without |
| 6 |
modification, are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
1. Redistributions of source code must retain the above copyright notice, |
| 9 |
this list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
2. Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 |
POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
/* Test case: |
| 29 |
Create login entry, add logout time. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <time.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <string.h> |
| 37 |
#include "basics.h" |
| 38 |
|
| 39 |
#include "wtmpdb.h" |
| 40 |
|
| 41 |
static int |
| 42 |
test_args (const char *db_path, const char *user, const char *tty, |
| 43 |
const char *rhost, const char *service, const int days) |
| 44 |
{ |
| 45 |
char *error = NULL; |
| 46 |
int64_t id; |
| 47 |
struct timespec ts; |
| 48 |
uint64_t login_t; |
| 49 |
uint64_t logout_t; |
| 50 |
|
| 51 |
clock_gettime (CLOCK_REALTIME, &ts); |
| 52 |
ts.tv_sec -= 86400 * days; |
| 53 |
login_t = wtmpdb_timespec2usec (ts); |
| 54 |
|
| 55 |
if ((id = wtmpdb_login (db_path, USER_PROCESS, user, |
| 56 |
login_t, tty, rhost, service, &error)) < 0) |
| 57 |
{ |
| 58 |
if (error) |
| 59 |
{ |
| 60 |
fprintf (stderr, "%s\n", error); |
| 61 |
free (error); |
| 62 |
} |
| 63 |
else |
| 64 |
fprintf (stderr, "wtmpdb_login failed\n"); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
|
| 68 |
clock_gettime (CLOCK_REALTIME, &ts); |
| 69 |
logout_t = wtmpdb_timespec2usec (ts); |
| 70 |
|
| 71 |
if (wtmpdb_logout (db_path, id, logout_t, &error) != 0) |
| 72 |
{ |
| 73 |
if (error) |
| 74 |
{ |
| 75 |
fprintf (stderr, "%s\n", error); |
| 76 |
free (error); |
| 77 |
} |
| 78 |
else |
| 79 |
fprintf (stderr, "wtmpdb_logout failed\n"); |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int counter = 0; |
| 87 |
|
| 88 |
static int |
| 89 |
count_entry (void *unused __attribute__((__unused__)), |
| 90 |
int argc, char **argv, char **azColName) |
| 91 |
{ |
| 92 |
(void)argc; |
| 93 |
(void)argv; |
| 94 |
(void)azColName; |
| 95 |
counter++; |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static int |
| 100 |
test_rotate (const char *db_path, const int days) |
| 101 |
{ |
| 102 |
int expected; |
| 103 |
char *error = NULL; |
| 104 |
|
| 105 |
counter = 0; |
| 106 |
if (wtmpdb_read_all (db_path, count_entry, &error) != 0) |
| 107 |
{ |
| 108 |
if (error) |
| 109 |
{ |
| 110 |
fprintf (stderr, "%s\n", error); |
| 111 |
free (error); |
| 112 |
} |
| 113 |
else |
| 114 |
fprintf (stderr, "wtmpdb_read_all failed\n"); |
| 115 |
return 1; |
| 116 |
} |
| 117 |
expected = (days-1) * 5; |
| 118 |
if (counter != expected) |
| 119 |
{ |
| 120 |
fprintf (stderr, "wtmpdb_read_all returned %d expected %d\n", counter, expected); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
if (wtmpdb_rotate (db_path, days, &error, NULL, NULL) != 0) |
| 125 |
{ |
| 126 |
if (error) |
| 127 |
{ |
| 128 |
fprintf (stderr, "%s\n", error); |
| 129 |
free (error); |
| 130 |
} |
| 131 |
else |
| 132 |
fprintf (stderr, "wtmpdb_rotate failed\n"); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
counter = 0; |
| 137 |
if (wtmpdb_read_all (db_path, count_entry, &error) != 0) |
| 138 |
{ |
| 139 |
if (error) |
| 140 |
{ |
| 141 |
fprintf (stderr, "%s\n", error); |
| 142 |
free (error); |
| 143 |
} |
| 144 |
else |
| 145 |
fprintf (stderr, "wtmpdb_read_all failed\n"); |
| 146 |
return 1; |
| 147 |
} |
| 148 |
expected = (days-2) * 5; |
| 149 |
if (counter != expected) |
| 150 |
{ |
| 151 |
fprintf (stderr, "wtmpdb_read_all returned %d expected %d\n", counter, expected); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
remove_backup_db(int days) |
| 160 |
{ |
| 161 |
_cleanup_(freep) char *backup_path = NULL; |
| 162 |
struct timespec ts_now; |
| 163 |
|
| 164 |
clock_gettime (CLOCK_REALTIME, &ts_now); |
| 165 |
|
| 166 |
time_t offset = ts_now.tv_sec - days * 86400; |
| 167 |
struct tm *tm = localtime (&offset); |
| 168 |
char date[10]; |
| 169 |
strftime (date, 10, "%Y%m%d", tm); |
| 170 |
|
| 171 |
if (asprintf (&backup_path, "tst-login-logout_%s.db", date) < 0) |
| 172 |
{ |
| 173 |
fprintf (stderr, "Out of memory"); |
| 174 |
return; |
| 175 |
} |
| 176 |
remove (backup_path); |
| 177 |
} |
| 178 |
|
| 179 |
int |
| 180 |
main(void) |
| 181 |
{ |
| 182 |
const char *db_path = "tst-login-logout.db"; |
| 183 |
|
| 184 |
/* make sure there is no old stuff flying around. The backup file is not so important. */ |
| 185 |
remove (db_path); |
| 186 |
|
| 187 |
if (test_args (db_path, "user1", "test-tty", "localhost", NULL, 3) != 0) |
| 188 |
return 1; |
| 189 |
if (test_args (db_path, "user2", NULL, NULL, NULL, 3) != 0) |
| 190 |
return 1; |
| 191 |
if (test_args (db_path, "user3", NULL, NULL, NULL, 3) != 0) |
| 192 |
return 1; |
| 193 |
if (test_args (db_path, "user4", "test-tty", NULL, NULL, 3) != 0) |
| 194 |
return 1; |
| 195 |
if (test_args (db_path, "user5", NULL, "localhost", NULL, 3) != 0) |
| 196 |
return 1; |
| 197 |
|
| 198 |
if (test_args (db_path, "user1", "test-tty", "localhost", NULL, 2) != 0) |
| 199 |
return 1; |
| 200 |
if (test_args (db_path, "user2", NULL, NULL, NULL, 2) != 0) |
| 201 |
return 1; |
| 202 |
if (test_args (db_path, "user3", NULL, NULL, NULL, 2) != 0) |
| 203 |
return 1; |
| 204 |
if (test_args (db_path, "user4", "test-tty", NULL, NULL, 2) != 0) |
| 205 |
return 1; |
| 206 |
if (test_args (db_path, "user5", NULL, "localhost", NULL, 2) != 0) |
| 207 |
return 1; |
| 208 |
|
| 209 |
if (test_rotate (db_path, 3) != 0) |
| 210 |
return 1; |
| 211 |
|
| 212 |
if (test_rotate (db_path, 2) != 0) |
| 213 |
return 1; |
| 214 |
|
| 215 |
/* cleanup */ |
| 216 |
remove_backup_db(2); |
| 217 |
remove_backup_db(3); |
| 218 |
remove (db_path); |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |