test_datacache_quota.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2006, 2009, 2010 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 datacache/test_datacache_quota.c
  18. * @brief Test for the quota code of the datacache implementations.
  19. * @author Nils Durner
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_datacache_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
  26. __LINE__); goto FAILURE; \
  27. } } while (0)
  28. static int ok;
  29. /**
  30. * Name of plugin under test.
  31. */
  32. static const char *plugin_name;
  33. /**
  34. * Quota is 1 MB. Each iteration of the test puts in about 1 MB of
  35. * data. We do 10 iterations. Afterwards we check that the data from
  36. * the first 5 iterations has all been discarded and that at least
  37. * some of the data from the last iteration is still there.
  38. */
  39. static void
  40. run (void *cls,
  41. char *const *args,
  42. const char *cfgfile,
  43. const struct GNUNET_CONFIGURATION_Handle *cfg)
  44. {
  45. struct GNUNET_DATACACHE_Handle *h;
  46. struct GNUNET_HashCode k;
  47. struct GNUNET_HashCode n;
  48. char buf[3200];
  49. struct GNUNET_TIME_Absolute exp;
  50. (void) cls;
  51. (void) args;
  52. (void) cfgfile;
  53. ok = 0;
  54. h = GNUNET_DATACACHE_create (cfg,
  55. "testcache");
  56. if (h == NULL)
  57. {
  58. fprintf (stderr,
  59. "%s",
  60. "Failed to initialize datacache. Database likely not setup, skipping test.\n");
  61. return;
  62. }
  63. exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
  64. memset (buf, 1, sizeof(buf));
  65. memset (&k, 0, sizeof(struct GNUNET_HashCode));
  66. for (unsigned int i = 0; i < 10; i++)
  67. {
  68. fprintf (stderr,
  69. "%s",
  70. ".");
  71. GNUNET_CRYPTO_hash (&k,
  72. sizeof(struct GNUNET_HashCode),
  73. &n);
  74. for (unsigned int j = i; j < sizeof(buf); j += 10)
  75. {
  76. exp.abs_value_us++;
  77. buf[j] = i;
  78. ASSERT (GNUNET_OK ==
  79. GNUNET_DATACACHE_put (h,
  80. &k,
  81. GNUNET_YES,
  82. j,
  83. buf,
  84. 1 + i,
  85. exp,
  86. 0,
  87. NULL));
  88. ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
  89. }
  90. k = n;
  91. }
  92. fprintf (stderr, "%s", "\n");
  93. memset (&k, 0, sizeof(struct GNUNET_HashCode));
  94. for (unsigned int i = 0; i < 10; i++)
  95. {
  96. fprintf (stderr, "%s", ".");
  97. GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
  98. if (i < 2)
  99. ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
  100. if (i == 9)
  101. ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
  102. k = n;
  103. }
  104. fprintf (stderr, "%s", "\n");
  105. GNUNET_DATACACHE_destroy (h);
  106. return;
  107. FAILURE:
  108. if (h != NULL)
  109. GNUNET_DATACACHE_destroy (h);
  110. ok = GNUNET_SYSERR;
  111. }
  112. int
  113. main (int argc,
  114. char *argv[])
  115. {
  116. char cfg_name[PATH_MAX];
  117. char *const xargv[] = {
  118. "test-datacache-quota",
  119. "-c",
  120. cfg_name,
  121. NULL
  122. };
  123. struct GNUNET_GETOPT_CommandLineOption options[] = {
  124. GNUNET_GETOPT_OPTION_END
  125. };
  126. (void) argc;
  127. GNUNET_log_setup ("test-datacache-quota",
  128. "WARNING",
  129. NULL);
  130. plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
  131. GNUNET_snprintf (cfg_name,
  132. sizeof(cfg_name),
  133. "test_datacache_data_%s.conf",
  134. plugin_name);
  135. GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
  136. xargv,
  137. "test-datacache-quota",
  138. "nohelp",
  139. options,
  140. &run,
  141. NULL);
  142. if (0 != ok)
  143. fprintf (stderr,
  144. "Missed some testcases: %d\n",
  145. ok);
  146. return ok;
  147. }
  148. /* end of test_datacache_quota.c */