paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / testing / test_testing_sharedservices.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008, 2009, 2012 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file testing/test_testing_sharedservices.c
21  * @brief test case for testing service sharing among peers started by testing
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28
29 #define LOG(kind,...)                           \
30   GNUNET_log (kind, __VA_ARGS__)
31
32 #define NUM_PEERS 4
33
34 /**
35  * The status of the test
36  */
37 int status;
38
39 /**
40  * The testing context
41  */
42 struct TestingContext
43 {
44   /**
45    * The testing system
46    */
47   struct GNUNET_TESTING_System *system;
48
49   /**
50    * The peer which has been started by the testing system
51    */
52   struct GNUNET_TESTING_Peer *peers[NUM_PEERS];
53
54   /**
55    * The running configuration of the peer
56    */
57   struct GNUNET_CONFIGURATION_Handle *cfg;
58 };
59
60
61 /**
62  * Task for shutdown
63  *
64  * @param cls the testing context
65  */
66 static void
67 do_shutdown (void *cls)
68 {
69   struct TestingContext *test_ctx = cls;
70   struct GNUNET_TESTING_Peer *peer;
71   unsigned int cnt;
72
73   GNUNET_assert (NULL != test_ctx);
74   for (cnt = 0; cnt < NUM_PEERS; cnt++)
75   {
76     peer = test_ctx->peers[cnt];
77     if (NULL == peer)
78       continue;
79     (void) GNUNET_TESTING_peer_stop (peer);
80     GNUNET_TESTING_peer_destroy (peer);
81   }
82   if (NULL != test_ctx->cfg)
83     GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
84   if (NULL != test_ctx->system)
85     GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
86   GNUNET_free (test_ctx);
87 }
88
89
90 /**
91  * Main point of test execution
92  */
93 static void
94 run (void *cls, char *const *args, const char *cfgfile,
95      const struct GNUNET_CONFIGURATION_Handle *cfg)
96 {
97   struct TestingContext *test_ctx;
98   char *emsg;
99   struct GNUNET_PeerIdentity id;
100   struct GNUNET_TESTING_SharedService ss[] = {
101     {"peerinfo", cfg, 2},
102     {NULL, NULL, 0}
103   };
104   struct GNUNET_TESTING_Peer *peer;
105   unsigned int cnt;
106
107   test_ctx = GNUNET_new (struct TestingContext);
108   test_ctx->system =
109       GNUNET_TESTING_system_create ("test-gnunet-testing",
110                                     "127.0.0.1", NULL, ss);
111   emsg = NULL;
112   if (NULL == test_ctx->system)
113     goto end;
114   test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
115   for (cnt = 0; cnt < NUM_PEERS; cnt++)
116   {
117     peer = GNUNET_TESTING_peer_configure (test_ctx->system,
118                                           test_ctx->cfg,
119                                           0, &id, &emsg);
120     if (NULL == peer)
121     {
122       if (NULL != emsg)
123         printf ("Test failed upon error: %s", emsg);
124       goto end;
125     }
126     if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
127     {
128       GNUNET_TESTING_peer_destroy (peer);
129       goto end;
130     }
131     test_ctx->peers[cnt] = peer;
132   }
133   status = GNUNET_OK;
134   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
135                                 &do_shutdown, test_ctx);
136   return;
137
138  end:
139   GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
140   GNUNET_free_non_null (emsg);
141 }
142
143
144 int main (int argc, char *argv[])
145 {
146   struct GNUNET_GETOPT_CommandLineOption options[] = {
147     GNUNET_GETOPT_OPTION_END
148   };
149   char *const argv2[] = { "test_testing_sharedservices",
150     "-c", "test_testing_sharedservices.conf",
151     NULL
152   };
153
154   status = GNUNET_SYSERR;
155   if (GNUNET_OK !=
156       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
157                           "test_testing_sharedservices",
158                           "test case for testing service sharing among peers started by testing",
159                           options, &run, NULL))
160     return 1;
161   return (GNUNET_OK == status) ? 0 : 3;
162 }
163
164 /* end of test_testing_sharedservices.c */