iodine/tests/user.c

211 lines
5.4 KiB
C
Raw Normal View History

2007-02-11 14:39:43 +00:00
/*
2014-06-01 06:46:42 +00:00
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
* 2006-2009 Bjorn Andersson <flex@kryo.se>
2007-02-11 14:39:43 +00:00
*
* Permission to use, copy, modify, and/or distribute this software for any
2007-02-11 14:39:43 +00: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 09:39:24 +00:00
#include <arpa/inet.h>
2007-02-11 14:39:43 +00:00
#include "common.h"
2007-06-09 16:57:33 +00:00
#include "encoding.h"
2007-02-11 14:39:43 +00:00
#include "user.h"
#include "test.h"
START_TEST(test_init_users)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
char givenip[16];
int i;
int count;
ip = inet_addr("127.0.0.1");
count = init_users(ip, 27);
for (i = 0; i < count; i++) {
fail_unless(users[i].id == i);
fail_unless(users[i].q.id == 0);
fail_unless(users[i].inpacket.len == 0);
fail_unless(users[i].outpacket.len == 0);
snprintf(givenip, sizeof(givenip), "127.0.0.%d", i + 2);
fail_unless(users[i].tun_ip == inet_addr(givenip));
}
2007-02-11 14:39:43 +00:00
}
END_TEST
START_TEST(test_users_waiting)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
2014-08-09 01:48:41 +00:00
ip = inet_addr("127.0.0.1");
init_users(ip, 27);
2014-08-09 01:48:41 +00:00
fail_unless(users_waiting_on_reply() == 0);
2014-08-09 01:48:41 +00:00
users[3].active = 1;
2014-08-09 01:48:41 +00:00
fail_unless(users_waiting_on_reply() == 0);
2014-08-09 01:48:41 +00:00
users[3].last_pkt = time(NULL);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(users_waiting_on_reply() == 0);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
users[3].conn = CONN_DNS_NULL;
users[3].q.id = 1;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(users_waiting_on_reply() == 1);
}
END_TEST
2007-02-11 14:39:43 +00:00
START_TEST(test_find_user_by_ip)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
unsigned int testip;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
ip = inet_addr("127.0.0.1");
init_users(ip, 27);
users[0].conn = CONN_DNS_NULL;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
testip = (unsigned int) inet_addr("10.0.0.1");
fail_unless(find_user_by_ip(testip) == -1);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
testip = (unsigned int) inet_addr("127.0.0.2");
fail_unless(find_user_by_ip(testip) == -1);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
users[0].active = 1;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
testip = (unsigned int) inet_addr("127.0.0.2");
fail_unless(find_user_by_ip(testip) == -1);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
users[0].last_pkt = time(NULL);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
testip = (unsigned int) inet_addr("127.0.0.2");
fail_unless(find_user_by_ip(testip) == -1);
2014-08-09 01:48:41 +00:00
users[0].authenticated = 1;
2014-08-09 01:48:41 +00:00
testip = (unsigned int) inet_addr("127.0.0.2");
fail_unless(find_user_by_ip(testip) == 0);
2007-02-11 14:39:43 +00:00
}
END_TEST
START_TEST(test_all_users_waiting_to_send)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
ip = inet_addr("127.0.0.1");
init_users(ip, 27);
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(all_users_waiting_to_send() == 1);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
users[0].conn = CONN_DNS_NULL;
users[0].active = 1;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(all_users_waiting_to_send() == 1);
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
users[0].last_pkt = time(NULL);
users[0].outpacket.len = 0;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(all_users_waiting_to_send() == 0);
2007-02-11 14:39:43 +00:00
2009-09-20 21:10:45 +00:00
#ifdef OUTPACKETQ_LEN
2014-08-09 01:48:41 +00:00
users[0].outpacketq_filled = 1;
2009-09-20 21:10:45 +00:00
#else
2014-08-09 01:48:41 +00:00
users[0].outpacket.len = 44;
2009-09-20 21:10:45 +00:00
#endif
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(all_users_waiting_to_send() == 1);
2007-02-11 14:39:43 +00:00
}
END_TEST
START_TEST(test_find_available_user)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
int i;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
ip = inet_addr("127.0.0.1");
init_users(ip, 27);
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
for (i = 0; i < USERS; i++) {
users[i].authenticated = 1;
users[i].authenticated_raw = 1;
fail_unless(find_available_user() == i);
fail_if(users[i].authenticated);
fail_if(users[i].authenticated_raw);
}
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
for (i = 0; i < USERS; i++) {
fail_unless(find_available_user() == -1);
}
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
users[3].active = 0;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(find_available_user() == 3);
fail_unless(find_available_user() == -1);
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
users[3].last_pkt = 55;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(find_available_user() == 3);
fail_unless(find_available_user() == -1);
2007-02-11 14:39:43 +00:00
}
END_TEST
START_TEST(test_find_available_user_small_net)
{
2014-08-09 01:48:41 +00:00
in_addr_t ip;
int i;
2014-08-09 01:48:41 +00:00
ip = inet_addr("127.0.0.1");
init_users(ip, 29); /* this should result in 5 enabled users */
2014-08-09 01:48:41 +00:00
for (i = 0; i < 5; i++) {
fail_unless(find_available_user() == i);
}
2014-08-09 01:48:41 +00:00
for (i = 0; i < USERS; i++) {
fail_unless(find_available_user() == -1);
}
2014-08-09 01:48:41 +00:00
users[3].active = 0;
2014-08-09 01:48:41 +00:00
fail_unless(find_available_user() == 3);
fail_unless(find_available_user() == -1);
2014-08-09 01:48:41 +00:00
users[3].last_pkt = 55;
2014-06-01 06:34:18 +00:00
2014-08-09 01:48:41 +00:00
fail_unless(find_available_user() == 3);
fail_unless(find_available_user() == -1);
}
END_TEST
2007-02-11 14:39:43 +00:00
TCase *
test_user_create_tests()
{
2014-08-09 01:48:41 +00:00
TCase *tc;
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
tc = tcase_create("User");
tcase_add_test(tc, test_init_users);
tcase_add_test(tc, test_users_waiting);
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);
tcase_add_test(tc, test_find_available_user_small_net);
2007-02-11 14:39:43 +00:00
2014-08-09 01:48:41 +00:00
return tc;
2007-02-11 14:39:43 +00:00
}