#include <assert.h>
#include <demi/libos.h>
#include <demi/sga.h>
#include <demi/wait.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
static bool inval_socket(void)
{
int *qd = NULL;
int domain = -1;
int type = -1;
int protocol = -1;
return (demi_socket(qd, domain, type, protocol) != 0);
}
static bool inval_listen(void)
{
int qd = -1;
int backlog = -1;
return (demi_listen(qd, backlog) != 0);
}
static bool inval_bind(void)
{
int qd = -1;
struct sockaddr *saddr = NULL;
socklen_t size = 0;
return (demi_bind(qd, saddr, size) != 0);
}
static bool inval_accept(void)
{
demi_qtoken_t *qt = NULL;
int sockqd = -1;
return (demi_accept(qt, sockqd) != 0);
}
static bool inval_connect(void)
{
demi_qtoken_t *qt = NULL;
int qd = -1;
struct sockaddr *saddr = NULL;
socklen_t size = -1;
return (demi_connect(qt, qd, saddr, size) != 0);
}
static bool inval_close(void)
{
int qd = -1;
return (demi_close(qd) != 0);
}
static bool inval_push(void)
{
demi_qtoken_t *qt = NULL;
int qd = -1;
demi_sgarray_t *sga = NULL;
return (demi_push(qt, qd, sga) != 0);
}
static bool inval_pushto(void)
{
demi_qtoken_t *qt = NULL;
int qd = -1;
demi_sgarray_t *sga = NULL;
struct sockaddr *saddr = NULL;
socklen_t size = -1;
return (demi_pushto(qt, qd, sga, saddr, size) != 0);
}
static bool inval_pop(void)
{
demi_qtoken_t *qt = NULL;
int qd = -1;
return (demi_pop(qt, qd) != 0);
}
static bool inval_setsockopt(void)
{
int qd = -1;
int level = -1;
int optname = -1;
const void *optval = NULL;
socklen_t optlen = 0;
return (demi_setsockopt(qd, level, optname, optval, optlen) != 0);
}
static bool inval_getsockopt(void)
{
int qd = -1;
int level = -1;
int optname = -1;
void *optval = NULL;
socklen_t *optlen = NULL;
return (demi_getsockopt(qd, level, optname, optval, optlen) != 0);
}
static bool inval_getpeername(void)
{
int qd = -1;
return (demi_getpeername(qd, NULL, NULL) != 0);
}
static bool inval_sgaalloc(void)
{
size_t len = 0;
demi_sgarray_t sga = demi_sgaalloc(len);
return (sga.sga_buf == NULL);
}
static bool inval_sgafree(void)
{
demi_sgarray_t *sga = NULL;
return (demi_sgafree(sga) != 0);
}
static bool inval_wait(void)
{
demi_qresult_t *qr = NULL;
demi_qtoken_t qt = -1;
struct timespec *timeout = NULL;
return (demi_wait(qr, qt, timeout) != 0);
}
static bool inval_wait_any(void)
{
demi_qresult_t *qr = NULL;
int *ready_offset = NULL;
demi_qtoken_t *qts = NULL;
int num_qts = -1;
struct timespec *timeout = NULL;
return (demi_wait_any(qr, ready_offset, qts, num_qts, timeout) != 0);
}
#pragma GCC diagnostic pop
struct test
{
bool (*fn)(void);
const char *name;
};
static struct test tests_libos[] = {{inval_socket, "invalid demi_socket()"}, {inval_accept, "invalid demi_accept()"},
{inval_bind, "invalid demi_bind()"}, {inval_close, "invalid_demi_close()"},
{inval_connect, "invalid demi_connect()"}, {inval_listen, "invalid demi_listen()"},
{inval_pop, "invalid demi_pop()"}, {inval_push, "invalid demi_push()"},
{inval_pushto, "invalid demi_pushto()"}, {inval_getpeername, "invalid demi_getpeername()"},
{inval_setsockopt, "invalid demi_setsockopt()"}, {inval_getsockopt, "invalid demi_getsockopt()}"}};
static struct test tests_sga[] = {{inval_sgaalloc, "invalid demi_sgaalloc()"},
{inval_sgafree, "invalid demi_sgafree()"}};
static struct test tests_wait[] = {{inval_wait, "invalid demi_wait()"}, {inval_wait_any, "invalid demi_wait_any()"}};
int main(int argc, char *const argv[])
{
((void)argc);
((void)argv);
const struct demi_args args = {
.argc = argc,
.argv = argv,
.callback = NULL,
};
assert(demi_init(&args) == 0);
for (size_t i = 0; i < sizeof(tests_libos) / sizeof(struct test); i++)
{
if (tests_libos[i].fn() == true)
fprintf(stderr, "test result: passed %s\n", tests_libos[i].name);
else
{
fprintf(stderr, "test result: FAILED %s\n", tests_libos[i].name);
return (EXIT_FAILURE);
}
}
for (size_t i = 0; i < sizeof(tests_sga) / sizeof(struct test); i++)
{
if (tests_sga[i].fn() == true)
fprintf(stderr, "test result: passed %s\n", tests_sga[i].name);
else
{
fprintf(stderr, "test result: FAILED %s\n", tests_sga[i].name);
return (EXIT_FAILURE);
}
}
for (size_t i = 0; i < sizeof(tests_wait) / sizeof(struct test); i++)
{
if (tests_wait[i].fn() == true)
fprintf(stderr, "test result: passed %s\n", tests_wait[i].name);
else
{
fprintf(stderr, "test result: FAILED %s\n", tests_wait[i].name);
return (EXIT_FAILURE);
}
}
return (EXIT_SUCCESS);
}