fix warnings
[oweals/gnunet.git] / src / transport / transport-testing-filenames.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2015, 2016 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file transport-testing-filenames.c
22  * @brief convenience string manipulation functions for tests
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  */
26 #include "transport-testing.h"
27
28
29 /**
30  * Removes all directory separators from absolute filename
31  *
32  * @param file the absolute file name, e.g. as found in argv[0]
33  * @return extracted file name, has to be freed by caller
34  */
35 static char *
36 extract_filename (const char *file)
37 {
38   char *pch = GNUNET_strdup (file);
39   char *backup = pch;
40   char *filename = NULL;
41   char *res;
42
43   if (NULL != strstr (pch, "/"))
44   {
45     pch = strtok (pch, "/");
46     while (pch != NULL)
47     {
48       pch = strtok (NULL, "/");
49       if (pch != NULL)
50       {
51         filename = pch;
52       }
53     }
54   }
55   else
56     filename = pch;
57
58   res = GNUNET_strdup (filename);
59   GNUNET_free (backup);
60   return res;
61 }
62
63
64 /**
65  * Extracts the test filename from an absolute file name and removes
66  * the extension
67  *
68  * @param file absolute file name
69  * @return the result
70  */
71 char *
72 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file)
73 {
74   char *backup = extract_filename (file);
75   char *filename = backup;
76   char *dotexe;
77   char *ret;
78
79   if (NULL == filename)
80     return NULL;
81
82   /* remove "lt-" */
83   filename = strstr (filename, "test");
84   if (NULL == filename)
85   {
86     GNUNET_free (backup);
87     return NULL;
88   }
89
90   /* remove ".exe" */
91   if (NULL != (dotexe = strstr (filename, ".exe")))
92     dotexe[0] = '\0';
93   ret = GNUNET_strdup (filename);
94   GNUNET_free (backup);
95   return ret;
96 }
97
98
99 /**
100  * Extracts the filename from an absolute file name and removes the extension
101  *
102  * @param file absolute file name
103  * @return the result
104  */
105 char *
106 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file)
107 {
108   char *src = extract_filename (file);
109   char *split;
110
111   split = strstr (src, ".");
112   if (NULL != split)
113     split[0] = '\0';
114   return src;
115 }
116
117
118 /**
119  * Extracts the plugin name from an absolute file name and the test name
120  *
121  * @param file absolute file name
122  * @param test test name
123  * @return the result
124  */
125 char *
126 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
127                                                const char *test)
128 {
129   char *filename;
130   char *dotexe;
131   char *e = extract_filename (file);
132   char *t = extract_filename (test);
133   char *ret;
134
135   if (NULL == e)
136     goto fail;
137   /* remove "lt-" */
138   filename = strstr (e, "tes");
139   if (NULL == filename)
140     goto fail;
141   /* remove ".exe" */
142   if (NULL != (dotexe = strstr (filename, ".exe")))
143     dotexe[0] = '\0';
144
145   /* find last _ */
146   filename = strstr (filename, t);
147   if (NULL == filename)
148     goto fail;
149   /* copy plugin */
150   filename += strlen (t);
151   if ('\0' != *filename)
152     filename++;
153   ret = GNUNET_strdup (filename);
154   goto suc;
155 fail:
156   ret = NULL;
157 suc:
158   GNUNET_free (t);
159   GNUNET_free (e);
160   return ret;
161 }
162
163
164 /**
165  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
166  * if existing ".exe"-prefix and adds the peer-number
167  *
168  * @param file filename of the test, e.g. argv[0]
169  * @param count peer number
170  * @return the result
171  */
172 char *
173 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
174                                           int count)
175 {
176   char *filename = extract_filename (file);
177   char *backup = filename;
178   char *dotexe;
179   char *ret;
180
181   if (NULL == filename)
182     return NULL;
183   /* remove "lt-" */
184   filename = strstr (filename, "test");
185   if (NULL == filename)
186     goto fail;
187   /* remove ".exe" */
188   if (NULL != (dotexe = strstr (filename, ".exe")))
189     dotexe[0] = '\0';
190   GNUNET_asprintf (&ret,
191                    "%s_peer%u.conf",
192                    filename,
193                    count);
194   GNUNET_free (backup);
195   return ret;
196 fail:
197   GNUNET_free (backup);
198   return NULL;
199 }
200
201
202 /* end of transport-testing-filenames.c */