-add adv port
[oweals/gnunet.git] / src / testbed / test_testbed_api_operations.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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 testbed/test_testbed_api_operations.c
23  * @brief tests cases for testbed_api_operations.c
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "testbed_api_operations.h"
30
31 /**
32  * Generic logging shortcut
33  */
34 #define LOG(kind,...)                           \
35   GNUNET_log (kind, __VA_ARGS__)
36
37 /**
38  * Queue A
39  */
40 struct OperationQueue *q1;
41
42 /**
43  * Queue B
44  */
45 struct OperationQueue *q2;
46
47 /**
48  * This operation should go into both queues and block op2 until it is done
49  */
50 struct GNUNET_TESTBED_Operation *op1;
51
52 /**
53  * This operation should go into q1 and q2
54  */
55 struct GNUNET_TESTBED_Operation *op2;
56
57
58 /**
59  * Enumeration of test stages
60  */
61 enum Test
62 {
63     /**
64      * Initial stage
65      */
66   TEST_INIT,
67
68     /**
69      * op1 has been started
70      */
71   TEST_OP1_STARTED,
72
73     /**
74      * op1 has been released
75      */
76   TEST_OP1_RELEASED,
77
78     /**
79      * op2 has started
80      */
81   TEST_OP2_STARTED,
82
83     /**
84      * op2 released
85      */
86   TEST_OP2_RELEASED
87 };
88
89 /**
90  * The test result
91  */
92 enum Test result;
93
94
95 /**
96  * Task to simulate artificial delay and change the test stage
97  *
98  * @param cls NULL
99  * @param tc the task context
100  */
101 static void
102 step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 {
104   switch (result)
105   {
106   case TEST_OP1_STARTED:
107     GNUNET_TESTBED_operation_release_ (op1);
108     break;
109   case TEST_OP2_STARTED:
110     GNUNET_TESTBED_operation_release_ (op2);
111     break;
112   default:
113     GNUNET_assert (0);
114   }
115 }
116
117
118 /**
119  * Function to call to start an operation once all
120  * queues the operation is part of declare that the
121  * operation can be activated.
122  */
123 static void
124 start_cb (void *cls)
125 {
126   switch (result)
127   {
128   case TEST_INIT:
129     GNUNET_assert (&op1 == cls);
130     result = TEST_OP1_STARTED;
131     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
132     break;
133   case TEST_OP1_RELEASED:
134     GNUNET_assert (&op2 == cls);
135     result = TEST_OP2_STARTED;
136     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &step, NULL);
137     break;
138   default:
139     GNUNET_assert (0);
140   }
141 }
142
143
144 /**
145  * Function to call to cancel an operation (release all associated
146  * resources).  This can be because of a call to
147  * "GNUNET_TESTBED_operation_cancel" (before the operation generated
148  * an event) or AFTER the operation generated an event due to a call
149  * to "GNUNET_TESTBED_operation_done".  Thus it is not guaranteed that
150  * a callback to the 'OperationStart' preceeds the call to
151  * 'OperationRelease'.  Implementations of this function are expected
152  * to clean up whatever state is in 'cls' and release all resources
153  * associated with the operation.
154  */
155 static void
156 release_cb (void *cls)
157 {
158   switch (result)
159   {
160   case TEST_OP1_STARTED:
161     GNUNET_assert (&op1 == cls);
162     result = TEST_OP1_RELEASED;
163     break;
164   case TEST_OP2_STARTED:
165     GNUNET_assert (&op2 == cls);
166     result = TEST_OP2_RELEASED;
167     GNUNET_TESTBED_operation_queue_destroy_ (q1);
168     GNUNET_TESTBED_operation_queue_destroy_ (q2);
169     break;
170   default:
171     GNUNET_assert (0);
172   }
173 }
174
175
176 /**
177  * Main run function.
178  *
179  * @param cls NULL
180  * @param args arguments passed to GNUNET_PROGRAM_run
181  * @param cfgfile the path to configuration file
182  * @param cfg the configuration file handle
183  */
184 static void
185 run (void *cls, char *const *args, const char *cfgfile,
186      const struct GNUNET_CONFIGURATION_Handle *config)
187 {
188   q1 = GNUNET_TESTBED_operation_queue_create_ (1);
189   GNUNET_assert (NULL != q1);
190   q2 = GNUNET_TESTBED_operation_queue_create_ (2);
191   GNUNET_assert (NULL != q2);
192   op1 = GNUNET_TESTBED_operation_create_ (&op1, start_cb, release_cb);
193   GNUNET_assert (NULL != op1);
194   op2 = GNUNET_TESTBED_operation_create_ (&op2, start_cb, release_cb);
195   GNUNET_TESTBED_operation_queue_insert_ (q1, op1);
196   GNUNET_TESTBED_operation_queue_insert_ (q2, op1);
197   GNUNET_TESTBED_operation_begin_wait_ (op1);
198   GNUNET_TESTBED_operation_queue_insert_ (q1, op2);
199   GNUNET_TESTBED_operation_queue_insert_ (q2, op2);
200   GNUNET_TESTBED_operation_begin_wait_ (op2);
201   result = TEST_INIT;
202 }
203
204 /**
205  * Main function
206  */
207 int
208 main (int argc, char **argv)
209 {
210   int ret;
211   char *const argv2[] =
212       { "test_testbed_api_operations", "-c", "test_testbed_api.conf", NULL };
213   struct GNUNET_GETOPT_CommandLineOption options[] =
214       { GNUNET_GETOPT_OPTION_END };
215
216   ret =
217       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
218                           "test_testbed_api_operations", "nohelp", options,
219                           &run, NULL);
220   if ((GNUNET_OK != ret) || (TEST_OP2_RELEASED != result))
221     return 1;
222   op1 = NULL;
223   op2 = NULL;
224   q1 = NULL;
225   q2 = NULL;
226   return 0;
227 }
228
229 /* end of test_testbed_api_operations.c */