Debian Codegraph @

liblastlog2/src/tests/tst_y2038_ll2_read_all.c

raw
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 with an 3*INT32_MAX timestamp, store that,
30 read that via ll2_read_all callback again and make sure the
31 timestamp is correct.
32 */
33
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <errno.h>
40
41 #include "lastlog2P.h"
42
43 #define BIG_TIME_VALUE ((int64_t)3*INT32_MAX)
44
45 const char *user = "y2038";
46 const char *on_tty = "pts/test";
47 const char *rhost = NULL;
48 const char *service = "sshd";
49
50 static int
51 check_y2038 (const char *res_user, int64_t ll_time, const char *res_tty,
52 const char *res_rhost, const char *res_service, const char *error)
53 {
54
55 if (strcmp (user, res_user) != 0) {
56 fprintf (stderr, "write/read entry user mismatch: written: %s, got: %s\n",
57 user, res_user);
58 exit (1);
59 }
60
61 if (ll_time != BIG_TIME_VALUE) {
62 fprintf (stderr, "write/read entry time mismatch: written: %lld, got: %lld\n",
63 (long long int)BIG_TIME_VALUE, (long long int)ll_time);
64 exit (1);
65 }
66
67 if (strcmp (on_tty, res_tty) != 0) {
68 fprintf (stderr, "write/read entry tty mismatch: written: %s, got: %s\n",
69 on_tty, res_tty);
70 exit (1);
71 }
72
73 if (rhost != NULL) {
74 fprintf (stderr, "write/read entry rhost mismatch: written: NULL, got: %s\n",
75 res_rhost);
76 exit (1);
77 }
78
79 if (strcmp (service, res_service) != 0) {
80 fprintf (stderr, "write/read entry service mismatch: written: %s, got: %s\n",
81 service, res_service);
82 exit (1);
83 }
84
85 if (error != NULL) {
86 fprintf (stderr, "got error: %s\n",
87 error);
88 exit (1);
89 }
90
91 return 0;
92 }
93
94 int
95 main(void)
96 {
97 struct ll2_context *context = ll2_new_context("y2038-ll2_read_all.db");
98 char *error = NULL;
99
100 remove (context->lastlog2_path);
101
102 printf ("Big time value is: %lld\n", (long long int)BIG_TIME_VALUE);
103
104 if (ll2_write_entry (context, user, BIG_TIME_VALUE, on_tty, rhost, service,
105 &error) != 0) {
106 if (error) {
107 fprintf (stderr, "%s\n", error);
108 free (error);
109 }
110 else
111 fprintf (stderr, "ll2_write_entry failed\n");
112 ll2_unref_context(context);
113 return 1;
114 }
115
116 if (ll2_read_all (context, check_y2038, &error) != 0) {
117 if (error) {
118 fprintf (stderr, "%s\n", error);
119 free (error);
120 } else
121 fprintf (stderr, "Couldn't read entries for all users\n");
122 ll2_unref_context(context);
123 return 1;
124 }
125
126 /* Checking errno if the db file does not exist */
127 remove (context->lastlog2_path);
128
129 if (ll2_read_all (context, check_y2038, &error) != 0) {
130 if (error) {
131 fprintf (stderr, "%s\n", error);
132 free (error);
133 } else
134 fprintf (stderr, "Couldn't read entries for all users\n");
135
136 if(errno) {
137 if (errno == ENOENT)
138 {
139 fprintf (stderr, "Returning the correct errno: %s\n",
140 strerror (errno));
141 ll2_unref_context(context);
142 return 0;
143 }
144 fprintf (stderr, "errno: %s\n",
145 strerror (errno));
146 } else {
147 fprintf (stderr, "errno: NULL\n");
148 }
149
150 ll2_unref_context(context);
151 return 1;
152 }
153
154 ll2_unref_context(context);
155 return 0;
156 }