PageRenderTime 63ms CodeModel.GetById 11ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/unit_socket.c

https://github.com/cloud-hot/picotcp
C | 497 lines | 376 code | 62 blank | 59 comment | 70 complexity | 0943909418fea62c24198a309e513f6e MD5 | raw file
  1
  2START_TEST (test_socket)
  3{
  4    int ret = 0;
  5    uint16_t port_be = 0, porta, proto, port_got;
  6    char buf[] = "test";
  7    struct pico_socket *sk_tcp, *sk_udp, *s, *sl, *sa;
  8    struct pico_device *dev;
  9    struct pico_ip4 inaddr_dst, inaddr_link, inaddr_incorrect, inaddr_uni, inaddr_null, netmask, orig, inaddr_got;
 10
 11    int getnodelay = -1;
 12    int nodelay = -1;
 13
 14    uint32_t getsocket_buffer = 0;
 15    uint32_t socket_buffer = 0;
 16
 17    pico_stack_init();
 18
 19    printf("START SOCKET TEST\n");
 20
 21    pico_string_to_ipv4("224.7.7.7", &inaddr_dst.addr);
 22    pico_string_to_ipv4("10.40.0.2", &inaddr_link.addr);
 23    pico_string_to_ipv4("224.8.8.8", &inaddr_incorrect.addr);
 24    pico_string_to_ipv4("0.0.0.0", &inaddr_null.addr);
 25    pico_string_to_ipv4("10.40.0.3", &inaddr_uni.addr);
 26
 27    dev = pico_null_create("dummy");
 28    netmask.addr = long_be(0xFFFF0000);
 29    ret = pico_ipv4_link_add(dev, inaddr_link, netmask);
 30    fail_if(ret < 0, "socket> error adding link");
 31
 32
 33    /* socket_open passing wrong parameters */
 34    s = pico_socket_open(PICO_PROTO_IPV4, 99, NULL);
 35    fail_if(s != NULL, "Error got socket wrong parameters");
 36
 37    s = pico_socket_open(PICO_PROTO_IPV4, -109, NULL);
 38    fail_if(s != NULL, "Error got socket");
 39
 40    s = pico_socket_open(99, PICO_PROTO_UDP, NULL);
 41    fail_if(s != NULL, "Error got socket");
 42
 43    s = pico_socket_open(-99, PICO_PROTO_UDP, NULL);
 44    fail_if(s != NULL, "Error got socket");
 45
 46
 47    sk_tcp = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL);
 48    fail_if(sk_tcp == NULL, "socket> tcp socket open failed");
 49
 50    port_be = short_be(5555);
 51    /* socket_bind passing wrong parameters */
 52    ret = pico_socket_bind(NULL, &inaddr_link, &port_be);
 53    fail_if(ret == 0, "socket> tcp socket bound wrong parameter");
 54    ret = pico_socket_bind(sk_tcp, NULL, &port_be);
 55    fail_if(ret == 0, "socket> tcp socket bound wrong parameter");
 56    ret = pico_socket_bind(sk_tcp, &inaddr_link, NULL);
 57    fail_if(ret == 0, "socket> tcp socket bound wrong parameter");
 58    /* socket_getname passing wrong parameters */
 59    ret = pico_socket_getname(NULL, &inaddr_link, &port_be, &proto);
 60    fail_if(ret == 0, "socket> tcp socket getname with wrong parameter");
 61    ret = pico_socket_getname(sk_tcp, NULL, &port_be, &proto);
 62    fail_if(ret == 0, "socket> tcp socket getname with wrong parameter");
 63    ret = pico_socket_getname(sk_tcp, &inaddr_link, NULL, &proto);
 64    fail_if(ret == 0, "socket> tcp socket getname with wrong parameter");
 65    ret = pico_socket_getname(sk_tcp, &inaddr_link, &port_be, NULL);
 66    fail_if(ret == 0, "socket> tcp socket getname with wrong parameter");
 67    /* socket_bind passing correct parameters */
 68    ret = pico_socket_bind(sk_tcp, &inaddr_link, &port_be);
 69    fail_if(ret < 0, "socket> tcp socket bind failed");
 70    sk_udp = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_UDP, NULL);
 71    fail_if(sk_udp == NULL, "socket> udp socket open failed");
 72    port_be = short_be(5555);
 73    ret = pico_socket_bind(sk_udp, &inaddr_link, &port_be);
 74    fail_if(ret < 0, "socket> udp socket bind failed");
 75
 76    ret = pico_socket_getname(sk_udp, &inaddr_got, &port_got, &proto);
 77    fail_if(ret < 0, "socket> udp socket getname failed");
 78    fail_if(inaddr_got.addr != inaddr_link.addr, "Getname: Address is different");
 79    fail_if(port_be != port_got, "Getname: Port is different");
 80    fail_if(proto != PICO_PROTO_IPV4, "Getname: proto is wrong");
 81
 82    /* socket_close passing wrong parameter */
 83    ret = pico_socket_close(NULL);
 84    fail_if(ret == 0, "Error socket close with wrong parameters");
 85
 86
 87    /* socket_connect passing wrong parameters */
 88    ret = pico_socket_connect(sk_udp, NULL, port_be);
 89    fail_if(ret == 0, "Error socket connect with wrong parameters");
 90    ret = pico_socket_connect(NULL, &inaddr_dst, port_be);
 91    fail_if(ret == 0, "Error socket connect with wrong parameters");
 92
 93    /* socket_connect passing correct parameters */
 94    ret = pico_socket_connect(sk_udp, &inaddr_dst, port_be);
 95    fail_if(ret < 0, "Error socket connect");
 96    ret = pico_socket_connect(sk_tcp, &inaddr_dst, port_be);
 97    fail_if(ret < 0, "Error socket connect");
 98
 99
