2007-02-11 16:39:43 +02:00
|
|
|
/*
|
2014-06-01 09:46:42 +03:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2007-02-11 16:39:43 +02:00
|
|
|
*
|
2014-08-07 22:14:10 +03:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2007-02-11 16:39:43 +02:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <check.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
2012-02-12 11:39:24 +02:00
|
|
|
#include <arpa/inet.h>
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
2007-06-09 19:57:33 +03:00
|
|
|
#include "encoding.h"
|
2007-02-11 16:39:43 +02:00
|
|
|
#include "user.h"
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
START_TEST(test_init_users)
|
|
|
|
{
|
|
|
|
in_addr_t ip;
|
|
|
|
char givenip[16];
|
|
|
|
int i;
|
2011-04-23 21:55:59 +03:00
|
|
|
int count;
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
ip = inet_addr("127.0.0.1");
|
2011-04-23 21:55:59 +03:00
|
|
|
count = init_users(ip, 27);
|
|
|
|
for (i = 0; i < count; i++) {
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(users[i].id == i);
|
|
|
|
ck_assert(users[i].q.id == 0);
|
|
|
|
ck_assert(users[i].inpacket.len == 0);
|
|
|
|
ck_assert(users[i].outpacket.len == 0);
|
2007-02-11 16:39:43 +02:00
|
|
|
snprintf(givenip, sizeof(givenip), "127.0.0.%d", i + 2);
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(users[i].tun_ip == inet_addr(givenip));
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_find_user_by_ip)
|
|
|
|
{
|
|
|
|
in_addr_t ip;
|
|
|
|
unsigned int testip;
|
|
|
|
|
|
|
|
ip = inet_addr("127.0.0.1");
|
2009-01-04 14:03:35 +02:00
|
|
|
init_users(ip, 27);
|
2009-09-21 00:10:45 +03:00
|
|
|
users[0].conn = CONN_DNS_NULL;
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
testip = (unsigned int) inet_addr("10.0.0.1");
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_user_by_ip(testip) == -1);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
testip = (unsigned int) inet_addr("127.0.0.2");
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_user_by_ip(testip) == -1);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
users[0].active = 1;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
testip = (unsigned int) inet_addr("127.0.0.2");
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_user_by_ip(testip) == -1);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
users[0].last_pkt = time(NULL);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2014-06-16 22:12:49 +03:00
|
|
|
testip = (unsigned int) inet_addr("127.0.0.2");
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_user_by_ip(testip) == -1);
|
2014-06-16 22:12:49 +03:00
|
|
|
|
|
|
|
users[0].authenticated = 1;
|
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
testip = (unsigned int) inet_addr("127.0.0.2");
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_user_by_ip(testip) == 0);
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_all_users_waiting_to_send)
|
|
|
|
{
|
|
|
|
in_addr_t ip;
|
|
|
|
|
|
|
|
ip = inet_addr("127.0.0.1");
|
2009-01-04 14:03:35 +02:00
|
|
|
init_users(ip, 27);
|
2007-02-11 16:39:43 +02:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(all_users_waiting_to_send() == 1);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2009-09-21 00:10:45 +03:00
|
|
|
users[0].conn = CONN_DNS_NULL;
|
2007-02-11 16:39:43 +02:00
|
|
|
users[0].active = 1;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(all_users_waiting_to_send() == 1);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
users[0].last_pkt = time(NULL);
|
2009-09-21 00:10:45 +03:00
|
|
|
users[0].outpacket.len = 0;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(all_users_waiting_to_send() == 0);
|
2007-02-11 16:39:43 +02:00
|
|
|
|
2009-09-21 00:10:45 +03:00
|
|
|
#ifdef OUTPACKETQ_LEN
|
|
|
|
users[0].outpacketq_filled = 1;
|
|
|
|
#else
|
2007-02-11 16:39:43 +02:00
|
|
|
users[0].outpacket.len = 44;
|
2009-09-21 00:10:45 +03:00
|
|
|
#endif
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(all_users_waiting_to_send() == 1);
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_find_available_user)
|
|
|
|
{
|
|
|
|
in_addr_t ip;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ip = inet_addr("127.0.0.1");
|
2009-01-04 14:03:35 +02:00
|
|
|
init_users(ip, 27);
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2014-06-16 22:12:49 +03:00
|
|
|
users[i].authenticated = 1;
|
|
|
|
users[i].authenticated_raw = 1;
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == i);
|
|
|
|
ck_assert(users[i].authenticated == 0);
|
|
|
|
ck_assert(users[i].authenticated_raw == 0);
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == -1);
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
users[3].active = 0;
|
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == 3);
|
|
|
|
ck_assert(find_available_user() == -1);
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
users[3].last_pkt = 55;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == 3);
|
|
|
|
ck_assert(find_available_user() == -1);
|
2007-02-11 16:39:43 +02:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2009-01-04 14:03:35 +02:00
|
|
|
START_TEST(test_find_available_user_small_net)
|
|
|
|
{
|
|
|
|
in_addr_t ip;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ip = inet_addr("127.0.0.1");
|
|
|
|
init_users(ip, 29); /* this should result in 5 enabled users */
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == i);
|
2009-01-04 14:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == -1);
|
2009-01-04 14:03:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
users[3].active = 0;
|
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == 3);
|
|
|
|
ck_assert(find_available_user() == -1);
|
2009-01-04 14:03:35 +02:00
|
|
|
|
|
|
|
users[3].last_pkt = 55;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2020-07-23 23:31:53 +03:00
|
|
|
ck_assert(find_available_user() == 3);
|
|
|
|
ck_assert(find_available_user() == -1);
|
2009-01-04 14:03:35 +02:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2007-02-11 16:39:43 +02:00
|
|
|
TCase *
|
2024-07-16 00:54:25 +03:00
|
|
|
test_user_create_tests(void)
|
2007-02-11 16:39:43 +02:00
|
|
|
{
|
|
|
|
TCase *tc;
|
|
|
|
|
|
|
|
tc = tcase_create("User");
|
|
|
|
tcase_add_test(tc, test_init_users);
|
|
|
|
tcase_add_test(tc, test_find_user_by_ip);
|
|
|
|
tcase_add_test(tc, test_all_users_waiting_to_send);
|
|
|
|
tcase_add_test(tc, test_find_available_user);
|
2009-01-04 14:03:35 +02:00
|
|
|
tcase_add_test(tc, test_find_available_user_small_net);
|
2007-02-11 16:39:43 +02:00
|
|
|
|
|
|
|
return tc;
|
|
|
|
}
|