src/test/transport.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 196 873796250c60
permissions -rw-r--r--
nexus.c compiles
#include "transport.h"
#include "test.h"

void assert_transport_read (transport_t *transport, const char *str)
{
    size_t len = strlen(str);
    char buf[len];
    error_t err;

    log_debug("read: %p: %s", transport, dump_str(str));
    
    // read it
    assert(transport_read(transport, buf, len, &err) == (int) len);

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

void assert_transport_write (transport_t *transport, const char *str)
{
    size_t len = strlen(str);
    error_t err;

    log_debug("write: %p: %s", transport, dump_str(str));

    // write it
    assert(transport_write(transport, str, len, &err) == (int) len);
}

void assert_transport_eof (transport_t *transport)
{
    char buf;
    error_t err;

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

    assert_err(-transport_read(transport, &buf, 1, &err), ERR_EOF);
}

void assert_transport_data_buf (struct transport_test *tp, const char *data, size_t len)
{
    // get the data out
    char *out;
    size_t out_len;
    
    transport_test_pull_buf(tp, &out, &out_len);
    
    log_debug("pull_buf: %s", dump_strn(out, out_len));
    
    // should be the same
    assert_memcmp(out, data, out_len);
    assert(out_len == len);

    // cleanup
    free(out);
}

void assert_transport_data (struct transport_test *tp, const char *fmt, ...)
{
    char buf[TRANSPORT_TEST_FMT_MAX];
    va_list vargs;
    size_t len;
    
    va_start(vargs, fmt);

    if ((len = vsnprintf(buf, sizeof(buf), fmt, vargs)) >= sizeof(buf))
        FATAL("input too long: %zu bytes", len);

    va_end(vargs);
    
    assert_transport_data_buf(tp, buf, len);
}

struct transport_test* setup_transport_test (void)
{
    struct transport_test *tp;
   
    assert ((tp = transport_test_create(NULL)) != NULL);

    return tp;
}

void test_transport_test (void)
{
    struct transport_info info = { NULL, NULL, 0 };
    struct transport_test *tp = transport_test_create(&info);
    transport_t *transport = transport_test_cast(tp);

    // put the read data
    log_info("test transport_test_push_*");
    transport_test_push_buf(tp, "foo", 3);
    transport_test_push_str(tp, "barx");
    transport_test_push_fmt(tp, "xx %s xx", "quux");
    transport_test_push_eof(tp);
    
    // read it out
    log_info("test transport_test_read");

    assert_transport_read(transport, "foo");
    assert_transport_read(transport, "ba");
    assert_transport_read(transport, "rx");
    assert_transport_read(transport, "xx quux xx");
    assert_transport_eof(transport);

    // write some data in
    log_info("test transport_test_write");

    assert_transport_write(transport, "test ");
    assert_transport_write(transport, "data");
    
    // check output
    log_info("test transport_test_pull_*");

    assert_transport_data(tp, "test data");
    assert_transport_data(tp, "");

    // cleanup
    transport_destroy(transport);
}