PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/rps/rps-test_util.c

https://gitlab.com/taler/gnunet
C | 197 lines | 135 code | 34 blank | 28 comment | 21 complexity | 802e511be311467fb3c434a23b4f5d5a MD5 | raw file
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file rps/rps-test_util.c
  19. * @brief Some utils faciliating the view into the internals for the sampler
  20. * needed for evaluation
  21. *
  22. * @author Julius Bünger
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include <inttypes.h>
  27. #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
  28. #ifndef TO_FILE
  29. #define TO_FILE
  30. #endif /* TO_FILE */
  31. #ifdef TO_FILE
  32. void
  33. to_file_ (char *file_name, char *line)
  34. {
  35. struct GNUNET_DISK_FileHandle *f;
  36. char output_buffer[512];
  37. //size_t size;
  38. int size;
  39. size_t size2;
  40. if (NULL == (f = GNUNET_DISK_file_open (file_name,
  41. GNUNET_DISK_OPEN_APPEND |
  42. GNUNET_DISK_OPEN_WRITE |
  43. GNUNET_DISK_OPEN_CREATE,
  44. GNUNET_DISK_PERM_USER_READ |
  45. GNUNET_DISK_PERM_USER_WRITE |
  46. GNUNET_DISK_PERM_GROUP_READ |
  47. GNUNET_DISK_PERM_OTHER_READ)))
  48. {
  49. LOG (GNUNET_ERROR_TYPE_WARNING,
  50. "Not able to open file %s\n",
  51. file_name);
  52. return;
  53. }
  54. size = GNUNET_snprintf (output_buffer,
  55. sizeof (output_buffer),
  56. "%llu %s\n",
  57. GNUNET_TIME_absolute_get ().abs_value_us,
  58. line);
  59. if (0 > size)
  60. {
  61. LOG (GNUNET_ERROR_TYPE_WARNING,
  62. "Failed to write string to buffer (size: %i)\n",
  63. size);
  64. return;
  65. }
  66. size2 = GNUNET_DISK_file_write (f, output_buffer, size);
  67. if (size != size2)
  68. {
  69. LOG (GNUNET_ERROR_TYPE_WARNING,
  70. "Unable to write to file! (Size: %u, size2: %u)\n",
  71. size,
  72. size2);
  73. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  74. LOG (GNUNET_ERROR_TYPE_WARNING,
  75. "Unable to close file\n");
  76. return;
  77. }
  78. if (GNUNET_YES != GNUNET_DISK_file_close (f))
  79. LOG (GNUNET_ERROR_TYPE_WARNING,
  80. "Unable to close file\n");
  81. }
  82. char *
  83. auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
  84. {
  85. int size;
  86. size_t name_buf_size;
  87. char *end;
  88. char *buf;
  89. char *name_buf;
  90. size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
  91. name_buf_size = 512 * sizeof (char);
  92. name_buf = GNUNET_malloc (name_buf_size);
  93. if (keylen % 5 > 0)
  94. keylen += 5 - keylen % 5;
  95. keylen /= 5;
  96. buf = GNUNET_malloc (keylen + 1);
  97. end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
  98. sizeof (struct GNUNET_CRYPTO_AuthKey),
  99. buf,
  100. keylen);
  101. if (NULL == end)
  102. {
  103. GNUNET_free (buf);
  104. GNUNET_break (0);
  105. }
  106. else
  107. {
  108. *end = '\0';
  109. }
  110. size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
  111. if (0 > size)
  112. LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
  113. GNUNET_free (buf);
  114. return name_buf;
  115. }
  116. struct GNUNET_CRYPTO_AuthKey
  117. string_to_auth_key (const char *str)
  118. {
  119. struct GNUNET_CRYPTO_AuthKey auth_key;
  120. if (GNUNET_OK !=
  121. GNUNET_STRINGS_string_to_data (str,
  122. strlen (str),
  123. &auth_key.key,
  124. sizeof (struct GNUNET_CRYPTO_AuthKey)))
  125. {
  126. LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
  127. }
  128. return auth_key;
  129. }
  130. char *
  131. create_file (const char *name)
  132. {
  133. int size;
  134. size_t name_buf_size;
  135. char *name_buf;
  136. char *prefix;
  137. char *file_name;
  138. prefix = "/tmp/rps/";
  139. name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
  140. name_buf = GNUNET_malloc (name_buf_size);
  141. size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
  142. if (0 > size)
  143. LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
  144. if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
  145. {
  146. LOG (GNUNET_ERROR_TYPE_WARNING,
  147. "Could not create directory %s.\n",
  148. prefix);
  149. }
  150. if (NULL == strstr (name, "sampler_el"))
  151. {/* only append random string to sampler */
  152. if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
  153. LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
  154. GNUNET_free (name_buf);
  155. return file_name;
  156. }
  157. return name_buf;
  158. }
  159. #endif /* TO_FILE */
  160. /* end of gnunet-service-rps.c */