src/sock_test.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 21:12:48 +0200
changeset 41 40f7aa051acb
parent 40 51678c7eae03
child 42 13cfc41f76a7
permissions -rw-r--r--
add line_proto test, enhance others
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
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
static err_t sock_test_read (struct sock_stream *base_sock, void *buf_ptr, size_t *len)
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!
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    61
            return SET_ERROR(SOCK_TEST_ERR(sock), ERR_READ_EOF);
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
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
static err_t sock_test_write (struct sock_stream *base_sock, const void *buf_ptr, size_t *len)
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;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
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
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   101
    assert(sock_test_grow_buf(buf) == SUCCESS);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   102
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   103
    // the next buffer
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   104
    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
   105
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    // store
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    vec->len = *len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    assert((vec->buf = malloc(vec->len)));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    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
   110
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
    // move vec onwards
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   112
    buf->write_vec++;
40
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
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
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
   119
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   120
    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
   121
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   122
    // set the nonblocking flag
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   123
    sock->nonblocking = true;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   124
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
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
   129
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   130
    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
   131
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
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
   136
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   137
    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
   138
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   139
    sock_test_destroy(sock);
40
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
/*
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
 * Our sock_stream_type
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   144
 */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
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
   146
    .methods                = {
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   147
        .read               = &sock_test_read,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   148
        .write              = &sock_test_write,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
        .event_init         = &sock_test_event_init,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
        .event_enable       = &sock_test_event_enable,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
        .release            = &sock_test_release,
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
    },
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
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
   156
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
    struct sock_test *sock;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
    // allocate
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
    assert((sock = calloc(1, sizeof(*sock))));
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
    // 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
   163
    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
   164
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
    return sock;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   169
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
   170
{
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   171
    size_t i;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   172
    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
   173
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   174
    // free the send buffer
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   175
    for (i = 0; i < sbuf->count; i++) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   176
        free(sbuf->vecs[i].buf);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   177
    }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   178
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   179
    // free the buffer vector lists
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   180
    free(sbuf->vecs);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   181
    free(rbuf->vecs);
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
    // free the sock itself
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   184
    free(sock);
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
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   187
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
   188
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   189
    struct io_buf *buf = &sock->recv_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   190
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   191
    // allocate + copy
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   192
    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
   193
    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
   194
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   195
    // set
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   196
    buf->count = count;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   197
    buf->read_vec = buf->vecs;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   198
    buf->write_vec = buf->vecs + count;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   199
    buf->off = 0;
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
    // set EOF flag?
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   202
    if (eof)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   203
        sock->eof = true;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   204
}
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
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
   207
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   208
    struct io_buf *buf = &sock->recv_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   209
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   210
    // ensure there's room
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   211
    assert(sock_test_grow_buf(buf) == SUCCESS);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   212
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   213
    // copy    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   214
    *(buf->write_vec++) = new_vec;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   215
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
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
   218
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   219
    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
   220
    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
   221
    char *out;
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
    // calculate total size
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
    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
   225
        len += buf->vecs[i].len;
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
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   228
    // alloc
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   229
    assert((out = malloc(len)));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   230
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   231
    // copy
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   232
    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
   233
        memcpy(out + off, buf->vecs[i].buf, buf->vecs[i].len);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
        off += buf->vecs[i].len;
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
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
    // update return
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   238
    *buf_ptr = out;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   239
    *len_ptr = len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   240
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   241