fix
[oweals/gnunet.git] / src / fs / test_fs_start_stop.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 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 /**
22  * @file fs/test_fs_start_stop.c
23  * @brief testcase for fs.c (start-stop only)
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define VERBOSE GNUNET_NO
33
34 #define START_ARM GNUNET_YES
35
36 static struct GNUNET_SCHEDULER_Handle *sched;
37
38 static struct PeerContext p1;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43 #if START_ARM
44   pid_t arm_pid;
45 #endif
46 };
47
48
49 static void *
50 progress_cb (void *cls, 
51              const struct GNUNET_FS_ProgressInfo *event)
52 {
53   return NULL;
54 }
55
56
57 static void
58 setup_peer (struct PeerContext *p, const char *cfgname)
59 {
60   p->cfg = GNUNET_CONFIGURATION_create ();
61 #if START_ARM
62   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
63                                         "gnunet-service-arm",
64 #if VERBOSE
65                                         "-L", "DEBUG",
66 #endif
67                                         "-c", cfgname, NULL);
68 #endif
69   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
70 }
71
72
73 static void
74 stop_arm (struct PeerContext *p)
75 {
76 #if START_ARM
77   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
78     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
79   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
80     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
82               "ARM process %u stopped\n", p->arm_pid);
83 #endif
84   GNUNET_CONFIGURATION_destroy (p->cfg);
85 }
86
87
88 static void
89 run (void *cls,
90      struct GNUNET_SCHEDULER_Handle *s,
91      char *const *args,
92      const char *cfgfile,
93      const struct GNUNET_CONFIGURATION_Handle *cfg)
94 {
95   struct GNUNET_FS_Handle *fs;
96
97   sched = s;
98   setup_peer (&p1, "test_fs_data.conf");
99   fs = GNUNET_FS_start (sched,
100                         cfg,
101                         "test-fs-start-stop",
102                         &progress_cb,
103                         NULL,
104                         GNUNET_FS_FLAGS_NONE,
105                         GNUNET_FS_OPTIONS_END);
106   GNUNET_assert (NULL != fs); 
107   sleep (1); /* FIXME */
108   GNUNET_FS_stop (fs);
109 }
110
111
112 int
113 main (int argc, char *argv[])
114 {
115   char *const argvx[] = { 
116     "test-fs-start-stop",
117     "-c",
118     "test_fs_data.conf",
119 #if VERBOSE
120     "-L", "DEBUG",
121 #endif
122     NULL
123   };
124   struct GNUNET_GETOPT_CommandLineOption options[] = {
125     GNUNET_GETOPT_OPTION_END
126   };
127
128   GNUNET_log_setup ("test_fs_start_stop", 
129 #if VERBOSE
130                     "DEBUG",
131 #else
132                     "WARNING",
133 #endif
134                     NULL);
135   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
136                       argvx, "test-fs-start-stop",
137                       "nohelp", options, &run, NULL);
138   stop_arm (&p1);
139   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs/");
140   return 0;
141 }
142
143 /* end of test_fs_start_stop.c */