test_sqlite.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute iut and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import os
  20. import unittest
  21. from pwman.data.drivers.sqlite import SQLite
  22. from pwman.data.nodes import Node
  23. from pwman.util.crypto_engine import CryptoEngine
  24. from .test_crypto_engine import give_key, DummyCallback
  25. class TestSQLite(unittest.TestCase):
  26. def setUp(self):
  27. self.db = SQLite('test.db')
  28. self.db._open()
  29. def test_1_create_tables(self):
  30. self.db._create_tables()
  31. self.db._con.commit()
  32. # the method _open calls _create_tables
  33. self.db.save_crypto_info("foo", "bar")
  34. self.db._create_tables()
  35. def test_1a_create_tables(self):
  36. self.db._create_tables()
  37. def test_2_crypto_info(self):
  38. self.db._create_tables()
  39. self.db.save_crypto_info("foo", "bar")
  40. f = self.db.fetch_crypto_info()
  41. self.assertListEqual([u'foo', u'bar'], list(f))
  42. def test_3_add_node(self):
  43. node = Node(clear_text=True,
  44. **{'username': u"alice", 'password': u"secret",
  45. 'url': u"wonderland.com",
  46. 'notes': u"a really great place",
  47. 'tags': [u'foo', u'bar']})
  48. self.db.add_node(node)
  49. rv = self.db._cur.execute("select * from node")
  50. # clearly this fails, while alice is not found in clear text in the
  51. # database!
  52. ce = CryptoEngine.get()
  53. res = rv.fetchone()
  54. self.assertIn(ce.encrypt(u'alice'), res[1])
  55. def test_4_test_tags(self):
  56. node = Node(clear_text=True,
  57. **{'username': u"alice", 'password': u"secret",
  58. 'url': u"wonderland.com",
  59. 'notes': u"a really great place",
  60. 'tags': [u'foo', u'bar']})
  61. ce = CryptoEngine.get()
  62. self.db._get_or_create_tag(node._tags[0])
  63. self.assertEqual(1, self.db._get_or_create_tag(node._tags[0]))
  64. rv = self.db._get_or_create_tag(ce.encrypt('baz'))
  65. self.db._con.commit()
  66. self.assertEqual(3, rv)
  67. def test_5_test_lookup(self):
  68. self.db._cur.execute('SELECT nodeid, tagid FROM LOOKUP')
  69. rows = self.db._cur.fetchall()
  70. self.assertEqual(2, len(rows))
  71. def test_6_listnodes(self):
  72. node = Node(clear_text=True,
  73. **{'username': u"hatman", 'password': u"secret",
  74. 'url': u"wonderland.com",
  75. 'notes': u"a really great place",
  76. 'tags': [u'baz', u'bar']})
  77. self.db.add_node(node)
  78. ids = self.db.listnodes()
  79. self.assertEqual(2, len(ids))
  80. def test_7_listnodes_w_filter(self):
  81. ce = CryptoEngine.get()
  82. # the tag 'bar' is found in a node created in:
  83. # test_3_add_node
  84. # test_6_listnodes
  85. tag = ce.encrypt(u'bar')
  86. rv = self.db.listnodes(tag)
  87. self.assertEqual(len(rv), 2)
  88. tag = ce.encrypt(u'baz')
  89. # the tag 'baz' is found in a node created in
  90. # test_6_listnodes
  91. rv = self.db.listnodes(tag)
  92. self.assertEqual(len(rv), 1)
  93. def test_8_getnodes(self):
  94. nodes = self.db.getnodes([1, 2])
  95. self.assertEqual(len(nodes), 2)
  96. def test_9_editnode(self):
  97. # delibertly insert clear text into the database
  98. node = {'user': 'transparent', 'password': 'notsecret',
  99. 'tags': ['foo', 'bank']}
  100. self.db.editnode('2', **node)
  101. self.db._cur.execute('SELECT USER, PASSWORD FROM NODE WHERE ID=2')
  102. rv = self.db._cur.fetchone()
  103. self.assertEqual(rv, (u'transparent', u'notsecret'))
  104. node = {'user': 'modify', 'password': 'notsecret',
  105. 'tags': ['foo', 'auto']}
  106. # now the tags bank and baz are orphand ...
  107. # what happens? it should be completely removed.
  108. # To spare IO we only delete orphand tags when
  109. # db.close is called.
  110. self.db.editnode('2', **node)
  111. def test_9_test_orphands(self):
  112. ce = CryptoEngine.get()
  113. baz_encrypted = ce.encrypt(u'baz').decode()
  114. self.db._cur.execute('SELECT DATA FROM TAG')
  115. rv = self.db._cur.fetchall()
  116. for data in rv:
  117. if isinstance(data[0], str):
  118. self.assertNotIn(u'bank', data[0])
  119. else:
  120. self.assertNotIn(baz_encrypted, data[0].decode())
  121. def test_a10_test_listtags(self):
  122. tags = self.db.listtags()
  123. self.assertEqual(4, len(list(tags)))
  124. def test_a11_test_rmnodes(self):
  125. self.db.removenodes([1, 2])
  126. rv = self.db._cur.execute("select * from node").fetchall()
  127. self.assertListEqual(rv, [])
  128. def test_a12_test_savekey(self):
  129. ce = CryptoEngine.get()
  130. self.db.savekey(ce.get_cryptedkey())
  131. self.assertEqual(ce.get_cryptedkey(), self.db.loadkey())
  132. def tearDown(self):
  133. self.db.close()
  134. if __name__ == '__main__':
  135. ce = CryptoEngine.get()
  136. ce.callback = DummyCallback()
  137. ce.changepassword(reader=give_key)
  138. try:
  139. unittest.main(verbosity=2, failfast=True)
  140. except SystemExit:
  141. os.remove('test.db')