gnunet

Main GNUnet Logic
Log | Files | Refs | Submodules | README | LICENSE

test_plugin_namestore.c (7582B)


      1 /*
      2      This file is part of GNUnet.
      3      Copyright (C) 2012 GNUnet e.V.
      4 
      5      GNUnet is free software: you can redistribute it and/or modify it
      6      under the terms of the GNU Affero General Public License as published
      7      by the Free Software Foundation, either version 3 of the License,
      8      or (at your 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      Affero General Public License for more details.
     14 
     15      You should have received a copy of the GNU Affero General Public License
     16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18      SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @file namestore/test_plugin_namestore.c
     22  * @brief Test for the namestore plugins
     23  * @author Christian Grothoff
     24  */
     25 #include "gnunet_util_lib.h"
     26 #include "gnunet_namestore_plugin.h"
     27 
     28 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
     29 
     30 static int ok;
     31 
     32 /**
     33  * Name of plugin under test.
     34  */
     35 static const char *plugin_name;
     36 
     37 #ifndef DARWIN // #5582
     38 /**
     39  * Function called when the service shuts down.  Unloads our namestore
     40  * plugin.
     41  *
     42  * @param api api to unload
     43  */
     44 static void
     45 unload_plugin (struct GNUNET_NAMESTORE_PluginFunctions *api)
     46 {
     47   char *libname;
     48 
     49   GNUNET_asprintf (&libname, "libgnunet_plugin_namestore_%s", plugin_name);
     50   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
     51   GNUNET_free (libname);
     52 }
     53 
     54 
     55 #endif
     56 
     57 /**
     58  * Load the namestore plugin.
     59  *
     60  * @param cfg configuration to pass
     61  * @return NULL on error
     62  */
     63 static struct GNUNET_NAMESTORE_PluginFunctions *
     64 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
     65 {
     66   struct GNUNET_NAMESTORE_PluginFunctions *ret;
     67   char *libname;
     68 
     69   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     70               "Loading `%s' namestore plugin\n",
     71               plugin_name);
     72   GNUNET_asprintf (&libname,
     73                    "libgnunet_plugin_namestore_%s",
     74                    plugin_name);
     75   if (NULL == (ret = GNUNET_PLUGIN_load (GNUNET_OS_project_data_gnunet (),
     76                                          libname,
     77                                          (void *) cfg)))
     78   {
     79     fprintf (stderr,
     80              "Failed to load plugin `%s'!\n",
     81              plugin_name);
     82     GNUNET_free (libname);
     83     return NULL;
     84   }
     85   GNUNET_free (libname);
     86   if (GNUNET_OK != ret->drop_tables (ret->cls))
     87   {
     88     GNUNET_break (0);
     89     return NULL;
     90   }
     91   if (GNUNET_OK != ret->create_tables (ret->cls))
     92   {
     93     GNUNET_break (0);
     94     return NULL;
     95   }
     96   return ret;
     97 }
     98 
     99 
    100 static void
    101 test_record (void *cls,
    102              uint64_t seq,
    103              const char *editor_hint,
    104              const struct GNUNET_CRYPTO_BlindablePrivateKey *private_key,
    105              const char *label,
    106              unsigned int rd_count,
    107              const struct GNUNET_GNSRECORD_Data *rd)
    108 {
    109   int *idp = cls;
    110   int id = *idp;
    111   struct GNUNET_CRYPTO_BlindablePrivateKey tzone_private_key;
    112   char tname[64];
    113   unsigned int trd_count = 1 + (id % 1024);
    114 
    115   GNUNET_snprintf (tname, sizeof(tname), "a%u", (unsigned int) id);
    116   GNUNET_assert (trd_count == rd_count);
    117   for (unsigned int i = 0; i < trd_count; i++)
    118   {
    119     GNUNET_assert (rd[i].data_size == id % 10);
    120     GNUNET_assert (0 == memcmp ("Hello World", rd[i].data, id % 10));
    121     GNUNET_assert (rd[i].record_type == TEST_RECORD_TYPE);
    122     GNUNET_assert (rd[i].flags == 0);
    123   }
    124   memset (&tzone_private_key, (id % 241), sizeof(tzone_private_key));
    125   GNUNET_assert (0 == strcmp (label, tname));
    126   GNUNET_assert (0 == GNUNET_memcmp (&tzone_private_key, private_key));
    127 }
    128 
    129 
    130 static void
    131 get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
    132 {
    133   GNUNET_assert (
    134     GNUNET_OK ==
    135     nsp->iterate_records (nsp->cls, NULL, 0, 1, &test_record, &id));
    136 }
    137 
    138 
    139 static void
    140 put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id)
    141 {
    142   struct GNUNET_CRYPTO_BlindablePrivateKey zone_private_key;
    143   char label[64];
    144   unsigned int rd_count = 1 + (id % 1024);
    145   struct GNUNET_GNSRECORD_Data rd[GNUNET_NZL (rd_count)];
    146   struct GNUNET_CRYPTO_EcdsaSignature signature;
    147 
    148   GNUNET_snprintf (label, sizeof(label), "a%u", (unsigned int) id);
    149   for (unsigned int i = 0; i < rd_count; i++)
    150   {
    151     rd[i].data = "Hello World";
    152     rd[i].data_size = id % 10;
    153     rd[i].expiration_time =
    154       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES).abs_value_us;
    155     rd[i].record_type = TEST_RECORD_TYPE;
    156     rd[i].flags = 0;
    157   }
    158   memset (&zone_private_key, (id % 241), sizeof(zone_private_key));
    159   memset (&signature, (id % 243), sizeof(signature));
    160   GNUNET_assert (
    161     GNUNET_OK ==
    162     nsp->store_records (nsp->cls, &zone_private_key, label, rd_count, rd));
    163 }
    164 
    165 
    166 static void
    167 edit_iter (void *cls,
    168            uint64_t seq,
    169            const char *editor_hint,
    170            const struct GNUNET_CRYPTO_BlindablePrivateKey *private_key,
    171            const char *label,
    172            unsigned int rd_count,
    173            const struct GNUNET_GNSRECORD_Data *rd)
    174 {
    175   const char*expected_hint = cls;
    176   printf ("Expected hint: `%s', got `%s'\n", expected_hint, editor_hint);
    177   GNUNET_assert (0 == strcmp (expected_hint, editor_hint));
    178 }
    179 
    180 
    181 static void
    182 edit_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id,
    183              const char *new_hint, const char*old_hint)
    184 {
    185   struct GNUNET_CRYPTO_BlindablePrivateKey zone_private_key;
    186   char label[64];
    187 
    188   GNUNET_snprintf (label, sizeof(label), "a%u", (unsigned int) id);
    189   memset (&zone_private_key, (id % 241), sizeof(zone_private_key));
    190   GNUNET_assert (
    191     GNUNET_OK ==
    192     nsp->edit_records (nsp->cls, new_hint, &zone_private_key, label, &edit_iter,
    193                        (void*) old_hint));
    194 }
    195 
    196 
    197 static void
    198 run (void *cls,
    199      char *const *args,
    200      const char *cfgfile,
    201      const struct GNUNET_CONFIGURATION_Handle *cfg)
    202 {
    203   struct GNUNET_NAMESTORE_PluginFunctions *nsp;
    204 
    205   ok = 0;
    206   nsp = load_plugin (cfg);
    207   if (NULL == nsp)
    208   {
    209     fprintf (
    210       stderr,
    211       "%s",
    212       "Failed to initialize namestore.  Database likely not setup, skipping test.\n");
    213     return;
    214   }
    215   put_record (nsp, 1);
    216   get_record (nsp, 1);
    217   edit_record (nsp, 1, "hello", "");
    218   edit_record (nsp, 1, "world", "hello");
    219   edit_record (nsp, 1, "gnunet", "world");
    220 #ifndef DARWIN // #5582
    221   unload_plugin (nsp);
    222 #endif
    223 }
    224 
    225 
    226 int
    227 main (int argc, char *argv[])
    228 {
    229   char cfg_name[PATH_MAX];
    230   char *const xargv[] = {
    231     (char*) "test-plugin-namestore",
    232     (char*) "-c",
    233     cfg_name,
    234     NULL
    235   };
    236   struct GNUNET_GETOPT_CommandLineOption options[] =
    237   { GNUNET_GETOPT_OPTION_END };
    238 
    239   GNUNET_log_setup ("test-plugin-namestore", "WARNING", NULL);
    240   plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]);
    241   GNUNET_snprintf (cfg_name,
    242                    sizeof(cfg_name),
    243                    "test_plugin_namestore_%s.conf",
    244                    plugin_name);
    245   GNUNET_DISK_purge_cfg_dir (GNUNET_OS_project_data_gnunet (),
    246                              cfg_name,
    247                              "GNUNET_TMP");
    248   GNUNET_PROGRAM_run (GNUNET_OS_project_data_gnunet (),
    249                       (sizeof(xargv) / sizeof(char *)) - 1,
    250                       xargv,
    251                       "test-plugin-namestore",
    252                       "nohelp",
    253                       options,
    254                       &run,
    255                       NULL);
    256   GNUNET_DISK_purge_cfg_dir (GNUNET_OS_project_data_gnunet (),
    257                              cfg_name,
    258                              "GNUNET_TMP");
    259   if (ok != 0)
    260     fprintf (stderr, "Missed some testcases: %d\n", ok);
    261   return ok;
    262 }
    263 
    264 
    265 /* end of test_plugin_namestore.c */