- lookup API send recv
[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 (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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "name_lookup_proc %p `%s' %i %p %p\n", zone_key, name, rd_count, rd, signature);
116   res = 0;
117   GNUNET_SCHEDULER_add_now(&end, NULL);
118 }
119
120 static void
121 run (void *cls, char *const *args, const char *cfgfile,
122      const struct GNUNET_CONFIGURATION_Handle *cfg)
123 {
124   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT,endbadly, NULL);
125
126   GNUNET_HashCode zone;
127   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &zone);
128   char * name = "dummy.dummy.gnunet";
129
130   start_arm (cfgfile);
131   GNUNET_assert (arm != NULL);
132
133   nsh = GNUNET_NAMESTORE_connect (cfg);
134   GNUNET_break (NULL != nsh);
135
136   GNUNET_NAMESTORE_lookup_record (nsh, &zone, name, 0, &name_lookup_proc, NULL);
137 }
138
139 static int
140 check ()
141 {
142   static char *const argv[] = { "test-namestore-api",
143     "-c",
144     "test_namestore_api.conf",
145 #if VERBOSE
146     "-L", "DEBUG",
147 #endif
148     NULL
149   };
150   static struct GNUNET_GETOPT_CommandLineOption options[] = {
151     GNUNET_GETOPT_OPTION_END
152   };
153
154   res = 1;
155   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
156                       "nohelp", options, &run, &res);
157   return res;
158 }
159
160 int
161 main (int argc, char *argv[])
162 {
163   int ret;
164
165   ret = check ();
166
167   return ret;
168 }
169
170 /* end of test_namestore_api.c */