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