1 /* SILC Local Net Stream API tests */
3 #include "silcruntime.h"
9 SilcFSMEventStruct sema;
10 SilcFSMThreadStruct thread;
11 SilcNetListener server;
12 SilcStream client_stream;
13 SilcResult client_status;
14 SilcStream server_stream;
15 SilcResult server_status;
19 SILC_FSM_STATE(test_st_start);
20 SILC_FSM_STATE(test_st_second);
21 SILC_FSM_STATE(test_st_finish);
23 SILC_FSM_STATE(test_st_connect);
24 SILC_FSM_STATE(test_st_connected);
26 static void test_accept_connection(SilcResult status, SilcStream stream,
30 SILC_LOG_DEBUG(("Accepted new connection"));
31 f->client_status = status;
32 f->client_stream = stream;
33 SILC_FSM_EVENT_SIGNAL(&f->sema);
36 static void test_connected(SilcResult status, SilcStream stream,
40 SILC_LOG_DEBUG(("Connected to server"));
41 f->server_status = status;
42 f->server_stream = stream;
43 SILC_FSM_CALL_CONTINUE(&f->thread);
46 SILC_FSM_STATE(test_st_connect)
50 SILC_LOG_DEBUG(("test_st_connect"));
51 SILC_LOG_DEBUG(("Connecting to server"));
53 silc_fsm_next(fsm, test_st_connected);
54 SILC_FSM_CALL(silc_local_net_connect("local_net",
55 silc_fsm_get_schedule(fsm),
59 SILC_FSM_STATE(test_st_connected)
62 const char *host, *ip;
65 SILC_LOG_DEBUG(("test_st_connected"));
67 if (f->server_status != SILC_OK) {
68 SILC_LOG_DEBUG(("Creating connection failed"));
69 return SILC_FSM_FINISH;
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));
75 return SILC_FSM_FINISH;
78 SILC_FSM_STATE(test_st_start)
82 SILC_LOG_DEBUG(("test_st_start"));
84 SILC_LOG_DEBUG(("Creating local network listener"));
85 f->server = silc_local_net_create_listener("local_net",
88 silc_fsm_get_schedule(fsm),
89 test_accept_connection, f);
91 /** Creating network listener failed */
92 SILC_LOG_DEBUG(("Listener creation failed"));
93 silc_fsm_next(fsm, test_st_finish);
94 return SILC_FSM_CONTINUE;
97 /* Create thread to connect to the listener */
98 silc_fsm_thread_init(&f->thread, fsm, f, NULL, NULL, FALSE);
99 silc_fsm_start(&f->thread, test_st_connect);
101 /** Start waiting connection */
102 SILC_LOG_DEBUG(("Start waiting for incoming connections"));
103 silc_fsm_event_init(&f->sema, fsm);
104 silc_fsm_next(fsm, test_st_second);
105 return SILC_FSM_CONTINUE;
108 SILC_FSM_STATE(test_st_second)
111 const char *ip, *host;
114 SILC_LOG_DEBUG(("test_st_second"));
116 SILC_FSM_EVENT_WAIT(&f->sema);
118 if (f->client_status != SILC_OK) {
119 /** Accepting new connection failed */
120 SILC_LOG_DEBUG(("Accepting failed %d", f->client_status));
121 silc_fsm_next(fsm, test_st_finish);
122 return SILC_FSM_CONTINUE;
125 silc_socket_stream_get_info(f->client_stream, NULL, &host, &ip, &port);
126 SILC_LOG_DEBUG(("Accepted new connection %s, %s:%d", host, ip, port));
128 /** Wait thread to terminate */
130 silc_fsm_next(fsm, test_st_finish);
131 SILC_FSM_THREAD_WAIT(&f->thread);
134 SILC_FSM_STATE(test_st_finish)
138 SILC_LOG_DEBUG(("test_st_finish"));
140 if (f->server_stream) {
141 silc_stream_close(f->server_stream);
142 silc_stream_destroy(f->server_stream);
144 if (f->client_stream) {
145 silc_stream_close(f->client_stream);
146 silc_stream_destroy(f->client_stream);
149 SILC_LOG_DEBUG(("Closing network listener"));
151 silc_local_net_close_listener(f->server);
153 SILC_LOG_DEBUG(("Finish machine"));
154 return SILC_FSM_FINISH;
157 static void destructor(SilcFSM fsm, void *fsm_context,
158 void *destructor_context)
160 SILC_LOG_DEBUG(("FSM destructor, stopping scheduler"));
162 silc_schedule_stop(schedule);
165 int main(int argc, char **argv)
167 SilcBool success = FALSE;
171 if (argc > 1 && !strcmp(argv[1], "-d")) {
172 silc_log_debug(TRUE);
173 silc_log_debug_hexdump(TRUE);
174 silc_log_set_debug_string("*net*,*stream*,*errno*");
177 SILC_LOG_DEBUG(("Allocating scheduler"));
178 schedule = silc_schedule_init(0, NULL, NULL, NULL);
180 f = silc_calloc(1, sizeof(*f));
184 SILC_LOG_DEBUG(("Allocating FSM context"));
185 fsm = silc_fsm_alloc(f, destructor, NULL, schedule);
188 silc_fsm_start(fsm, test_st_start);
191 SILC_LOG_DEBUG(("Running scheduler"));
192 silc_schedule(schedule);
197 silc_schedule_uninit(schedule);
203 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
204 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");