- changess
[oweals/gnunet.git] / src / namestore / test_namestore_api.c
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 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  * @file namestore/test_namestore_api.c
22  * @brief testcase for namestore_api.c
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27
28 #define VERBOSE GNUNET_EXTRA_LOGGING
29
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
31
32 static struct GNUNET_NAMESTORE_Handle * nsh;
33
34 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
35 static struct GNUNET_OS_Process *arm;
36
37 static int res;
38
39
40 static void
41 start_arm (const char *cfgname)
42 {
43   arm = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
44                                "gnunet-service-arm", "-c", cfgname,
45 #if VERBOSE_PEERS
46                                "-L", "DEBUG",
47 #else
48                                "-L", "ERROR",
49 #endif
50                                NULL);
51 }
52
53 static void
54 stop_arm ()
55 {
56   if (NULL != arm)
57   {
58     if (0 != GNUNET_OS_process_kill (arm, SIGTERM))
59       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
60     GNUNET_OS_process_wait (arm);
61     GNUNET_OS_process_close (arm);
62     arm = NULL;
63   }
64 }
65
66 /**
67  * Re-establish the connection to the service.
68  *
69  * @param cls handle to use to re-connect.
70  * @param tc scheduler context
71  */
72 static void
73 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   if (NULL != arm)
76     stop_arm();
77   res = 1;
78 }
79
80
81 static void
82 end (void)
83 {
84   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
85   {
86     GNUNET_SCHEDULER_cancel (endbadly_task);
87     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
88   }
89
90   if (NULL != arm)
91     stop_arm();
92
93   res = 0;
94 }
95
96
97 static void
98 run (void *cls, char *const *args, const char *cfgfile,
99      const struct GNUNET_CONFIGURATION_Handle *cfg)
100 {
101   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
102
103   start_arm (cfgfile);
104   GNUNET_assert (arm != NULL);
105
106   nsh = GNUNET_NAMESTORE_connect (cfg);
107   GNUNET_break (NULL != nsh);
108   GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
109
110   stop_arm ();
111   end ();
112   res = 0;
113 }
114
115 static int
116 check ()
117 {
118   static char *const argv[] = { "test-namestore-api",
119     "-c",
120     "test_namestore_api.conf",
121 #if VERBOSE
122     "-L", "DEBUG",
123 #endif
124     NULL
125   };
126   static struct GNUNET_GETOPT_CommandLineOption options[] = {
127     GNUNET_GETOPT_OPTION_END
128   };
129
130   res = 1;
131   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
132                       "nohelp", options, &run, &res);
133   return res;
134 }
135
136 int
137 main (int argc, char *argv[])
138 {
139   int ret;
140
141   ret = check ();
142
143   return ret;
144 }
145
146 /* end of test_namestore_api.c */