No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

98 líneas
3.8 KiB

  1. """test passlib.hosts"""
  2. #=============================================================================
  3. # imports
  4. #=============================================================================
  5. from __future__ import with_statement
  6. # core
  7. import logging; log = logging.getLogger(__name__)
  8. # site
  9. # pkg
  10. from passlib import hosts, hash as hashmod
  11. from passlib.utils import unix_crypt_schemes
  12. from passlib.tests.utils import TestCase
  13. # module
  14. #=============================================================================
  15. # test predefined app contexts
  16. #=============================================================================
  17. class HostsTest(TestCase):
  18. """perform general tests to make sure contexts work"""
  19. # NOTE: these tests are not really comprehensive,
  20. # since they would do little but duplicate
  21. # the presets in apps.py
  22. #
  23. # they mainly try to ensure no typos
  24. # or dynamic behavior foul-ups.
  25. def check_unix_disabled(self, ctx):
  26. for hash in [
  27. "",
  28. "!",
  29. "*",
  30. "!$1$TXl/FX/U$BZge.lr.ux6ekjEjxmzwz0",
  31. ]:
  32. self.assertEqual(ctx.identify(hash), 'unix_disabled')
  33. self.assertFalse(ctx.verify('test', hash))
  34. def test_linux_context(self):
  35. ctx = hosts.linux_context
  36. for hash in [
  37. ('$6$rounds=41128$VoQLvDjkaZ6L6BIE$4pt.1Ll1XdDYduEwEYPCMOBiR6W6'
  38. 'znsyUEoNlcVXpv2gKKIbQolgmTGe6uEEVJ7azUxuc8Tf7zV9SD2z7Ij751'),
  39. ('$5$rounds=31817$iZGmlyBQ99JSB5n6$p4E.pdPBWx19OajgjLRiOW0itGny'
  40. 'xDGgMlDcOsfaI17'),
  41. '$1$TXl/FX/U$BZge.lr.ux6ekjEjxmzwz0',
  42. 'kAJJz.Rwp0A/I',
  43. ]:
  44. self.assertTrue(ctx.verify("test", hash))
  45. self.check_unix_disabled(ctx)
  46. def test_bsd_contexts(self):
  47. for ctx in [
  48. hosts.freebsd_context,
  49. hosts.openbsd_context,
  50. hosts.netbsd_context,
  51. ]:
  52. for hash in [
  53. '$1$TXl/FX/U$BZge.lr.ux6ekjEjxmzwz0',
  54. 'kAJJz.Rwp0A/I',
  55. ]:
  56. self.assertTrue(ctx.verify("test", hash))
  57. h1 = "$2a$04$yjDgE74RJkeqC0/1NheSSOrvKeu9IbKDpcQf/Ox3qsrRS/Kw42qIS"
  58. if hashmod.bcrypt.has_backend():
  59. self.assertTrue(ctx.verify("test", h1))
  60. else:
  61. self.assertEqual(ctx.identify(h1), "bcrypt")
  62. self.check_unix_disabled(ctx)
  63. def test_host_context(self):
  64. ctx = getattr(hosts, "host_context", None)
  65. if not ctx:
  66. return self.skipTest("host_context not available on this platform")
  67. # validate schemes is non-empty,
  68. # and contains unix_disabled + at least one real scheme
  69. schemes = list(ctx.schemes())
  70. self.assertTrue(schemes, "appears to be unix system, but no known schemes supported by crypt")
  71. self.assertTrue('unix_disabled' in schemes)
  72. schemes.remove("unix_disabled")
  73. self.assertTrue(schemes, "should have schemes beside fallback scheme")
  74. self.assertTrue(set(unix_crypt_schemes).issuperset(schemes))
  75. # check for hash support
  76. self.check_unix_disabled(ctx)
  77. for scheme, hash in [
  78. ("sha512_crypt", ('$6$rounds=41128$VoQLvDjkaZ6L6BIE$4pt.1Ll1XdDYduEwEYPCMOBiR6W6'
  79. 'znsyUEoNlcVXpv2gKKIbQolgmTGe6uEEVJ7azUxuc8Tf7zV9SD2z7Ij751')),
  80. ("sha256_crypt", ('$5$rounds=31817$iZGmlyBQ99JSB5n6$p4E.pdPBWx19OajgjLRiOW0itGny'
  81. 'xDGgMlDcOsfaI17')),
  82. ("md5_crypt", '$1$TXl/FX/U$BZge.lr.ux6ekjEjxmzwz0'),
  83. ("des_crypt", 'kAJJz.Rwp0A/I'),
  84. ]:
  85. if scheme in schemes:
  86. self.assertTrue(ctx.verify("test", hash))
  87. #=============================================================================
  88. # eof
  89. #=============================================================================