diff --git a/tests/Makefile b/tests/Makefile index 17fdbab..c08d443 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,7 +1,7 @@ CC = gcc TEST = test -OBJS = test.o base32.o read.o dns.o encoding.o login.o -SRCOBJS = ../src/base32.o ../src/read.o ../src/dns.o ../src/encoding.o ../src/login.o ../src/md5.o +OBJS = test.o base32.o read.o dns.o encoding.o login.o user.o +SRCOBJS = ../src/base32.o ../src/read.o ../src/dns.o ../src/encoding.o ../src/login.o ../src/md5.o ../src/user.o OS = `uname | tr "a-z" "A-Z"` diff --git a/tests/test.c b/tests/test.c index ae83e61..850d1f7 100644 --- a/tests/test.c +++ b/tests/test.c @@ -61,6 +61,9 @@ main() test = test_login_create_tests(); suite_add_tcase(iodine, test); + test = test_user_create_tests(); + suite_add_tcase(iodine, test); + runner = srunner_create(iodine); srunner_run_all(runner, CK_VERBOSE); failed = srunner_ntests_failed(runner); diff --git a/tests/test.h b/tests/test.h index a0dbf8a..492cd9b 100644 --- a/tests/test.h +++ b/tests/test.h @@ -22,6 +22,7 @@ TCase *test_dns_create_tests(); TCase *test_encoding_create_tests(); TCase *test_read_create_tests(); TCase *test_login_create_tests(); +TCase *test_user_create_tests(); char *va_str(const char *, ...); diff --git a/tests/user.c b/tests/user.c new file mode 100644 index 0000000..d7a437d --- /dev/null +++ b/tests/user.c @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2006 Bjorn Andersson , Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * 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 +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "user.h" +#include "test.h" + +START_TEST(test_init_users) +{ + in_addr_t ip; + char givenip[16]; + int i; + + ip = inet_addr("127.0.0.1"); + init_users(ip); + for (i = 0; i < USERS; 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)); + } +} +END_TEST + +START_TEST(test_users_waiting) +{ + in_addr_t ip; + + ip = inet_addr("127.0.0.1"); + init_users(ip); + + fail_unless(users_waiting_on_reply() == 0); + + users[3].active = 1; + + fail_unless(users_waiting_on_reply() == 0); + + users[3].last_pkt = time(NULL); + + fail_unless(users_waiting_on_reply() == 0); + + users[3].q.id = 1; + + fail_unless(users_waiting_on_reply() == 1); +} +END_TEST + +START_TEST(test_find_user_by_ip) +{ + in_addr_t ip; + unsigned int testip; + + ip = inet_addr("127.0.0.1"); + init_users(ip); + + testip = (unsigned int) inet_addr("10.0.0.1"); + fail_unless(find_user_by_ip(testip) == -1); + + testip = (unsigned int) inet_addr("127.0.0.2"); + fail_unless(find_user_by_ip(testip) == -1); + + users[0].active = 1; + + testip = (unsigned int) inet_addr("127.0.0.2"); + fail_unless(find_user_by_ip(testip) == -1); + + users[0].last_pkt = time(NULL); + + testip = (unsigned int) inet_addr("127.0.0.2"); + fail_unless(find_user_by_ip(testip) == 0); +} +END_TEST + +START_TEST(test_all_users_waiting_to_send) +{ + in_addr_t ip; + + ip = inet_addr("127.0.0.1"); + init_users(ip); + + fail_unless(all_users_waiting_to_send() == 1); + + users[0].active = 1; + + fail_unless(all_users_waiting_to_send() == 1); + + users[0].last_pkt = time(NULL); + + fail_unless(all_users_waiting_to_send() == 0); + + users[0].outpacket.len = 44; + + fail_unless(all_users_waiting_to_send() == 1); +} +END_TEST + +START_TEST(test_find_available_user) +{ + in_addr_t ip; + int i; + + ip = inet_addr("127.0.0.1"); + init_users(ip); + + for (i = 0; i < USERS; i++) { + fail_unless(find_available_user() == i); + } + + for (i = 0; i < USERS; i++) { + fail_unless(find_available_user() == -1); + } + + users[3].active = 0; + + fail_unless(find_available_user() == 3); + + users[3].last_pkt = 55; + + fail_unless(find_available_user() == 3); +} +END_TEST + +TCase * +test_user_create_tests() +{ + TCase *tc; + + 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); + + return tc; +}