]> git.meshlink.io Git - utcp/blob - selftest.c
Fix warnings when compiling with -Wall -W.
[utcp] / selftest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <errno.h>
6
7 #include "utcp.h"
8
9 struct utcp *a;
10 struct utcp *b;
11 struct utcp_connection *c;
12
13 ssize_t do_recv(struct utcp_connection *x, const void *data, size_t len) {
14         if(!len) {
15                 if(errno)
16                         fprintf(stderr, "%p Error: %s\n", x->utcp, strerror(errno));
17                 else
18                         fprintf(stderr, "%p Connection closed by peer\n", x->utcp);
19                 if(x != c) {
20                         fprintf(stderr, "closing my side too...\n");
21                         utcp_close(x);
22                 }
23                 return -1;
24         }
25
26         if(x == c)
27                 return write(0, data, len);
28         else
29                 return utcp_send(x, data, len);
30 }
31
32 bool do_pre_accept(struct utcp *utcp, uint16_t port) {
33         (void)utcp;
34         fprintf(stderr, "pre-accept\n");
35         if(port != 7)
36                 return false;
37         return true;
38 }
39
40 void do_accept(struct utcp_connection *c, uint16_t port) {
41         (void)port;
42         fprintf(stderr, "accept\n");
43         utcp_accept(c, do_recv, NULL);
44 }
45
46 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
47         static int count = 0;
48         if(++count > 1000) {
49                 fprintf(stderr, "Too many packets!\n");
50                 abort();
51         }
52
53         if(utcp == a)
54                 return utcp_recv(b, data, len);
55         else
56                 return utcp_recv(a, data, len);
57 }
58
59 int main(int argc, char *argv[]) {
60         (void)argc;
61         (void)argv;
62
63         srand(time(NULL));
64
65         a = utcp_init(do_accept, do_pre_accept, do_send, NULL);
66         b = utcp_init(NULL, NULL, do_send, NULL);
67
68         fprintf(stderr, "Testing connection to closed port\n\n");
69         c = utcp_connect(b, 6, do_recv, NULL);
70
71         fprintf(stderr, "\nTesting conection to non-listening side\n\n");
72         c = utcp_connect(a, 7, do_recv, NULL);
73
74         fprintf(stderr, "\nTesting connection to open port, close\n\n");
75         c = utcp_connect(b, 7, do_recv, NULL);
76         fprintf(stderr, "closing...\n");
77         utcp_close(c);
78
79         fprintf(stderr, "\nTesting connection to open port, abort\n\n");
80         c = utcp_connect(b, 7, do_recv, NULL);
81         fprintf(stderr, "aborting...\n");
82         utcp_abort(c);
83
84         fprintf(stderr, "\nTesting connection with data transfer\n\n");
85
86         c = utcp_connect(b, 7, do_recv, NULL);
87         ssize_t len = utcp_send(c, "Hello world!\n", 13);
88
89         if(len != 13) {
90                 if(len == -1)
91                         fprintf(stderr, "Error: %s\n", strerror(errno));
92                 else
93                         fprintf(stderr, "Short write %zd!\n", len);
94         }
95         len = utcp_send(c, "This is a test.\n", 16);
96
97         if(len != 16) {
98                 if(len == -1)
99                         fprintf(stderr, "Error: %s\n", strerror(errno));
100                 else
101                         fprintf(stderr, "Short write %zd!\n", len);
102         }
103
104         fprintf(stderr, "closing...\n");
105         utcp_close(c);
106
107         fprintf(stderr, "\nTesting connection with huge data transfer\n\n");
108
109         c = utcp_connect(b, 7, do_recv, NULL);
110         utcp_set_sndbuf(c, 10240);
111         char buf[20480] = "buf";
112
113         len = utcp_send(c, buf, sizeof buf);
114         if(len != 10240)
115                 fprintf(stderr, "Error: utcp_send() returned %zd, expected 10240\n", len);
116
117         fprintf(stderr, "closing...\n");
118         utcp_close(c);
119
120         utcp_exit(a);
121         utcp_exit(b);
122
123         return 0;
124 }