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