1 /* SILC FD Stream tests */
3 #include "silcincludes.h"
5 SilcBool success = FALSE;
9 int buf1_len = sizeof(buf1);
11 static void stream_notifier(SilcStream stream, SilcStreamStatus status,
14 SILC_LOG_DEBUG(("Notifier"));
16 /* XXX we probably never get here with this test program */
18 silc_fsm_continue(context);
21 static void stream_notifier2(SilcStream stream, SilcStreamStatus status,
24 SILC_LOG_DEBUG(("Notifier"));
26 /* XXX we probably never get here with this test program */
28 silc_fsm_continue(context);
31 SILC_FSM_STATE(st_end)
33 unlink("/tmp/test_silcfdstream");
34 unlink("/tmp/test_silcfdstream_copy");
35 return SILC_FSM_FINISH;
38 SILC_FSM_STATE(st_readwrite)
40 int ret, i, k, fd1, fd2;
43 SILC_LOG_DEBUG(("Opening two files, one for reading, one for writing"));
45 SILC_LOG_DEBUG(("Open file /tmp/test_silcfdstream for reading"));
46 fd1 = silc_file_open("/tmp/test_silcfdstream", O_RDONLY);
48 SILC_LOG_DEBUG(("Error opening file"));
52 SILC_LOG_DEBUG(("Open file /tmp/test_silcfdstream_copy for writing"));
53 unlink("/tmp/test_silcfdstream_copy");
54 fd2 = silc_file_open("/tmp/test_silcfdstream_copy", O_CREAT | O_WRONLY);
56 SILC_LOG_DEBUG(("Error opening file"));
60 SILC_LOG_DEBUG(("Creating FD stream (two fds)"));
61 stream = silc_fd_stream_create2(fd1, fd2, schedule);
63 SILC_LOG_DEBUG(("Error creating stream"));
67 silc_stream_set_notifier(stream, stream_notifier2, fsm);
69 /* Stream between the fiels */
70 SILC_LOG_DEBUG(("Read/write 3 bytes at a time"));
71 memset(buf1, 0, sizeof(buf1));
72 while ((ret = silc_stream_read(stream, buf1, 3)) > 0) {
76 i = silc_stream_write(stream, cp, k);
79 SILC_LOG_DEBUG(("EOF"));
84 SILC_LOG_DEBUG(("Would block, write later"));
85 silc_fsm_next(fsm, st_end);
90 SILC_LOG_DEBUG(("Error: %s", strerror(silc_fd_stream_get_error(stream))));
100 SILC_LOG_DEBUG(("Would block, read later"));
101 silc_fsm_next(fsm, st_end);
102 return SILC_FSM_WAIT;
106 SILC_LOG_DEBUG(("Error: %s", strerror(silc_fd_stream_get_error(stream))));
111 SILC_LOG_DEBUG(("EOF, ok"));
113 SILC_LOG_DEBUG(("Closing stream"));
114 silc_stream_close(stream);
115 SILC_LOG_DEBUG(("Destroying stream"));
116 silc_stream_destroy(stream);
119 silc_fsm_next(fsm, st_end);
120 return SILC_FSM_CONTINUE;
123 silc_fsm_next(fsm, st_end);
124 return SILC_FSM_CONTINUE;
127 SILC_FSM_STATE(st_write)
132 /* Simple writing example */
133 SILC_LOG_DEBUG(("Open file /tmp/test_silcfdstream for writing"));
135 unlink("/tmp/test_silcfdstream");
136 fd = silc_file_open("/tmp/test_silcfdstream", O_CREAT | O_RDWR);
138 SILC_LOG_DEBUG(("Error opening file"));
142 SILC_LOG_DEBUG(("Creating FD stream"));
143 stream = silc_fd_stream_create(fd, schedule);
145 SILC_LOG_DEBUG(("Error creating stream"));
149 silc_stream_set_notifier(stream, stream_notifier, fsm);
151 memset(buf1, 0, sizeof(buf1));
152 for (i = 0; i < sizeof(buf1); i++)
155 SILC_LOG_DEBUG(("Writing data"));
159 ret = silc_stream_write(stream, cp, k);
162 SILC_LOG_DEBUG(("EOF"));
167 SILC_LOG_DEBUG(("Would block, write later"));
168 silc_fsm_next(fsm, st_readwrite);
169 return SILC_FSM_WAIT;
173 SILC_LOG_DEBUG(("Error: %s", strerror(silc_fd_stream_get_error(stream))));
181 SILC_LOG_DEBUG(("Closing stream"));
182 silc_stream_close(stream);
184 SILC_LOG_DEBUG(("Destroying stream"));
185 silc_stream_destroy(stream);
187 SILC_LOG_DEBUG(("Continue to next state"));
188 silc_fsm_next(fsm, st_readwrite);
189 return SILC_FSM_CONTINUE;
192 silc_fsm_next(fsm, st_end);
193 return SILC_FSM_CONTINUE;
196 static void fsm_dest(SilcFSM fsm, void *fsm_context, void *context)
199 silc_schedule_stop(schedule);
202 int main(int argc, char **argv)
206 if (argc > 1 && !strcmp(argv[1], "-d")) {
207 silc_log_debug(TRUE);
208 silc_log_debug_hexdump(TRUE);
209 silc_log_set_debug_string("fdstream*");
212 SILC_LOG_DEBUG(("Allocating scheduler"));
213 schedule = silc_schedule_init(0, NULL);
217 SILC_LOG_DEBUG(("Allocating FSM"));
218 fsm = silc_fsm_alloc(NULL, fsm_dest, NULL, schedule);
222 silc_fsm_start(fsm, st_write);
224 SILC_LOG_DEBUG(("Running scheduler"));
225 silc_schedule(schedule);
230 silc_schedule_uninit(schedule);
234 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
235 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");