Debian Codegraph @

tests/tst-varlink.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause
2
3 Copyright (c) 2024, 2025 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 login entry, search for ID, add logout time, get boottime.
30 */
31
32 #include <inttypes.h>
33 #include <time.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39
40 #include "basics.h"
41 #include "wtmpdb.h"
42
43 static void
44 format_time (char *dst, size_t dstlen, uint64_t time)
45 {
46 time_t t = (time_t)time;
47 snprintf (dst, dstlen, "%s", ctime (&t));
48 dst[strlen (dst)-1] = '\0'; /* Remove trailing '\n' */
49 }
50
51 int
52 main(void)
53 {
54 const char *user = "wtmpdb-test";
55 const char *tty = "ttyX";
56 const char *rhost = "remote-host";
57 const char *service = "sshd";
58 char *error = NULL;
59 int64_t id;
60 struct timespec ts;
61 uint64_t login_t;
62 uint64_t logout_t;
63
64 if (getuid() != 0)
65 return 77;
66
67 clock_gettime (CLOCK_REALTIME, &ts);
68 login_t = wtmpdb_timespec2usec (ts);
69
70 if ((id = wtmpdb_login ("varlink", USER_PROCESS, user,
71 login_t, tty, rhost, service, &error)) < 0)
72 {
73 if (error)
74 {
75 fprintf (stderr, "wtmpdb_login: %s\n", error);
76 free (error);
77 }
78 else
79 fprintf (stderr, "wtmpdb_login failed (%" PRId64 ")\n", id);
80
81 if (id == -ECONNREFUSED || id == -ENOENT ||
82 id == -EACCES || id == -EPROTONOSUPPORT)
83 return 77;
84
85 return 1;
86 }
87 printf ("wtmpdb_login id: %" PRId64 "\n", id);
88
89 /* wtmpdb_get_id should return the same ID as wtmpdb_login */
90 int64_t newid;
91 if ((newid = wtmpdb_get_id ("varlink", tty, &error)) < 0)
92 {
93 if (error)
94 {
95 fprintf (stderr, "wtmpdb_get_id: %s\n", error);
96 free (error);
97 }
98 else
99 fprintf (stderr, "wtmpdb_get_id failed\n");
100 return 1;
101 }
102 printf ("wtmpdb_get_id: %" PRId64 "\n", newid);
103
104 if (newid != id)
105 {
106 fprintf (stderr, "IDs don't match: %" PRId64 " != %" PRId64 "\n", id, newid);
107 return 1;
108 }
109
110 clock_gettime (CLOCK_REALTIME, &ts);
111 logout_t = wtmpdb_timespec2usec (ts);
112
113 if (wtmpdb_logout ("varlink", id, logout_t, &error) != 0)
114 {
115 if (error)
116 {
117 fprintf (stderr, "wtmpdb_logout: %s\n", error);
118 free (error);
119 }
120 else
121 fprintf (stderr, "wtmpdb_logout failed\n");
122 return 1;
123 }
124
125 uint64_t boottime = wtmpdb_get_boottime ("varlink", &error);
126 if (boottime == 0 || error != NULL)
127 {
128 if (error)
129 {
130 fprintf (stderr, "wtmpdb_get_boottime: %s\n", error);
131 free (error);
132 }
133 else
134 fprintf (stderr, "wtmpdb_get_boottime failed\n");
135 return 1;
136 }
137 else
138 {
139 char timebuf[32];
140 format_time (timebuf, sizeof (timebuf), boottime/USEC_PER_SEC);
141
142 printf ("wtmpdb_get_boottime: %s\n", timebuf);
143 }
144
145 _cleanup_(freep) char *backup = NULL;
146 uint64_t entries = 0;
147 if (wtmpdb_rotate ("varlink", 30, &error, &backup, &entries) < 0)
148 {
149 if (error)
150 {
151 fprintf (stderr, "wtmpdb_rotate: %s\n", error);
152 free (error);
153 }
154 else
155 fprintf (stderr, "wtmpdb_rotate failed\n");
156 return 1;
157 }
158 else if (entries == 0)
159 printf ("Nothing to move for wtmpdb_rotate\n");
160 else
161 printf ("wtmpdb_rotate moved %" PRIu64 " entries into %s\n", entries, backup);
162
163 return 0;
164 }