]> git.meshlink.io Git - utcp/blob - selftest.c
Fix memory and resource leaks.
[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 int 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                 write(0, data, len);
28         else
29                 utcp_send(x, data, len);
30
31         return 0;
32 }
33
34 bool do_pre_accept(struct utcp *utcp, uint16_t port) {
35         fprintf(stderr, "pre-accept\n");
36         if(port != 7)
37                 return false;
38         return true;
39 }
40
41 void do_accept(struct utcp_connection *c, uint16_t port) {
42         fprintf(stderr, "accept\n");
43         utcp_accept(c, do_recv, NULL);
44 }
45
46 int 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         srand(time(NULL));
61
62         a = utcp_init(do_accept, do_pre_accept, do_send, NULL);
63         b = utcp_init(NULL, NULL, do_send, NULL);
64
65         fprintf(stderr, "Testing connection to closed port\n\n");
66         c = utcp_connect(b, 6, do_recv, NULL);
67
68         fprintf(stderr, "\nTesting conection to non-listening side\n\n");
69         c = utcp_connect(a, 7, do_recv, NULL);
70
71         fprintf(stderr, "\nTesting connection to open port, close\n\n");
72         c = utcp_connect(b, 7, do_recv, NULL);
73         fprintf(stderr, "closing...\n");
74         utcp_close(c);
75
76         fprintf(stderr, "\nTesting connection to open port, abort\n\n");
77         c = utcp_connect(b, 7, do_recv, NULL);
78         fprintf(stderr, "aborting...\n");
79         utcp_abort(c);
80
81         fprintf(stderr, "\nTesting connection with data transfer\n\n");
82
83         c = utcp_connect(b, 7, do_recv, NULL);
84         ssize_t len = utcp_send(c, "Hello world!\n", 13);
85
86         if(len != 13) {
87                 if(len < 0)
88                         fprintf(stderr, "Error: %s\n", strerror(errno));
89                 else
90                         fprintf(stderr, "Short write %zd!\n", len);
91         }
92         len = utcp_send(c, "This is a test.\n", 16);
93
94         if(len != 16) {
95                 if(len < 0)
96                         fprintf(stderr, "Error: %s\n", strerror(errno));
97                 else
98                         fprintf(stderr, "Short write %zd!\n", len);
99         }
100
101         fprintf(stderr, "closing...\n");
102         utcp_close(c);
103
104         fprintf(stderr, "\nTesting connection with huge data transfer\n\n");
105
106         c = utcp_connect(b, 7, do_recv, NULL);
107         utcp_set_sndbuf(c, 10240);
108         char buf[20480] = "buf";
109
110         len = utcp_send(c, buf, sizeof buf);
111         if(len != 10240)
112                 fprintf(stderr, "Error: utcp_send() returned %zd, expected 10240\n", len);
113
114         fprintf(stderr, "closing...\n");
115         utcp_close(c);
116
117         utcp_exit(a);
118         utcp_exit(b);
119
120         return 0;
121 }