src/test.c
author Tero Marttila <terom@fixme.fi>
Fri, 13 Mar 2009 14:59:38 +0200
changeset 50 46c3983638d3
parent 49 96e0f703a58c
child 51 cc61eaa841ef
permissions -rw-r--r--
add dump_str function to test
/**
 * The main test code entry point
 */
#include "sock_test.h"
#include "line_proto.h"
#include "irc_conn.h"
#include "irc_net.h"
#include "log.h"
#include "error.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#define DUMP_STR_BUF 1024
#define DUMP_STR_COUNT 8
#define DUMP_STR_TAIL 10

char *dump_str_append (char *buf, const char *str)
{
    while (*str)
        *buf++ = *str++;

    return buf;
}

/**
 * This re-formats the given string to escape values, and returns a pointer to an internal static buffer.
 *
 * If len is given as >= 0, only the given number of chars will be dumped from str
 *
 * The buffer cycles a bit, so you pointers remain valid across DUMP_STR_COUNT calls.
 */
const char *dump_strn (const char *str, ssize_t len)
{
    static char dump_buf[DUMP_STR_COUNT][DUMP_STR_BUF];
    static size_t dump_idx = 0;
    
    // pick a buffer to use
    const char *str_ptr = str;
    char *buf_ptr = dump_buf[dump_idx++], *buf = buf_ptr;
    
    // cycle
    if (dump_idx >= DUMP_STR_COUNT)
        dump_idx = 0;

    // NULL?
    if (str == NULL) {
        buf = dump_str_append(buf, "NULL");
        *buf = '\0';

        return buf_ptr;
    }

    // quote
    *buf++ = '\'';
    
    // dump each char
    for (; *str && (size_t) (buf - buf_ptr) < DUMP_STR_BUF - DUMP_STR_TAIL && (len < 0 || (size_t) (str - str_ptr) < (size_t) len); str++) {
        if (*str == '\'') {
            buf = dump_str_append(buf, "\\'"); break;

        } else if (isprint(*str)) {
            *buf++ = *str;

        } else {
            switch (*str) {
                case '\r':
                    buf = dump_str_append(buf, "\\r"); break;

                case '\n':
                    buf = dump_str_append(buf, "\\n"); break;

                default:
                    buf += snprintf(buf, (DUMP_STR_BUF - (buf - buf_ptr)), "\\x%02x", *str);
                    break;
            }
        }
    }

    // quote
    *buf++ = '\'';

    // overflow?
    if ((size_t)(buf - buf_ptr) == DUMP_STR_BUF - DUMP_STR_TAIL)
        buf = dump_str_append(buf, "...");

    // terminate
    *buf = '\0';

    // ok
    return buf_ptr;
}

const char *dump_str (const char *str) 
{
    return dump_strn(str, -1);
}

void assert_strcmp (const char *is, const char *should_be)
{
    if (strcmp(is, should_be))
        FATAL("%s != %s", dump_str(is), dump_str(should_be));
}

void assert_strncmp (const char *is, const char *should_be, size_t n)
{   
    if (strncmp(is, should_be, n))
        FATAL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
}

void assert_strlen (const char *str, size_t n)
{
    if (strlen(str) != n)
        FATAL("strlen(%s) != %u", dump_str(str), (unsigned) n);
}

void assert_strnull (const char *str)
{
    if (str != NULL)
        FATAL("%s != NULL", dump_str(str));
}

void assert_success (err_t err)
{
    if (err != SUCCESS)
        FATAL("error: %s", error_name(err));

}

void assert_err (err_t err, err_t target)
{
    if (err != target)
        FATAL("error: <%s> != <%s>", error_name(err), error_name(target));
}

void assert_error_info (struct error_info *is, struct error_info *should_be)
{
    if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
        FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
}

void assert_sock_read (struct sock_stream *sock, const char *str)
{
    char buf[strlen(str)];

    log_debug("read: %p: %s", sock, dump_str(str));
    
    // read it
    assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str));

    // cmp
    assert_strncmp(buf, str, strlen(str));
}

void assert_sock_write (struct sock_stream *sock, const char *str)
{
    log_debug("write: %p: %s", sock, dump_str(str));

    // write it
    assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str));
}

void assert_sock_eof (struct sock_stream *sock)
{
    char buf;

    log_debug("eof: %p", sock);

    assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF);
}

void assert_sock_data (struct sock_test *sock, const char *data)
{
    // get the data out
    char *buf;
    size_t len;
    
    sock_test_get_send_data(sock, &buf, &len);
    
    log_debug("get_send_data: %s", dump_strn(buf, len));
    
    // should be the same
    assert_strncmp(buf, data, len);
    assert_strlen(data, len);

    // cleanup
    free(buf);
}

