transport-testing-filenames.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 (NULL != strstr (pch, "/"))
  37. {
  38. pch = strtok (pch, "/");
  39. while (pch != NULL)
  40. {
  41. pch = strtok (NULL, "/");
  42. if (pch != NULL)
  43. {
  44. filename = pch;
  45. }
  46. }
  47. }
  48. else
  49. filename = pch;
  50. res = GNUNET_strdup (filename);
  51. GNUNET_free (backup);
  52. return res;
  53. }
  54. /**
  55. * Extracts the test filename from an absolute file name and removes
  56. * the extension
  57. *
  58. * @param file absolute file name
  59. * @return the result
  60. */
  61. char *
  62. GNUNET_TRANSPORT_TESTING_get_test_name (const char *file)
  63. {
  64. char *backup = extract_filename (file);
  65. char *filename = backup;
  66. char *dotexe;
  67. char *ret;
  68. if (NULL == filename)
  69. return NULL;
  70. /* remove "lt-" */
  71. filename = strstr (filename, "test");
  72. if (NULL == filename)
  73. {
  74. GNUNET_free (backup);
  75. return NULL;
  76. }
  77. /* remove ".exe" */
  78. if (NULL != (dotexe = strstr (filename, ".exe")))
  79. dotexe[0] = '\0';
  80. ret = GNUNET_strdup (filename);
  81. GNUNET_free (backup);
  82. return ret;
  83. }
  84. /**
  85. * Extracts the filename from an absolute file name and removes the extension
  86. *
  87. * @param file absolute file name
  88. * @return the result
  89. */
  90. char *
  91. GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file)
  92. {
  93. char *src = extract_filename (file);
  94. char *split;
  95. split = strstr (src, ".");
  96. if (NULL != split)
  97. split[0] = '\0';
  98. return src;
  99. }
  100. /**
  101. * Extracts the plugin name from an absolute file name and the test name
  102. *
  103. * @param file absolute file name
  104. * @param test test name
  105. * @return the result
  106. */
  107. char *
  108. GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
  109. const char *test)
  110. {
  111. char *filename;
  112. char *dotexe;
  113. char *e = extract_filename (file);
  114. char *t = extract_filename (test);
  115. char *ret;
  116. if (NULL == e)
  117. goto fail;
  118. /* remove "lt-" */
  119. filename = strstr (e, "tes");
  120. if (NULL == filename)
  121. goto fail;
  122. /* remove ".exe" */
  123. if (NULL != (dotexe = strstr (filename, ".exe")))
  124. dotexe[0] = '\0';
  125. /* find last _ */
  126. filename = strstr (filename, t);
  127. if (NULL == filename)
  128. goto fail;
  129. /* copy plugin */
  130. filename += strlen (t);
  131. if ('\0' != *filename)
  132. filename++;
  133. ret = GNUNET_strdup (filename);
  134. goto suc;
  135. fail:
  136. ret = NULL;
  137. suc:
  138. GNUNET_free (t);
  139. GNUNET_free (e);
  140. return ret;
  141. }
  142. /**
  143. * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
  144. * if existing ".exe"-prefix and adds the peer-number
  145. *
  146. * @param file filename of the test, e.g. argv[0]
  147. * @param count peer number
  148. * @return the result
  149. */
  150. char *
  151. GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
  152. int count)
  153. {
  154. char *filename = extract_filename (file);
  155. char *backup = filename;
  156. char *dotexe;
  157. char *ret;
  158. if (NULL == filename)
  159. return NULL;
  160. /* remove "lt-" */
  161. filename = strstr (filename, "test");
  162. if (NULL == filename)
  163. goto fail;
  164. /* remove ".exe" */
  165. if (NULL != (dotexe = strstr (filename, ".exe")))
  166. dotexe[0] = '\0';
  167. GNUNET_asprintf (&ret,
  168. "%s_peer%u.conf",
  169. filename,
  170. count);
  171. GNUNET_free (backup);
  172. return ret;
  173. fail:
  174. GNUNET_free (backup);
  175. return NULL;
  176. }
  177. /* end of transport-testing-filenames.c */