- communication
[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, 5)
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 (nsh != NULL)
76     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
77   nsh = NULL;
78
79   if (NULL != arm)
80     stop_arm();
81
82   res = 1;
83 }
84
85
86 static void
87 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
90   {
91     GNUNET_SCHEDULER_cancel (endbadly_task);
92     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
93   }
94
95   if (nsh != NULL)
96     GNUNET_NAMESTORE_disconnect (nsh, GNUNET_YES);
97   nsh = NULL;
98
99
100   if (NULL != arm)
101     stop_arm();
102
103   res = 0;
104 }
105
106
107 void name_lookup_proc (void *cls,
108                             const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
109                             struct GNUNET_TIME_Absolute expire,
110                             const char *name,
111                             unsigned int rd_count,
112                             const struct GNUNET_NAMESTORE_RecordData *rd,
113                             const struct GNUNET_CRYPTO_RsaSignature *signature)
114 {
115   res = 0;
116   GNUNET_SCHEDULER_add_now(&end, NULL);
117 }
118
119 static void
120 run (void *cls, char *const *args, const char *cfgfile,
121      const struct GNUNET_CONFIGURATION_Handle *cfg)
122 {
123   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
124
125   start_arm (cfgfile);
126   GNUNET_assert (arm != NULL);
127
128   nsh = GNUNET_NAMESTORE_connect (cfg);
129   GNUNET_break (NULL != nsh);
130
131   GNUNET_NAMESTORE_lookup_record (nsh, NULL, NULL, 0, &name_lookup_proc, NULL);
132 }
133
134 static int
135 check ()
136 {
137   static char *const argv[] = { "test-namestore-api",
138     "-c",
139     "test_namestore_api.conf",
140 #if VERBOSE
141     "-L", "DEBUG",
142 #endif
143     NULL
144   };
145   static struct GNUNET_GETOPT_CommandLineOption options[] = {
146     GNUNET_GETOPT_OPTION_END
147   };
148
149   res = 1;
150   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
151                       "nohelp", options, &run, &res);
152   return res;
153 }
154
155 int
156 main (int argc, char *argv[])
157 {
158   int ret;
159
160   ret = check ();
161
162   return ret;
163 }
164
165 /* end of test_namestore_api.c */