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