test_database_v1.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from pwman_tstlib import *
  2. initTest(__file__)
  3. import pathlib
  4. from libpwman.database import *
  5. class Test_Database_v1(TestCase):
  6. """Version 1 database format.
  7. """
  8. def test_read_contents(self):
  9. db = PWManDatabase(filename=pathlib.Path("tests", "test_database_v1.db"),
  10. passphrase="test")
  11. self.assertEqual(db.getCategoryNames(),
  12. sorted([ "123",
  13. "test2",
  14. "testcat1" ]))
  15. self.assertEqual(db.getEntryTitles("123"),
  16. sorted([ "4711",
  17. "456" ]))
  18. self.assertEqual(db.getEntryTitles("test2"),
  19. sorted([ "test2" ]))
  20. self.assertEqual(db.getEntryTitles("testcat1"),
  21. sorted([ "testtitle1",
  22. "testtitle2",
  23. "test title3",
  24. "test-title3",
  25. "test title4",
  26. "test title 5",
  27. "test title 6" ]))
  28. entry = db.getEntry("testcat1", "testtitle1")
  29. self.assertEqual(entry.user, "uuuuuuuuu")
  30. self.assertEqual(entry.pw, "pppppp")
  31. entryBulk = db.getEntryBulk(entry)
  32. self.assertEqual(entryBulk.data, "bbbbbbbb")
  33. entryAttr = db.getEntryAttr(entry, "AAAA")
  34. self.assertEqual(entryAttr.data, "BBBB")
  35. entryAttr = db.getEntryAttr(entry, "CCCC")
  36. self.assertEqual(entryAttr.data, "DDDDDDD")
  37. entryTotp = db.getEntryTotp(entry)
  38. self.assertEqual(entryTotp.key, "MZXW63YK")
  39. self.assertEqual(entryTotp.digits, 6)
  40. self.assertEqual(entryTotp.hmacHash, "SHA1")
  41. entry = db.getEntry("testcat1", "testtitle2")
  42. self.assertEqual(entry.user, "user")
  43. self.assertEqual(entry.pw, "pw")
  44. entryBulk = db.getEntryBulk(entry)
  45. self.assertIsNone(entryBulk)
  46. entryTotp = db.getEntryTotp(entry)
  47. self.assertEqual(entryTotp.key, "MJQXE4QK")
  48. self.assertEqual(entryTotp.digits, 8)
  49. self.assertEqual(entryTotp.hmacHash, "SHA256")
  50. entry = db.getEntry("testcat1", "test-title3")
  51. self.assertEqual(entry.user, "aa bb")
  52. self.assertEqual(entry.pw, "cc dd")
  53. entryBulk = db.getEntryBulk(entry)
  54. self.assertIsNone(entryBulk)
  55. entry = db.getEntry("testcat1", "test title3")
  56. self.assertEqual(entry.user, "xx yy")
  57. self.assertEqual(entry.pw, "oo pp")
  58. entryBulk = db.getEntryBulk(entry)
  59. self.assertIsNone(entryBulk)
  60. entry = db.getEntry("testcat1", "test title4")
  61. self.assertEqual(entry.user, "4")
  62. self.assertEqual(entry.pw, "44")
  63. entryBulk = db.getEntryBulk(entry)
  64. self.assertIsNone(entryBulk)
  65. entry = db.getEntry("testcat1", "test title 5")
  66. self.assertEqual(entry.user, "5")
  67. self.assertEqual(entry.pw, "55")
  68. entryBulk = db.getEntryBulk(entry)
  69. self.assertIsNone(entryBulk)
  70. entry = db.getEntry("testcat1", "test title 6")
  71. self.assertEqual(entry.user, "6")
  72. self.assertEqual(entry.pw, "66")
  73. entryBulk = db.getEntryBulk(entry)
  74. self.assertIsNone(entryBulk)
  75. entry = db.getEntry("123", "4711")
  76. self.assertEqual(entry.user, "UUU")
  77. self.assertEqual(entry.pw, "password")
  78. entryBulk = db.getEntryBulk(entry)
  79. self.assertIsNone(entryBulk)
  80. entryAttr = db.getEntryAttr(entry, "git")
  81. self.assertEqual(entryAttr.data, "https://git.example.com")
  82. entryAttr = db.getEntryAttr(entry, "www")
  83. self.assertEqual(entryAttr.data, "https://www.example.com")
  84. entry = db.getEntry("123", "456")
  85. self.assertEqual(entry.user, "789")
  86. self.assertEqual(entry.pw, "")
  87. entryBulk = db.getEntryBulk(entry)
  88. self.assertIsNone(entryBulk)
  89. entry = db.getEntry("test2", "test2")
  90. self.assertEqual(entry.user, "TEST!")
  91. self.assertEqual(entry.pw, "TEST!!")
  92. entryBulk = db.getEntryBulk(entry)
  93. self.assertEqual(entryBulk.data, "TEST!!!")
  94. def test_legacy_pbkdf2(self):
  95. # Test legacy PBKDF2 read support.
  96. # Just check if we can open and decrypt it successfully.
  97. db = PWManDatabase(filename=pathlib.Path("tests", "test_database_v1_pbkdf2.db"),
  98. passphrase="test")
  99. self.assertEqual(db.getCategoryNames(),
  100. sorted([ "123", "test2", "testcat1" ]))