aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_os_start_process.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-02-17 12:54:03 +0000
committerNathan S. Evans <evans@in.tum.de>2010-02-17 12:54:03 +0000
commit7c6939172c3a6d54b26c44dd04b8cf1489bd2a49 (patch)
tree84a1d9a1ac9ea04b77a6530885ce57cf713b39dc /src/util/test_os_start_process.c
parent1bceb8e169e337ff73a09ca43fb7dd57d879bc01 (diff)
downloadgnunet-7c6939172c3a6d54b26c44dd04b8cf1489bd2a49.tar.gz
gnunet-7c6939172c3a6d54b26c44dd04b8cf1489bd2a49.zip
use gnunet read function for testcase (mostly to give me an example), add dwFlags as instructed by Nils
Diffstat (limited to 'src/util/test_os_start_process.c')
-rw-r--r--src/util/test_os_start_process.c120
1 files changed, 98 insertions, 22 deletions
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
index 8114f399f..a3c935aeb 100644
--- a/src/util/test_os_start_process.c
+++ b/src/util/test_os_start_process.c
@@ -25,6 +25,10 @@
25 * giving a file descriptor to write stdout to. If the 25 * giving a file descriptor to write stdout to. If the
26 * correct data "HELLO" is read then all is well. 26 * correct data "HELLO" is read then all is well.
27 * 27 *
28 * TODO: This test case will not work on windows because
29 * there is no cat (unless there is). Perhaps we should
30 * add a gnunet_cat program/test program to util so we can
31 * adequately test this functionality on windows?
28 */ 32 */
29#include "platform.h" 33#include "platform.h"
30#include "gnunet_common.h" 34#include "gnunet_common.h"
@@ -36,20 +40,74 @@
36 40
37#define VERBOSE GNUNET_NO 41#define VERBOSE GNUNET_NO
38 42
39static int 43static char *test_phrase = "HELLO WORLD";
40check () 44static int ok;
45
46pid_t pid;
47/* Pipe to write to started processes stdin (on write end) */
48struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
49/* Pipe to read from started processes stdout (on read end) */
50struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
51
52GNUNET_SCHEDULER_TaskIdentifier die_task;
53
54static void
55end_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
56{
57
58 if (0 != PLIBC_KILL (pid, SIGTERM))
59 {
60 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
61 }
62 GNUNET_OS_process_wait (pid);
63 GNUNET_DISK_pipe_close(hello_pipe_stdout);
64 GNUNET_DISK_pipe_close(hello_pipe_stdin);
65}
66
67static void
68read_call (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69{
70 struct GNUNET_DISK_FileHandle *stdout_read_handle = cls;
71 char buf[16];
72 memset(&buf, 0, sizeof(buf));
73 int bytes;
74 bytes = GNUNET_DISK_file_read(stdout_read_handle, &buf, sizeof(buf));
75
76 if (bytes < 1)
77 {
78 ok = 1;
79 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
80 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
81 return;
82 }
83
84 ok = strncmp(&buf[0], test_phrase, strlen(test_phrase));
85#if VERBOSE
86 fprintf(stderr, "read %s\n", &buf[0]);
87#endif
88 if (ok == 0)
89 {
90 GNUNET_SCHEDULER_cancel(tc->sched, die_task);
91 GNUNET_SCHEDULER_add_now(tc->sched, &end_task, NULL);
92 return;
93 }
94
95 GNUNET_SCHEDULER_add_read_file (tc->sched,
96 GNUNET_TIME_UNIT_FOREVER_REL,
97 stdout_read_handle, &read_call, stdout_read_handle);
98
99}
100
101
102static void
103task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
41{ 104{
42 char *fn; 105 char *fn;
43 pid_t pid;
44 char *buf; 106 char *buf;
45 int fd_stdout; 107 int fd_stdout;
46 int fd_stdin; 108 int fd_stdin;
47 int ret; 109
48 static char *test_phrase = "HELLO WORLD"; 110 const struct GNUNET_DISK_FileHandle *stdout_read_handle;
49 /* Pipe to write to started processes stdin (on write end) */
50 struct GNUNET_DISK_PipeHandle *hello_pipe_stdin;
51 /* Pipe to read from started processes stdout (on read end) */
52 struct GNUNET_DISK_PipeHandle *hello_pipe_stdout;
53 111
54 buf = GNUNET_malloc(strlen(test_phrase) + 1); 112 buf = GNUNET_malloc(strlen(test_phrase) + 1);
55 GNUNET_asprintf(&fn, "cat"); 113 GNUNET_asprintf(&fn, "cat");
@@ -58,7 +116,10 @@ check ()
58 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES); 116 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
59 117
60 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL)) 118 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
61 return GNUNET_SYSERR; 119 {
120 ok = 1;
121 return;
122 }
62 123
63 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn, 124 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
64 "test_gnunet_echo_hello", NULL); 125 "test_gnunet_echo_hello", NULL);
@@ -73,29 +134,44 @@ check ()
73 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), &fd_stdin, sizeof (int)); 134 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), &fd_stdin, sizeof (int));
74 135
75 /* Write the test_phrase to the cat process */ 136 /* Write the test_phrase to the cat process */
76 ret = write(fd_stdin, test_phrase, strlen(test_phrase) + 1); 137 if (write(fd_stdin, test_phrase, strlen(test_phrase) + 1) == GNUNET_SYSERR)
138 {
139 ok = 1;
140 return;
141 }
77 142
78 /* Close the write end to end the cycle! */ 143 /* Close the write end to end the cycle! */
79 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE); 144 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
80 145
81 ret = 0; 146 stdout_read_handle = GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ);
147
148 die_task = GNUNET_SCHEDULER_add_delayed(tc->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_task, NULL);
149
150 GNUNET_SCHEDULER_add_read_file (tc->sched,
151 GNUNET_TIME_UNIT_FOREVER_REL,
152 stdout_read_handle, &read_call, (void *)stdout_read_handle);
82 /* Read from the cat process, hopefully get the phrase we wrote to it! */ 153 /* Read from the cat process, hopefully get the phrase we wrote to it! */
83 while (read(fd_stdout, buf, strlen(test_phrase) + 1) > 0)
84 {
85 ret = strncmp(buf, test_phrase, strlen(test_phrase));
86 }
87 154
88 if (0 != PLIBC_KILL (pid, SIGTERM)) 155 /*while (read(fd_stdout, buf, strlen(test_phrase) + 1) > 0)
89 { 156 {
90 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 157 ret = strncmp(buf, test_phrase, strlen(test_phrase));
91 } 158 }
92 GNUNET_OS_process_wait (pid); 159 */
93 GNUNET_DISK_pipe_close(hello_pipe_stdout); 160}
94 GNUNET_DISK_pipe_close(hello_pipe_stdin);
95 161
96 return ret; 162/**
163 * Main method, starts scheduler with task1,
164 * checks that "ok" is correct at the end.
165 */
166static int
167check ()
168{
169 ok = 1;
170 GNUNET_SCHEDULER_run (&task, &ok);
171 return ok;
97} 172}
98 173
174
99int 175int
100main (int argc, char *argv[]) 176main (int argc, char *argv[])
101{ 177{