aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing-filenames.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-09 22:34:49 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-09 22:34:49 +0000
commitc0b81510ffe194dc662c3a6267d5e66a69873c4b (patch)
tree5ad5cc473d6993523be80bcf1430230e224d9216 /src/transport/transport-testing-filenames.c
parenta7345f7436c421fd15e18e87ec399ea93b6dde03 (diff)
downloadgnunet-c0b81510ffe194dc662c3a6267d5e66a69873c4b.tar.gz
gnunet-c0b81510ffe194dc662c3a6267d5e66a69873c4b.zip
-start to refactor testcases for sanity
Diffstat (limited to 'src/transport/transport-testing-filenames.c')
-rw-r--r--src/transport/transport-testing-filenames.c221
1 files changed, 221 insertions, 0 deletions
diff --git a/src/transport/transport-testing-filenames.c b/src/transport/transport-testing-filenames.c
new file mode 100644
index 000000000..0f8076b41
--- /dev/null
+++ b/src/transport/transport-testing-filenames.c
@@ -0,0 +1,221 @@
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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 */
35static char *
36extract_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 */
90char *
91GNUNET_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 */
124char *
125GNUNET_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 */
144char *
145GNUNET_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;
174fail:
175 ret = NULL;
176suc:
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 */
191char *
192GNUNET_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;
215fail:
216 GNUNET_free (backup);
217 return NULL;
218}
219
220
221/* end of transport-testing-filenames.c */