first batch of license fixes (boring)
[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 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
16 /**
17  * @file testing/test_testing_sharedservices.c
18  * @brief test case for testing service sharing among peers started by testing
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testing_lib.h"
25
26 #define LOG(kind,...)                           \
27   GNUNET_log (kind, __VA_ARGS__)
28
29 #define NUM_PEERS 4
30
31 /**
32  * The status of the test
33  */
34 int status;
35
36 /**
37  * The testing context
38  */
39 struct TestingContext
40 {
41   /**
42    * The testing system
43    */
44   struct GNUNET_TESTING_System *system;
45
46   /**
47    * The peer which has been started by the testing system
48    */
49   struct GNUNET_TESTING_Peer *peers[NUM_PEERS];
50
51   /**
52    * The running configuration of the peer
53    */
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55 };
56
57
58 /**
59  * Task for shutdown
60  *
61  * @param cls the testing context
62  */
63 static void
64 do_shutdown (void *cls)
65 {
66   struct TestingContext *test_ctx = cls;
67   struct GNUNET_TESTING_Peer *peer;
68   unsigned int cnt;
69
70   GNUNET_assert (NULL != test_ctx);
71   for (cnt = 0; cnt < NUM_PEERS; cnt++)
72   {
73     peer = test_ctx->peers[cnt];
74     if (NULL == peer)
75       continue;
76     (void) GNUNET_TESTING_peer_stop (peer);
77     GNUNET_TESTING_peer_destroy (peer);
78   }
79   if (NULL != test_ctx->cfg)
80     GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
81   if (NULL != test_ctx->system)
82     GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
83   GNUNET_free (test_ctx);
84 }
85
86
87 /**
88  * Main point of test execution
89  */
90 static void
91 run (void *cls, char *const *args, const char *cfgfile,
92      const struct GNUNET_CONFIGURATION_Handle *cfg)
93 {
94   struct TestingContext *test_ctx;
95   char *emsg;
96   struct GNUNET_PeerIdentity id;
97   struct GNUNET_TESTING_SharedService ss[] = {
98     {"peerinfo", cfg, 2},
99     {NULL, NULL, 0}
100   };
101   struct GNUNET_TESTING_Peer *peer;
102   unsigned int cnt;
103
104   test_ctx = GNUNET_new (struct TestingContext);
105   test_ctx->system =
106       GNUNET_TESTING_system_create ("test-gnunet-testing",
107                                     "127.0.0.1", NULL, ss);
108   emsg = NULL;
109   if (NULL == test_ctx->system)
110     goto end;
111   test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
112   for (cnt = 0; cnt < NUM_PEERS; cnt++)
113   {
114     peer = GNUNET_TESTING_peer_configure (test_ctx->system,
115                                           test_ctx->cfg,
116                                           0, &id, &emsg);
117     if (NULL == peer)
118     {
119       if (NULL != emsg)
120         printf ("Test failed upon error: %s", emsg);
121       goto end;
122     }
123     if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
124     {
125       GNUNET_TESTING_peer_destroy (peer);
126       goto end;
127     }
128     test_ctx->peers[cnt] = peer;
129   }
130   status = GNUNET_OK;
131   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
132                                 &do_shutdown, test_ctx);
133   return;
134
135  end:
136   GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
137   GNUNET_free_non_null (emsg);
138 }
139
140
141 int main (int argc, char *argv[])
142 {
143   struct GNUNET_GETOPT_CommandLineOption options[] = {
144     GNUNET_GETOPT_OPTION_END
145   };
146   char *const argv2[] = { "test_testing_sharedservices",
147     "-c", "test_testing_sharedservices.conf",
148     NULL
149   };
150
151   status = GNUNET_SYSERR;
152   if (GNUNET_OK !=
153       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
154                           "test_testing_sharedservices",
155                           "test case for testing service sharing among peers started by testing",
156                           options, &run, NULL))
157     return 1;
158   return (GNUNET_OK == status) ? 0 : 3;
159 }
160
161 /* end of test_testing_sharedservices.c */