test_pwman.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. import os
  23. import os.path
  24. import sys
  25. sys.path.insert(0, os.getcwd())
  26. _saveconfig = True
  27. import sys
  28. from pwman.util.crypto import CryptoEngine
  29. if 'darwin' in sys.platform:
  30. from pwman.ui.mac import PwmanCliMac as PwmanCli
  31. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  32. OSX = True
  33. elif 'win' in sys.platform:
  34. from pwman.ui.cli import PwmanCli
  35. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  36. OSX = False
  37. else:
  38. from pwman.ui.cli import PwmanCliNew
  39. OSX = False
  40. import pwman.util.config as config
  41. import pwman.data.factory
  42. def which(cmd):
  43. _, cmdname = os.path.split(cmd)
  44. for path in os.environ["PATH"].split(os.pathsep):
  45. cmd = os.path.join(path, cmdname)
  46. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  47. return cmd
  48. return None
  49. try:
  50. # set cls_timout to negative number (e.g. -1) to disable
  51. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  52. 'cls_timeout': '5'
  53. },
  54. 'Database': {'type': 'SQLite',
  55. 'filename':
  56. os.path.join(os.path.dirname(__file__),
  57. "test.pwman.db")},
  58. 'Encryption': {'algorithm': 'AES'},
  59. 'Readline': {'history': os.path.join("tests",
  60. "history")}
  61. }
  62. config.set_defaults(default_config)
  63. if 'win' in sys.platform:
  64. try:
  65. import colorama
  66. colorama.init()
  67. except ImportError:
  68. config.set_value("Global", "colors", 'no')
  69. if not OSX:
  70. xselpath = which("xsel")
  71. config.set_value("Global", "xsel", xselpath)
  72. elif OSX:
  73. pbcopypath = which("pbcopy")
  74. config.set_value("Global", "xsel", pbcopypath)
  75. # set umask before creating/opening any files
  76. umask = int(config.get_value("Global", "umask"))
  77. os.umask(umask)
  78. enc = CryptoEngine.get()
  79. dbtype = config.get_value("Database", "type")
  80. # if it is done here, we could do the following:
  81. # if db.ver == 0.4 :
  82. # db = pwman.data.factory.create(dbtyp, new_version)
  83. # else:
  84. # we use the old code untouched ... insecure, but
  85. # keeps backwards compatibility ...
  86. # if the database file exists check it's version
  87. # else: force version 0.4
  88. if os.path.exists(config.get_value("Database", "filename")):
  89. dbver = pwman.data.factory.check_db_version(dbtype)
  90. dbver = float(dbver.strip("\'"))
  91. else:
  92. dbver = 0.4
  93. # the method create could create an old instance that
  94. # accepts cPickle object or new style instance that
  95. # accepts only strings.
  96. # The user should be STRONGLY Prompted to CONVERT the
  97. # database to the new format using a command line tool.
  98. # version 0.5 pwman will depreciate that old and insecure
  99. # code ...
  100. db = pwman.data.factory.create(dbtype, dbver)
  101. if dbver >= 0.4:
  102. cli = PwmanCliNew(db, xselpath)
  103. elif dbver < 0.4:
  104. cli = PwmanCli(db, xselpath)
  105. except SystemExit, e:
  106. sys.exit(e)
  107. import unittest
  108. from db_tests import DBTests
  109. from crypto_tests import CryptoTest
  110. def suite():
  111. loader = unittest.TestLoader()
  112. suite = unittest.TestSuite()
  113. suite.addTest(loader.loadTestsFromTestCase(DBTests))
  114. suite.addTest(loader.loadTestsFromTestCase(CryptoTest))
  115. return suite
  116. if __name__ == '__main__':
  117. unittest.TextTestRunner(verbosity=2).run(suite())