1 /* Test-Suite for pfinger
2  *
3  * Copyright (C) 2001 Michael Baumer
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #include <string.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 
28 #include "statusdb.h"
29 #include "log.h"
30 #include "finger.h"
31 #include "compat.h"
32 #include "parser.h"
33 
34 #define NUM_CLIENTS 256
35 #define NUM_USER 128
36 
37 char users[NUM_USER][5];
38 int user_idle[NUM_USER];
39 int user_host[NUM_USER];
40 int user_login_t[NUM_USER];
41 
42 int cur_host;
43 
44 Node mynode;
45 
46 fingerconf config;
47 int debug;
48 
db_insertlist(db_entry * list)49 int db_insertlist(db_entry *list)
50 {
51   int i;
52   int j;
53 
54   i = 0;
55   while(list->next) {
56 
57     /* Iterate over list and compare with saved values */
58 
59     for (j=0; j<NUM_USER; j++)
60       if (!strcmp(list->login_user, users[j]))
61 	break;
62 
63     if (j==NUM_USER)
64       log(LOG_ERR, "User %d not found in users[]", list->login_user);
65     else {
66       /* Compare */
67       if (list->idle_time != user_idle[j])
68 	log(LOG_ERR, "Idle times do not match: %d vs. %d (user: %s)", list->idle_time, user_idle[j], list->login_user);
69 
70       if (strcmp(list->hostname, config.clients[user_host[j]]))
71 	log(LOG_ERR, "Hosts do not match: %s vs. %s (user: %s)", list->hostname, config.clients[user_host[j]], list->login_user);
72 
73       if (list->login_time != user_login_t[j])
74 	log(LOG_ERR, "login times do not match: %d vs. %d (user: %s)", list->login_time, user_login_t[j], list->login_user);
75 
76     }
77 
78     list = list->next;
79     i++;
80   }
81 
82   if (i<NUM_USER)
83     log(LOG_ERR, "too few users in list");
84 
85   return 1;
86 }
87 
88 /* called by pollClients()
89  *
90  * create some socket that will be closed by
91  * pollClients()
92  */
make_conn(char * hostname,char ** res_host)93 int make_conn(char *hostname, char **res_host)
94 {
95   int s;
96   int i;
97 
98   s = open("/tmp/statusdb", O_RDONLY);
99 
100   cur_host++;
101   if (!cur_host)
102     for (i=0; i<NUM_USER; i++)
103       user_idle[i] = 1000;
104 
105   return s;
106 }
107 
108 /* called by pollClients
109  *
110  * it should fill the list of users logged in
111  * (i.e. call handle_online_user)
112  */
send_request(int fdin,int fdout,char * cmd)113 int send_request(int fdin, int fdout, char *cmd)
114 {
115   int i;
116   int idle;
117   int login_t;
118 
119   /* for now we emulate, that all users are logged in on
120      all hosts with random idle times
121   */
122 
123   for (i=0; i<NUM_USER; i++) {
124     /* login name */
125     strcpy(mynode.child->child->value, users[i]);
126     /* idle time */
127     idle = rand() % 1000;
128     sprintf(mynode.child->child->next->value, "%d", idle);
129     /* login time */
130     login_t = time(NULL)-(rand() % 10000);
131     sprintf(mynode.child->child->next->next->value, "%d", login_t);
132     /* host name */
133     sprintf(mynode.child->child->next->next->next->value, "%s", config.clients[cur_host]);
134 
135     /* save values for most active user/host for later check */
136     if (user_idle[i] > idle) {
137       user_idle[i] = idle;
138       user_login_t[i] = login_t;
139       user_host[i] = cur_host;
140     }
141 
142     handle_online_user(&mynode);
143   }
144   return 1;
145 }
146 
create_logins()147 void create_logins()
148 {
149   int i, j;
150 
151   char login[5];
152 
153   strcpy(login, "aaaa");
154 
155   for (i=0; i<NUM_USER; i++) {
156     strcpy(users[i], login);
157     login[3]++;
158     if (login[3] == 'z') {
159       login[2]++;
160       login[3] = 'a';
161       if (login[2] == '}') {
162 	login[1]++;
163 	login[2] = 'a';
164 	if (login[1] == '}') {
165 	  login[0]++;
166 	  login[1] = 'a';
167 	  if (login[0] == '}') {
168 	    fprintf(stderr, "NUM_USER is too high. Stopping at %d", i);
169 	    exit(-1);
170 	  }
171 	}
172       }
173     }
174   }
175 }
176 
177 /* create a node for handle_online_user
178  *
179  * online_reply -> user -> login
180  *                         utctime
181  *                         ...
182  */
create_node()183 void create_node()
184 {
185   Node *node;
186 
187   /* top node */
188   strcpy(mynode.id, "online_reply");
189   mynode.child = (Node *)calloc(1, (sizeof(Node)));
190   node = mynode.child;
191 
192   /* user node */
193   strcpy(node->id, "user");
194   node->child = (Node *)calloc(1, (sizeof(Node)));
195   node = node->child;
196 
197   /* field nodes */
198   /* the first one is the login name */
199   strcpy(node->id, "login");
200   node->value = (char *)calloc(1, 256);
201   node->next = (Node *)calloc(1, (sizeof(Node)));
202   node = node->next;
203 
204   /* the second is the idle time */
205   strcpy(node->id, "idle");
206   node->value = (char *)calloc(1, 256);
207   node->next = (Node *)calloc(1, (sizeof(Node)));
208   node = node->next;
209 
210   /* the third is the login time */
211   strcpy(node->id, "utctime");
212   node->value = (char *)calloc(1, 256);
213   node->next = (Node *)calloc(1, (sizeof(Node)));
214   node = node->next;
215 
216   /* the 4th is the host name */
217   strcpy(node->id, "host");
218   node->value = (char *)calloc(1, 256);
219 
220 }
221 
init_clients()222 void init_clients()
223 {
224   char host[5];
225   int i;
226 
227   config.clients = (char **)malloc(NUM_CLIENTS*sizeof(void *));
228 
229   strcpy(host, "aaaa");
230 
231   for (i=0; i<NUM_CLIENTS; i++) {
232     config.clients[i] = strdup(host);
233     host[3]++;
234     if (host[3] == 'z') {
235       host[2]++;
236       host[3] = 'a';
237       if (host[2] == '}') {
238 	host[1]++;
239 	host[2] = 'a';
240 	if (host[1] == '}') {
241 	  host[0]++;
242 	  host[1] = 'a';
243 	  if (host[0] == '}') {
244 	    fprintf(stderr, "NUM_CLIENTS is too high. Stopping at %d", i);
245 	    exit(-1);
246 	  }
247 	}
248       }
249     }
250   }
251   config.numclients = NUM_CLIENTS;
252 }
253 
main()254 int main()
255 {
256   int j;
257 
258   debug = 0;
259   log_open("poll_test", LOGTYPE_STDERR);
260   init_clients();
261   create_logins();
262   create_node();
263 
264   for (j=0; j<10; j++)
265     {
266       log(LOG_DEBUG, "run %d", j);
267       cur_host = -1;
268       pollClients();
269     }
270 
271   return 0;
272 }
273