From 17d40ef43c80e074546c781bf7cfa5fb3c26d682 Mon Sep 17 00:00:00 2001 From: Muzahid Date: Wed, 24 Feb 2021 21:43:04 +0530 Subject: [PATCH] test case for versioning 10,9,8,1 --- src/tests/sql/__init__.py | 0 src/tests/sql/init_version_1.sql | 104 ++++++++++++++++++++ src/tests/sql/init_version_10.sql | 9 ++ src/tests/sql/init_version_8.sql | 16 +++ src/tests/sql/init_version_9.sql | 37 +++++++ src/tests/test_sqlthread.py | 157 ++++++++++++++++++++---------- 6 files changed, 273 insertions(+), 50 deletions(-) create mode 100644 src/tests/sql/__init__.py create mode 100644 src/tests/sql/init_version_1.sql create mode 100644 src/tests/sql/init_version_10.sql create mode 100644 src/tests/sql/init_version_8.sql create mode 100644 src/tests/sql/init_version_9.sql diff --git a/src/tests/sql/__init__.py b/src/tests/sql/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/tests/sql/init_version_1.sql b/src/tests/sql/init_version_1.sql new file mode 100644 index 00000000..782db0a9 --- /dev/null +++ b/src/tests/sql/init_version_1.sql @@ -0,0 +1,104 @@ +--- CREATE TABLE IF NOT EXISTS inbox (msgid blob, toaddress text, fromaddress text, subject text, received text, message text, folder text, encodingtype int, read bool, sighash blob, UNIQUE(msgid) ON CONFLICT REPLACE)); + +-- CREATE TABLE IF NOT EXISTS `inbox` ( +-- `msgid` blob NOT NULL, +-- `toaddress` text DEFAULT NULL, +-- `fromaddress` text DEFAULT NULL, +-- `subject` text DEFAULT NULL, +-- `received` text DEFAULT NULL, +-- `message` text DEFAULT NULL, +-- `folder` text DEFAULT NULL, +-- `encodingtype` int DEFAULT NULL, +-- `read` bool DEFAULT NULL, +-- `sighash` blob DEFAULT NULL, +-- UNIQUE(msgid) ON CONFLICT REPLACE +-- ) ; + +-- CREATE TABLE IF NOT EXISTS `sent` ( +-- `msgid` blob NOT NULL, +-- `toaddress` text DEFAULT NULL, +-- `toripe` blob DEFAULT NULL, +-- `fromaddress` text DEFAULT NULL, +-- `subject` text DEFAULT NULL, +-- `message` text DEFAULT NULL, +-- `ackdata` blob DEFAULT NULL, +-- `senttime` integer DEFAULT NULL, +-- `lastactiontime` integer DEFAULT NULL, +-- `sleeptill` integer DEFAULT NULL, +-- `status` text DEFAULT NULL, +-- `retrynumber` integer DEFAULT NULL, +-- `folder` text DEFAULT NULL, +-- `encodingtype` int DEFAULT NULL, +-- `ttl` int DEFAULT NULL, +-- UNIQUE(msgid) ON CONFLICT REPLACE +-- ) ; + +-- +-- Table structure for table `settings` +-- + +CREATE TABLE IF NOT EXISTS `settings` ( + `key` blob NOT NULL, + `value` text DEFAULT NULL, + UNIQUE(key) ON CONFLICT REPLACE +) ; + + +-- +-- Dumping data for table `settings` +-- + + +INSERT INTO `settings` VALUES ('version','1'); + +-- +-- Table structure for table `inventory` +-- + +CREATE TABLE IF NOT EXISTS `inventory` ( + `hash` blob NOT NULL, + `objecttype` int DEFAULT NULL, + `streamnumber` int NOT NULL, + `payload` blob DEFAULT NULL, + `integer` integer NOT NULL, + -- `tag` blob DEFAULT NULL, + UNIQUE(hash) ON CONFLICT REPLACE +) ; + + + -- self.cur.execute( + -- '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,''' + -- ''' received text, message text, folder text, encodingtype int, read bool, sighash blob,''' + -- ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') + -- self.cur.execute( + -- '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text,''' + -- ''' message text, ackdata blob, senttime integer, lastactiontime integer,''' + -- ''' sleeptill integer, status text, retrynumber integer, folder text, encodingtype int, ttl int)''') + -- self.cur.execute( + -- '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') + -- self.cur.execute( + -- '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') + -- self.cur.execute( + -- '''CREATE TABLE blacklist (label text, address text, enabled bool)''') + -- self.cur.execute( + -- '''CREATE TABLE whitelist (label text, address text, enabled bool)''') + -- self.cur.execute( + -- '''CREATE TABLE pubkeys (address text, addressversion int, transmitdata blob, time int,''' + -- ''' usedpersonally text, UNIQUE(address) ON CONFLICT REPLACE)''') + -- # self.cur.execute( + -- # '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' + -- # ''' expirestime integer, tag blob, UNIQUE(hash) ON CONFLICT REPLACE)''') + -- self.cur.execute( + -- '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' + -- ''' expirestime integer, UNIQUE(hash) ON CONFLICT REPLACE)''') + -- # self.cur.execute( + -- # '''INSERT INTO subscriptions VALUES''' + -- # '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') + -- self.cur.execute('''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') + -- # self.cur.execute('''INSERT INTO settings VALUES('version','11')''') + -- # self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( + -- # int(time.time()),)) + -- self.cur.execute( + -- '''CREATE TABLE objectprocessorqueue''' + -- ''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''') + -- self.conn.commit() diff --git a/src/tests/sql/init_version_10.sql b/src/tests/sql/init_version_10.sql new file mode 100644 index 00000000..1fb6bc6d --- /dev/null +++ b/src/tests/sql/init_version_10.sql @@ -0,0 +1,9 @@ +-- +-- Table structure for table `addressbook` +-- + +CREATE TABLE IF NOT EXISTS `addressbook` ( + `label` blob NOT NULL, + `address` text DEFAULT NULL, + UNIQUE(address) ON CONFLICT IGNORE +) ; diff --git a/src/tests/sql/init_version_8.sql b/src/tests/sql/init_version_8.sql new file mode 100644 index 00000000..a577cfd3 --- /dev/null +++ b/src/tests/sql/init_version_8.sql @@ -0,0 +1,16 @@ +-- +-- Table structure for table `inbox` +-- + +CREATE TABLE IF NOT EXISTS `inbox` ( + `msgid` blob NOT NULL, + `toaddress` text DEFAULT NULL, + `fromaddress` text DEFAULT NULL, + `subject` text DEFAULT NULL, + `received` text DEFAULT NULL, + `message` text DEFAULT NULL, + `folder` text DEFAULT NULL, + `encodingtype` int DEFAULT NULL, + `read` bool DEFAULT NULL, + UNIQUE(msgid) ON CONFLICT REPLACE +) ; diff --git a/src/tests/sql/init_version_9.sql b/src/tests/sql/init_version_9.sql new file mode 100644 index 00000000..e89b33d4 --- /dev/null +++ b/src/tests/sql/init_version_9.sql @@ -0,0 +1,37 @@ +-- +-- Table structure for table `sent` +-- + + +CREATE TABLE IF NOT EXISTS `sent` ( + `msgid` blob NOT NULL, + `toaddress` text DEFAULT NULL, + `toripe` blob DEFAULT NULL, + `fromaddress` text DEFAULT NULL, + `subject` text DEFAULT NULL, + `message` text DEFAULT NULL, + `ackdata` blob DEFAULT NULL, + `senttime` integer DEFAULT NULL, + `lastactiontime` integer DEFAULT NULL, + `sleeptill` integer DEFAULT NULL, + `status` text DEFAULT NULL, + `retrynumber` integer DEFAULT NULL, + `folder` text DEFAULT NULL, + `encodingtype` int DEFAULT NULL, + `ttl` int DEFAULT NULL, + UNIQUE(msgid) ON CONFLICT REPLACE +) ; + + +-- +-- Table structure for table `pubkeys` +-- + +CREATE TABLE IF NOT EXISTS `pubkeys` ( + `hash` text, + `addressversion` int, + `transmitdata` blob, + `time` int, + `usedpersonally` text, + UNIQUE(hash) ON CONFLICT REPLACE +) ; diff --git a/src/tests/test_sqlthread.py b/src/tests/test_sqlthread.py index f45e7139..649cdd40 100644 --- a/src/tests/test_sqlthread.py +++ b/src/tests/test_sqlthread.py @@ -1,6 +1,7 @@ """ -Test for ECC blind signatures + Test for sqlThread blind signatures """ + import os import unittest import shutil # used for moving the messages.dat file @@ -10,9 +11,10 @@ import threading import time # print sys.path # import state +# import sql +# from sql import init_version_1.sql from ..state import appdata from ..helper_sql import sqlStoredProcedure - # import threading # from hashlib import sha256 # from ..helper_sql import sqlQuery @@ -20,13 +22,12 @@ from ..helper_sql import sqlStoredProcedure # from common import cleanup from ..class_sqlThread import (sqlThread, UpgradeDB) + class TestSqlThread(unittest.TestCase): """ Test case for SQLThread """ - # def __init__(self): - # initialise Db connection conn = sqlite3.connect(appdata + 'messages.dat') conn.text_factory = str cur = conn.cursor() @@ -38,77 +39,133 @@ class TestSqlThread(unittest.TestCase): sqlLookup.daemon = False sqlLookup.start() + @classmethod + def setUp(cls): + tables = list(cls.cur.execute("select name from sqlite_master where type is 'table'")) + cls.cur.executescript(';'.join(["drop table if exists %s" % i for i in tables])) + + @classmethod + def tearDown(cls): + pass + @classmethod def tearDownClass(cls): # Stop sql thread sqlStoredProcedure('exit') - print "...TearDown" def db_connection(self): conn = sqlite3.connect(appdata + 'messages.dat') conn.text_factory = str return conn.cursor() - def normalize_version(self): + def normalize_version(self, file): try: - self.cur.execute( - '''CREATE TABLE inbox (msgid blob, toaddress text, fromaddress text, subject text,''' - ''' received text, message text, folder text, encodingtype int, read bool, sighash blob,''' - ''' UNIQUE(msgid) ON CONFLICT REPLACE)''') - self.cur.execute( - '''CREATE TABLE sent (msgid blob, toaddress text, toripe blob, fromaddress text, subject text,''' - ''' message text, ackdata blob, senttime integer, lastactiontime integer,''' - ''' sleeptill integer, status text, retrynumber integer, folder text, encodingtype int, ttl int)''') - self.cur.execute( - '''CREATE TABLE subscriptions (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE addressbook (label text, address text, UNIQUE(address) ON CONFLICT IGNORE)''') - self.cur.execute( - '''CREATE TABLE blacklist (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE whitelist (label text, address text, enabled bool)''') - self.cur.execute( - '''CREATE TABLE pubkeys (address text, addressversion int, transmitdata blob, time int,''' - ''' usedpersonally text, UNIQUE(address) ON CONFLICT REPLACE)''') - # self.cur.execute( - # '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' - # ''' expirestime integer, tag blob, UNIQUE(hash) ON CONFLICT REPLACE)''') - self.cur.execute( - '''CREATE TABLE inventory (hash blob, objecttype int, streamnumber int, payload blob,''' - ''' expirestime integer, UNIQUE(hash) ON CONFLICT REPLACE)''') - # self.cur.execute( - # '''INSERT INTO subscriptions VALUES''' - # '''('Bitmessage new releases/announcements','BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw',1)''') - self.cur.execute('''CREATE TABLE settings (key blob, value blob, UNIQUE(key) ON CONFLICT REPLACE)''') - # self.cur.execute('''INSERT INTO settings VALUES('version','11')''') - # self.cur.execute('''INSERT INTO settings VALUES('lastvacuumtime',?)''', ( - # int(time.time()),)) - self.cur.execute( - '''CREATE TABLE objectprocessorqueue''' - ''' (objecttype int, data blob, UNIQUE(objecttype, data) ON CONFLICT REPLACE)''') + root_path = os.path.dirname(os.path.dirname(__file__)) + sql_file_path = os.path.join(root_path, 'tests/sql/') + sql_file_path = os.path.join(sql_file_path, "init_version_"+file+".sql") + sql_file = open(sql_file_path) + sql_as_string = sql_file.read() + self.cur.executescript(sql_as_string) self.conn.commit() except Exception as err: - print err.message if str(err) == 'table inbox already exists': - print "table inbox already exists" + return "table inbox already exists" else: sys.stderr.write( 'ERROR trying to create database file (message.dat). Error message: %s\n' % str(err)) os._exit(0) - def clean_db(self): - tables = list(self.cur.execute("select name from sqlite_master where type is 'table'")) - return self.cur.executescript(';'.join(["drop table if exists %s" % i for i in tables])) + def filter_table_column(self, schema, column): + for x in schema: + for y in x: + if y == column: + yield y - def test_sql_thread_version_one(self): + def test_sql_thread_version_1(self): """ Test with version 1 """ - self.clean_db() - self.normalize_version() - self.cur.execute('''INSERT INTO settings VALUES('version','1')''') + # normalise Db for version 1 + self.normalize_version("1") + + # Test versioning upgrade_db = UpgradeDB() upgrade_db.cur = self.cur upgrade_db.upgrade_schema_data_1() + # Assertion after versioning + res = self.cur.execute('''PRAGMA table_info('inventory');''') + res = res.fetchall() + result = list(self.filter_table_column(res, "tag")) + res = [tup for tup in res if any(i in tup for i in ["tag"])] + self.assertEqual(result, ['tag'] , "Data not migrated for version 1") + self.assertEqual(res, [(5, 'tag', 'blob', 0, "''", 0)] , "Data not migrated for version 1") + + + def test_sql_thread_version_10(self): + """ + Test with version 10 + """ + + # Update version 10 + self.normalize_version("10") + + # Test versioning + upgrade_db = UpgradeDB() + upgrade_db.cur = self.cur + upgrade_db.upgrade_schema_data_10() + + # Assertion + self.cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='old_addressbook' ''') + self.assertNotEqual(self.cur.fetchone(), 1 , "Table old_addressbook not deleted") + + res = self.cur.execute('''PRAGMA table_info('addressbook');''') + res = res.fetchall() + result = list(self.filter_table_column(res, "address")) + self.assertEqual(result, ['address'] , "Data not migrated for version 10") + + + def test_sql_thread_version_9(self): + """ + Test with version 9 + """ + + # Update version 9 + self.normalize_version("9") + + # Test versioning + upgrade_db = UpgradeDB() + upgrade_db.cur = self.cur + upgrade_db.upgrade_schema_data_9() + + # Assertion + self.cur.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='pubkeys_backup' ''') + self.assertNotEqual(self.cur.fetchone(), 1 , "Table pubkeys_backup not deleted") + + res = self.cur.execute('''PRAGMA table_info('pubkeys');''') + res = res.fetchall() + result = list(self.filter_table_column(res, "address")) + self.assertEqual(result, ['address'] , "Data not migrated for version 9") + + + def test_sql_thread_version_8(self): + """ + Test with version 8 + """ + + # Update version 8 + self.normalize_version("8") + + # Test versioning + upgrade_db = UpgradeDB() + upgrade_db.cur = self.cur + upgrade_db.upgrade_schema_data_8() + + # # Assertion + res = self.cur.execute('''PRAGMA table_info('inbox');''') + res = res.fetchall() + result = list(self.filter_table_column(res, "sighash")) + self.assertEqual(result, ['sighash'] , "Data not migrated for version 8") + +