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