transport-testing-filenames.c 4.9 KB

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