Added SILC Server library.
[silc.git] / lib / silcutil / tests / test_silcnet.c
1 /* SILC Net API tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6
7 typedef struct {
8   SilcFSM fsm;
9   SilcFSMSemaStruct sema;
10   SilcFSMThreadStruct thread;
11   SilcNetServer server;
12   SilcStream client_stream;
13   SilcNetStatus client_status;
14   SilcStream server_stream;
15   SilcNetStatus server_status;
16   SilcBool success;
17 } *Foo;
18
19 SILC_FSM_STATE(test_st_start);
20 SILC_FSM_STATE(test_st_second);
21 SILC_FSM_STATE(test_st_finish);
22
23 SILC_FSM_STATE(test_st_connect);
24 SILC_FSM_STATE(test_st_connected);
25
26 static void test_accept_connection(SilcNetStatus status, SilcStream stream,
27                                    void *context)
28 {
29   Foo f = context;
30   SILC_LOG_DEBUG(("Accepted new connection"));
31   f->client_status = status;
32   f->client_stream = stream;
33   SILC_FSM_SEMA_POST(&f->sema);
34 }
35
36 static void test_connected(SilcNetStatus status, SilcStream stream,
37                            void *context)
38 {
39   Foo f = context;
40   SILC_LOG_DEBUG(("Connected to server"));
41   f->server_status = status;
42   f->server_stream = stream;
43   SILC_FSM_CALL_CONTINUE(&f->thread);
44 }
45
46 SILC_FSM_STATE(test_st_connect)
47 {
48   Foo f = fsm_context;
49
50   SILC_LOG_DEBUG(("test_st_connect"));
51   SILC_LOG_DEBUG(("Connecting to server"));
52
53   silc_fsm_next(fsm, test_st_connected, NULL);
54   SILC_FSM_CALL(silc_net_tcp_connect(NULL, "localhost", 5000,
55                                      silc_fsm_get_schedule(fsm),
56                                      test_connected, f));
57 }
58
59 SILC_FSM_STATE(test_st_connected)
60 {
61   Foo f = fsm_context;
62   const char *host, *ip;
63   SilcUInt16 port;
64
65   SILC_LOG_DEBUG(("test_st_connected"));
66
67   if (f->server_status != SILC_NET_OK) {
68     SILC_LOG_DEBUG(("Creating connection failed"));
69     return SILC_FSM_FINISH;
70   }
71
72   silc_socket_stream_get_info(f->server_stream, NULL, &host, &ip, &port);
73   SILC_LOG_DEBUG(("Connected to server %s, %s:%d", host, ip, port));
74
75   return SILC_FSM_FINISH;
76 }
77
78 SILC_FSM_STATE(test_st_start)
79 {
80   Foo f = fsm_context;
81
82   SILC_LOG_DEBUG(("test_st_start"));
83
84   SILC_LOG_DEBUG(("Creating network listener"));
85   f->server = silc_net_create_server(NULL, 0, 5000, FALSE,
86                                      silc_fsm_get_schedule(fsm),
87                                      test_accept_connection, f);
88   if (!f->server) {
89     /** Creating network listener failed */
90     SILC_LOG_DEBUG(("Listener creation failed"));
91     silc_fsm_next(fsm, test_st_finish, NULL);
92     return SILC_FSM_CONTINUE;
93   }
94
95   /* Create thread to connect to the listener */
96   silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, FALSE);
97   silc_fsm_start(&f->thread, test_st_connect, NULL);
98
99   /** Start waiting connection */
100   SILC_LOG_DEBUG(("Start waiting for incoming connections"));
101   silc_fsm_sema_init(&f->sema, fsm, 0);
102   silc_fsm_next(fsm, test_st_second, NULL);
103   return SILC_FSM_CONTINUE;
104 }
105
106 SILC_FSM_STATE(test_st_second)
107 {
108   Foo f = fsm_context;
109   const char *ip, *host;
110   SilcUInt16 port;
111
112   SILC_LOG_DEBUG(("test_st_second"));
113
114   SILC_FSM_SEMA_WAIT(&f->sema);
115
116   if (f->client_status != SILC_NET_OK) {
117     /** Accepting new connection failed */
118     SILC_LOG_DEBUG(("Accepting failed %d", f->client_status));
119     silc_fsm_next(fsm, test_st_finish, NULL);
120     return SILC_FSM_CONTINUE;
121   }
122
123   silc_socket_stream_get_info(f->client_stream, NULL, &host, &ip, &port);
124   SILC_LOG_DEBUG(("Accepted new connection %s, %s:%d", host, ip, port));
125
126   /** Finish */
127   f->success = TRUE;
128   silc_fsm_next(fsm, test_st_finish, NULL);
129   return SILC_FSM_CONTINUE;
130 }
131
132 SILC_FSM_STATE(test_st_finish)
133 {
134   Foo f = fsm_context;
135
136   SILC_LOG_DEBUG(("test_st_finish"));
137
138   if (f->server_stream) {
139     silc_stream_close(f->server_stream);
140     silc_stream_destroy(f->server_stream);
141   }
142   if (f->client_stream) {
143     silc_stream_close(f->client_stream);
144     silc_stream_destroy(f->client_stream);
145   }
146
147   SILC_LOG_DEBUG(("Closing network listener"));
148   silc_net_close_server(f->server);
149
150   SILC_LOG_DEBUG(("Finish machine"));
151   return SILC_FSM_FINISH;
152 }
153
154 static void destructor(SilcFSM fsm, void *fsm_context,
155                        void *destructor_context)
156 {
157   SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
158   silc_fsm_free(fsm);
159   silc_schedule_stop(schedule);
160 }
161
162 int main(int argc, char **argv)
163 {
164   SilcBool success = FALSE;
165   SilcFSM fsm;
166   Foo f;
167
168   if (argc > 1 && !strcmp(argv[1], "-d")) {
169     silc_log_debug(TRUE);
170     silc_log_debug_hexdump(TRUE);
171     silc_log_set_debug_string("*net*,*stream*");
172   }
173
174   SILC_LOG_DEBUG(("Allocating scheduler"));
175   schedule = silc_schedule_init(0, NULL);
176
177   f = silc_calloc(1, sizeof(*f));
178   if (!f)
179     goto err;
180
181   SILC_LOG_DEBUG(("Allocating FSM context"));
182   fsm = silc_fsm_alloc(f, destructor, NULL, schedule);
183   if (!fsm)
184     goto err;
185   silc_fsm_start(fsm, test_st_start, NULL);
186   f->fsm = fsm;
187
188   SILC_LOG_DEBUG(("Running scheduler"));
189   silc_schedule(schedule);
190
191   if (!f->success)
192     goto err;
193
194   silc_schedule_uninit(schedule);
195   silc_free(f);
196
197   success = TRUE;
198
199  err:
200   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
201   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
202
203   return success;
204 }