void test_dump_str (void)
{
    log_info("dumping example strings on stdout:");

    log_debug("normal: %s", dump_str("Hello World"));
    log_debug("escapes: %s", dump_str("foo\r\nbar\a\001"));
    log_debug("length: %s", dump_strn("<-->**", 4));
    log_debug("overflow: %s", dump_str( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
    log_debug("null: %s", dump_str(NULL));
}

void test_sock_test (void)
{
    struct sock_test *sock = sock_test_create();
    struct io_vec _read_data[] = {
        {   "foo",      3   },
        {   "barx",     4   }
    };
    const char *_write_data = "test data";
    
    // put the read data
    log_debug("set_recv_buffer: %p, %d", _read_data, 2);
    sock_test_set_recv_buffer(sock, _read_data, 2, true);
    
    // read it out
    log_info("test sock_test_read");

    assert_sock_read(SOCK_TEST_BASE(sock), "foo");
    assert_sock_read(SOCK_TEST_BASE(sock), "ba");
    assert_sock_read(SOCK_TEST_BASE(sock), "rx");
    assert_sock_eof(SOCK_TEST_BASE(sock));

    // write the data in
    log_info("test sock_test_write");

    assert_sock_write(SOCK_TEST_BASE(sock), "test ");
    assert_sock_write(SOCK_TEST_BASE(sock), "data");
    
    // check output
    assert_sock_data(sock, _write_data);

    // check output
    assert_sock_data(sock, "");

    // cleanup
    sock_test_destroy(sock);
}

void assert_read_line (struct line_proto *lp, const char *line_str)
{
    char *line_buf;
    
    log_debug("expect: %s", dump_str(line_str));

    assert_success(line_proto_recv(lp, &line_buf));

    if (line_str) {
        assert(line_buf != NULL);
        assert_strcmp(line_buf, line_str);

    } else {
        assert_strnull(line_buf);

    }
}

/**
 * Context info for test_line_proto callbacks
 */
struct _lp_test_ctx {
    /** Expected line */
    const char *line;

    /** Expected error */
    struct error_info err;
};

static void _lp_on_line (char *line, void *arg)
{
    struct _lp_test_ctx *ctx = arg;

    log_debug("%s", dump_str(line));

    assert_strcmp(line, ctx->line);

    ctx->line = NULL;
}

static void _lp_on_error (struct error_info *err, void *arg)
{
    struct _lp_test_ctx *ctx = arg;

    assert_error_info(err, &ctx->err);
}

static struct line_proto_callbacks _lp_callbacks = {
    .on_line        = &_lp_on_line,
    .on_error       = &_lp_on_error,
};

void test_line_proto (void)
{
    struct sock_test *sock = sock_test_create();
    struct io_vec _read_data[] = {
        {   "hello\r\n",    7   },
        {   "world\n",      6   },
        {   "this ",        5   },
        {   "is a line\r",  10  },
        {   "\nfragment",   9   },
    }, _trailing_data = {   "\r\n",     2 };
    struct line_proto *lp;
    struct _lp_test_ctx ctx;
    struct error_info err;
    
    // put the read data
    log_debug("set_recv_buffer: %p, %d", _read_data, 5);
    sock_test_set_recv_buffer(sock, _read_data, 5, false);
    
    // create the lp
    assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err));
    
    log_info("test line_proto_recv");

    // then read some lines from it
    assert_read_line(lp, "hello");
    assert_read_line(lp, "world");
    assert_read_line(lp, "this is a line");
    assert_read_line(lp, NULL);

    // then add a final bit to trigger on_line
    log_info("test on_line");

    ctx.line = "fragment";
    sock_test_add_recv_vec(sock, _trailing_data);
    assert_strnull(ctx.line);

    // test writing
    log_info("test line_proto_send");
    assert_success(-line_proto_send(lp, "foobar\r\n"));
    assert_success(-line_proto_send(lp, "quux\r\n"));
    assert_sock_data(sock, "foobar\r\nquux\r\n");

    // XXX: test partial writes

    // cleanup
    line_proto_release(lp);
}

struct _test_irc_conn_ctx {
    bool on_registered, on_TEST, on_error, on_quit;
};

static void _conn_on_registered (struct irc_conn *conn, void *arg)
{
    struct _test_irc_conn_ctx *ctx = arg;

    (void) conn;

    if (ctx) ctx->on_registered = true;

    log_debug("registered");
}

static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg)
{
    struct _test_irc_conn_ctx *ctx = arg;
    
    (void) conn;
    (void) err;

    if (ctx) ctx->on_error = true;

    log_debug("on_error");
}

static void _conn_on_quit (struct irc_conn *conn, void *arg)
{
    struct _test_irc_conn_ctx *ctx = arg;

    (void) conn;

    if (ctx) ctx->on_quit = true;

    log_debug("on_quit");
}

static void _conn_on_TEST (const struct irc_line *line, void *arg)
{
    struct _test_irc_conn_ctx *ctx = arg;

    assert_strcmp(line->prefix, "foobar-prefix");
    assert_strcmp(line->command, "TEST");
    assert_strcmp(line->args[0], "arg0");
    assert_strnull(line->args[1]);

    if (ctx) ctx->on_TEST = true;

    log_debug("on_TEST");
}

