138 lines
2.7 KiB
C
138 lines
2.7 KiB
C
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
|
|
*
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include "minunit.h"
|
|
#include "erluv.h"
|
|
|
|
extern short creation;
|
|
extern int id;
|
|
extern char cookie[];
|
|
extern char node_name[];
|
|
|
|
int tests_run = 0;
|
|
|
|
static char *test_init_cookie() {
|
|
int ret;
|
|
|
|
ret = 0;
|
|
bzero(cookie, sizeof(char)*COOKIE_SIZE);
|
|
unsetenv("COOKIE");
|
|
ret = init_cookie();
|
|
mu_assert("cookie not set", ret == 1);
|
|
|
|
ret = 0;
|
|
bzero(cookie, sizeof(char)*COOKIE_SIZE);
|
|
setenv("COOKIE", "test", 1);
|
|
ret = init_cookie();
|
|
mu_assert("valid cookie", ret == 0);
|
|
|
|
/* find a decent way to test if the cookie
|
|
is too long.
|
|
|
|
ret = 0;
|
|
bzero(cookie, sizeof(char)*COOKIE_SIZE);
|
|
char long_cookie[COOKIE_SIZE+100];
|
|
char *c = "a";
|
|
memcpy(long_cookie, c, COOKIE_SIZE+100);
|
|
setenv("COOKIE", long_cookie, 1);
|
|
mu_assert("cookie too long", ret == 1);
|
|
*/
|
|
|
|
return 0;
|
|
}
|
|
|
|
static char *test_init_node_name() {
|
|
int ret;
|
|
|
|
ret = 0;
|
|
bzero(node_name, sizeof(char)*NODE_NAME_SIZE);
|
|
unsetenv("NODE_NAME");
|
|
ret = init_node_name();
|
|
mu_assert("node_name not set", ret == 1);
|
|
|
|
ret = 0;
|
|
bzero(node_name, sizeof(char)*NODE_NAME_SIZE);
|
|
setenv("NODE_NAME", "test@localhost", 1);
|
|
ret = init_node_name();
|
|
mu_assert("valid cookie", ret == 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static char *test_init_id() {
|
|
int ret;
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
unsetenv("ID");
|
|
ret = init_id();
|
|
mu_assert("default id", id == 1 && ret == 1);
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
setenv("ID", "1", 1);
|
|
ret = init_id();
|
|
mu_assert("valid number (1)", id == 1 && ret == 0);
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
setenv("ID", "255", 1);
|
|
ret = init_id();
|
|
mu_assert("valid number (255)", id == 255 && ret == 0);
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
setenv("ID", "0", 1);
|
|
ret = init_id();
|
|
mu_assert("not a valid number (0)", id == 1 && ret == 1);
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
setenv("ID", "abcd", 1);
|
|
ret = init_id();
|
|
mu_assert("not a number (abcd)", id == 1 && ret == 1);
|
|
|
|
id = 1;
|
|
ret = 0;
|
|
char str[] = "1234123412341234";
|
|
setenv("ID", str, 1);
|
|
ret = init_id();
|
|
mu_assert("too big", id == 1 && ret == 1);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static char *test_init_creation() {
|
|
int ret = 0;
|
|
|
|
ret = init_creation();
|
|
mu_assert("default creation", creation != 0 && ret == 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static char * all_tests() {
|
|
mu_run_test(test_init_cookie);
|
|
mu_run_test(test_init_node_name);
|
|
mu_run_test(test_init_id);
|
|
mu_run_test(test_init_creation);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
char *result = all_tests();
|
|
if (result != 0) {
|
|
printf("%s\n", result);
|
|
}
|
|
else {
|
|
printf("ALL TESTS PASSED\n");
|
|
}
|
|
printf("Tests run: %d\n", tests_run);
|
|
return result != 0;
|
|
}
|
|
|