summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-03-17 16:27:52 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-03-17 16:27:52 -0700
commitcea3f2815589f956f699ad589a66c9d09f406ee9 (patch)
tree8d0c317f37f06f9b742884133fdf0bf1cff6c5c9
parentba69240bcbabdb667b8fcc9208eeb3ebb5845832 (diff)
downloadinfinitytier-cea3f2815589f956f699ad589a66c9d09f406ee9.tar.gz
infinitytier-cea3f2815589f956f699ad589a66c9d09f406ee9.zip
DB init works now.
-rw-r--r--netconf/SqliteNetworkConfigMaster.cpp31
-rw-r--r--selftest.cpp26
2 files changed, 46 insertions, 11 deletions
diff --git a/netconf/SqliteNetworkConfigMaster.cpp b/netconf/SqliteNetworkConfigMaster.cpp
index 48711c86..88d96c2a 100644
--- a/netconf/SqliteNetworkConfigMaster.cpp
+++ b/netconf/SqliteNetworkConfigMaster.cpp
@@ -66,27 +66,36 @@ SqliteNetworkConfigMaster::SqliteNetworkConfigMaster(const Identity &signingId,c
throw std::runtime_error("SqliteNetworkConfigMaster cannot open database file");
sqlite3_busy_timeout(_db,10000);
- sqlite3_stmt *s = (sqlite3_stmt *)0;
- if (sqlite3_prepare_v2(_db,"SELECT v FROM Config WHERE k = 'schemaVersion';",-1,&s,(const char **)0) != SQLITE_OK) {
- sqlite3_close(_db);
- throw std::runtime_error("SqliteNetworkConfigMaster cannot create prepared statement (library problem?)");
+ sqlite3_stmt *s;
+ for(int k=0;k<2;++k) {
+ s = (sqlite3_stmt *)0;
+ if ((sqlite3_prepare_v2(_db,"SELECT 'v' FROM Config WHERE 'k' = 'schemaVersion';",-1,&s,(const char **)0) != SQLITE_OK)||(!s)) {
+ if (sqlite3_exec(_db,ZT_NETCONF_SCHEMA_SQL"INSERT INTO Config (k,v) VALUES ('schemaVersion',"ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR");",0,0,0) != SQLITE_OK) {
+ sqlite3_close(_db);
+ throw std::runtime_error("SqliteNetworkConfigMaster cannot initialize database and/or insert schemaVersion into Config table");
+ } else {
+ // Initialized database and set schema version, so we are done.
+ return;
+ }
+ } else break;
}
if (!s) {
sqlite3_close(_db);
- throw std::runtime_error("SqliteNetworkConfigMaster cannot create prepared statement (library problem?)");
+ throw std::runtime_error("SqliteNetworkConfigMaster unable to create prepared statement or initialize database");
}
- int schemaVersion = -1;
+ // If we made it here, database was opened and prepared statement was created
+ // to check schema version. Check and upgrade if needed.
+
+ int schemaVersion = -1234;
if (sqlite3_step(s) == SQLITE_ROW)
schemaVersion = sqlite3_column_int(s,0);
sqlite3_finalize(s);
- if (schemaVersion == -1) {
- if (sqlite3_exec(_db,ZT_NETCONF_SCHEMA_SQL"INSERT INTO Config (k,v) VALUES ('schemaVersion',"ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR");",0,0,0) != SQLITE_OK) {
- sqlite3_close(_db);
- throw std::runtime_error("SqliteNetworkConfigMaster cannot initialize database and/or insert schemaVersion into Config table");
- }
+ if (schemaVersion == -1234) {
+ sqlite3_close(_db);
+ throw std::runtime_error("SqliteNetworkConfigMaster schemaVersion not found in Config table (init failure?)");
} else if (schemaVersion != ZT_NETCONF_SQLITE_SCHEMA_VERSION) {
// Note -- this will eventually run auto-upgrades so this isn't how it'll work going forward
sqlite3_close(_db);
diff --git a/selftest.cpp b/selftest.cpp
index f083018a..e8f61d8d 100644
--- a/selftest.cpp
+++ b/selftest.cpp
@@ -55,6 +55,10 @@
#include "node/Defaults.hpp"
#include "node/Node.hpp"
+#ifdef ZT_ENABLE_NETCONF_MASTER
+#include "netconf/SqliteNetworkConfigMaster.hpp"
+#endif // ZT_ENABLE_NETCONF_MASTER
+
#ifdef __WINDOWS__
#include <tchar.h>
#endif
@@ -636,6 +640,27 @@ static int testOther()
return 0;
}
+static int testSqliteNetconfMaster()
+{
+#ifdef ZT_ENABLE_NETCONF_MASTER
+ try {
+ std::cout << "[netconf] Generating signing identity..." << std::endl;
+ Identity signingId;
+ signingId.generate();
+
+ std::cout << "[netconf] Creating database..." << std::endl;
+ SqliteNetworkConfigMaster netconf(signingId,"netconf-test.db");
+ } catch (std::runtime_error &exc) {
+ std::cout << "FAIL! (unexpected exception: " << exc.what() << ")" << std::endl;
+ return -1;
+ } catch ( ... ) {
+ std::cout << "FAIL! (unexpected exception: ...)" << std::endl;
+ return -1;
+ }
+#endif // ZT_ENABLE_NETCONF_MASTER
+ return 0;
+}
+
#ifdef __WINDOWS__
int _tmain(int argc, _TCHAR* argv[])
#else
@@ -686,6 +711,7 @@ int main(int argc,char **argv)
srand((unsigned int)time(0));
+ r |= testSqliteNetconfMaster();
r |= testCrypto();
r |= testHttp();
r |= testPacket();