| 1 |
/* SPDX-License-Identifier: BSD-2-Clause |
| 2 |
|
| 3 |
Copyright (c) 2023, 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 an entry, rename that entry, and try to read the old and |
| 30 |
new entry again. Reading the old entry should fail. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <time.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <errno.h> |
| 38 |
|
| 39 |
#include "lastlog2P.h" |
| 40 |
|
| 41 |
static int |
| 42 |
test_args (struct ll2_context *context, const char *user, int64_t ll_time, |
| 43 |
const char *tty, const char *rhost, const char *service) |
| 44 |
{ |
| 45 |
int rc = 1; |
| 46 |
char *error = NULL; |
| 47 |
int64_t res_time; |
| 48 |
char *res_tty = NULL; |
| 49 |
char *res_rhost = NULL; |
| 50 |
char *res_service = NULL; |
| 51 |
|
| 52 |
if (ll2_write_entry (context, user, ll_time, tty, rhost, service, &error) != 0) { |
| 53 |
if (error) { |
| 54 |
fprintf (stderr, "%s\n", error); |
| 55 |
free (error); |
| 56 |
} else |
| 57 |
fprintf (stderr, "ll2_write_entry failed\n"); |
| 58 |
goto done; |
| 59 |
} |
| 60 |
|
| 61 |
if (ll2_read_entry (context, user, &res_time, &res_tty, &res_rhost, &res_service, &error) != 0) { |
| 62 |
if (error) { |
| 63 |
fprintf (stderr, "%s\n", error); |
| 64 |
free (error); |
| 65 |
} else |
| 66 |
fprintf (stderr, "Unknown error reading database %s", context->lastlog2_path); |
| 67 |
goto done; |
| 68 |
} |
| 69 |
|
| 70 |
if (ll_time != res_time) { |
| 71 |
fprintf (stderr, "Wrong time: got %lld, expect %lld\n", |
| 72 |
(long long int)res_time, (long long int)ll_time); |
| 73 |
goto done; |
| 74 |
} |
| 75 |
|
| 76 |
if ((tty == NULL && res_tty != NULL) || |
| 77 |
(tty != NULL && res_tty == NULL) || |
| 78 |
(tty != NULL && res_tty != NULL && strcmp (tty, res_tty) != 0)) { |
| 79 |
fprintf (stderr, "Wrong tty: got %s, expect %s\n", tty, res_tty); |
| 80 |
goto done; |
| 81 |
} |
| 82 |
|
| 83 |
if ((rhost == NULL && res_rhost != NULL) || |
| 84 |
(rhost != NULL && res_rhost == NULL) || |
| 85 |
(rhost != NULL && res_rhost != NULL && strcmp (rhost, res_rhost) != 0)) { |
| 86 |
fprintf (stderr, "Wrong rhost: got %s, expect %s\n", rhost, res_rhost); |
| 87 |
goto done; |
| 88 |
} |
| 89 |
|
| 90 |
if ((service == NULL && res_service != NULL) || |
| 91 |
(service != NULL && res_service == NULL) || |
| 92 |
(service != NULL && res_service != NULL && strcmp (service, res_service) != 0)) { |
| 93 |
fprintf (stderr, "Wrong service: got %s, expect %s\n", service, res_service); |
| 94 |
goto done; |
| 95 |
} |
| 96 |
|
| 97 |
rc = 0; |
| 98 |
done: |
| 99 |
free (res_tty); |
| 100 |
free (res_rhost); |
| 101 |
free (res_service); |
| 102 |
return rc; |
| 103 |
} |
| 104 |
|
| 105 |
int |
| 106 |
main(void) |
| 107 |
{ |
| 108 |
struct ll2_context *context = ll2_new_context("tst-write-read-user.db"); |
| 109 |
char *error = NULL; |
| 110 |
int64_t res_time; |
| 111 |
char *res_tty = NULL; |
| 112 |
char *res_rhost = NULL; |
| 113 |
char *res_service = NULL; |
| 114 |
|
| 115 |
if (test_args (context, "user1", time (NULL), "test-tty", "localhost", "test") != 0) { |
| 116 |
ll2_unref_context(context); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
if (test_args (context, "user2", 0, NULL, NULL, NULL) != 0) { |
| 120 |
ll2_unref_context(context); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
if (test_args (context, "user3", time (NULL), NULL, NULL, NULL) != 0) { |
| 124 |
ll2_unref_context(context); |
| 125 |
return 1; |
| 126 |
} |
| 127 |
if (test_args (context, "user4", time (NULL), "test-tty", NULL, NULL) != 0) { |
| 128 |
ll2_unref_context(context); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
if (test_args (context, "user5", time (NULL), NULL, "localhost", NULL) != 0) { |
| 132 |
ll2_unref_context(context); |
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
/* Checking errno if the db file does not exist */ |
| 137 |
struct ll2_context *context_not_found = ll2_new_context("no_file"); |
| 138 |
if (ll2_read_entry (context_not_found, "user", &res_time, &res_tty, &res_rhost, &res_service, &error) != 0) { |
| 139 |
if (error) { |
| 140 |
fprintf (stderr, "%s\n", error); |
| 141 |
free (error); |
| 142 |
} else |
| 143 |
fprintf (stderr, "Couldn't read entries for all users\n"); |
| 144 |
|
| 145 |
if(errno) { |
| 146 |
if (errno == ENOENT) |
| 147 |
{ |
| 148 |
fprintf (stderr, "Returning the correct errno: %s\n", |
| 149 |
strerror (errno)); |
| 150 |
ll2_unref_context(context_not_found); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
fprintf (stderr, "errno: %s\n", |
| 154 |
strerror (errno)); |
| 155 |
} else { |
| 156 |
fprintf (stderr, "errno: NULL\n"); |
| 157 |
} |
| 158 |
|
| 159 |
ll2_unref_context(context_not_found); |
| 160 |
ll2_unref_context(context); |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
ll2_unref_context(context); |
| 165 |
return 0; |
| 166 |
} |