fix bad free
[oweals/gnunet.git] / src / testing / test_testing_peerstartup.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_new_peerstartup.c
21  * @brief test case for testing peer startup and shutdown using new testing
22  *          library
23  * @author Sree Harsha Totakura
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29
30 #define LOG(kind,...)                           \
31   GNUNET_log (kind, __VA_ARGS__)
32
33 /**
34  * The status of the test
35  */
36 int status;
37
38 /**
39  * The testing context
40  */
41 struct TestingContext
42 {
43   /**
44    * The testing system
45    */
46   struct GNUNET_TESTING_System *system;
47
48   /**
49    * The peer which has been started by the testing system
50    */
51   struct GNUNET_TESTING_Peer *peer;
52
53   /**
54    * The running configuration of the peer
55    */
56   struct GNUNET_CONFIGURATION_Handle *cfg;
57 };
58
59
60 /**
61  * Task for shutdown
62  *
63  * @param cls the testing context
64  */
65 static void
66 do_shutdown (void *cls)
67 {
68   struct TestingContext *test_ctx = cls;
69
70   GNUNET_assert (NULL != test_ctx);
71   if (NULL != test_ctx->peer)
72   {
73     (void) GNUNET_TESTING_peer_stop (test_ctx->peer);
74     GNUNET_TESTING_peer_destroy (test_ctx->peer);
75   }
76   if (NULL != test_ctx->cfg)
77     GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
78   if (NULL != test_ctx->system)
79     GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
80   GNUNET_free (test_ctx);
81 }
82
83
84 /**
85  * Main point of test execution
86  */
87 static void
88 run (void *cls, char *const *args, const char *cfgfile,
89      const struct GNUNET_CONFIGURATION_Handle *cfg)
90 {
91   struct TestingContext *test_ctx;
92   char *emsg;
93   struct GNUNET_PeerIdentity id;
94
95   test_ctx = GNUNET_new (struct TestingContext);
96   test_ctx->system =
97       GNUNET_TESTING_system_create ("test-gnunet-testing",
98                                     "127.0.0.1", NULL, NULL);
99   emsg = NULL;
100   if (NULL == test_ctx->system)
101     goto end;
102   test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
103   test_ctx->peer =
104       GNUNET_TESTING_peer_configure (test_ctx->system,
105                                      test_ctx->cfg,
106                                      0, &id, &emsg);
107   if (NULL == test_ctx->peer)
108   {
109     if (NULL != emsg)
110       printf ("Test failed upon error: %s", emsg);
111     goto end;
112   }
113   if (GNUNET_OK != GNUNET_TESTING_peer_start (test_ctx->peer))
114     goto end;
115   status = GNUNET_OK;
116
117  end:
118   GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
119   GNUNET_free_non_null (emsg);
120 }
121
122
123 int main (int argc, char *argv[])
124 {
125   struct GNUNET_GETOPT_CommandLineOption options[] = {
126     GNUNET_GETOPT_OPTION_END
127   };
128
129   status = GNUNET_SYSERR;
130   if (GNUNET_OK !=
131       GNUNET_PROGRAM_run (argc, argv,
132                           "test_testing_peerstartup",
133                           "test case for peerstartup using new testing library",
134                           options, &run, NULL))
135     return 1;
136   return (GNUNET_OK == status) ? 0 : 1;
137 }
138
139 /* end of test_testing_peerstartup.c */