| 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, delete that entry, and try to read entry again. |
| 30 |
Reading the entry should fail. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <time.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
#include "lastlog2.h" |
| 39 |
|
| 40 |
int |
| 41 |
main(void) |
| 42 |
{ |
| 43 |
const char *user = "user"; |
| 44 |
struct ll2_context *context = ll2_new_context("tst-delete-user.db"); |
| 45 |
int64_t ll_time = 0; |
| 46 |
char *tty = NULL; |
| 47 |
char *rhost = NULL; |
| 48 |
char *service = NULL; |
| 49 |
char *error = NULL; |
| 50 |
|
| 51 |
if (ll2_write_entry (context, user, time (NULL), "test-tty", |
| 52 |
"localhost", "sshd", &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 |
ll2_unref_context(context); |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
if (ll2_remove_entry (context, user, &error) != 0) { |
| 63 |
if (error) { |
| 64 |
fprintf (stderr, "%s\n", error); |
| 65 |
free (error); |
| 66 |
} else |
| 67 |
fprintf (stderr, "ll2_remove_entry failed\n"); |
| 68 |
ll2_unref_context(context); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
/* this needs to fail, as the old entry shouldn't exist anymore. */ |
| 73 |
if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, &service, &error) == 0) { |
| 74 |
fprintf (stderr, "Reading old user from database did not fail!\n"); |
| 75 |
fprintf (stderr, "ll_time=%lld, tty='%s', rhost='%s', service='%s'\n", |
| 76 |
(long long int)ll_time, tty, rhost, service); |
| 77 |
ll2_unref_context(context); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
ll2_unref_context(context); |
| 82 |
free (error); |
| 83 |
free (tty); |
| 84 |
free (rhost); |
| 85 |
free (service); |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |