test_pwman.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 PwmanCli
  39. from pwman.ui.cli import PwmanCliNew
  40. OSX = False
  41. import pwman.util.config as config
  42. import pwman.data.factory
  43. from pwman.data.convertdb import PwmanConvertDB
  44. def which(cmd):
  45. _, cmdname = os.path.split(cmd)
  46. for path in os.environ["PATH"].split(os.pathsep):
  47. cmd = os.path.join(path, cmdname)
  48. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  49. return cmd
  50. return None
  51. try:
  52. # set cls_timout to negative number (e.g. -1) to disable
  53. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  54. 'cls_timeout': '5'
  55. },
  56. 'Database': {'type': 'SQLite',
  57. 'filename': os.path.join("tests",
  58. "test.pwman.db")},
  59. 'Encryption': {'algorithm': 'AES'},
  60. 'Readline': {'history': os.path.join("tests",
  61. "history")}
  62. }
  63. config.set_defaults(default_config)
  64. if 'win' in sys.platform:
  65. try:
  66. import colorama
  67. colorama.init()
  68. except ImportError:
  69. config.set_value("Global", "colors", 'no')
  70. if not OSX:
  71. xselpath = which("xsel")
  72. config.set_value("Global", "xsel", xselpath)
  73. elif OSX:
  74. pbcopypath = which("pbcopy")
  75. config.set_value("Global", "xsel", pbcopypath)
  76. # set umask before creating/opening any files
  77. umask = int(config.get_value("Global", "umask"))
  78. os.umask(umask)
  79. enc = CryptoEngine.get()
  80. dbtype = config.get_value("Database", "type")
  81. # if it is done here, we could do the following:
  82. # if db.ver == 0.4 :
  83. # db = pwman.data.factory.create(dbtyp, new_version)
  84. # else:
  85. # we use the old code untouched ... insecure, but
  86. # keeps backwards compatibility ...
  87. # if the database file exists check it's version
  88. # else: force version 0.4
  89. if os.path.exists(config.get_value("Database", "filename")):
  90. dbver = pwman.data.factory.check_db_version(dbtype)
  91. dbver = float(dbver.strip("\'"))
  92. else:
  93. dbver = 0.4
  94. # the method create could create an old instance that
  95. # accepts cPickle object or new style instance that
  96. # accepts only strings.
  97. # The user should be STRONGLY Prompted to CONVERT the
  98. # database to the new format using a command line tool.
  99. # version 0.5 pwman will depreciate that old and insecure
  100. # code ...
  101. db = pwman.data.factory.create(dbtype, dbver)
  102. if dbver >= 0.4:
  103. cli = PwmanCliNew(db, xselpath)
  104. elif dbver < 0.4:
  105. cli = PwmanCli(db, xselpath)
  106. except SystemExit, e:
  107. sys.exit(e)
  108. import unittest
  109. from db_tests import CLITests
  110. from db_tests import DBTests
  111. from crypto_tests import CryptoTest
  112. if __name__ == '__main__':
  113. unittest.main()