test_pwman.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. config_dir = os.path.expanduser("~/.pwman")
  53. if not os.path.isdir(config_dir):
  54. os.mkdir(config_dir)
  55. config_file = os.path.join(config_dir, "config")
  56. # set cls_timout to negative number (e.g. -1) to disable
  57. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  58. 'cls_timeout': '5'
  59. },
  60. 'Database': {'type': 'SQLite',
  61. 'filename': os.path.join(config_dir,
  62. "pwman.db")},
  63. 'Encryption': {'algorithm': 'AES'},
  64. 'Readline': {'history': os.path.join(config_dir,
  65. "history")}
  66. }
  67. config.set_defaults(default_config)
  68. if 'win' in sys.platform:
  69. try:
  70. import colorama
  71. colorama.init()
  72. except ImportError:
  73. config.set_value("Global", "colors", 'no')
  74. if os.path.exists(config_file):
  75. config.load(config_file)
  76. xselpath = config.get_value("Global", "xselpath")
  77. elif not OSX:
  78. xselpath = which("xsel")
  79. config.set_value("Global", "xsel", xselpath)
  80. elif OSX:
  81. pbcopypath = which("pbcopy")
  82. config.set_value("Global", "xsel", pbcopypath)
  83. # set umask before creating/opening any files
  84. umask = int(config.get_value("Global", "umask"))
  85. os.umask(umask)
  86. enc = CryptoEngine.get()
  87. dbtype = config.get_value("Database", "type")
  88. # if it is done here, we could do the following:
  89. # if db.ver == 0.4 :
  90. # db = pwman.data.factory.create(dbtyp, new_version)
  91. # else:
  92. # we use the old code untouched ... insecure, but
  93. # keeps backwards compatibility ...
  94. # if the database file exists check it's version
  95. # else: force version 0.4
  96. if os.path.exists(config.get_value("Database", "filename")):
  97. dbver = pwman.data.factory.check_db_version(dbtype)
  98. dbver = float(dbver.strip("\'"))
  99. else:
  100. dbver = 0.4
  101. # the method create could create an old instance that
  102. # accepts cPickle object or new style instance that
  103. # accepts only strings.
  104. # The user should be STRONGLY Prompted to CONVERT the
  105. # database to the new format using a command line tool.
  106. # version 0.5 pwman will depreciate that old and insecure
  107. # code ...
  108. db = pwman.data.factory.create(dbtype, dbver)
  109. if dbver >= 0.4:
  110. cli = PwmanCliNew(db, xselpath)
  111. elif dbver < 0.4:
  112. cli = PwmanCli(db, xselpath)
  113. except SystemExit, e:
  114. sys.exit(e)
  115. import logging
  116. import unittest
  117. from db_tests import CLITests
  118. from db_tests import DBTests
  119. if __name__ == '__main__':
  120. unittest.main()