src/test.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 21:23:33 +0200
changeset 42 13cfc41f76a7
parent 41 40f7aa051acb
child 43 42f5dc680930
permissions -rw-r--r--
test async operation
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
 * The main test code entry point
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include "sock_test.h"
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     5
#include "line_proto.h"
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include "irc_conn.h"
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include "log.h"
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     8
#include "error.h"
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    10
#include <stdlib.h>
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
#include <string.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
#include <assert.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    14
void assert_strcmp (const char *is, const char *should_be)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    15
{
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    16
    if (strcmp(is, should_be))
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    17
        FATAL("'%s' != '%s'", is, should_be);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    18
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    19
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    20
void assert_strncmp (const char *is, const char *should_be, size_t n)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    21
{
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    22
    if (strncmp(is, should_be, n))
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    23
        FATAL("'%s':%d != '%s'", is, n, should_be);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    24
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    25
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    26
void assert_strlen (const char *str, size_t n)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    27
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    28
    if (strlen(str) != n)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    29
        FATAL("strlen('%s') != %u", str, n);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    30
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    31
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    32
void assert_strnull (const char *str)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    33
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    34
    if (str != NULL)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    35
        FATAL("'%s' != NULL", str);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    36
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    37
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    38
void assert_success (err_t err)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    39
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    40
    if (err != SUCCESS)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    41
        FATAL("error: %s", error_name(err));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    42
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    43
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    44
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    45
void assert_err (err_t err, err_t target)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    46
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    47
    if (err != target)
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    48
        FATAL("error: <%s> != <%s>", error_name(err), error_name(target));
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    49
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    50
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    51
void assert_error_info (struct error_info *is, struct error_info *should_be)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    52
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    53
    if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    54
        FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    55
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    56
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
void assert_sock_read (struct sock_stream *sock, const char *str)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    char buf[strlen(str)];
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    log_debug("read: %p: '%s'", sock, str);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    // read it
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    // cmp
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    67
    assert_strncmp(buf, str, strlen(str));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
void assert_sock_write (struct sock_stream *sock, const char *str)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    log_debug("write: %p: '%s'", sock, str);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    // write it
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    78
void assert_sock_eof (struct sock_stream *sock)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    79
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    80
    char buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    81
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    82
    log_debug("eof: %p", sock);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    83
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    84
    assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    86
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
void test_sock_test (void)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    struct sock_test *sock = sock_test_create();
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    struct io_vec _read_data[] = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
        {   "foo",      3   },
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
        {   "barx",     4   }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    };
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    const char *_write_data = "test data";
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    // put the read data
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    log_debug("set_recv_buffer: %p, %d", _read_data, 2);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    98
    sock_test_set_recv_buffer(sock, _read_data, 2, true);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    99
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   100
    // read it out
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   101
    log_info("test sock_test_read");
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    assert_sock_read(SOCK_TEST_BASE(sock), "foo");
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    assert_sock_read(SOCK_TEST_BASE(sock), "ba");
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    assert_sock_read(SOCK_TEST_BASE(sock), "rx");
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   106
    assert_sock_eof(SOCK_TEST_BASE(sock));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    // write the data in
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   109
    log_info("test sock_test_write");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   110
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    assert_sock_write(SOCK_TEST_BASE(sock), "test ");
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    assert_sock_write(SOCK_TEST_BASE(sock), "data");
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
    // get the data out
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    char *data;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    size_t len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   118
    log_info("test get_send_data");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   119
    
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
    sock_test_get_send_data(sock, &data, &len);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
    
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   122
    // should be the same
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   123
    assert_strlen(_write_data, len);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   124
    assert_strncmp(data, _write_data, len);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   126
    // cleanup
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   127
    free(data);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   128
    sock_test_destroy(sock);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   129
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   130
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   131
void assert_read_line (struct line_proto *lp, const char *line_str)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   132
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   133
    char *line_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   134
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   135
    log_debug("expect: '%s'", line_str);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   136
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   137
    assert_success(line_proto_recv(lp, &line_buf));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   138
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   139
    if (line_str) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   140
        assert(line_buf != NULL);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   141
        assert_strcmp(line_buf, line_str);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   142
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   143
    } else {
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   144
        assert_strnull(line_buf);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   145
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   146
    }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   147
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   148
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   149
/**
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   150
 * Context info for test_line_proto callbacks
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   151
 */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   152
