proper bail out if plugin loading fails
authorChristian Grothoff <christian@grothoff.org>
Tue, 11 Oct 2016 13:45:37 +0000 (13:45 +0000)
committerChristian Grothoff <christian@grothoff.org>
Tue, 11 Oct 2016 13:45:37 +0000 (13:45 +0000)
src/psycstore/plugin_psycstore_mysql.c
src/psycstore/test_plugin_psycstore.c

index d857262d6311a95fb1a4da50af81a90670a7d2a3..73f55186b82a2746dc3a4f9569932ef623639267 100644 (file)
@@ -101,7 +101,6 @@ struct Plugin
    */
   struct GNUNET_MYSQL_StatementHandle *insert_channel_key;
 
-
   /**
    * Precompiled SQL for slave_key_store()
    */
@@ -268,50 +267,58 @@ mysql_prepare (struct GNUNET_MYSQL_Context *mc,
  * as needed as well).
  *
  * @param plugin the plugin context (state for this module)
- * @return GNUNET_OK on success
+ * @return #GNUNET_OK on success
  */
 static int
 database_setup (struct Plugin *plugin)
 {
   /* Open database and precompile statements */
-  plugin->mc = GNUNET_MYSQL_context_create (plugin->cfg, "psycstore-mysql");
+  plugin->mc = GNUNET_MYSQL_context_create (plugin->cfg,
+                                            "psycstore-mysql");
 
   if (NULL == plugin->mc)
   {
-    LOG(GNUNET_ERROR_TYPE_ERROR,
-   _("Unable to initialize Mysql.\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("Unable to initialize Mysql.\n"));
     return GNUNET_SYSERR;
   }
 
-  /* Create tables */
-
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS channels (\n"
-                              " id INT AUTO_INCREMENT,\n"
-                              " pub_key BLOB,\n"
-                              " max_state_message_id INT,\n"
-                              " state_hash_message_id INT,\n"
-                              " PRIMARY KEY(id),\n"
-                              " UNIQUE KEY(pub_key(5))\n"
-                              ");");
-
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS slaves (\n"
-                              " id INT AUTO_INCREMENT,\n"
-                              " pub_key BLOB,\n"
-                              " PRIMARY KEY(id),\n"
-                              " UNIQUE KEY(pub_key(5))\n"
-                              ");");
+#define STMT_RUN(sql) \
+  if (GNUNET_OK != \
+      GNUNET_MYSQL_statement_run (plugin->mc, \
+                                  sql)) \
+  { \
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
+                _("Failed to run SQL statement `%s'\n"), \
+                sql); \
+    return GNUNET_SYSERR; \
+  }
 
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS membership (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  slave_id INT NOT NULL REFERENCES slaves(id),\n"
-                              "  did_join INT NOT NULL,\n"
-                              "  announced_at BIGINT UNSIGNED NOT NULL,\n"
-                              "  effective_since BIGINT UNSIGNED NOT NULL,\n"
-                              "  group_generation BIGINT UNSIGNED NOT NULL\n"
-                              ");");
+  /* Create tables */
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS channels (\n"
+            " id INT AUTO_INCREMENT,\n"
+            " pub_key BLOB,\n"
+            " max_state_message_id INT,\n"
+            " state_hash_message_id INT,\n"
+            " PRIMARY KEY(id),\n"
+            " UNIQUE KEY(pub_key(5))\n"
+            ");");
+
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS slaves (\n"
+            " id INT AUTO_INCREMENT,\n"
+            " pub_key BLOB,\n"
+            " PRIMARY KEY(id),\n"
+            " UNIQUE KEY(pub_key(5))\n"
+            ");");
+
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS membership (\n"
+            "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+            "  slave_id INT NOT NULL REFERENCES slaves(id),\n"
+            "  did_join INT NOT NULL,\n"
+            "  announced_at BIGINT UNSIGNED NOT NULL,\n"
+            "  effective_since BIGINT UNSIGNED NOT NULL,\n"
+            "  group_generation BIGINT UNSIGNED NOT NULL\n"
+            ");");
 
 /*** FIX because IF NOT EXISTS doesn't work ***/
   GNUNET_MYSQL_statement_run (plugin->mc,
@@ -319,39 +326,37 @@ database_setup (struct Plugin *plugin)
                               "ON membership (channel_id, slave_id);");
 
   /** @todo messages table: add method_name column */
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS messages (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  hop_counter BIGINT UNSIGNED NOT NULL,\n"
-                              "  signature BLOB,\n"
-                              "  purpose BLOB,\n"
-                              "  fragment_id BIGINT UNSIGNED NOT NULL,\n"
-                              "  fragment_offset BIGINT UNSIGNED NOT NULL,\n"
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS messages (\n"
+            "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+            "  hop_counter BIGINT UNSIGNED NOT NULL,\n"
+            "  signature BLOB,\n"
+            "  purpose BLOB,\n"
+            "  fragment_id BIGINT UNSIGNED NOT NULL,\n"
+            "  fragment_offset BIGINT UNSIGNED NOT NULL,\n"
                               "  message_id BIGINT UNSIGNED NOT NULL,\n"
-                              "  group_generation BIGINT UNSIGNED NOT NULL,\n"
-                              "  multicast_flags BIGINT UNSIGNED NOT NULL,\n"
-                              "  psycstore_flags BIGINT UNSIGNED NOT NULL,\n"
-                              "  data BLOB,\n"
-                              "  PRIMARY KEY (channel_id, fragment_id),\n"
-                              "  UNIQUE KEY(channel_id, message_id, fragment_offset)\n"
-                              ");");
-
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS state (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  name TEXT NOT NULL,\n"
-                              "  value_current BLOB,\n"
-                              "  value_signed BLOB,\n"
-                              "  PRIMARY KEY (channel_id, name(5))\n"
-                              ");");
-
-  GNUNET_MYSQL_statement_run (plugin->mc,
-                              "CREATE TABLE IF NOT EXISTS state_sync (\n"
-                              "  channel_id INT NOT NULL REFERENCES channels(id),\n"
-                              "  name TEXT NOT NULL,\n"
-                              "  value BLOB,\n"
-                              "  PRIMARY KEY (channel_id, name(5))\n"
-                              ");");
+            "  group_generation BIGINT UNSIGNED NOT NULL,\n"
+            "  multicast_flags BIGINT UNSIGNED NOT NULL,\n"
+            "  psycstore_flags BIGINT UNSIGNED NOT NULL,\n"
+            "  data BLOB,\n"
+            "  PRIMARY KEY (channel_id, fragment_id),\n"
+            "  UNIQUE KEY(channel_id, message_id, fragment_offset)\n"
+            ");");
+
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS state (\n"
+            "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+            "  name TEXT NOT NULL,\n"
+            "  value_current BLOB,\n"
+            "  value_signed BLOB,\n"
+            "  PRIMARY KEY (channel_id, name(5))\n"
+            ");");
+
+  STMT_RUN ("CREATE TABLE IF NOT EXISTS state_sync (\n"
+            "  channel_id INT NOT NULL REFERENCES channels(id),\n"
+            "  name TEXT NOT NULL,\n"
+            "  value BLOB,\n"
+            "  PRIMARY KEY (channel_id, name(5))\n"
+            ");");
+#undef STMT_RUN
 
   /* Prepare statements */
   mysql_prepare (plugin->mc,
index c1a456e600f1066e200383997064b80f6a6998b2..d24405cc2c656e5a22c50ba03bdba50875335790 100644 (file)
@@ -173,6 +173,7 @@ run (void *cls, char *const *args, const char *cfgfile,
              "%s",
             "Failed to initialize PSYCstore.  "
              "Database likely not setup, skipping test.\n");
+    ok = 77;
     return;
   }
 
@@ -511,7 +512,8 @@ main (int argc, char *argv[])
   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
                       "test-plugin-psycstore", "nohelp", options, &run, NULL);
 
-  if (ok != 0)
+  if ( (0 != ok) &&
+       (77 != ok) )
     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
 
 #if ! DEBUG_PSYCSTORE