test_ui.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. from pwman_tstlib import *
  2. initTest(__file__)
  3. import importlib
  4. import os
  5. import pathlib
  6. import shutil
  7. import tempfile
  8. class Test_UI(TestCase):
  9. """User interface.
  10. """
  11. def __fakeClearScreen(self):
  12. print("CLEAR SCREEN")
  13. def __fakeReadPassphrase(self, prompt, verify=False):
  14. print("READ PASSPHRASE")
  15. return "test"
  16. def setUp(self):
  17. import libpwman.util
  18. self.__origClearScreen = libpwman.util.clearScreen
  19. self.__origReadPassphrase = libpwman.util.readPassphrase
  20. libpwman.util.clearScreen = self.__fakeClearScreen
  21. libpwman.util.readPassphrase = self.__fakeReadPassphrase
  22. import libpwman
  23. importlib.reload(libpwman.ui)
  24. self.dbFile = tempfile.NamedTemporaryFile(suffix="_" + self.id())
  25. self.dbFileSecondary = tempfile.NamedTemporaryFile(suffix="_secondary_" + self.id())
  26. print("DB file:", self.dbFile.name)
  27. print("Secondary DB file:", self.dbFileSecondary.name)
  28. shutil.copy(pathlib.Path("tests", "test_database_v1.db"),
  29. self.dbFile.name)
  30. self.ui = libpwman.PWMan(filename=self.dbFile.name,
  31. passphrase="test")
  32. def tearDown(self):
  33. try:
  34. self.dbFile.close()
  35. except FileNotFoundError as e:
  36. pass
  37. try:
  38. self.dbFileSecondary.close()
  39. except FileNotFoundError as e:
  40. pass
  41. self.dbFile = None
  42. self.dbFileSecondary = None
  43. import libpwman.util
  44. libpwman.util.clearScreen = self.__origClearScreen
  45. def test_base(self):
  46. import libpwman
  47. self.ui.do_help("")
  48. self.assertRaises(libpwman.PWMan.Quit,
  49. lambda: self.ui.do_quit(""))
  50. self.ui.do_cls("")
  51. self.ui.do_database("") # No params: db listing.
  52. self.ui.do_database("main") # Re-select of main shall succeed.
  53. self.ui.do_commit("")
  54. self.ui.do_masterp("")
  55. self.ui.do_dbdump("")
  56. self.assertRaises(libpwman.PWMan.CommandError,
  57. lambda: self.ui.do_dbimport("/does/not/exist"))
  58. self.ui.do_drop("")
  59. self.ui.do_list("")
  60. self.ui.do_list("testcat1 testtitle1 totpkey")
  61. self.ui.do_find("test")
  62. self.ui.do_totp("testcat1 testtitle1")
  63. self.ui.do_diff("")
  64. self.ui.do_new("a b c")
  65. self.ui.do_edit_user("a b c")
  66. self.ui.do_edit_pw("a b c")
  67. self.ui.do_edit_bulk("a b c")
  68. self.ui.do_edit_totp("a b GEZDGNAK 6 SHA1")
  69. self.ui.do_edit_attr("a b c d")
  70. self.ui.do_move("a b c d")
  71. self.ui.do_remove("c d")
  72. def test_multidb_closemain(self):
  73. import libpwman
  74. self.assertRaises(libpwman.PWMan.Quit,
  75. lambda: self.ui.do_close(""))
  76. def test_multidb(self):
  77. import libpwman
  78. try:
  79. os.unlink(self.dbFileSecondary.name)
  80. except FileNotFoundError as e:
  81. pass
  82. # main db
  83. self.ui.do_database("main")
  84. self.ui.do_new("cat0 ent0 user0 pw0")
  85. self.ui.do_new("cat1 ent1 user1 pw1")
  86. self.ui.do_new("bigcat ent10 user10 pw10")
  87. self.ui.do_new("bigcat ent11 user11 pw11")
  88. self.ui.do_new("bigcat ent12 user12 pw12")
  89. self.ui.do_new("bigcat ent13 user13 pw13")
  90. self.ui.do_edit_bulk("cat0 ent0 bulk0")
  91. self.ui.do_edit_attr("cat0 ent0 attr0 attrdata0")
  92. self.ui.do_edit_totp("cat0 ent0 GEZDGNAK 6 SHA1")
  93. self.ui.do_edit_bulk("bigcat ent10 bulk10")
  94. self.ui.do_edit_attr("bigcat ent10 attr10 attrdata10")
  95. self.ui.do_edit_totp("bigcat ent10 GEZDGNAK 8 SHA256")
  96. # secondary db
  97. self.ui.do_database("-f %s secondary" % self.dbFileSecondary.name)
  98. self.ui.do_new("cat2 ent2 user2 pw2")
  99. # move
  100. self.ui.do_database("main")
  101. self.ui.do_move("-d secondary cat0 ent0 cat0")
  102. self.assertRaises(libpwman.PWMan.CommandError,
  103. lambda: self.ui.do_move("-d secondary cat0 ent0 cat0"))
  104. self.ui.do_move("-s secondary -d main cat2 ent2 cat2")
  105. self.assertRaises(libpwman.PWMan.CommandError,
  106. lambda: self.ui.do_move("-s secondary -d main cat2 ent2 cat2"))
  107. self.ui.do_move("-d secondary bigcat")
  108. self.assertRaises(libpwman.PWMan.CommandError,
  109. lambda: self.ui.do_move("-d secondary bigcat"))
  110. # copy
  111. self.ui.do_database("main")
  112. self.ui.do_copy("-s secondary -d main cat0 ent0 cat0copy ent0copy")
  113. self.ui.do_copy("-s secondary -d main bigcat bigcatcopy")
  114. # check data
  115. db = self.ui._getDb("main")
  116. # cat1/ent1
  117. entry = db.getEntry("cat1", "ent1")
  118. self.assertEqual(entry.user, "user1")
  119. self.assertEqual(entry.pw, "pw1")
  120. bulk = db.getEntryBulk(entry)
  121. self.assertIsNone(bulk)
  122. attr = db.getEntryAttr(entry, "attr1")
  123. self.assertIsNone(attr)
  124. totp = db.getEntryTotp(entry)
  125. self.assertIsNone(totp)
  126. # cat2/ent2
  127. entry = db.getEntry("cat2", "ent2")
  128. self.assertEqual(entry.user, "user2")
  129. self.assertEqual(entry.pw, "pw2")
  130. bulk = db.getEntryBulk(entry)
  131. self.assertIsNone(bulk)
  132. attr = db.getEntryAttr(entry, "attr2")
  133. self.assertIsNone(attr)
  134. totp = db.getEntryTotp(entry)
  135. self.assertIsNone(totp)
  136. # cat0copy/ent0copy
  137. entry = db.getEntry("cat0copy", "ent0copy")
  138. self.assertEqual(entry.user, "user0")
  139. self.assertEqual(entry.pw, "pw0")
  140. bulk = db.getEntryBulk(entry)
  141. self.assertEqual(bulk.data, "bulk0")
  142. attr = db.getEntryAttr(entry, "attr0")
  143. self.assertEqual(attr.name, "attr0")
  144. self.assertEqual(attr.data, "attrdata0")
  145. totp = db.getEntryTotp(entry)
  146. self.assertEqual(totp.key, "GEZDGNAK")
  147. self.assertEqual(totp.digits, 6)
  148. self.assertEqual(totp.hmacHash, "SHA1")
  149. # bigcatcopy/ent10
  150. entry = db.getEntry("bigcatcopy", "ent10")
  151. self.assertEqual(entry.user, "user10")
  152. self.assertEqual(entry.pw, "pw10")
  153. bulk = db.getEntryBulk(entry)
  154. self.assertEqual(bulk.data, "bulk10")
  155. attr = db.getEntryAttr(entry, "attr10")
  156. self.assertEqual(attr.name, "attr10")
  157. self.assertEqual(attr.data, "attrdata10")
  158. totp = db.getEntryTotp(entry)
  159. self.assertEqual(totp.key, "GEZDGNAK")
  160. self.assertEqual(totp.digits, 8)
  161. self.assertEqual(totp.hmacHash, "SHA256")
  162. # bigcatcopy/ent11
  163. entry = db.getEntry("bigcatcopy", "ent11")
  164. self.assertEqual(entry.user, "user11")
  165. self.assertEqual(entry.pw, "pw11")
  166. bulk = db.getEntryBulk(entry)
  167. self.assertIsNone(bulk)
  168. attr = db.getEntryAttr(entry, "attr11")
  169. self.assertIsNone(attr)
  170. totp = db.getEntryTotp(entry)
  171. self.assertIsNone(totp)
  172. # bigcatcopy/ent12
  173. entry = db.getEntry("bigcatcopy", "ent12")
  174. self.assertEqual(entry.user, "user12")
  175. self.assertEqual(entry.pw, "pw12")
  176. bulk = db.getEntryBulk(entry)
  177. self.assertIsNone(bulk)
  178. attr = db.getEntryAttr(entry, "attr12")
  179. self.assertIsNone(attr)
  180. totp = db.getEntryTotp(entry)
  181. self.assertIsNone(totp)
  182. # bigcatcopy/ent13
  183. entry = db.getEntry("bigcatcopy", "ent13")
  184. self.assertEqual(entry.user, "user13")
  185. self.assertEqual(entry.pw, "pw13")
  186. bulk = db.getEntryBulk(entry)
  187. self.assertIsNone(bulk)
  188. attr = db.getEntryAttr(entry, "attr13")
  189. self.assertIsNone(attr)
  190. totp = db.getEntryTotp(entry)
  191. self.assertIsNone(totp)
  192. db = self.ui._getDb("secondary")
  193. # cat0/ent0
  194. entry = db.getEntry("cat0", "ent0")
  195. self.assertEqual(entry.user, "user0")
  196. self.assertEqual(entry.pw, "pw0")
  197. bulk = db.getEntryBulk(entry)
  198. self.assertEqual(bulk.data, "bulk0")
  199. attr = db.getEntryAttr(entry, "attr0")
  200. self.assertEqual(attr.name, "attr0")
  201. self.assertEqual(attr.data, "attrdata0")
  202. totp = db.getEntryTotp(entry)
  203. self.assertEqual(totp.key, "GEZDGNAK")
  204. self.assertEqual(totp.digits, 6)
  205. self.assertEqual(totp.hmacHash, "SHA1")
  206. # bigcat/ent10
  207. entry = db.getEntry("bigcat", "ent10")
  208. self.assertEqual(entry.user, "user10")
  209. self.assertEqual(entry.pw, "pw10")
  210. bulk = db.getEntryBulk(entry)
  211. self.assertEqual(bulk.data, "bulk10")
  212. attr = db.getEntryAttr(entry, "attr10")
  213. self.assertEqual(attr.name, "attr10")
  214. self.assertEqual(attr.data, "attrdata10")
  215. totp = db.getEntryTotp(entry)
  216. self.assertEqual(totp.key, "GEZDGNAK")
  217. self.assertEqual(totp.digits, 8)
  218. self.assertEqual(totp.hmacHash, "SHA256")
  219. # bigcat/ent11
  220. entry = db.getEntry("bigcat", "ent11")
  221. self.assertEqual(entry.user, "user11")
  222. self.assertEqual(entry.pw, "pw11")
  223. bulk = db.getEntryBulk(entry)
  224. self.assertIsNone(bulk)
  225. attr = db.getEntryAttr(entry, "attr11")
  226. self.assertIsNone(attr)
  227. totp = db.getEntryTotp(entry)
  228. self.assertIsNone(totp)
  229. # bigcat/ent12
  230. entry = db.getEntry("bigcat", "ent12")
  231. self.assertEqual(entry.user, "user12")
  232. self.assertEqual(entry.pw, "pw12")
  233. bulk = db.getEntryBulk(entry)
  234. self.assertIsNone(bulk)
  235. attr = db.getEntryAttr(entry, "attr12")
  236. self.assertIsNone(attr)
  237. totp = db.getEntryTotp(entry)
  238. self.assertIsNone(totp)
  239. # bigcat/ent13
  240. entry = db.getEntry("bigcat", "ent13")
  241. self.assertEqual(entry.user, "user13")
  242. self.assertEqual(entry.pw, "pw13")
  243. bulk = db.getEntryBulk(entry)
  244. self.assertIsNone(bulk)
  245. attr = db.getEntryAttr(entry, "attr13")
  246. self.assertIsNone(attr)
  247. totp = db.getEntryTotp(entry)
  248. self.assertIsNone(totp)
  249. self.ui.do_commit("-a")