aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_os_start_process.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-02-16 14:02:23 +0000
committerNathan S. Evans <evans@in.tum.de>2010-02-16 14:02:23 +0000
commit17c88acd603c4f8ee1805c0db851cc9ce112f75f (patch)
tree5dc387f78a67ccffed8d546124ece2702fd1265e /src/util/test_os_start_process.c
parentaee82888eabdaee16aa78529f78361afcf8b2b01 (diff)
downloadgnunet-17c88acd603c4f8ee1805c0db851cc9ce112f75f.tar.gz
gnunet-17c88acd603c4f8ee1805c0db851cc9ce112f75f.zip
actually add test case, and added proper stdin support to os_start_process function...
Diffstat (limited to 'src/util/test_os_start_process.c')
-rw-r--r--src/util/test_os_start_process.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/util/test_os_start_process.c b/src/util/test_os_start_process.c
new file mode 100644
index 000000000..8114f399f
--- /dev/null
+++ b/src/util/test_os_start_process.c
@@ -0,0 +1,114 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file util/test_os_start_process.c
22 * @brief testcase for os start process code
23 *
24 * This testcase simply calls the os start process code
25 * giving a file descriptor to write stdout to. If the
26 * correct data "HELLO" is read then all is well.
27 *
28 */
29#include "platform.h"
30#include "gnunet_common.h"
31#include "gnunet_getopt_lib.h"
32#include "gnunet_os_lib.h"
33#include "gnunet_program_lib.h"
34#include "gnunet_scheduler_lib.h"
35#include "disk.h"
36
37#define VERBOSE GNUNET_NO
38
39static int
40check ()
41{
42 char *fn;
43 pid_t pid;
44 char *buf;
45 int fd_stdout;
46 int fd_stdin;
47 int ret;
48 static char *test_phrase = "HELLO WORLD";
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
54 buf = GNUNET_malloc(strlen(test_phrase) + 1);
55 GNUNET_asprintf(&fn, "cat");
56
57 hello_pipe_stdin = GNUNET_DISK_pipe(GNUNET_YES);
58 hello_pipe_stdout = GNUNET_DISK_pipe(GNUNET_YES);
59
60 if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
61 return GNUNET_SYSERR;
62
63 pid = GNUNET_OS_start_process (hello_pipe_stdin, hello_pipe_stdout, fn,
64 "test_gnunet_echo_hello", NULL);
65
66 /* Close the write end of the read pipe */
67 GNUNET_DISK_pipe_close_end(hello_pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
68 /* Close the read end of the write pipe */
69 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_READ);
70 /* Get the FD to read from */
71 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdout, GNUNET_DISK_PIPE_END_READ), &fd_stdout, sizeof (int));
72 /* Get the FD to write to */
73 GNUNET_DISK_internal_file_handle_ (GNUNET_DISK_pipe_handle(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE), &fd_stdin, sizeof (int));
74
75 /* Write the test_phrase to the cat process */
76 ret = write(fd_stdin, test_phrase, strlen(test_phrase) + 1);
77
78 /* Close the write end to end the cycle! */
79 GNUNET_DISK_pipe_close_end(hello_pipe_stdin, GNUNET_DISK_PIPE_END_WRITE);
80
81 ret = 0;
82 /* 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
88 if (0 != PLIBC_KILL (pid, SIGTERM))
89 {
90 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
91 }
92 GNUNET_OS_process_wait (pid);
93 GNUNET_DISK_pipe_close(hello_pipe_stdout);
94 GNUNET_DISK_pipe_close(hello_pipe_stdin);
95
96 return ret;
97}
98
99int
100main (int argc, char *argv[])
101{
102 int ret;
103
104 GNUNET_log_setup ("test-start-process",
105#if VERBOSE
106 "DEBUG",
107#else
108 "WARNING",
109#endif
110 NULL);
111 ret = check ();
112
113 return ret;
114}