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