| 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 |
|
| 38 |
#include "lastlog2P.h" |
| 39 |
|
| 40 |
int |
| 41 |
main(void) |
| 42 |
{ |
| 43 |
const char *user = "user"; |
| 44 |
const char *newname = "new"; |
| 45 |
struct ll2_context *context = ll2_new_context("tst-rename-user.db"); |
| 46 |
int64_t ll_time = 0; |
| 47 |
char *tty = NULL; |
| 48 |
char *rhost = NULL; |
| 49 |
char *service = NULL; |
| 50 |
char *error = NULL; |
| 51 |
|
| 52 |
if (ll2_write_entry (context, user, time (NULL), "test-tty", |
| 53 |
"localhost", "test-service", &error) != 0) { |
| 54 |
if (error) { |
| 55 |
fprintf (stderr, "%s\n", error); |
| 56 |
free (error); |
| 57 |
} |
| 58 |
else |
| 59 |
fprintf (stderr, "ll2_write_entry failed\n"); |
| 60 |
ll2_unref_context(context); |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
if (ll2_rename_user (context, user, newname, &error) != 0) { |
| 65 |
if (error) { |
| 66 |
fprintf (stderr, "%s\n", error); |
| 67 |
free (error); |
| 68 |
} |
| 69 |
else |
| 70 |
fprintf (stderr, "ll2_rename_user failed\n"); |
| 71 |
ll2_unref_context(context); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
|
| 75 |
/* this needs to fail, as the old entry shouldn't exist anymore. */ |
| 76 |
if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, |
| 77 |
&service, &error) == 0) { |
| 78 |
fprintf (stderr, "Reading old user from database did not fail!\n"); |
| 79 |
fprintf (stderr, "ll_time=%lld, tty='%s', rhost='%s', service='%s'\n", |
| 80 |
(long long int)ll_time, tty, rhost, service); |
| 81 |
ll2_unref_context(context); |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
if (error) { |
| 86 |
free (error); |
| 87 |
error = NULL; |
| 88 |
} |
| 89 |
|
| 90 |
if (ll2_read_entry (context, newname, &ll_time, &tty, &rhost, &service, |
| 91 |
&error) != 0) { |
| 92 |
if (error) { |
| 93 |
fprintf (stderr, "%s\n", error); |
| 94 |
free (error); |
| 95 |
} else |
| 96 |
fprintf (stderr, "Unknown error reading database %s", context->lastlog2_path); |
| 97 |
ll2_unref_context(context); |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
if (strcmp (tty, "test-tty") != 0 || strcmp (rhost, "localhost") != 0 || |
| 102 |
strcmp (service, "test-service") != 0) { |
| 103 |
fprintf (stderr, "New entry data does not match old entry data!\n"); |
| 104 |
} |
| 105 |
|
| 106 |
ll2_unref_context(context); |
| 107 |
free (tty); |
| 108 |
free (rhost); |
| 109 |
free (service); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |