src/transport_test.c
author Tero Marttila <terom@fixme.fi>
Sun, 03 May 2009 17:19:05 +0300
branchnew-transport
changeset 166 cb8cb023cf06
parent 139 src/sock_test.c@55b9dcc2b73a
child 182 471ca1e744da
permissions -rw-r--r--
replace old sock_test with new transport_test
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     1
#include "transport_test.h"
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     2
#include "transport_internal.h"
40
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 <stdlib.h>
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <string.h>
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     6
#include <stdio.h>
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
     7
#include <stdarg.h>
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
#include <assert.h>
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
/**
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    11
 * Simple IO vector
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    12
 */
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    13
struct io_vec {
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    14
    /** The buffer */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    15
    char *buf;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    16
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    17
    /** Buffer size */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    18
    size_t len;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    19
};
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    20
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    21
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    22
 * Simple vectored IO-buffer
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    23
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    24
struct io_buf {
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    25
    /** The array of buffer-vectors, {NULL}-terminated */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    26
    struct io_vec *vecs;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    27
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    28
    /** The number of io_vecs */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    29
    size_t count;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    30
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    31
    /** Current read/write vector */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    32
    struct io_vec *read_vec, *write_vec;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    33
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    34
    /** Offset into current vector */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    35
    size_t off;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    36
};
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    37
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    38
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    39
 * Forward-declare our transport_type
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    40
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    41
extern const struct transport_type transport_test_type;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    42
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    43
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    44
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    45
 * A dummy sock_stream implementation intended for testing purposes.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    46
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    47
struct transport_test {
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    48
    /** The base transport stuff */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    49
    struct transport base;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    50
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    51
    /** The send/recieve buffers */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    52
    struct io_buf send_buf, recv_buf;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    53
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    54
    /** No more data is going to be added, return EOF once all the rest is consumed */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    55
    bool eof;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    56
};
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    57
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    58
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    59
 * Get a transport pointer from a transport_test pointer
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    60
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    61
#define TRANSPORT_TEST_BASE(tp_ptr) (&(tp_ptr)->base)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    62
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    63
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    64
 * Grow buf->vecs if needed to ensure that buf->write_vec points to a valid io_vec
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    65
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    66
static err_t io_buf_grow (struct io_buf *buf)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    67
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    68
    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
    69
    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
    70
    struct io_vec *v;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    71
    struct io_vec *vecs_tmp = buf->vecs;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    72
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    73
    // don't grow if not full
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    74
    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
    75
        return SUCCESS;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    76
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    77
    // new size
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    78
    buf->count = buf->count * 2 + 1;
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
    // grow
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    81
    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
    82
        // restore old value
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    83
        buf->vecs = vecs_tmp;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    84
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    85
        return ERR_CALLOC;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    86
    }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    88
    // restore vec positions
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    89
    buf->write_vec = buf->vecs + write_vec_offset;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    90
    buf->read_vec = buf->vecs + read_vec_offset;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    91
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    92
    // zero new vecs
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    93
    for (v = buf->write_vec; v < buf->vecs + buf->count; v++)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
    94
        memset(v, 0, sizeof(*v));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    95
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    96
    // ok
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    97
    return SUCCESS;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    98
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    99
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   100
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   101
 * Write some data to an io_buf, copying it.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   102
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   103
static err_t io_buf_write (struct io_buf *buf, const char *data, size_t len)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   105
    error_t err;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   106
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   107
    // ensure there's room
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   108
    if ((ERROR_CODE(&err) = io_buf_grow(buf)))
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   109
        goto error;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   110
    
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   111
    // the vector to use
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   112
    struct io_vec *vec = buf->write_vec;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   113
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   114
    // allocate
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   115
    if ((vec->buf = malloc(len)) == NULL)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   116
        JUMP_SET_ERROR(&err, ERR_MEM);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   117
    
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   118
    // store
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   119
    vec->len = len;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   120
    memcpy(vec->buf, data, len);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   121
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   122
    // vec consumed
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   123
    buf->write_vec++;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   124
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   125
    // ok
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   126
    return SUCCESS;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   127
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   128
error:
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   129
    return ERROR_CODE(&err);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   130
}
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   131
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   132
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   133
 * Destroy the io_buf, freeing all resources.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   134
 *
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   135
 * The io_buf must not be used anymore.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   136
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   137
static void io_buf_destroy (struct io_buf *buf)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   138
{
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   139
    size_t i;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   140
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   141
    // free the io_vec buffers
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   142
    for (i = 0; i < buf->count; i++) {
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   143
        free(buf->vecs[i].buf);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   144
    }
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   145
    
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   146
    // free the vector list
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   147
    free(buf->vecs);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   148
}
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   149
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   150
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   151
 * transport_methods::read implementation.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   152
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   153
static err_t transport_test_read (transport_t *transport, void *buf_ptr, size_t *len, error_t *err)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   154
{
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   155
    struct transport_test *tp = transport_check(transport, &transport_test_type);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   156
    struct io_buf *buf = &tp->recv_buf;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   157
    struct io_vec *vec = buf->read_vec;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   158
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   159
    // 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
   160
    if (!vec || vec == buf->vecs + buf->count || buf->off >= vec->len) {
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   161
        if (!tp->eof) {
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   162
            // wait for more to be fed in
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   163
            *len = 0;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   164
            return SUCCESS;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   166
        } else {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   167
            // EOF!
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   168
            return SET_ERROR(err, ERR_EOF);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   169
        }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   170
    }
40
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
    // 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
   173
    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
   174
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
    // amount to read
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
    size_t to_read = *len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
    // trim down?
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
    if (to_read > available)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
        to_read = available;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    // copy
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
    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
   184
    
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   185
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
    if (to_read < available) {
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   187
        // bytes still left in the vector
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
        buf->off += to_read;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
    } else {
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   191
        // consumed the whole vector
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   192
        // XXX: release data?
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   193
        buf->read_vec++;
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   194
        buf->off = 0;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   196
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
    // update len
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
    *len = to_read;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   200
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   201
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   202
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   203
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   204
/**
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   205
 * transport_methods::write implementation.
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   206
 */
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   207
static err_t transport_test_write (transport_t *transport, const void *data, size_t *len, error_t *err)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   208
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   209
    struct transport_test *tp = transport_check(transport, &transport_test_type);
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   210
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   211
    // write it out
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   212
    // XXX: partial writes?
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   213
    if ((ERROR_CODE(err) = io_buf_write(&tp->send_buf, data, *len)))
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   214
        goto error;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   215
    
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   216
    // ok
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   217
    return SUCCESS;
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   218
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   219
error:
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   220
    return ERROR_CODE(err);
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
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   223
static err_t transport_test_events (transport_t *transport, short mask, error_t *err)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   224
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   225
    struct transport_test *tp = transport_check(transport, &transport_test_type);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   226
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   227
    (void) tp;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   228
    (void) mask;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   229
    (void) err;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   230
    
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   231
    // XXX: don't re-trigger anything
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   232
    
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   233
    return SUCCESS;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   234
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   235
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   236
static void _transport_test_destroy (transport_t *transport)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   237
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   238
    struct transport_test *tp = transport_check(transport, &transport_test_type);
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   239
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   240
    transport_test_destroy(tp);
40
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
 * Our sock_stream_type
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   245
 */
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   246
const struct transport_type transport_test_type = {
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   247
    .methods                = {
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   248
        .read               = transport_test_read,
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   249
        .write              = transport_test_write,
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   250
        .events             = transport_test_events,
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   251
        .destroy            = _transport_test_destroy
40
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
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   254
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   255
struct transport_test* transport_test_create (struct transport_info *info)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   256
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   257
    struct transport_test *tp;
40
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
    // allocate
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   260
    assert((tp = calloc(1, sizeof(*tp))));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   261
    
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   262
    // initialize base with our transport_type
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   263
    transport_init(TRANSPORT_TEST_BASE(tp), &transport_test_type, info);
40
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
    // ok
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   266
    return tp;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   267
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   268
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   269
transport_t* transport_test_cast (struct transport_test *tp)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   270
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   271
    return TRANSPORT_TEST_BASE(tp);
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
   272
}
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   273
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   274
void transport_test_event (struct transport_test *tp, short what)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   275
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   276
    // invoke, masking out as needed
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   277
    // this won't do anything if all the bits are masked out
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   278
    transport_invoke(TRANSPORT_TEST_BASE(tp), what & TRANSPORT_TEST_BASE(tp)->info.ev_mask);
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
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   281
void transport_test_push_buf (struct transport_test *tp, const char *data, size_t len)
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   282
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   283
    // push it
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   284
    assert(io_buf_write(&tp->recv_buf, data, len) == SUCCESS);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   285
    
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   286
    // notify
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   287
    transport_test_event(tp, TRANSPORT_READ);
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   288
}
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   289
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   290
void transport_test_push_str (struct transport_test *tp, const char *str)
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
   291
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   292
    // push it
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   293
    transport_test_push_buf(tp, str, strlen(str));
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
   294
}
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   295
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   296
void transport_test_push_fmt (struct transport_test *tp, const char *fmt, ...)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   297
{
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   298
    char buf[TRANSPORT_TEST_FMT_MAX];
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   299
    size_t ret;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   300
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   301
    // format
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   302
    va_list vargs; va_start(vargs, fmt);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   303
    assert((ret = vsnprintf(buf, sizeof(buf), fmt, vargs)) <= sizeof(buf));
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   304
    va_end(vargs);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   305
       
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   306
    // push it
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   307
    transport_test_push_buf(tp, buf, ret);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   308
}
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   309
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   310
void transport_test_push_eof (struct transport_test *tp)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   311
{
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   312
    // update state
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   313
    tp->eof = true;
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   314
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   315
    transport_test_event(tp, TRANSPORT_READ);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   316
}
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   317
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   318
void transport_test_pull_buf (struct transport_test *tp, char **buf_ptr, size_t *len_ptr)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   319
{
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   320
    struct io_buf *buf = &tp->send_buf;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   321
    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
   322
    char *out;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   323
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   324
    // calculate total size
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   325
    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
   326
        len += buf->vecs[i].len;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   327
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   328
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   329
    // alloc
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   330
    assert((out = malloc(len)));
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   331
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   332
    // copy
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   333
    for (i = 0; i < buf->count; i++) {
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   334
        struct io_vec *vec = buf->vecs + i;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   335
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   336
        memcpy(out + off, vec->buf, vec->len);
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   337
        off += vec->len;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   338
        
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   339
        // zero
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   340
        free(vec->buf); vec->buf = NULL;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   341
        vec->len = 0;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   342
    }
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   343
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   344
    // update return
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   345
    *buf_ptr = out;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   346
    *len_ptr = len;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   347
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   348
    // update write_vec
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   349
    buf->write_vec = buf->vecs;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   350
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   351
166
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   352
void transport_test_destroy (struct transport_test *tp)
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   353
{
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   354
    // free the buffers
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   355
    io_buf_destroy(&tp->send_buf);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   356
    io_buf_destroy(&tp->recv_buf);
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   357
}
cb8cb023cf06 replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents: 139
diff changeset
   358