282df6ee2b5e454f5d0a99c0fcf38402615482c7
[oweals/gnunet.git] / src / testing / test_testing_portreservation.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 Affero 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_portreservation.c
18  * @brief test case for testing port reservation routines from the new testing
19  *          library API
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  * Main point of test execution
37  */
38 static void
39 run (void *cls, char *const *args, const char *cfgfile,
40      const struct GNUNET_CONFIGURATION_Handle *cfg)
41 {
42   struct GNUNET_TESTING_System *system;
43   uint16_t new_port1;
44   uint16_t new_port2;
45   uint16_t old_port1;
46
47   system = GNUNET_TESTING_system_create ("/tmp/gnunet-testing-new",
48                                          "localhost", NULL, NULL);
49   GNUNET_assert (NULL != system);
50   new_port1 = GNUNET_TESTING_reserve_port (system);
51   LOG (GNUNET_ERROR_TYPE_DEBUG,
52                 "Reserved TCP port %u\n", new_port1);
53   if (0 == new_port1)
54     goto end;
55   new_port2 = GNUNET_TESTING_reserve_port (system);
56   LOG (GNUNET_ERROR_TYPE_DEBUG,
57                 "Reserved TCP port %u\n", new_port2);
58   if (0 == new_port2)
59     goto end;
60   GNUNET_assert (new_port1 != new_port2);
61   GNUNET_TESTING_release_port (system, new_port1);
62   old_port1 = new_port1;
63   new_port1 = 0;
64   new_port1 = GNUNET_TESTING_reserve_port (system);
65   LOG (GNUNET_ERROR_TYPE_DEBUG,
66        "Reserved TCP port %u\n", new_port1);
67   GNUNET_assert (0 != new_port1);
68   GNUNET_assert (old_port1 == new_port1);
69   GNUNET_TESTING_release_port (system, new_port1);
70   GNUNET_TESTING_release_port (system, new_port2);
71   status = GNUNET_OK;
72
73  end:
74   GNUNET_TESTING_system_destroy (system, GNUNET_YES);
75 }
76
77
78 int main (int argc, char *argv[])
79 {
80   struct GNUNET_GETOPT_CommandLineOption options[] = {
81     GNUNET_GETOPT_OPTION_END
82   };
83
84   status = GNUNET_SYSERR;
85   if (GNUNET_OK !=
86       GNUNET_PROGRAM_run (argc,
87                           argv,
88                           "test_testing_new_portreservation",
89                           "test case for testing port reservation routines"
90                           " from the new testing library API",
91                           options,
92                           &run,
93                           NULL))
94     return 1;
95   return (GNUNET_OK == status) ? 0 : 1;
96 }
97
98 /* end of test_testing_portreservation.c */