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
168
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "transport.h"
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include "test.h"
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
void assert_transport_read (transport_t *transport, const char *str)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
    size_t len = strlen(str);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
    char buf[len];
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
    error_t err;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    log_debug("read: %p: %s", transport, dump_str(str));
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    // read it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    assert(transport_read(transport, buf, len, &err) == (int) len);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    // cmp
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    assert_strncmp(buf, str, len);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
void assert_transport_write (transport_t *transport, const char *str)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    size_t len = strlen(str);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    error_t err;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    log_debug("write: %p: %s", transport, dump_str(str));
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    // write it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    assert(transport_write(transport, str, len, &err) == (int) len);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
void assert_transport_eof (transport_t *transport)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    char buf;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    error_t err;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    log_debug("eof: %p", transport);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    assert_err(-transport_read(transport, &buf, 1, &err), ERR_EOF);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
196
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    40
void assert_transport_data_buf (struct transport_test *tp, const char *data, size_t len)
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    41
{
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    42
    // get the data out
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    43
    char *out;
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    44
    size_t out_len;
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    45
    
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    46
    transport_test_pull_buf(tp, &out, &out_len);
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    47
    
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    48
    log_debug("pull_buf: %s", dump_strn(out, out_len));
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    49
    
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    50
    // should be the same
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    51
    assert_memcmp(out, data, out_len);
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    52
    assert(out_len == len);
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    53
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    54
    // cleanup
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    55
    free(out);
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    56
}
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    57
168
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
void assert_transport_data (struct transport_test *tp, const char *fmt, ...)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
    char buf[TRANSPORT_TEST_FMT_MAX];
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    va_list vargs;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    size_t len;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    va_start(vargs, fmt);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    if ((len = vsnprintf(buf, sizeof(buf), fmt, vargs)) >= sizeof(buf))
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        FATAL("input too long: %zu bytes", len);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    va_end(vargs);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    
196
873796250c60 implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents: 171
diff changeset
    71
    assert_transport_data_buf(tp, buf, len);
168
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
struct transport_test* setup_transport_test (void)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    struct transport_test *tp;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
   
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    assert ((tp = transport_test_create(NULL)) != NULL);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    return tp;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
void test_transport_test (void)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    struct transport_info info = { NULL, NULL, 0 };
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    struct transport_test *tp = transport_test_create(&info);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    transport_t *transport = transport_test_cast(tp);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    // put the read data
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    log_info("test transport_test_push_*");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    transport_test_push_buf(tp, "foo", 3);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    transport_test_push_str(tp, "barx");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    transport_test_push_fmt(tp, "xx %s xx", "quux");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    transport_test_push_eof(tp);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    // read it out
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    log_info("test transport_test_read");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    assert_transport_read(transport, "foo");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
    assert_transport_read(transport, "ba");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
    assert_transport_read(transport, "rx");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    assert_transport_read(transport, "xx quux xx");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    assert_transport_eof(transport);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    // write some data in
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    log_info("test transport_test_write");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    assert_transport_write(transport, "test ");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    assert_transport_write(transport, "data");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    // check output
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    log_info("test transport_test_pull_*");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    assert_transport_data(tp, "test data");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    assert_transport_data(tp, "");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    // cleanup
171
b54f393c3df0 evil chain.h macro magic, fix irc_conn_set_nickname bug, misc. test bugs (mem leaks, missing #includes)
Tero Marttila <terom@fixme.fi>
parents: 168
diff changeset
   118
    transport_destroy(transport);
168
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120