| 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 |
Store defined data into the database, read it, create the time |
| 30 |
string like pam_lastlog2 does, compare the result. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <time.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <stdint.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "lastlog2P.h" |
| 40 |
|
| 41 |
const char *expected = "Last login: Mon Mar 13 07:13:41 UTC 2023 from 192.168.122.1 on pts/0"; |
| 42 |
const time_t login_time = 1678691621; |
| 43 |
int |
| 44 |
main(void) |
| 45 |
{ |
| 46 |
const char *user = "root"; |
| 47 |
struct ll2_context *context = ll2_new_context("pam_lastlog2-output.db"); |
| 48 |
int64_t ll_time = 0; |
| 49 |
char *tty = NULL; |
| 50 |
char *rhost = NULL; |
| 51 |
char *date = NULL; |
| 52 |
char the_time[256]; |
| 53 |
char *error = NULL; |
| 54 |
char *output = NULL; |
| 55 |
|
| 56 |
if (ll2_write_entry (context, user, login_time, "pts/0", |
| 57 |
"192.168.122.1", NULL, &error) != 0) { |
| 58 |
if (error) { |
| 59 |
fprintf (stderr, "%s\n", error); |
| 60 |
free (error); |
| 61 |
} else |
| 62 |
fprintf (stderr, "ll2_write_entry failed\n"); |
| 63 |
ll2_unref_context(context); |
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, |
| 68 |
NULL, &error) != 0) { |
| 69 |
if (error) { |
| 70 |
fprintf (stderr, "%s\n", error); |
| 71 |
free (error); |
| 72 |
} else |
| 73 |
fprintf (stderr, "Unknown error reading database %s", context ? context->lastlog2_path : "NULL"); |
| 74 |
ll2_unref_context(context); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
if (ll_time) { |
| 79 |
struct tm *tm, tm_buf; |
| 80 |
/* this is necessary if you compile this on architectures with |
| 81 |
a 32bit time_t type. */ |
| 82 |
time_t t_time = ll_time; |
| 83 |
|
| 84 |
if ((tm = localtime_r (&t_time, &tm_buf)) != NULL) { |
| 85 |
strftime (the_time, sizeof (the_time), |
| 86 |
" %a %b %e %H:%M:%S %Z %Y", tm); |
| 87 |
date = the_time; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if (asprintf (&output, "Last login:%s%s%s%s%s", |
| 92 |
date ? date : "", |
| 93 |
rhost ? " from " : "", |
| 94 |
rhost ? rhost : "", |
| 95 |
tty ? " on " : "", |
| 96 |
tty ? tty : "") < 0) { |
| 97 |
fprintf (stderr, "Out of memory!\n"); |
| 98 |
ll2_unref_context(context); |
| 99 |
return 1; |
| 100 |
} |
| 101 |
|
| 102 |
if (strcmp (output, expected) != 0) { |
| 103 |
fprintf (stderr, "Output '%s'\n does not match '%s'\n", |
| 104 |
output, expected); |
| 105 |
ll2_unref_context(context); |
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
ll2_unref_context(context); |
| 110 |
free (output); |
| 111 |
free (tty); |
| 112 |
free (rhost); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |