e38a4b965f1ce67e611bbc6603092ca5508b0973
[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 3, 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 PeerContext p1;
37
38 struct PeerContext
39 {
40   struct GNUNET_CONFIGURATION_Handle *cfg;
41 #if START_ARM
42   struct GNUNET_OS_Process *arm_proc;
43 #endif
44 };
45
46
47 static void *
48 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
49 {
50   return NULL;
51 }
52
53
54 static void
55 setup_peer (struct PeerContext *p, const char *cfgname)
56 {
57   p->cfg = GNUNET_CONFIGURATION_create ();
58 #if START_ARM
59   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
60                                          "gnunet-service-arm",
61 #if VERBOSE
62                                          "-L", "DEBUG",
63 #endif
64                                          "-c", cfgname, NULL);
65 #endif
66   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
67 }
68
69
70 static void
71 stop_arm (struct PeerContext *p)
72 {
73 #if START_ARM
74   if (NULL != p->arm_proc)
75   {
76     if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
77       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
78     if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
79       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
80     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
81                 "ARM process %u stopped\n",
82                 GNUNET_OS_process_get_pid (p->arm_proc));
83     GNUNET_OS_process_close (p->arm_proc);
84     p->arm_proc = NULL;
85   }
86 #endif
87   GNUNET_CONFIGURATION_destroy (p->cfg);
88 }
89
90
91 static void
92 run (void *cls,
93      char *const *args,
94      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
95 {
96   struct GNUNET_FS_Handle *fs;
97
98   setup_peer (&p1, "test_fs_data.conf");
99   fs = GNUNET_FS_start (cfg,
100                         "test-fs-start-stop",
101                         &progress_cb,
102                         NULL, GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
103   GNUNET_assert (NULL != fs);
104   GNUNET_FS_stop (fs);
105 }
106
107
108 int
109 main (int argc, char *argv[])
110 {
111   char *const argvx[] = {
112     "test-fs-start-stop",
113     "-c",
114     "test_fs_data.conf",
115 #if VERBOSE
116     "-L", "DEBUG",
117 #endif
118     NULL
119   };
120   struct GNUNET_GETOPT_CommandLineOption options[] = {
121     GNUNET_GETOPT_OPTION_END
122   };
123
124   GNUNET_log_setup ("test_fs_start_stop",
125 #if VERBOSE
126                     "DEBUG",
127 #else
128                     "WARNING",
129 #endif
130                     NULL);
131   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
132                       argvx, "test-fs-start-stop",
133                       "nohelp", options, &run, NULL);
134   stop_arm (&p1);
135   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs/");
136   return 0;
137 }
138
139 /* end of test_fs_start_stop.c */