author | Tero Marttila <terom@fixme.fi> |
Thu, 28 May 2009 01:17:36 +0300 | |
branch | new-lib-errors |
changeset 219 | cefec18b8268 |
parent 196 | src/transport_test.c@873796250c60 |
permissions | -rw-r--r-- |
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 |
*/ |
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
153 |
static err_t transport_test__read (transport_t *transport, void *buf_ptr, size_t *len, error_t *err) |
166
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 |
*/ |
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
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 |
|
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
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 |
|
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
236 |
static void transport_test__deinit (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 | 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 = { |
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
247 |
.base_type = { |
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
248 |
.parent = &transport_type_type, |
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
249 |
}, |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
250 |
.methods = { |
182
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
251 |
.read = transport_test__read, |
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
252 |
.write = transport_test__write, |
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
253 |
.events = transport_test__events, |
471ca1e744da
fix transport_fd to have the right parent, and fix transport_test
Tero Marttila <terom@fixme.fi>
parents:
166
diff
changeset
|
254 |
.deinit = transport_test__deinit |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
255 |
}, |
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 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
258 |
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
|
259 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
260 |
struct transport_test *tp; |
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
261 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
262 |
// allocate |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
263 |
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
|
264 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
265 |
// initialize base with our transport_type |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
266 |
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
|
267 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
268 |
// ok |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
269 |
return tp; |
41
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
270 |
} |
40f7aa051acb
add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents:
40
diff
changeset
|
271 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
272 |
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
|
273 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
274 |
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
|
275 |
} |
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
276 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
277 |
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
|
278 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
279 |
// invoke, masking out as needed |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
280 |
// 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
|
281 |
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
|
282 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
283 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
284 |
void transport_test_push_buf (struct transport_test *tp, const char *data, size_t len) |
43 | 285 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
286 |
// push it |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
287 |
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
|
288 |
|
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
289 |
// notify |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
290 |
transport_test_event(tp, TRANSPORT_READ); |
43 | 291 |
} |
292 |
||
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
293 |
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
|
294 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
295 |
// push it |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
296 |
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
|
297 |
} |
0c13bca53ae1
add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents:
43
diff
changeset
|
298 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
299 |
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
|
300 |
{ |
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
301 |
char buf[TRANSPORT_TEST_FMT_MAX]; |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
302 |
size_t ret; |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
303 |
|
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
304 |
// format |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
305 |
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
|
306 |
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
|
307 |
va_end(vargs); |
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 |
// push it |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
310 |
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
|
311 |
} |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
312 |
|
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
313 |
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
|
314 |
{ |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
315 |
// update state |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
316 |
tp->eof = true; |
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 |
transport_test_event(tp, TRANSPORT_READ); |
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 |
|
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
321 |
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
|
322 |
{ |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
323 |
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
|
324 |
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
|
325 |
char *out; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
326 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
327 |
// calculate total size |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
328 |
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
|
329 |
len += buf->vecs[i].len; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
330 |
} |
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 |
// alloc |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
333 |
assert((out = malloc(len))); |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
334 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
335 |
// copy |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
336 |
for (i = 0; i < buf->count; i++) { |
43 | 337 |
struct io_vec *vec = buf->vecs + i; |
338 |
||
339 |
memcpy(out + off, vec->buf, vec->len); |
|
340 |
off += vec->len; |
|
341 |
||
342 |
// zero |
|
343 |
free(vec->buf); vec->buf = NULL; |
|
344 |
vec->len = 0; |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
345 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
346 |
|
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
347 |
// update return |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
348 |
*buf_ptr = out; |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
349 |
*len_ptr = len; |
43 | 350 |
|
351 |
// update write_vec |
|
352 |
buf->write_vec = buf->vecs; |
|
40
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
353 |
} |
51678c7eae03
add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
354 |
|
196
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
182
diff
changeset
|
355 |
void transport_test_async_error (struct transport_test *tp, const error_t *err) |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
182
diff
changeset
|
356 |
{ |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
182
diff
changeset
|
357 |
transport_error(&tp->base, err); |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
182
diff
changeset
|
358 |
} |
873796250c60
implement msg_proto and associated test, fix misc. other bugs (including changing error_info::code to a signed int\!)
Tero Marttila <terom@fixme.fi>
parents:
182
diff
changeset
|
359 |
|
166
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
360 |
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
|
361 |
{ |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
362 |
// free the buffers |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
363 |
io_buf_destroy(&tp->send_buf); |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
364 |
io_buf_destroy(&tp->recv_buf); |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
365 |
} |
cb8cb023cf06
replace old sock_test with new transport_test
Tero Marttila <terom@fixme.fi>
parents:
139
diff
changeset
|
366 |