This commit is contained in:
niamtokik
2020-06-18 06:06:06 +00:00
parent cf89bbd699
commit 54ad83d1ee
7 changed files with 252 additions and 100 deletions

View File

@@ -3,19 +3,22 @@
######################################################################
ERLANG_VERSION ?= 21
ERL_INTERFACE_VERSION ?= 3.10.4
INCLUDES += -I/usr/local/lib/erlang$(ERLANG_VERSION)/usr/include
ERLANG_PATH = /usr/local/lib/erlang$(ERLANG_VERSION)
INCLUDES += -I$(ERLANG_PATH)/lib/erl_interface-$(ERL_INTERFACE_VERSION)/include
INCLUDES += -I/usr/local/include
INCLUDES += -I/usr/include
INCLUDE_LIBS += -L/usr/local/lib/erlang21/usr/lib
INCLUDE_LIBS += -L$(ERLANG_PATH)/lib/erl_interface-$(ERL_INTERFACE_VERSION)/lib
INCLUDE_LIBS += -L/usr/local/lib/
INCLUDE_LIBS += -L/usr/lib
LIBS += -luv -lei -lerl_interface -lerts
LIBS += -luv -lei -lerl_interface
CFLAGS += $(INCLUDES) $(INCLUDE_LIBS) $(LIBS)
LDFLAGS += $(LIBS)
CFLAGS += -g $(INCLUDES) $(INCLUDE_LIBS) $(LIBS)
LDFLAGS += $(INCLUDES) $(INCLUDE_LIBS) $(LIBS)
all: erluv erluv_test

View File

@@ -1,4 +1,5 @@
/*
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
*
* http://docs.libuv.org/en/v1.x
* https://erlang.org/doc/apps/erl_interface/ei_users_guide.html
*/
@@ -6,85 +7,19 @@
#include <stdlib.h>
#include <time.h>
#include <ei.h>
#include <erl_interface.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <uv.h>
#include <uv/unix.h>
#include <uv/bsd.h>
#include "erluv.h"
#define COOKIE_SIZE 256
#define NODE_NAME_SIZE 256
/* interfaces */
int init_cookie(void);
int init_node_name(void);
int init_id(void);
int init_creation(void);
void init(void);
/* global variables */
short creation = 0;
int id = 0;
char cookie[COOKIE_SIZE];
char node_name[NODE_NAME_SIZE];
/* set the node cookie from environment */
int
init_cookie() {
if (getenv("COOKIE") == NULL) {
return 1;
}
strncpy(cookie, getenv("COOKIE"), COOKIE_SIZE);
return 0;
}
/* set the node name from environment
* todo: ensure the node name is valid
*/
int
init_node_name() {
if (getenv("NODE_NAME") == NULL) {
return 1;
}
strncpy(node_name, getenv("NODE_NAME"), NODE_NAME_SIZE);
return 0;
}
/* set the node id from environment
*/
int
init_id() {
const char **errstr;
if (getenv("ID") == NULL) {
return 1;
}
id = (int)strtonum(getenv("ID"), 0, 0xff, errstr);
return 0;
}
int
init_creation() {
creation = time(NULL)+1;
return 0;
}
/* sanitize global variables and configure them with
* environment variable
*/
void
init() {
bzero(&creation, sizeof(short));
bzero(&id, sizeof(int));
bzero(&cookie, sizeof(char)*COOKIE_SIZE);
bzero(&node_name, sizeof(char)*NODE_NAME_SIZE);
init_cookie();
init_node_name();
init_id();
init_creation();
}
extern short creation;
extern int id;
extern char cookie[];
extern char node_name[];
/* main function */
int
@@ -92,8 +27,5 @@ main(int argc, char *argv[]) {
(void)init();
ei_cnode ec;
ei_connect_init(&ec, node_name, cookie, creation);
int sockfd;
if ((sockfd = ei_connect(&ec, node_name)) < 0)
fprintf(stderr, "ERROR: ei_connect failed");
}

View File

@@ -0,0 +1,12 @@
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
*
*/
#define COOKIE_SIZE 255
#define NODE_NAME_SIZE 255
/* interfaces */
int init_cookie(void);
int init_node_name(void);
int init_id(void);
int init_creation(void);
void init(void);

View File

@@ -1,29 +1,137 @@
/* 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_foo() {
mu_assert("error, foo != 7", foo == 7);
return 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_foo);
return 0;
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;
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;
}

View File

@@ -1 +1,95 @@
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ei.h>
#include <erl_interface.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <uv.h>
#include <uv/unix.h>
#include <uv/bsd.h>
#include "erluv.h"
/* global variables */
short creation = 0;
int id = 1;
char cookie[COOKIE_SIZE];
char node_name[NODE_NAME_SIZE];
/* set the node cookie from environment */
int
init_cookie() {
if (getenv("COOKIE") == NULL)
return 1;
/* the last char must be set to 0. */
strncpy(cookie, getenv("COOKIE"), COOKIE_SIZE-1);
return 0;
}
/* set the node name from environment
* todo: ensure the node name is valid
*/
int
init_node_name() {
if (getenv("NODE_NAME") == NULL) {
return 1;
}
fprintf(stdout, "set node_name ");
/* the last char must be set to 0. */
strncpy(node_name, getenv("NODE_NAME"), NODE_NAME_SIZE-1);
fprintf(stdout, "%s\n", node_name);
return 0;
}
/* set the node id from environment
*/
int
init_id() {
int tmpid = 1;
const char *errstr;
if (getenv("ID") == NULL) {
return 1;
}
tmpid = (int)strtonum(getenv("ID"), 1, 0xffff, &errstr);
if (errstr != NULL) {
fprintf(stderr, "id is %s\n", errstr);
return 1;
}
id = tmpid;
return 0;
}
/* set the creation time
*/
int
init_creation() {
fprintf(stdout, "init creation...\n");
creation = time(NULL)+1;
return 0;
}
/* sanitize global variables and configure them with
* environment variable
*/
void
init() {
bzero(&creation, sizeof(short));
bzero(&id, sizeof(int));
bzero(&cookie, sizeof(char)*COOKIE_SIZE);
bzero(&node_name, sizeof(char)*NODE_NAME_SIZE);
init_cookie();
init_node_name();
init_id();
init_creation();
}

View File

@@ -1,4 +1,6 @@
/* file: minunit.h
/* copyright (c) 2020 Mathieu Kerjouan <contact@steepath.eu>
*
* file: minunit.h
* from http://www.jera.com/techinfo/jtns/jtn002.html
*/
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)