1 /* SILC FD Stream tests */
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);
63 SILC_LOG_DEBUG(("Error creating stream"));
67 silc_stream_set_notifier(stream, schedule, 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"));
134 SILC_LOG_DEBUG(("Creating FD stream"));
135 unlink("/tmp/test_silcfdstream");
136 stream = silc_fd_stream_file("/tmp/test_silcfdstream", FALSE, TRUE);
138 SILC_LOG_DEBUG(("Error creating stream"));
142 silc_stream_set_notifier(stream, schedule, stream_notifier, fsm);
144 memset(buf1, 0, sizeof(buf1));
145 for (i = 0; i < sizeof(buf1); i++)
148 SILC_LOG_DEBUG(("Writing data"));
152 ret = silc_stream_write(stream, cp, k);
155 SILC_LOG_DEBUG(("EOF"));
160 SILC_LOG_DEBUG(("Would block, write later"));
161 silc_fsm_next(fsm, st_readwrite);
162 return SILC_FSM_WAIT;
166 SILC_LOG_DEBUG(("Error: %s", strerror(silc_fd_stream_get_error(stream))));
174 SILC_LOG_DEBUG(("Closing stream"));
175 silc_stream_close(stream);
177 SILC_LOG_DEBUG(("Destroying stream"));
178 silc_stream_destroy(stream);
180 SILC_LOG_DEBUG(("Continue to next state"));
181 silc_fsm_next(fsm, st_readwrite);
182 return SILC_FSM_CONTINUE;
185 silc_fsm_next(fsm, st_end);
186 return SILC_FSM_CONTINUE;
189 static void fsm_dest(SilcFSM fsm, void *fsm_context, void *context)
192 silc_schedule_stop(schedule);
195 int main(int argc, char **argv)
199 if (argc > 1 && !strcmp(argv[1], "-d")) {
200 silc_log_debug(TRUE);
201 silc_log_debug_hexdump(TRUE);
202 silc_log_set_debug_string("*fdstream*");
205 SILC_LOG_DEBUG(("Allocating scheduler"));
206 schedule = silc_schedule_init(0, NULL);
210 SILC_LOG_DEBUG(("Allocating FSM"));
211 fsm = silc_fsm_alloc(NULL, fsm_dest, NULL, schedule);
215 silc_fsm_start(fsm, st_write);
217 SILC_LOG_DEBUG(("Running scheduler"));
218 silc_schedule(schedule);
223 silc_schedule_uninit(schedule);
227 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
228 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");