static struct irc_conn_callbacks _conn_callbacks = {
    .on_registered      = &_conn_on_registered,
    .on_error           = &_conn_on_error,
    .on_quit            = &_conn_on_quit,
};

static struct irc_cmd_handler _conn_handlers[] = {
    {   "TEST",         &_conn_on_TEST  },
    {   NULL,           NULL            }
};

struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct _test_irc_conn_ctx *ctx)
{
    struct irc_conn *conn;
    struct error_info err;
    struct irc_conn_register_info register_info = {
        "nick", "user", "realname"
    };

    // create the irc_conn
    assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err));

    // test register
    if (noisy) log_info("test irc_conn_register");
    assert_success(irc_conn_register(conn, &register_info));
    assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n");
 
    // test on_register callback    
    if (noisy) log_info("test irc_conn_callbacks.on_register");
    sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
    if (ctx) assert(ctx->on_registered);
    assert_strcmp(conn->nickname, "mynick");
   
    // ok
    return conn;
}

void test_irc_conn (void)
{
    struct sock_test *sock;
    struct irc_conn *conn;
    struct _test_irc_conn_ctx ctx = { false, false, false, false };

    // create the test socket
    assert((sock = sock_test_create()));

    // setup the basic irc_conn
    conn = setup_irc_conn(sock, true, &ctx);
    
    // add our test handlers
    assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));

    // test on_TEST handler
    log_info("test irc_conn.handlers");
    sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n");
    assert(ctx.on_TEST);

    // test PING/PONG
    log_info("test PING/PONG");
    sock_test_add_recv_str(sock, "PING foo\r\n");
    assert_sock_data(sock, "PONG foo\r\n");

    // quit nicely
    log_info("test QUIT");
    assert_success(irc_conn_QUIT(conn, "bye now"));
    assert_sock_data(sock, "QUIT :bye now\r\n");
    assert(conn->quitting);

    sock_test_add_recv_str(sock, "ERROR :Closing Link: Quit\r\n");
    sock_test_set_recv_eof(sock);
    assert(conn->quit && !conn->quitting && !conn->registered);
    assert(ctx.on_quit);
    assert(!ctx.on_error);

    // destroy it
    irc_conn_destroy(conn);
}

struct _test_net_ctx {
    struct irc_chan *chan;

    bool on_chan_self_join;
};

void _on_chan_self_join (struct irc_chan *chan, void *arg)
{
    struct _test_net_ctx *ctx = arg;

    assert(chan == ctx->chan);
    
    ctx->on_chan_self_join = true;

    log_debug("on_self_join");
}

struct irc_chan_callbacks _chan_callbacks = {
    .on_self_join       = &_on_chan_self_join,
};

void test_irc_net (void)
{
    struct sock_test *sock;
    struct irc_net *net;
    struct irc_net_info net_info = {
        .register_info = {
            "nick", "user", "realname"
        },
    };
    struct irc_chan *chan;
    struct irc_chan_info chan_info = {
        .channel        = "#test",
    };
    struct error_info err;
    struct _test_net_ctx ctx = { NULL, false };

    // create the test socket
    assert((sock = sock_test_create()));
    
    // create the irc_net
    net_info.raw_sock = SOCK_TEST_BASE(sock);
    assert_success(irc_net_create(&net, &net_info, &err));

    // add a channel
    log_info("test offline irc_net_add_chan");
    assert((chan = irc_net_add_chan(net, &chan_info)));
    assert(!chan->joining && !chan->joined);
    assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, &ctx));
    ctx.chan = chan;
    
    // test register output
    assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n");
    
    // registration reply
    log_info("test irc_conn_on_RPL_WELCOME");
    sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
    assert(net->conn->registered);
    assert_strcmp(net->conn->nickname, "mynick");
    
    // JOIN request
    log_info("test irc_net_conn_registered -> irc_chan_join");
    assert(chan->joining);
    assert_sock_data(sock, "JOIN #test\r\n");

    // JOIN reply
    log_info("test irc_chan_on_JOIN");
    sock_test_add_recv_str(sock, ":mynick!user@host JOIN #test\r\n");
    assert(!chan->joining && chan->joined);
    assert(ctx.on_chan_self_join);

    // test errors by setting EOF
    log_info("test irc_net_error");
    sock_test_set_recv_eof(sock);
    assert(net->conn == NULL);

    // cleanup
    irc_net_destroy(net);
}

/**
 * Test definition
 */
static struct test {
    /** Test name */
    const char *name;

    /** Test func */
    void (*func) (void);

} _tests[] = {
    {   "dump_str",     &test_dump_str      },
    {   "sock_test",    &test_sock_test     },
    {   "line_proto",   &test_line_proto    },
    {   "irc_conn",     &test_irc_conn      },
    {   "irc_net",      &test_irc_net       },
    {   NULL,           NULL                }
};

int main (int argc, char **argv)
{
    struct test *test;

    (void) argv;
    
    // no arguments
    assert(argc == 1);

    // run tests
    for (test = _tests; test->name; test++) {
        log_info("Running test: %s", test->name);

        test->func();
    }

    log_info("done");
}