src/sock_test.c
author Tero Marttila <terom@fixme.fi>
Sat, 11 Apr 2009 06:03:24 +0300
changeset 130 ffefb6d85ea6
parent 118 05b8d5150313
child 139 55b9dcc2b73a
permissions -rw-r--r--
implement logwatch_filter::format using str_format
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "sock_test.h"
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <stdlib.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <string.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <assert.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     7
/**
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     8
 * Grow buf->vecs as needed, to ensure that buf->write_vec is valid
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     9
 */
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    10
static err_t sock_test_grow_buf (struct io_buf *buf)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    11
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    12
    size_t read_vec_offset = buf->read_vec ? (buf->read_vec - buf->vecs) : 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    13
    size_t write_vec_offset = buf->write_vec ? (buf->write_vec - buf->vecs) : 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    14
    struct io_vec *v;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    15
    struct io_vec *vecs_tmp = buf->vecs;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    16
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    17
    // don't grow if not full
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    18
    if (buf->vecs && buf->write_vec < buf->vecs + buf->count)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    19
        return SUCCESS;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    20
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    21
    // new size
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    22
    buf->count = buf->count * 2 + 1;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    23
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    24
    // grow
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    25
    if ((buf->vecs = realloc(buf->vecs, buf->count * sizeof(struct io_vec))) == NULL) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    26
        // restore old value
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    27
        buf->vecs = vecs_tmp;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    28
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    29
        return ERR_CALLOC;
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
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    32
    // set vec
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    33
    buf->write_vec = buf->vecs + write_vec_offset;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    34
    buf->read_vec = buf->vecs + read_vec_offset;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    35
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    36
    // zero new vecs
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    37
    for (v = buf->write_vec; v < buf->vecs + buf->count; v++) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    38
        v->buf = NULL;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    39
        v->len = 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    40
    }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    42
    // ok
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    43
    return SUCCESS;
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
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    46
static err_t sock_test_read (struct sock_stream *base_sock, void *buf_ptr, size_t *len, struct error_info *err)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    struct io_buf *buf = &sock->recv_buf;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    50
    struct io_vec *vec = buf->read_vec;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    51
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    52
    // EOF/nonblock if we're past the end of the last vector
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    53
    if (!vec || vec == buf->vecs + buf->count || buf->off >= vec->len) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    54
        if (sock->nonblocking && !sock->eof) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    55
            // wait for more to be fed in
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    56
            *len = 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    57
            return SUCCESS;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    59
        } else {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    60
            // EOF!
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    61
            return SET_ERROR(err, ERR_READ_EOF);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    62
        }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    63
    }
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    // amount of data available in this iovec
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    size_t available = vec->len - buf->off;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
    // amount to read
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
    size_t to_read = *len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
    // trim down?
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
    if (to_read > available)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        to_read = available;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    // copy
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    memcpy(buf_ptr, vec->buf + buf->off, to_read);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    // consumed the whole vec?
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    if (to_read < available) {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
        // move offset
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
        buf->off += to_read;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    } else {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        // next vector
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
        buf->read_vec++;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    // update len
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    *len = to_read;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    return SUCCESS;
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
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    95
static err_t sock_test_write (struct sock_stream *base_sock, const void *buf_ptr, size_t *len, struct error_info *err)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    struct io_buf *buf = &sock->send_buf;
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
    99
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   100
    // ensure there's room
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   101
    if ((ERROR_CODE(err) = sock_test_grow_buf(buf)))
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   102
        goto error;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   103
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   104
    // the next buffer
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   105
    struct io_vec *vec = buf->write_vec;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    // store
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    vec->len = *len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    assert((vec->buf = malloc(vec->len)));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    memcpy(vec->buf, buf_ptr, vec->len);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    // move vec onwards
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   113
    buf->write_vec++;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    return SUCCESS;
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   117
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   118
error:
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   119
    return ERROR_CODE(err);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   120
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   121
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
static err_t sock_test_event_init (struct sock_stream *base_sock)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   124
    struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   125
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   126
    // set the nonblocking flag
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   127
    sock->nonblocking = true;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   128
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
static err_t sock_test_event_enable (struct sock_stream *base_sock, short mask)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   134
    struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   135
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   136
    // store mask
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   137
    sock->ev_mask = mask;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   138
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
static void sock_test_release (struct sock_stream *base_sock)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   144
    struct sock_test *sock = SOCK_FROM_BASE(base_sock, struct sock_test);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   146
    sock_test_destroy(sock);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
/*
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
 * Our sock_stream_type
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
static struct sock_stream_type sock_test_type = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
    .methods                = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
        .read               = &sock_test_read,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
        .write              = &sock_test_write,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
        .event_init         = &sock_test_event_init,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
        .event_enable       = &sock_test_event_enable,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
        .release            = &sock_test_release,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
    },
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
struct sock_test* sock_test_create (void)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
    struct sock_test *sock;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
    // allocate
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
    assert((sock = calloc(1, sizeof(*sock))));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
    // initialize base with our sock_stream_type
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
    sock_stream_init(SOCK_TEST_BASE(sock), &sock_test_type);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
    return sock;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   176
void sock_test_destroy (struct sock_test *sock)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   178
    size_t i;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   179
    struct io_buf *sbuf = &sock->send_buf, *rbuf = &sock->recv_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   180
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   181
    // free the send buffer
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   182
    for (i = 0; i < sbuf->count; i++) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   183
        free(sbuf->vecs[i].buf);
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
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   186
    // free the buffer vector lists
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   187
    free(sbuf->vecs);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   188
    free(rbuf->vecs);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   189
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   190
    // free the sock itself
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   191
    free(sock);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   192
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   193
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   194
void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   195
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   196
    struct io_buf *buf = &sock->recv_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   197
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   198
    // allocate + copy
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   199
    assert((buf->vecs = calloc(count, sizeof(struct io_vec))));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   200
    memcpy(buf->vecs, vecs, count * sizeof(struct io_vec));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   201
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   202
    // set
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   203
    buf->count = count;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   204
    buf->read_vec = buf->vecs;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   205
    buf->write_vec = buf->vecs + count;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   206
    buf->off = 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   207
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   208
    // set EOF flag?
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   209
    if (eof)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   210
        sock->eof = true;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   211
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   212
46
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   213
void sock_test_notify_events (struct sock_test *sock)
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   214
{
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   215
    // notify if events are enabled
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   216
    if (sock->ev_mask) {
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   217
        // zero mask
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   218
        int mask = sock->ev_mask;
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   219
        sock->ev_mask = 0;
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   220
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   221
        sock_stream_invoke_callbacks(SOCK_TEST_BASE(sock), mask);
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   222
    }
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   223
}
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   224
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   225
void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec new_vec)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   226
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   227
    struct io_buf *buf = &sock->recv_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   228
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   229
    // ensure there's room
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   230
    assert(sock_test_grow_buf(buf) == SUCCESS);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   231
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   232
    // copy    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   233
    *(buf->write_vec++) = new_vec;
46
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   234
    
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   235
    // notify
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   236
    sock_test_notify_events(sock);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   239
void sock_test_add_recv_str (struct sock_test *sock, const char *str)
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   240
{
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   241
    struct io_vec vec = {
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   242
        (char*) str, strlen(str)
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   243
    };
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   244
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   245
    sock_test_add_recv_vec(sock, vec);
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   246
}
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   247
46
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   248
void sock_test_set_recv_eof (struct sock_test *sock)
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   249
{
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   250
    sock->eof = true;
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   251
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   252
    sock_test_notify_events(sock);
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   253
}
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   254
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   255
void sock_test_get_send_data (struct sock_test *sock, char **buf_ptr, size_t *len_ptr)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   257
    struct io_buf *buf = &sock->send_buf;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   258
    size_t len = 0, i, off = 0;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   259
    char *out;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   260
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    // calculate total size
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   262
    for (i = 0; i < buf->count; i++) {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   263
        len += buf->vecs[i].len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   264
    }
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
    // alloc
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   267
    assert((out = malloc(len)));
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
    // copy
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   270
    for (i = 0; i < buf->count; i++) {
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   271
        struct io_vec *vec = buf->vecs + i;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   272
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   273
        memcpy(out + off, vec->buf, vec->len);
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   274
        off += vec->len;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   275
        
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   276
        // zero
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   277
        free(vec->buf); vec->buf = NULL;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   278
        vec->len = 0;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   279
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   280
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   281
    // update return
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   282
    *buf_ptr = out;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   283
    *len_ptr = len;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   284
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   285
    // update write_vec
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   286
    buf->write_vec = buf->vecs;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   287
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   288