From f6884a301a25d5038b0eeebaa4678eb164dc395b Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 22 Feb 2012 22:34:13 +0000 Subject: [PATCH] -extended testcase plus misc fixes --- src/namestore/plugin_namestore_sqlite.c | 81 ++--- src/namestore/test_plugin_namestore.c | 293 +++++++++++++++++- .../test_plugin_namestore_sqlite.conf | 2 +- 3 files changed, 336 insertions(+), 40 deletions(-) diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c index 6b96977c0..a03f8cc37 100644 --- a/src/namestore/plugin_namestore_sqlite.c +++ b/src/namestore/plugin_namestore_sqlite.c @@ -244,6 +244,9 @@ database_setup (struct Plugin *plugin) CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL)); + CHECK (SQLITE_OK == + sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL, + NULL, ENULL)); CHECK (SQLITE_OK == sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, ENULL)); @@ -266,9 +269,9 @@ database_setup (struct Plugin *plugin) (sqlite3_exec (plugin->dbh, "CREATE TABLE ns090records (" - " zone_hash TEXT NOT NULL DEFAULT ''," + " zone_hash BLOB NOT NULL DEFAULT ''," " zone_revision INT4 NOT NULL DEFAULT 0," - " record_name_hash TEXT NOT NULL DEFAULT ''," + " record_name_hash BLOB NOT NULL DEFAULT ''," " record_name TEXT NOT NULL DEFAULT ''," " record_type INT4 NOT NULL DEFAULT 0," " node_location_depth INT4 NOT NULL DEFAULT 0," @@ -293,7 +296,7 @@ database_setup (struct Plugin *plugin) (sqlite3_exec (plugin->dbh, "CREATE TABLE ns090nodes (" - " zone_hash TEXT NOT NULL DEFAULT ''," + " zone_hash BLOB NOT NULL DEFAULT ''," " zone_revision INT4 NOT NULL DEFAULT 0," " node_location_depth INT4 NOT NULL DEFAULT 0," " node_location_offset INT8 NOT NULL DEFAULT 0," @@ -317,10 +320,10 @@ database_setup (struct Plugin *plugin) (sqlite3_exec (plugin->dbh, "CREATE TABLE ns090signatures (" - " zone_hash TEXT NOT NULL DEFAULT ''," + " zone_hash BLOB NOT NULL DEFAULT ''," " zone_revision INT4 NOT NULL DEFAULT 0," " zone_time INT8 NOT NULL DEFAULT 0," - " zone_root_hash TEXT NOT NULL DEFAULT 0," + " zone_root_hash BLOB NOT NULL DEFAULT 0," " zone_root_depth INT4 NOT NULL DEFAULT 0," " zone_public_key BLOB NOT NULL DEFAULT 0," " zone_signature BLOB NOT NULL DEFAULT 0" @@ -480,7 +483,7 @@ namestore_sqlite_put_record (void *cls, if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) || (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 2, loc->revision)) || (SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 3, &nh, sizeof (GNUNET_HashCode), SQLITE_STATIC)) || - (SQLITE_OK != sqlite3_bind_text (plugin->put_record, 4, name, name_len, SQLITE_STATIC)) || + (SQLITE_OK != sqlite3_bind_text (plugin->put_record, 4, name, -1, SQLITE_STATIC)) || (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 5, record_type)) || (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 6, loc->depth)) || (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 7, loc->offset)) || @@ -607,6 +610,7 @@ namestore_sqlite_iterate_records (void *cls, { struct Plugin *plugin = cls; unsigned int ret; + int sret; struct GNUNET_TIME_Absolute expiration; uint32_t record_type; enum GNUNET_NAMESTORE_RecordFlags flags; @@ -615,8 +619,12 @@ namestore_sqlite_iterate_records (void *cls, struct GNUNET_NAMESTORE_SignatureLocation loc; const char *name; - if ((SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) || - (SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 2, name_hash, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ) + if ((SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 1, + zone, sizeof (GNUNET_HashCode), + SQLITE_STATIC)) || + (SQLITE_OK != sqlite3_bind_blob (plugin->iterate_records, 2, + name_hash, sizeof (GNUNET_HashCode), + SQLITE_STATIC)) ) { LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); @@ -625,28 +633,27 @@ namestore_sqlite_iterate_records (void *cls, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); return GNUNET_SYSERR; - } ret = 0; - while (SQLITE_ROW == (ret = sqlite3_step (plugin->iterate_records))) + while (SQLITE_ROW == (sret = sqlite3_step (plugin->iterate_records))) { ret++; if (NULL == iter) continue; /* FIXME: just counting can be done more cheaply... */ - loc.revision = sqlite3_column_int (plugin->iterate_records, 1); - name = (const char*) sqlite3_column_text (plugin->iterate_records, 2); - record_type = sqlite3_column_int (plugin->iterate_records, 3); - loc.depth = sqlite3_column_int (plugin->iterate_records, 4); - loc.offset = sqlite3_column_int64 (plugin->iterate_records, 5); - expiration.abs_value = (uint64_t) sqlite3_column_int64 (plugin->iterate_records, 6); - flags = (enum GNUNET_NAMESTORE_RecordFlags) sqlite3_column_int (plugin->iterate_records, 7); - data = sqlite3_column_blob (plugin->iterate_records, 8); - data_size = sqlite3_column_bytes (plugin->iterate_records, 8); + loc.revision = sqlite3_column_int (plugin->iterate_records, 0); + name = (const char*) sqlite3_column_text (plugin->iterate_records, 1); + record_type = sqlite3_column_int (plugin->iterate_records, 2); + loc.depth = sqlite3_column_int (plugin->iterate_records, 3); + loc.offset = sqlite3_column_int64 (plugin->iterate_records, 4); + expiration.abs_value = (uint64_t) sqlite3_column_int64 (plugin->iterate_records, 5); + flags = (enum GNUNET_NAMESTORE_RecordFlags) sqlite3_column_int (plugin->iterate_records, 6); + data = sqlite3_column_blob (plugin->iterate_records, 7); + data_size = sqlite3_column_bytes (plugin->iterate_records, 7); iter (iter_cls, zone, &loc, name, record_type, expiration, flags, data_size, data); } - if (SQLITE_DONE != ret) + if (SQLITE_DONE != sret) LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); sqlite3_finalize (plugin->iterate_records); return ret; @@ -698,10 +705,10 @@ namestore_sqlite_get_node (void *cls, ret = GNUNET_NO; if (SQLITE_ROW == (ret = sqlite3_step (plugin->get_node))) { - ploc.offset = sqlite3_column_int64 (plugin->get_node, 1); - rloc.offset = sqlite3_column_int64 (plugin->get_node, 2); - hashcodes = sqlite3_column_blob (plugin->get_node, 3); - hashcodes_size = sqlite3_column_bytes (plugin->get_node, 3); + ploc.offset = sqlite3_column_int64 (plugin->get_node, 0); + rloc.offset = sqlite3_column_int64 (plugin->get_node, 1); + hashcodes = sqlite3_column_blob (plugin->get_node, 2); + hashcodes_size = sqlite3_column_bytes (plugin->get_node, 2); if (0 != (hashcodes_size % sizeof (GNUNET_HashCode))) { GNUNET_break (0); @@ -718,7 +725,7 @@ namestore_sqlite_get_node (void *cls, hashcodes); } } - if (SQLITE_DONE != ret) + else if (SQLITE_DONE != ret) LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); sqlite3_finalize (plugin->get_node); return ret; @@ -748,7 +755,7 @@ namestore_sqlite_get_signature (void *cls, const GNUNET_HashCode *top_hash; GNUNET_assert (NULL != cb); - if ((SQLITE_OK != sqlite3_bind_blob (plugin->get_signature, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC))) + if (SQLITE_OK != sqlite3_bind_blob (plugin->get_signature, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) { LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); @@ -762,16 +769,16 @@ namestore_sqlite_get_signature (void *cls, if (SQLITE_ROW == (ret = sqlite3_step (plugin->get_signature))) { top_loc.offset = 0; - top_loc.revision = sqlite3_column_int (plugin->get_signature, 1); - zone_time.abs_value = sqlite3_column_int64 (plugin->get_signature, 2); - top_hash = sqlite3_column_blob (plugin->get_signature, 3); - top_loc.depth = sqlite3_column_int (plugin->get_signature, 4); - zone_key = sqlite3_column_blob (plugin->get_signature, 5); - zone_sig = sqlite3_column_blob (plugin->get_signature, 6); - - if ((sizeof (GNUNET_HashCode) != sqlite3_column_bytes (plugin->get_signature, 3)) || - (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (plugin->get_signature, 5)) || - (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (plugin->get_signature, 6))) + top_loc.revision = sqlite3_column_int (plugin->get_signature, 0); + zone_time.abs_value = sqlite3_column_int64 (plugin->get_signature, 1); + top_hash = sqlite3_column_blob (plugin->get_signature, 2); + top_loc.depth = sqlite3_column_int (plugin->get_signature, 3); + zone_key = sqlite3_column_blob (plugin->get_signature, 4); + zone_sig = sqlite3_column_blob (plugin->get_signature, 5); + + if ((sizeof (GNUNET_HashCode) != sqlite3_column_bytes (plugin->get_signature, 2)) || + (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (plugin->get_signature, 4)) || + (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (plugin->get_signature, 5))) { GNUNET_break (0); /* FIXME: delete bogus record & full zone (!?) */ @@ -809,7 +816,7 @@ run_delete_statement (struct Plugin *plugin, { int n; - if (SQLITE_OK != sqlite3_bind_blob (plugin->delete_zone_records, 1, &zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) + if (SQLITE_OK != sqlite3_bind_blob (plugin->delete_zone_records, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) { LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX"); diff --git a/src/namestore/test_plugin_namestore.c b/src/namestore/test_plugin_namestore.c index 01f73df8e..6b606e62b 100644 --- a/src/namestore/test_plugin_namestore.c +++ b/src/namestore/test_plugin_namestore.c @@ -80,12 +80,295 @@ load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg) } +/** + * Function called by for each matching record. + * + * @param cls closure + * @param zone hash of the public key of the zone + * @param loc location of the signature for this record + * @param name name that is being mapped (at most 255 characters long) + * @param record_type type of the record (A, AAAA, PKEY, etc.) + * @param expiration expiration time for the content + * @param flags flags for the content + * @param data_size number of bytes in data + * @param data value, semantics depend on 'record_type' (see RFCs for DNS and + * GNS specification for GNS extensions) + */ +static void +test_record (void *cls, + const GNUNET_HashCode *zone, + const struct GNUNET_NAMESTORE_SignatureLocation *loc, + const char *name, + uint32_t record_type, + struct GNUNET_TIME_Absolute expiration, + enum GNUNET_NAMESTORE_RecordFlags flags, + size_t data_size, + const void *data) +{ + int *idp = cls; + int id = *idp; + size_t tdata_size = id * 17; + char tdata[tdata_size]; + GNUNET_HashCode tzone; + char tname[32]; + uint32_t trecord_type; + struct GNUNET_NAMESTORE_SignatureLocation tloc; + enum GNUNET_NAMESTORE_RecordFlags tflags; + + memset (&tzone, 42, sizeof (tzone)); + memset (tdata, id % 255, sizeof (tdata)); + GNUNET_snprintf (tname, sizeof (tname), + "aa%u", (unsigned int) id); + trecord_type = id % 4; + tloc.depth = (id % 10); + tloc.offset = (id % 3); + tloc.revision = id % 1024; + tflags = GNUNET_NAMESTORE_RF_AUTHORITY; + + GNUNET_assert (0 == memcmp (&tzone, zone, sizeof (GNUNET_HashCode))); + GNUNET_assert (trecord_type == record_type); + GNUNET_assert (0 == strcmp (tname, name)); + GNUNET_assert (tdata_size == data_size); + GNUNET_assert (0 == memcmp (data, tdata, data_size)); + GNUNET_assert (flags == tflags); + GNUNET_assert (0 == memcmp (loc, &tloc, sizeof (struct GNUNET_NAMESTORE_SignatureLocation))); +} + + +static void +get_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id) +{ + GNUNET_HashCode zone; + GNUNET_HashCode nh; + char name[32]; + + memset (&zone, 42, sizeof (zone)); + GNUNET_snprintf (name, sizeof (name), + "aa%u", (unsigned int) id); + GNUNET_CRYPTO_hash (name, strlen (name), &nh); + GNUNET_assert (1 == nsp->iterate_records (nsp->cls, + &zone, + &nh, + &test_record, &id)); +} + + +/** + * Function called with the matching node. + * + * @param cls closure + * @param zone hash of public key of the zone + * @param loc location in the B-tree + * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root + * @param num_entries number of entries at this node in the B-tree + * @param entries the 'num_entries' entries to store (hashes over the + * records) + */ +static void +test_node (void *cls, + const GNUNET_HashCode *zone, + const struct GNUNET_NAMESTORE_SignatureLocation *loc, + const struct GNUNET_NAMESTORE_SignatureLocation *ploc, + unsigned int num_entries, + const GNUNET_HashCode *entries) +{ + int *idp = cls; + int id = *idp; + struct GNUNET_NAMESTORE_SignatureLocation tloc; + struct GNUNET_NAMESTORE_SignatureLocation tploc; + unsigned int tnum_entries = 1 + (id % 15); + GNUNET_HashCode tentries[num_entries]; + unsigned int i; + + tloc.depth = (id % 10); + tloc.offset = (id % 3); + tloc.revision = id % 1024; + tploc.depth = tloc.depth + 1; + tploc.offset = (id % 5); + tploc.revision = tloc.revision; + for (i=0;iget_node (nsp->cls, + &zone, + &loc, + &test_node, + &id)); +} + + +/** + * Function called with the matching signature. + * + * @param cls closure + * @param zone public key of the zone + * @param loc location of the root in the B-tree (depth, revision) + * @param top_sig signature signing the zone + * @param zone_time time the signature was created + * @param root_hash top level hash that is being signed + */ +static void +test_signature (void *cls, + const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, + const struct GNUNET_NAMESTORE_SignatureLocation *loc, + const struct GNUNET_CRYPTO_RsaSignature *top_sig, + struct GNUNET_TIME_Absolute zone_time, + const GNUNET_HashCode *root_hash) +{ + int *idp = cls; + int id = *idp; + struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded tzone_key; + struct GNUNET_NAMESTORE_SignatureLocation tloc; + struct GNUNET_CRYPTO_RsaSignature ttop_sig; + GNUNET_HashCode troot_hash; + + memset (&tzone_key, 13, sizeof (tzone_key)); + tloc.depth = (id % 10); + tloc.offset = (id % 3); + tloc.revision = id % 1024; + memset (&ttop_sig, 24, sizeof (ttop_sig)); + memset (&troot_hash, 42, sizeof (troot_hash)); + + GNUNET_assert (0 == memcmp (&tzone_key, zone_key, sizeof (GNUNET_HashCode))); + GNUNET_assert (0 == memcmp (&tloc, loc, sizeof (struct GNUNET_NAMESTORE_SignatureLocation))); + GNUNET_assert (0 == memcmp (&ttop_sig, top_sig, sizeof (struct GNUNET_CRYPTO_RsaSignature))); + GNUNET_assert (0 == memcmp (&troot_hash, root_hash, sizeof (GNUNET_HashCode))); +} + + +static void +get_signature (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id) +{ + GNUNET_HashCode root_hash; + + memset (&root_hash, 42, sizeof (root_hash)); + GNUNET_assert (GNUNET_OK == + nsp->get_signature (nsp->cls, + &root_hash, + test_signature, + &id)); +} + + +static void +put_record (struct GNUNET_NAMESTORE_PluginFunctions *nsp, int id) +{ + size_t data_size = id * 17; + char data[data_size]; + GNUNET_HashCode zone; + char name[32]; + uint32_t record_type; + struct GNUNET_NAMESTORE_SignatureLocation loc; + struct GNUNET_TIME_Absolute expiration; + enum GNUNET_NAMESTORE_RecordFlags flags; + + memset (&zone, 42, sizeof (zone)); + memset (data, id % 255, sizeof (data)); + GNUNET_snprintf (name, sizeof (name), + "aa%u", (unsigned int) id); + record_type = id % 4; + loc.depth = (id % 10); + loc.offset = (id % 3); + loc.revision = id % 1024; + expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS); + flags = GNUNET_NAMESTORE_RF_AUTHORITY; + + GNUNET_assert (GNUNET_OK == + nsp->put_record (nsp->cls, + &zone, + name, + record_type, + &loc, + expiration, + flags, + data_size, + data)); +} + + +static void +put_node (struct GNUNET_NAMESTORE_PluginFunctions *nsp, + int id) +{ + GNUNET_HashCode zone; + struct GNUNET_NAMESTORE_SignatureLocation loc; + struct GNUNET_NAMESTORE_SignatureLocation ploc; + unsigned int num_entries = 1 + (id % 15); + GNUNET_HashCode entries[num_entries]; + unsigned int i; + + memset (&zone, 42, sizeof (zone)); + loc.depth = (id % 10); + loc.offset = (id % 3); + loc.revision = id % 1024; + ploc.depth = loc.depth + 1; + ploc.offset = (id % 5); + ploc.revision = loc.revision; + for (i=0;iput_node (nsp->cls, + &zone, + &loc, + &ploc, + num_entries, + entries)); +} + + +static void +put_signature (struct GNUNET_NAMESTORE_PluginFunctions *nsp, + int id) +{ + struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key; + struct GNUNET_NAMESTORE_SignatureLocation loc; + struct GNUNET_CRYPTO_RsaSignature top_sig; + GNUNET_HashCode root_hash; + struct GNUNET_TIME_Absolute zone_time; + + memset (&zone_key, 13, sizeof (zone_key)); + loc.depth = (id % 10); + loc.offset = (id % 3); + loc.revision = id % 1024; + memset (&top_sig, 24, sizeof (top_sig)); + memset (&root_hash, 42, sizeof (root_hash)); + zone_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS); + + GNUNET_assert (GNUNET_OK == + nsp->put_signature (nsp->cls, + &zone_key, + &loc, + &top_sig, + &root_hash, + zone_time)); +} + + static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { - struct GNUNET_NAMESTORE_PluginFunctions *nsp; - + struct GNUNET_NAMESTORE_PluginFunctions *nsp; + ok = 0; nsp = load_plugin (cfg); if (NULL == nsp) @@ -95,6 +378,12 @@ run (void *cls, char *const *args, const char *cfgfile, "Failed to initialize namestore. Database likely not setup, skipping test.\n"); return; } + put_record (nsp, 1); + get_record (nsp, 1); + put_node (nsp, 1); + get_node (nsp, 1); + put_signature (nsp, 1); + get_signature (nsp, 1); unload_plugin (nsp); } diff --git a/src/namestore/test_plugin_namestore_sqlite.conf b/src/namestore/test_plugin_namestore_sqlite.conf index 8f95b65bf..57b170f7a 100644 --- a/src/namestore/test_plugin_namestore_sqlite.conf +++ b/src/namestore/test_plugin_namestore_sqlite.conf @@ -1,2 +1,2 @@ [namestore-sqlite] -FILENAME = $SERVICEHOME/namestore/sqlite.db +FILENAME = /tmp/gnunet-test-plugin-namestore-sqlite/sqlite.db -- 2.25.1