Finished basic connect/disconnect test
[oweals/gnunet.git] / src / mesh / test_mesh_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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 mesh/test_mesh_api.c
23  * @brief test mesh api: dummy test of callbacks
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_mesh_service_new.h"
31
32 static struct GNUNET_MESH_MessageHandler        handlers[] = {{NULL, 0, 0}};
33 static struct GNUNET_OS_Process                 *arm_pid;
34 static struct GNUNET_MESH_Handle                *mesh;
35 static int                                      result;
36 GNUNET_SCHEDULER_TaskIdentifier                 abort_task;
37 GNUNET_SCHEDULER_TaskIdentifier                 test_task;
38
39 static void
40 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
41 {
42     if (0 != abort_task) {
43         GNUNET_SCHEDULER_cancel(abort_task);
44     }
45     if (NULL != mesh) {
46         GNUNET_MESH_disconnect (mesh);
47     }
48     if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) {
49         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
50     }
51     GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
52     GNUNET_OS_process_close (arm_pid);
53 }
54
55 static void
56 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
57 {
58     if (0 != test_task) {
59         GNUNET_SCHEDULER_cancel(test_task);
60     }
61     result = GNUNET_SYSERR;
62     abort_task = 0;
63     do_shutdown(cls, tc);
64 }
65
66 static void
67 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69     struct GNUNET_CONFIGURATION_Handle  *cfg = cls;
70     GNUNET_MESH_ApplicationType         app;
71
72     test_task = 0;
73     app = 0;
74     mesh = GNUNET_MESH_connect(cfg, NULL, NULL, handlers, &app);
75     if(NULL == mesh) {
76         GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to mesh :(\n");
77         return;
78     } else {
79         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO MESH :D\n");
80     }
81
82     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
83                                     GNUNET_TIME_UNIT_SECONDS, 1),
84                                     &do_shutdown,
85                                     NULL);
86 }
87
88
89 static void
90 run (void *cls,
91      char *const *args,
92      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
93 {
94     arm_pid = GNUNET_OS_start_process (NULL, NULL,
95                                        "gnunet-service-arm",
96                                        "gnunet-service-arm",
97                                        "-L", "DEBUG",
98                                        "-c", "test_mesh.conf",
99                                        NULL);
100
101     abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
102                                                 GNUNET_TIME_UNIT_SECONDS, 5),
103                                                 &do_abort,
104                                                 NULL);
105     test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
106                                                 GNUNET_TIME_UNIT_SECONDS, 1),
107                                                 &test,
108                                                 (void *)cfg);
109
110 }
111
112
113 int
114 main (int argc, char *argv[])
115 {
116     int ret;
117     char *const argv2[] = {"test-mesh-api",
118         "-c", "test_mesh.conf",
119         "-L", "DEBUG",
120         NULL
121     };
122     struct GNUNET_GETOPT_CommandLineOption options[] = {
123         GNUNET_GETOPT_OPTION_END
124     };
125       GNUNET_log_setup ("test-dht-api","DEBUG", NULL);
126     ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1,
127                         argv2, "test-mesh-api", "nohelp",
128                         options, &run, NULL);
129
130     if ( GNUNET_OK != ret ) {
131         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
132                    "test-mesh-api': run failed with error code %d\n", ret);
133         return 1;
134     }
135     if ( GNUNET_SYSERR == result ) {
136         GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
137                    "test-mesh-api': test failed\n");
138         return 1;
139     }
140     return 0;
141 }