100    /* testing listening socket */
101    sl = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, NULL);
102    fail_if(sl == NULL, "socket> tcp socket open failed");
103    port_be = short_be(6666);
104    ret = pico_socket_bind(sl, &inaddr_link, &port_be);
105    fail_if(ret < 0, "socket> tcp socket bind failed");
106    /* socket_listen passing wrong parameters */
107    ret = pico_socket_listen(sl, 0);
108    fail_if(ret == 0, "Error socket tcp socket listen done, wrong parameter");
109    ret = pico_socket_listen(NULL, 10);
110    fail_if(ret == 0, "Error socket tcp socket listen done, wrong parameter");
111    /* socket_listen passing correct parameters */
112    ret = pico_socket_listen(sl, 10);
113    fail_if(ret < 0, "socket> tcp socket listen failed: %s", strerror(pico_err));
114
115    /* socket_accept passing wrong parameters */
116    sa = pico_socket_accept(sl, &orig, NULL);
117    fail_if(sa != NULL, "Error socket tcp socket accept wrong argument");
118    sa = pico_socket_accept(sl, NULL, &porta);
119    fail_if(sa != NULL, "Error socket tcp socket accept wrong argument");
120    /* socket_accept passing correct parameters */
121    sa = pico_socket_accept(sl, &orig, &porta);
122    fail_if(sa == NULL && pico_err != PICO_ERR_EAGAIN, "socket> tcp socket accept failed: %s", strerror(pico_err));
123
124    ret = pico_socket_close(sl);
125    fail_if(ret < 0, "socket> tcp socket close failed: %s\n", strerror(pico_err));
126
127
128    /* testing socket read/write */
129    /* socket_write passing wrong parameters */
130    ret = pico_socket_write(NULL, (void *)buf, sizeof(buf));
131    fail_if(ret == 0, "Error socket write succeeded, wrong argument\n");
132    ret = pico_socket_write(sk_tcp, NULL, sizeof(buf));
133    fail_if(ret == 0, "Error socket write succeeded, wrong argument\n");
134    ret = pico_socket_write(sk_tcp, (void *)buf, 0);
135    fail_if(ret > 0, "Error socket write succeeded, wrong argument\n");
136    /* socket_write passing correct parameters */
137    ret = pico_socket_write(sk_tcp, (void *)buf, sizeof(buf));
138    fail_if(ret < 0, "socket> tcp socket write failed: %s\n", strerror(pico_err));
139    /* socket_read passing wrong parameters */
140    ret = pico_socket_read(NULL, (void *)buf, sizeof(buf));
141    fail_if(ret == 0, "Error socket read succeeded, wrong argument\n");
142    ret = pico_socket_read(sk_tcp, NULL, sizeof(buf));
143    fail_if(ret == 0, "Error socket read succeeded, wrong argument\n");
144    ret = pico_socket_read(sk_tcp, (void *)buf, 0);
145    fail_if(ret > 0, "Error socket read succeeded, wrong argument\n");
146    /* socket_read passing correct parameters */
147    ret = pico_socket_read(sk_tcp, (void *)buf, sizeof(buf));
148    fail_if(ret < 0, "socket> tcp socket read failed, ret = %d: %s\n", ret, strerror(pico_err)); /* tcp_recv returns 0 when no frame !? */
149
150
151    /* send/recv */
152    /* socket_send passing wrong parameters */
153    ret = pico_socket_send(NULL, (void *)buf, sizeof(buf));
154    fail_if(ret == 0, "Error socket send succeeded, wrong argument\n");
155    ret = pico_socket_send(sk_tcp, NULL, sizeof(buf));
156    fail_if(ret == 0, "Error socket send succeeded, wrong argument\n");
157    ret = pico_socket_send(sk_tcp, (void *)buf, 0);
158    fail_if(ret > 0, "Error socket send succeeded, wrong argument\n");
159    /* socket_write passing correct parameters */
160    ret = pico_socket_send(sk_tcp, (void *)buf, sizeof(buf));
161    fail_if(ret <= 0, "socket> tcp socket send failed: %s\n", strerror(pico_err));
162    /* socket_recv passing wrong parameters */
163    ret = pico_socket_recv(NULL, (void *)buf, sizeof(buf));
164    fail_if(ret == 0, "Error socket recv succeeded, wrong argument\n");
165    ret = pico_socket_recv(sk_tcp, NULL, sizeof(buf));
166    fail_if(ret == 0, "Error socket recv succeeded, wrong argument\n");
167    ret = pico_socket_recv(sk_tcp, (void *)buf, 0);
168    fail_if(ret > 0, "Error socket recv succeeded, wrong argument\n");
169    /* socket_recv passing correct parameters */
170    ret = pico_socket_recv(sk_tcp, (void *)buf, sizeof(buf));
171    fail_if(ret < 0, "socket> tcp socket recv failed, ret = %d: %s\n", ret, strerror(pico_err)); /* tcp_recv returns 0 when no frame !? */
172
173
174    /* sendto/recvfrom */
175    /* socket_sendto passing wrong parameters */
176    ret = pico_socket_sendto(NULL, (void *)buf, sizeof(buf), &inaddr_dst, port_be);
177    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
178    ret = pico_socket_sendto(sk_tcp, NULL, sizeof(buf), &inaddr_dst, port_be);
179    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
180    ret = pico_socket_sendto(sk_tcp, (void *)buf, 0, &inaddr_dst, port_be);
181    fail_if(ret > 0, "Error socket sendto succeeded, wrong argument\n");
182    ret = pico_socket_sendto(sk_tcp, (void *)buf, sizeof(buf), NULL, port_be);
183    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
184    ret = pico_socket_sendto(sk_tcp, (void *)buf, sizeof(buf), &inaddr_dst, -120);
185    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
186    /* socket_write passing correct parameters */
187    ret = pico_socket_sendto(sk_tcp, (void *)buf, sizeof(buf), &inaddr_dst, short_be(5555));
188    fail_if(ret <= 0, "socket> udp socket sendto failed, ret = %d: %s\n", ret, strerror(pico_err));
189    /* socket_recvfrom passing wrong parameters */
190    ret = pico_socket_recvfrom(NULL, (void *)buf, sizeof(buf), &orig, &porta);
191    fail_if(ret >= 0, "Error socket recvfrom succeeded, wrong argument\n");
192    ret = pico_socket_recvfrom(sk_tcp, NULL, sizeof(buf), &orig, &porta);
193    fail_if(ret >= 0, "Error socket recvfrom succeeded, wrong argument\n");
194    ret = pico_socket_recvfrom(sk_tcp, (void *)buf, 0, &orig, &porta);
195    fail_if(ret > 0, "Error socket recvfrom succeeded, wrong argument\n");
196    ret = pico_socket_recvfrom(sk_tcp, (void *)buf, sizeof(buf), NULL, &porta);
197    fail_if(ret > 0, "Error socket recvfrom succeeded, wrong argument\n");
198    ret = pico_socket_recvfrom(sk_tcp, (void *)buf, sizeof(buf), &orig, NULL);
199    fail_if(ret > 0, "Error socket recvfrom succeeded, wrong argument\n");
200    /* socket_recvfrom passing correct parameters */
201    ret = pico_socket_recvfrom(sk_tcp, (void *)buf, sizeof(buf), &orig, &porta);
202    fail_if(ret != 0, "socket> tcp socket recvfrom failed, ret = %d: %s\n", ret, strerror(pico_err)); /* tcp_recv returns -1 when no frame !? */
203
204
205    /* testing socket read/write */
206    /* socket_write passing wrong parameters */
207    ret = pico_socket_write(NULL, (void *)buf, sizeof(buf));
208    fail_if(ret == 0, "Error socket write succeeded, wrong argument\n");
209    ret = pico_socket_write(sk_udp, NULL, sizeof(buf));
210    fail_if(ret == 0, "Error socket write succeeded, wrong argument\n");
211    ret = pico_socket_write(sk_udp, (void *)buf, 0);
212    fail_if(ret > 0, "Error socket write succeeded, wrong argument\n");
213    /* socket_write passing correct parameters */
214    ret = pico_socket_write(sk_udp, (void *)buf, sizeof(buf));
215    fail_if(ret < 0, "socket> tcp socket write failed: %s\n", strerror(pico_err));
216    /* socket_read passing wrong parameters */
217    ret = pico_socket_read(NULL, (void *)buf, sizeof(buf));
218    fail_if(ret == 0, "Error socket read succeeded, wrong argument\n");
219    ret = pico_socket_read(sk_udp, NULL, sizeof(buf));
220    fail_if(ret == 0, "Error socket read succeeded, wrong argument\n");
221    ret = pico_socket_read(sk_udp, (void *)buf, 0);
222    fail_if(ret > 0, "Error socket read succeeded, wrong argument\n");
223    /* socket_read passing correct parameters */
224    ret = pico_socket_read(sk_udp, (void *)buf, sizeof(buf));
225    fail_if(ret != 0, "socket> udp socket read failed, ret = %d: %s\n", ret, strerror(pico_err));
226
227
228    /* send/recv */
229    /* socket_send passing wrong parameters */
230    ret = pico_socket_send(NULL, (void *)buf, sizeof(buf));
231    fail_if(ret == 0, "Error socket send succeeded, wrong argument\n");
232    ret = pico_socket_send(sk_udp, NULL, sizeof(buf));
233    fail_if(ret == 0, "Error socket send succeeded, wrong argument\n");
234    ret = pico_socket_send(sk_udp, (void *)buf, 0);
235    fail_if(ret > 0, "Error socket send succeeded, wrong argument\n");
236    /* socket_write passing correct parameters */
237    ret = pico_socket_send(sk_udp, (void *)buf, sizeof(buf));
238    fail_if(ret <= 0, "socket> tcp socket send failed: %s\n", strerror(pico_err));
239    /* socket_recv passing wrong parameters */
240    ret = pico_socket_recv(NULL, (void *)buf, sizeof(buf));
241    fail_if(ret == 0, "Error socket recv succeeded, wrong argument\n");
242    ret = pico_socket_recv(sk_udp, NULL, sizeof(buf));
243    fail_if(ret == 0, "Error socket recv succeeded, wrong argument\n");
244    ret = pico_socket_recv(sk_udp, (void *)buf, 0);
245    fail_if(ret > 0, "Error socket recv succeeded, wrong argument\n");
246    /* socket_recv passing correct parameters */
247    ret = pico_socket_recv(sk_udp, (void *)buf, sizeof(buf));
248    fail_if(ret != 0, "socket> udp socket recv failed, ret = %d: %s\n", ret, strerror(pico_err));
249
250
251    /* sendto/recvfrom */
252    /* socket_sendto passing wrong parameters */
253    ret = pico_socket_sendto(NULL, (void *)buf, sizeof(buf), &inaddr_dst, port_be);
254    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
255    ret = pico_socket_sendto(sk_udp, NULL, sizeof(buf), &inaddr_dst, port_be);
256    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
257    ret = pico_socket_sendto(sk_udp, (void *)buf, 0, &inaddr_dst, port_be);
258    fail_if(ret > 0, "Error socket sendto succeeded, wrong argument\n");
259    ret = pico_socket_sendto(sk_udp, (void *)buf, sizeof(buf), NULL, port_be);
260    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
261    ret = pico_socket_sendto(sk_udp, (void *)buf, sizeof(buf), &inaddr_dst, -120);
262    fail_if(ret >= 0, "Error socket sendto succeeded, wrong argument\n");
263    /* socket_write passing correct parameters */
264    ret = pico_socket_sendto(sk_udp, (void *)buf, sizeof(buf), &inaddr_dst, short_be(5555));
265    fail_if(ret <= 0, "socket> udp socket sendto failed, ret = %d: %s\n", ret, strerror(pico_err));
266    /* socket_recvfrom passing wrong parameters */
267    ret = pico_socket_recvfrom(NULL, (void *)buf, sizeof(buf), &orig, &porta);
268    fail_if(ret >= 0, "Error socket recvfrom succeeded, wrong argument\n");
269    ret = pico_socket_recvfrom(sk_udp, NULL, sizeof(buf), &orig, &porta);
270    fail_if(ret >= 0, "Error socket recvfrom succeeded, wrong argument\n");
271    /* socket_recvfrom passing correct parameters */
272    ret = pico_socket_recvfrom(sk_udp, (void *)buf, 0, &orig, &porta);
273    fail_if(ret != 0, "socket> udp socket recvfrom failed, ret = %d: %s\n", ret, strerror(pico_err));
274    ret = pico_socket_recvfrom(sk_udp, (void *)buf, sizeof(buf), &orig, &porta);
275    fail_if(ret != 0, "socket> udp socket recvfrom failed, ret = %d: %s\n", ret, strerror(pico_err));
276
277    /* temporary fix, until Nagle problems are analyzed and fixed */
278    {
279        nodelay = 0;
280        ret = pico_socket_setoption(sk_tcp, PICO_TCP_NODELAY, &nodelay);
281    }
282
283    /* setoption/getoption */
284    ret = pico_socket_getoption(sk_tcp, PICO_TCP_NODELAY, &getnodelay);
285    fail_if(ret < 0, "socket> socket_getoption: supported PICO_TCP_NODELAY failed (err = %s)\n", strerror(pico_err));
286    fail_if(getnodelay != 0, "socket> socket_setoption: default PICO_TCP_NODELAY != 0 (nagle disabled by default)\n");
287
288    nodelay = 1;
289    ret = pico_socket_setoption(sk_tcp, PICO_TCP_NODELAY, &nodelay);
290    fail_if(ret < 0, "socket> socket_setoption: supported PICO_TCP_NODELAY failed\n");
291    ret = pico_socket_getoption(sk_tcp, PICO_TCP_NODELAY, &getnodelay);
292    fail_if(ret < 0, "socket> socket_getoption: supported PICO_TCP_NODELAY failed\n");
293    fail_if(getnodelay == 0, "socket> socket_setoption: PICO_TCP_NODELAY is off (expected: on!)\n");
294
295    nodelay = 0;
296    ret = pico_socket_setoption(sk_tcp, PICO_TCP_NODELAY, &nodelay);
297    fail_if(ret < 0, "socket> socket_setoption: supported PICO_TCP_NODELAY failed\n");
298    ret = pico_socket_getoption(sk_tcp, PICO_TCP_NODELAY, &getnodelay);
299    fail_if(ret < 0, "socket> socket_getoption: supported PICO_TCP_NODELAY failed\n");
300    fail_if(getnodelay != 0, "socket> socket_setoption: PICO_TCP_NODELAY is on (expected: off!)\n");
301
302
303    /* Set/get recv buffer (TCP) */
304    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
305    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
306    fail_if(getsocket_buffer != PICO_DEFAULT_SOCKETQ,
307            "socket> socket_setoption: default PICO_SOCKET_OPT_SNDBUF != DEFAULT\n");
308
309    socket_buffer = PICO_DEFAULT_SOCKETQ;
310    ret = pico_socket_setoption(sk_tcp, PICO_SOCKET_OPT_RCVBUF, &socket_buffer);
311    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
312    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
313    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
314    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_RCVBUF is != than expected\n");
315
316    socket_buffer = 2 * PICO_DEFAULT_SOCKETQ;
317    ret = pico_socket_setoption(sk_tcp, PICO_SOCKET_OPT_RCVBUF, &socket_buffer);
318    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
319    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
320    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
321    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_RCVBUF is != than expected\n");
322
323    /* Set/get send buffer (TCP) */
324    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
325    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
326    fail_if(getsocket_buffer != PICO_DEFAULT_SOCKETQ,
327            "socket> socket_setoption: default PICO_SOCKET_OPT_SNDBUF != DEFAULT got: %d exp: %d\n", getsocket_buffer, PICO_DEFAULT_SOCKETQ);
328
329    socket_buffer = PICO_DEFAULT_SOCKETQ;
330    ret = pico_socket_setoption(sk_tcp, PICO_SOCKET_OPT_SNDBUF, &socket_buffer);
331    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
332    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
333    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
334    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_SNDBUF is != than expected\n");
335
336    socket_buffer = 2 * PICO_DEFAULT_SOCKETQ;
337    ret = pico_socket_setoption(sk_tcp, PICO_SOCKET_OPT_SNDBUF, &socket_buffer);
338    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
339    ret = pico_socket_getoption(sk_tcp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
340    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
341    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_SNDBUF is != than expected\n");
342
343    /* Set/get recv buffer (UDP) */
344    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
345    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
346    fail_if(getsocket_buffer != PICO_DEFAULT_SOCKETQ,
347            "socket> socket_setoption: default PICO_SOCKET_OPT_SNDBUF != DEFAULT\n");
348
349    socket_buffer = PICO_DEFAULT_SOCKETQ;
350    ret = pico_socket_setoption(sk_udp, PICO_SOCKET_OPT_RCVBUF, &socket_buffer);
351    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
352    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
353    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
354    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_RCVBUF is != than expected\n");
355
356    socket_buffer = 2 * PICO_DEFAULT_SOCKETQ;
357    ret = pico_socket_setoption(sk_udp, PICO_SOCKET_OPT_RCVBUF, &socket_buffer);
358    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
359    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_RCVBUF, &getsocket_buffer);
360    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_RCVBUF failed\n");
361    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_RCVBUF is != than expected\n");
362
363    /* Set/get send buffer (UDP) */
364    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
365    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
366    fail_if(getsocket_buffer != PICO_DEFAULT_SOCKETQ,
367            "socket> socket_setoption: default PICO_SOCKET_OPT_SNDBUF != DEFAULT\n");
368
369    socket_buffer = PICO_DEFAULT_SOCKETQ;
370    ret = pico_socket_setoption(sk_udp, PICO_SOCKET_OPT_SNDBUF, &socket_buffer);
371    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
372    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
373    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
374    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_SNDBUF is != than expected\n");
375
376    socket_buffer = 2 * PICO_DEFAULT_SOCKETQ;
377    ret = pico_socket_setoption(sk_udp, PICO_SOCKET_OPT_SNDBUF, &socket_buffer);
378    fail_if(ret < 0, "socket> socket_setoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
379    ret = pico_socket_getoption(sk_udp, PICO_SOCKET_OPT_SNDBUF, &getsocket_buffer);
380    fail_if(ret < 0, "socket> socket_getoption: supported PICO_SOCKET_OPT_SNDBUF failed\n");
381    fail_if(getsocket_buffer != socket_buffer, "UDP socket> socket_setoption: PICO_SOCKET_OPT_SNDBUF is != than expected\n");
382
383    /* Close sockets, eventually. */
384    ret = pico_socket_close(sk_tcp);
385    fail_if(ret < 0, "socket> tcp socket close failed: %s\n", strerror(pico_err));
386    ret = pico_socket_close(sk_udp);
387    fail_if(ret < 0, "socket> udp socket close failed: %s\n", strerror(pico_err));
388}
389END_TEST
390
391#ifdef PICO_SUPPORT_CRC_FAULTY_UNIT_TEST
392START_TEST (test_crc_check)
393{
394    uint8_t buffer[64] = {
395        0x45, 0x00, 0x00, 0x40,                  /* start of IP hdr */
396        0x91, 0xc3, 0x40, 0x00,
397        0x40, 0x11, 0x24, 0xcf,                  /* last 2 bytes are CRC */
398        0xc0, 0xa8, 0x01, 0x66,
399        0xc0, 0xa8, 0x01, 0x64,                  /* end of IP hdr */
400        0x15, 0xb3, 0x1F, 0x90,                  /* start of UDP/TCP hdr */
401        0x00, 0x2c, 0x27, 0x22,                  /* end of UDP hdr */
402        0x00, 0x00, 0x00, 0x00,
403        0x00, 0x00, 0x00, 0x00,
404        0x00, 0x0b, 0x00, 0x00,
405        0x00, 0x00, 0x00, 0x00,
406        0x00, 0x00, 0x00, 0x00,
407        0x00, 0x00, 0x00, 0x00,                  /* end of TCP hdr */
408        0x01, 0x23, 0x45, 0x67,                  /* start of data */
409        0x89, 0xab, 0xcd, 0xef,
410        0xc0, 0xca, 0xc0, 0x1a
411    };
412    struct pico_frame *f = NULL;
413    struct pico_ipv4_hdr *hdr = (struct pico_ipv4_hdr *) buffer;
414    struct pico_udp_hdr *udp_hdr = NULL;
415    struct pico_tcp_hdr *tcp_hdr = NULL;
416    uint32_t *f_usage_count = NULL;
417    uint8_t *f_buffer = NULL;
418    int ret = -1;
419
420    printf("START CRC TEST\n");
421    pico_stack_init();
422
423    /* IPv4 CRC unit tests */
424    /* Allocated memory will not be freed when pico_ipv4_crc_check fails */
425    f = calloc(1, sizeof(struct pico_frame));
426    f_usage_count = calloc(1, sizeof(uint32_t));
427    f_buffer = calloc(1, sizeof(uint8_t));
428    f->net_hdr = buffer;
429    f->net_len = PICO_SIZE_IP4HDR;
430    f->transport_hdr = buffer + PICO_SIZE_IP4HDR;
431    f->transport_len = sizeof(buffer) - PICO_SIZE_IP4HDR;
432    f->usage_count = f_usage_count;
433    f->buffer = f_buffer;
434    *(f->usage_count) = 512;
435
436    hdr->crc = 0;
437    printf(">>>>>>>>>>>>>>>>>>>>> CRC VALUE = %X\n", pico_checksum(hdr, PICO_SIZE_IP4HDR));
438    hdr->crc = short_be(0x24CF); /* Make check pass */
439    ret = pico_ipv4_crc_check(f);
440    fail_if(ret == 0, "correct IPv4 checksum got rejected\n");
441    hdr->crc = short_be(0x8899); /* Make check fail */
442    ret = pico_ipv4_crc_check(f);
443    fail_if(ret == 1, "incorrect IPv4 checksum got accepted\n");
444
445    /* UDP CRC unit tests */
446    /* Allocated memory will be freed when pico_transport_crc_check fails */
447    f = calloc(1, sizeof(struct pico_frame));
448    f_usage_count = calloc(1, sizeof(uint32_t));
449    f_buffer = calloc(1, sizeof(uint8_t));
450    f->net_hdr = buffer;
451    f->transport_hdr = buffer + PICO_SIZE_IP4HDR;
452    f->transport_len = sizeof(buffer) - PICO_SIZE_IP4HDR;
453    f->usage_count = f_usage_count;
454    f->buffer = f_buffer;
455    *(f->usage_count) = 1;
456    hdr->proto = 0x11; /* UDP */
457    hdr->crc = short_be(0x24cf); /* Set IPv4 CRC correct */
458    udp_hdr = (struct pico_udp_hdr *) f->transport_hdr;
459
460    /* udp_hdr->crc = 0; */
461    /* printf(">>>>>>>>>>>>>>>>>>>>> UDP CRC VALUE = %X\n", pico_udp_checksum_ipv4(f)); */
462    ret = pico_transport_crc_check(f);
463    fail_if(ret == 0, "correct UDP checksum got rejected\n");
464    udp_hdr->crc = 0;
465    ret = pico_transport_crc_check(f);
466    fail_if(ret == 0, "UDP checksum of 0 did not get ignored\n");
467    udp_hdr->crc = short_be(0x8899); /* Make check fail */
468    ret = pico_transport_crc_check(f);
469    fail_if(ret == 1, "incorrect UDP checksum got accepted\n");
470
471    /* TCP CRC unit tests */
472    /* Allocated memory will be freed when pico_transport_crc_check fails */
473    f = calloc(1, sizeof(struct pico_frame));
474    f_usage_count = calloc(1, sizeof(uint32_t));
475    f_buffer = calloc(1, sizeof(uint8_t));
476    f->net_hdr = buffer;
477    f->transport_hdr = buffer + PICO_SIZE_IP4HDR;
478    f->transport_len = sizeof(buffer) - PICO_SIZE_IP4HDR;
479    f->usage_count = f_usage_count;
480    f->buffer = f_buffer;
481    *(f->usage_count) = 1;
482    hdr->proto = 0x06; /* TCP */
483    hdr->crc = short_be(0x24cf); /* Set IPv4 CRC correct */
484    tcp_hdr = (struct pico_tcp_hdr *) f->transport_hdr;
485    tcp_hdr->seq = long_be(0x002c2722); /* Set sequence number correct */
486
487    /* tcp_hdr = 0; */
488    /* printf(">>>>>>>>>>>>>>>>>>>>> TCP CRC VALUE = %X\n", pico_tcp_checksum_ipv4(f)); */
489    tcp_hdr->crc = short_be(0x0016); /* Set correct TCP CRC */
490    ret = pico_transport_crc_check(f);
491    fail_if(ret == 0, "correct TCP checksum got rejected\n");
492    tcp_hdr->crc = short_be(0x8899); /* Make check fail */
493    ret = pico_transport_crc_check(f);
494    fail_if(ret == 1, "incorrect TCP checksum got accepted\n");
495}
496END_TEST
497#endif