src: for every AGPL3.0 file, add SPDX identifier.
[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 WINDOWS
44   if ((strlen (pch) >= 3) && pch[1] == ':')
45   {
46     if (NULL != strstr (pch, "\\"))
47     {
48       pch = strtok (pch, "\\");
49       while (pch != NULL)
50       {
51         pch = strtok (NULL, "\\");
52         if (pch != NULL)
53           filename = pch;
54       }
55     }
56   }
57   if (filename != NULL)
58     pch = filename; /* If we miss the next condition, filename = pch will
59                      * not harm us.
60                      */
61 #endif
62   if (NULL != strstr (pch, "/"))
63   {
64     pch = strtok (pch, "/");
65     while (pch != NULL)
66     {
67       pch = strtok (NULL, "/");
68       if (pch != NULL)
69       {
70         filename = pch;
71       }
72     }
73   }
74   else
75     filename = pch;
76
77   res = GNUNET_strdup (filename);
78   GNUNET_free (backup);
79   return res;
80 }
81
82
83 /**
84  * Extracts the test filename from an absolute file name and removes
85  * the extension
86  *
87  * @param file absolute file name
88  * @return the result
89  */
90 char *
91 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file)
92 {
93   char *backup = extract_filename (file);
94   char *filename = backup;
95   char *dotexe;
96   char *ret;
97
98   if (NULL == filename)
99     return NULL;
100
101   /* remove "lt-" */
102   filename = strstr (filename, "test");
103   if (NULL == filename)
104   {
105     GNUNET_free (backup);
106     return NULL;
107   }
108
109   /* remove ".exe" */
110   if (NULL != (dotexe = strstr (filename, ".exe")))
111     dotexe[0] = '\0';
112   ret = GNUNET_strdup (filename);
113   GNUNET_free (backup);
114   return ret;
115 }
116
117
118 /**
119  * Extracts the filename from an absolute file name and removes the extension
120  *
121  * @param file absolute file name
122  * @return the result
123  */
124 char *
125 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file)
126 {
127   char *src = extract_filename (file);
128   char *split;
129
130   split = strstr (src, ".");
131   if (NULL != split)
132     split[0] = '\0';
133   return src;
134 }
135
136
137 /**
138  * Extracts the plugin name from an absolute file name and the test name
139  *
140  * @param file absolute file name
141  * @param test test name
142  * @return the result
143  */
144 char *
145 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
146                                                const char *test)
147 {
148   char *filename;
149   char *dotexe;
150   char *e = extract_filename (file);
151   char *t = extract_filename (test);
152   char *ret;
153
154   if (NULL == e)
155     goto fail;
156   /* remove "lt-" */
157   filename = strstr (e, "tes");
158   if (NULL == filename)
159     goto fail;
160   /* remove ".exe" */
161   if (NULL != (dotexe = strstr (filename, ".exe")))
162     dotexe[0] = '\0';
163
164   /* find last _ */
165   filename = strstr (filename, t);
166   if (NULL == filename)
167     goto fail;
168   /* copy plugin */
169   filename += strlen (t);
170   if ('\0' != *filename)
171     filename++;
172   ret = GNUNET_strdup (filename);
173   goto suc;
174 fail:
175   ret = NULL;
176 suc:
177   GNUNET_free (t);
178   GNUNET_free (e);
179   return ret;
180 }
181
182
183 /**
184  * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
185  * if existing ".exe"-prefix and adds the peer-number
186  *
187  * @param file filename of the test, e.g. argv[0]
188  * @param count peer number
189  * @return the result
190  */
191 char *
192 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
193                                           int count)
194 {
195   char *filename = extract_filename (file);
196   char *backup = filename;
197   char *dotexe;
198   char *ret;
199
200   if (NULL == filename)
201     return NULL;
202   /* remove "lt-" */
203   filename = strstr (filename, "test");
204   if (NULL == filename)
205     goto fail;
206   /* remove ".exe" */
207   if (NULL != (dotexe = strstr (filename, ".exe")))
208     dotexe[0] = '\0';
209   GNUNET_asprintf (&ret,
210                    "%s_peer%u.conf",
211                    filename,
212                    count);
213   GNUNET_free (backup);
214   return ret;
215 fail:
216   GNUNET_free (backup);
217   return NULL;
218 }
219
220
221 /* end of transport-testing-filenames.c */