changeset ea8c40e46cad in /home/hg/repos/gajim-plugins

author: Bahtiar `kalkin-` Gadimov <[email protected]>
branches: 
details:gajim-plugins?cmd=changeset;node=ea8c40e46cad
description: Add EncryptionState

diffstat:

 omemo/store/encryption.py      |  58 ++++++++++++++++++++++++++++++++++++++++++
 omemo/test_encryption_store.py |  31 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 0 deletions(-)

diffs (97 lines):

diff -r afc9889d75c5 -r ea8c40e46cad omemo/store/encryption.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/omemo/store/encryption.py Sun Jan 10 19:51:55 2016 +0100
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2015 Bahtiar `kalkin-` Gadimov <[email protected]>
+# Copyright 2015 Daniel Gultsch <[email protected]>
+#
+# This file is part of Gajim-OMEMO plugin.
+#
+# The Gajim-OMEMO plugin is free software: you can redistribute it and/or 
modify
+# it under the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# Gajim-OMEMO is distributed in the hope that it will be useful, but WITHOUT 
ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# the Gajim-OMEMO plugin.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+
+class EncryptionState():
+    """ Used to store if OMEMO is enabled or not between gajim restarts """
+
+    def __init__(self, dbConn):
+        """
+        :type dbConn: Connection
+        """
+        self.dbConn = dbConn
+        dbConn.execute(""" CREATE TABLE IF NOT EXISTS encryption_state (
+                               jid TEXT PRIMARY KEY,
+                               encryption INTEGER,
+                               timestamp NUMBER DEFAULT CURRENT_TIMESTAMP
+                           ) WITHOUT ROWID;
+                       """)
+
+    def activate(self, jid):
+        q = """INSERT OR REPLACE INTO encryption_state (jid, encryption)
+               VALUES (?, 1) """
+        c = self.dbConn.cursor()
+        c.execute(q, (jid,))
+        self.dbConn.commit()
+
+    def deactivate(self, jid):
+        q = """INSERT OR REPLACE INTO encryption_state (jid, encryption)
+               VALUES (?, 0)"""
+        c = self.dbConn.cursor()
+        c.execute(q, (jid,))
+        self.dbConn.commit()
+
+    def is_active(self, jid):
+        q = 'SELECT encryption FROM encryption_state where jid = ?;'
+        c = self.dbConn.cursor()
+        c.execute(q, (jid,))
+        result = c.fetchone()
+        if result is None:
+            return False
+        return result[0] == 1
diff -r afc9889d75c5 -r ea8c40e46cad omemo/test_encryption_store.py
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/omemo/test_encryption_store.py    Sun Jan 10 19:51:55 2016 +0100
@@ -0,0 +1,31 @@
+import unittest
+from store.encryption import EncryptionState
+import os
+import sqlite3
+
+
+class TestEncryptionStateStore(unittest.TestCase):
+
+    def setUp(self):
+        self.conn = sqlite3.connect('test-db', check_same_thread=False)
+        self.e = EncryptionState(self.conn)
+
+    def test_create(self):
+        self.assertEquals(self.e.is_active('[email protected]'), False)
+
+    def test_enable_encryption(self):
+        self.e.activate('[email protected]')
+        self.assertEquals(self.e.is_active('[email protected]'), True)
+
+    def test_disable_encryption(self):
+        self.e.activate('[email protected]')
+        self.assertEquals(self.e.is_active('[email protected]'), True)
+        self.e.deactivate('[email protected]')
+        self.assertEquals(self.e.is_active('[email protected]'), False)
+
+    def tearDown(self):
+        os.remove('test-db')
+
+
+if __name__ == '__main__':
+    unittest.main()
_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to