test_env.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * This file is part of GNUnet.
  3. * (C) 2013 Christian Grothoff (and other contributing authors)
  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., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. */
  20. /**
  21. * @file env/test_env.c
  22. * @brief Tests for the environment library.
  23. * @author Gabor X Toth
  24. */
  25. #include "platform.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_testing_lib.h"
  28. #include "gnunet_env_lib.h"
  29. struct GNUNET_ENV_Modifier mods[] = {
  30. { .oper = GNUNET_ENV_OP_SET,
  31. .name = "_foo", .value = "foo", .value_size = 3 },
  32. { .oper = GNUNET_ENV_OP_ASSIGN,
  33. .name = "_foo_bar", .value = "foo bar", .value_size = 7 },
  34. { .oper = GNUNET_ENV_OP_AUGMENT,
  35. .name = "_foo_bar_baz", .value = "foo bar baz", .value_size = 11 }
  36. };
  37. struct ItCls
  38. {
  39. size_t n;
  40. };
  41. int
  42. iterator (void *cls, enum GNUNET_ENV_Operator oper,
  43. const char *name, const char *value, uint32_t value_size)
  44. {
  45. struct ItCls *it_cls = cls;
  46. struct GNUNET_ENV_Modifier *m = &mods[it_cls->n++];
  47. GNUNET_assert (oper == m->oper);
  48. GNUNET_assert (value_size == m->value_size);
  49. GNUNET_assert (0 == memcmp (name, m->name, strlen (m->name)));
  50. GNUNET_assert (0 == memcmp (value, m->value, m->value_size));
  51. return GNUNET_YES;
  52. }
  53. int
  54. main (int argc, char *argv[])
  55. {
  56. GNUNET_log_setup ("test-env", "WARNING", NULL);
  57. struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
  58. GNUNET_assert (NULL != env);
  59. int i, len = 3;
  60. for (i = 0; i < len; i++)
  61. {
  62. GNUNET_ENV_environment_add (env, mods[i].oper, mods[i].name,
  63. mods[i].value, mods[i].value_size);
  64. }
  65. struct ItCls it_cls = { .n = 0 };
  66. GNUNET_ENV_environment_iterate (env, iterator, &it_cls);
  67. GNUNET_assert (len == it_cls.n);
  68. for (i = 0; i < len; i++)
  69. {
  70. enum GNUNET_ENV_Operator oper;
  71. const char *name;
  72. const void *value;
  73. size_t value_size;
  74. GNUNET_ENV_environment_shift (env, &oper, &name, &value, &value_size);
  75. GNUNET_assert (len - i - 1 == GNUNET_ENV_environment_get_count (env));
  76. }
  77. GNUNET_ENV_environment_destroy (env);
  78. return 0;
  79. }