struct _lp_test_ctx {
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   153
    /** Expected line */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   154
    const char *line;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   155
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   156
    /** Expected error */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   157
    struct error_info err;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   158
};
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   159
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   160
static void _lp_on_line (char *line, void *arg)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   161
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   162
    struct _lp_test_ctx *ctx = arg;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   163
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   164
    log_debug("'%s'", line);
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   165
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   166
    assert_strcmp(line, ctx->line);
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   167
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   168
    ctx->line = NULL;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   169
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   170
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   171
static void _lp_on_error (struct error_info *err, void *arg)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   172
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   173
    struct _lp_test_ctx *ctx = arg;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   174
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   175
    assert_error_info(err, &ctx->err);
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   176
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   177
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   178
static struct line_proto_callbacks _lp_callbacks = {
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   179
    .on_line        = &_lp_on_line,
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   180
    .on_error       = &_lp_on_error,
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   181
};
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   182
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   183
void test_line_proto (void)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   184
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   185
    struct sock_test *sock = sock_test_create();
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   186
    struct io_vec _read_data[] = {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   187
        {   "hello\r\n",    7   },
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   188
        {   "world\n",      6   },
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   189
        {   "this ",        5   },
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   190
        {   "is a line\r",  10  },
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   191
        {   "\nfragment",   9   },
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   192
    }, _trailing_data = {   "\r\n",     2 };
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   193
    struct line_proto *lp;
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   194
    struct _lp_test_ctx ctx;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   195
    struct error_info err;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   196
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   197
    // put the read data
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   198
    log_debug("set_recv_buffer: %p, %d", _read_data, 5);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   199
    sock_test_set_recv_buffer(sock, _read_data, 5, false);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   200
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   201
    // create the lp
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   202
    assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   203
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   204
    log_info("test line_proto_recv");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   205
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   206
    // then read some lines from it
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   207
    assert_read_line(lp, "hello");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   208
    assert_read_line(lp, "world");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   209
    assert_read_line(lp, "this is a line");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   210
    assert_read_line(lp, NULL);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   211
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   212
    // then add a final bit to trigger on_line
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   213
    log_info("test on_line");
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   214
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   215
    ctx.line = "fragment";
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   216
    sock_test_add_recv_vec(sock, _trailing_data);
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   217
    assert_strnull(ctx.line);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   218
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   219
    // cleanup
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   220
    line_proto_release(lp);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   221
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   222
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   223
static struct irc_conn_callbacks _conn_callbacks = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
    .on_registered      = NULL,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   225
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   226
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   227
void test_irc_conn (void)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
    struct sock_test *sock;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
    struct irc_conn *conn;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
    struct error_info err;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
    // create the test socket
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
    assert((sock = sock_test_create()));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   236
    // create the irc_conn
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   237
    assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
    // destroy it
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
    irc_conn_destroy(conn);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   242
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   243
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   244
 * Test definition
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   246
static struct test {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    /** Test name */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   248
    const char *name;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   249
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   250
    /** Test func */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   251
    void (*func) (void);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   252
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   253
} _tests[] = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
    {   "sock_test",    &test_sock_test     },
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   255
    {   "line_proto",   &test_line_proto    },
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
    {   "irc_conn",     &test_irc_conn      },
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
    {   NULL,           NULL                }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   259
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
int main (int argc, char **argv)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
    struct test *test;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
    (void) argv;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   265
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   266
    // no arguments
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
    assert(argc == 1);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   268
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   269
    // run tests
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
    for (test = _tests; test->name; test++) {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   271
        log_info("Running test: %s", test->name);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   272
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   273
        test->func();
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   274
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   275
}