-trying to fix #2391: limit connect rate from topology
[oweals/gnunet.git] / src / testing / test_testing_new_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 #define TIME_REL_SEC(sec)                                       \
37   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
38
39 /**
40  * The testing context
41  */
42 struct TestingContext
43 {
44   /**
45    * The testing system
46    */
47   struct GNUNET_TESTING_System *system;
48   
49   /**
50    * The peer which has been started by the testing system
51    */
52   struct GNUNET_TESTING_Peer *peer;
53
54   /**
55    * The running configuration of the peer
56    */
57   struct GNUNET_CONFIGURATION_Handle *cfg;
58 };
59
60
61 /**
62  * Task for shutdown
63  *
64  * @param cls the testing context
65  * @param tc the tast context
66  */
67 static void
68 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   struct TestingContext *test_ctx = cls;
71   
72   GNUNET_assert (GNUNET_OK == GNUNET_TESTING_peer_stop (test_ctx->peer));
73   GNUNET_TESTING_peer_destroy (test_ctx->peer);
74   GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
75   GNUNET_TESTING_hostkeys_unload (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 GNUNET_TESTING_System *system;
89   struct GNUNET_TESTING_Peer *peer;
90   struct GNUNET_CONFIGURATION_Handle *new_cfg;
91   struct TestingContext *test_ctx;
92   char *data_dir;
93   char *hostkeys_file;
94   char *emsg;
95   char *_tmpdir;
96   char *tmpdir;
97 #ifdef MINGW
98   char *tmpdir_w;
99 #endif
100
101   struct GNUNET_PeerIdentity id;
102     
103   _tmpdir = getenv ("TMP");
104   if (NULL == _tmpdir)
105     _tmpdir = getenv ("TEMP");  
106   if (NULL == _tmpdir)
107     _tmpdir = getenv ("TMPDIR");
108   if (NULL == _tmpdir)
109     _tmpdir = "/tmp";
110   GNUNET_asprintf (&tmpdir, "%s/%s", _tmpdir, "test-gnunet-testing_new-XXXXXX");  
111 #ifdef MINGW
112   tmpdir_w = GNUNET_malloc (MAX_PATH + 1);
113   GNUNET_assert (ERROR_SUCCESS == plibc_conv_to_win_path (tmpdir, tmpdir_w));
114   GNUNET_free (tmpdir);
115   tmpdir = tmpdir_w;
116   //GNUNET_assert (0 == _mktemp_s (tmpdir, strlen (tmpdir) + 1));
117 #else
118   GNUNET_assert (mkdtemp (tmpdir) == tmpdir);
119 #endif
120   /* LOG (GNUNET_ERROR_TYPE_ERROR, */
121   /*      "Temporary directory: %s\n", tmpdir); */
122   system = GNUNET_TESTING_system_create (tmpdir,
123                                          "localhost");
124   GNUNET_assert (NULL != system);
125   GNUNET_free (tmpdir);
126   data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
127   GNUNET_asprintf (&hostkeys_file, "%s/testing_hostkeys.dat", data_dir);
128   GNUNET_free (data_dir);  
129   GNUNET_assert (GNUNET_OK == 
130                  GNUNET_TESTING_hostkeys_load (system, hostkeys_file));
131   GNUNET_free (hostkeys_file);
132   new_cfg = GNUNET_CONFIGURATION_dup (cfg);
133   emsg = NULL;
134   peer = GNUNET_TESTING_peer_configure (system, new_cfg, 0, &id, &emsg);
135   GNUNET_assert (NULL != peer);
136   GNUNET_assert (NULL == emsg);
137   GNUNET_assert (GNUNET_OK == GNUNET_TESTING_peer_start (peer));
138   test_ctx = GNUNET_malloc (sizeof (struct TestingContext));
139   test_ctx->system = system;
140   test_ctx->peer = peer;
141   test_ctx->cfg = new_cfg;
142   GNUNET_SCHEDULER_add_delayed (TIME_REL_SEC (5),
143                                 &do_shutdown, test_ctx);
144   
145 }
146
147
148 int main (int argc, char *argv[])
149 {
150   struct GNUNET_GETOPT_CommandLineOption options[] = {
151     GNUNET_GETOPT_OPTION_END
152   };
153
154   if (GNUNET_OK !=
155       GNUNET_PROGRAM_run (argc, argv,
156                           "test_testing_new_peerstartup",
157                           "test case for peerstartup using new testing library",
158                           options, &run, NULL))
159     return 1;
160   return 0;
161 }