- cleanup dead code
[oweals/gnunet.git] / src / psyc / test_psyc.c
1 /*
2  * This file is part of GNUnet
3  * (C) 2013 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 psycstore/test_psycstore.c
23  * @brief Test for the PSYCstore service.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_common.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_psyc_service.h"
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
36
37 #define DEBUG_SERVICE 1
38
39
40 /**
41  * Return value from 'main'.
42  */
43 static int res;
44
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46
47 /**
48  * Handle for task for timeout termination.
49  */
50 static GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
51
52 static struct GNUNET_PSYC_Master *mst;
53 static struct GNUNET_PSYC_Slave *slv;
54 static struct GNUNET_PSYC_Channel *ch;
55
56 static struct GNUNET_CRYPTO_EccPrivateKey *channel_key;
57 static struct GNUNET_CRYPTO_EccPrivateKey *slave_key;
58
59 static struct GNUNET_CRYPTO_EccPublicSignKey channel_pub_key;
60 static struct GNUNET_CRYPTO_EccPublicSignKey slave_pub_key;
61
62 /**
63  * Clean up all resources used.
64  */
65 static void
66 cleanup ()
67 {
68   if (mst != NULL)
69   {
70     GNUNET_PSYC_master_stop (mst);
71     mst = NULL;
72   }
73   GNUNET_SCHEDULER_shutdown ();
74 }
75
76
77 /**
78  * Terminate the testcase (failure).
79  *
80  * @param cls NULL
81  * @param tc scheduler context
82  */
83 static void
84 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   res = 1;
87   cleanup ();
88 }
89
90
91 /**
92  * Terminate the testcase (success).
93  *
94  * @param cls NULL
95  * @param tc scheduler context
96  */
97 static void
98 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   res = 0;
101   cleanup ();
102 }
103
104
105 /**
106  * Finish the testcase (successfully).
107  */
108 static void
109 end ()
110 {
111   if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
112   {
113     GNUNET_SCHEDULER_cancel (end_badly_task);
114     end_badly_task = GNUNET_SCHEDULER_NO_TASK;
115   }
116   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
117                                 &end_normally, NULL);
118 }
119
120
121 static int
122 method (void *cls, const struct GNUNET_CRYPTO_EccPublicSignKey *slave_key,
123         uint64_t message_id, const char *method_name,
124         size_t modifier_count, const struct GNUNET_ENV_Modifier *modifiers,
125         uint64_t data_offset, const void *data, size_t data_size,
126         enum GNUNET_PSYC_MessageFlags flags)
127 {
128   return GNUNET_OK;
129 }
130
131
132 static int
133 join (void *cls, const struct GNUNET_CRYPTO_EccPublicSignKey *slave_key,
134       const char *method_name,
135       size_t variable_count, const struct GNUNET_ENV_Modifier *variables,
136       const void *data, size_t data_size, struct GNUNET_PSYC_JoinHandle *jh)
137 {
138   return GNUNET_OK;
139 }
140
141
142 void
143 master_started (void *cls, uint64_t max_message_id)
144 {
145   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Master started: %lu\n", max_message_id);
146 }
147
148
149 void
150 slave_joined (void *cls, uint64_t max_message_id)
151 {
152   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Slave joined: %lu\n", max_message_id);
153 }
154
155
156 /**
157  * Main function of the test, run from scheduler.
158  *
159  * @param cls NULL
160  * @param cfg configuration we use (also to connect to PSYCstore service)
161  * @param peer handle to access more of the peer (not used)
162  */
163 static void
164 #if DEBUG_SERVICE
165 run (void *cls, char *const *args, const char *cfgfile,
166      const struct GNUNET_CONFIGURATION_Handle *c)
167 #else
168 run (void *cls,
169      const struct GNUNET_CONFIGURATION_Handle *c,
170      struct GNUNET_TESTING_Peer *peer)
171 #endif
172 {
173   cfg = c;
174   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
175
176   channel_key = GNUNET_CRYPTO_ecc_key_create ();
177   slave_key = GNUNET_CRYPTO_ecc_key_create ();
178
179   GNUNET_CRYPTO_ecc_key_get_public_for_signature (channel_key, &channel_pub_key);
180   GNUNET_CRYPTO_ecc_key_get_public_for_signature (slave_key, &slave_pub_key);
181
182   mst = GNUNET_PSYC_master_start (cfg, channel_key,
183                                   GNUNET_PSYC_CHANNEL_PRIVATE,
184                                   &method, &join, &master_started, NULL);
185
186   slv = GNUNET_PSYC_slave_join (cfg, &channel_pub_key, slave_key,
187                                 &method, &join, &slave_joined, NULL);
188 }
189
190
191 int
192 main (int argc, char *argv[])
193 {
194   res = 1;
195 #if DEBUG_SERVICE
196   const struct GNUNET_GETOPT_CommandLineOption opts[] = {
197     GNUNET_GETOPT_OPTION_END
198   };
199   if (GNUNET_OK != GNUNET_PROGRAM_run (argc, argv, "test-psyc",
200                                        "test-psyc [options]",
201                                        opts, &run, NULL))
202     return 1;
203 #else
204   if (0 != GNUNET_TESTING_service_run ("test-psyc", "psyc",
205                                        "test_psyc.conf", &run, NULL))
206     return 1;
207 #endif
208   return res;
209 }
210
211 /* end of test_psyc.c */