25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

140 satır
5.2 KiB

  1. """test passlib.apps"""
  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 apps, hash as hashmod
  11. from passlib.tests.utils import TestCase
  12. # module
  13. #=============================================================================
  14. # test predefined app contexts
  15. #=============================================================================
  16. class AppsTest(TestCase):
  17. """perform general tests to make sure contexts work"""
  18. # NOTE: these tests are not really comprehensive,
  19. # since they would do little but duplicate
  20. # the presets in apps.py
  21. #
  22. # they mainly try to ensure no typos
  23. # or dynamic behavior foul-ups.
  24. def test_master_context(self):
  25. ctx = apps.master_context
  26. self.assertGreater(len(ctx.schemes()), 50)
  27. def test_custom_app_context(self):
  28. ctx = apps.custom_app_context
  29. self.assertEqual(ctx.schemes(), ("sha512_crypt", "sha256_crypt"))
  30. for hash in [
  31. ('$6$rounds=41128$VoQLvDjkaZ6L6BIE$4pt.1Ll1XdDYduEwEYPCMOBiR6W6'
  32. 'znsyUEoNlcVXpv2gKKIbQolgmTGe6uEEVJ7azUxuc8Tf7zV9SD2z7Ij751'),
  33. ('$5$rounds=31817$iZGmlyBQ99JSB5n6$p4E.pdPBWx19OajgjLRiOW0itGny'
  34. 'xDGgMlDcOsfaI17'),
  35. ]:
  36. self.assertTrue(ctx.verify("test", hash))
  37. def test_django16_context(self):
  38. ctx = apps.django16_context
  39. for hash in [
  40. 'pbkdf2_sha256$29000$ZsgquwnCyBs2$fBxRQpfKd2PIeMxtkKPy0h7SrnrN+EU/cm67aitoZ2s=',
  41. 'sha1$0d082$cdb462ae8b6be8784ef24b20778c4d0c82d5957f',
  42. 'md5$b887a$37767f8a745af10612ad44c80ff52e92',
  43. 'crypt$95a6d$95x74hLDQKXI2',
  44. '098f6bcd4621d373cade4e832627b4f6',
  45. ]:
  46. self.assertTrue(ctx.verify("test", hash))
  47. self.assertEqual(ctx.identify("!"), "django_disabled")
  48. self.assertFalse(ctx.verify("test", "!"))
  49. def test_django_context(self):
  50. ctx = apps.django_context
  51. for hash in [
  52. 'pbkdf2_sha256$29000$ZsgquwnCyBs2$fBxRQpfKd2PIeMxtkKPy0h7SrnrN+EU/cm67aitoZ2s=',
  53. ]:
  54. self.assertTrue(ctx.verify("test", hash))
  55. self.assertEqual(ctx.identify("!"), "django_disabled")
  56. self.assertFalse(ctx.verify("test", "!"))
  57. def test_ldap_nocrypt_context(self):
  58. ctx = apps.ldap_nocrypt_context
  59. for hash in [
  60. '{SSHA}cPusOzd6d5n3OjSVK3R329ZGCNyFcC7F',
  61. 'test',
  62. ]:
  63. self.assertTrue(ctx.verify("test", hash))
  64. self.assertIs(ctx.identify('{CRYPT}$5$rounds=31817$iZGmlyBQ99JSB5'
  65. 'n6$p4E.pdPBWx19OajgjLRiOW0itGnyxDGgMlDcOsfaI17'), None)
  66. def test_ldap_context(self):
  67. ctx = apps.ldap_context
  68. for hash in [
  69. ('{CRYPT}$5$rounds=31817$iZGmlyBQ99JSB5n6$p4E.pdPBWx19OajgjLRiOW0'
  70. 'itGnyxDGgMlDcOsfaI17'),
  71. '{SSHA}cPusOzd6d5n3OjSVK3R329ZGCNyFcC7F',
  72. 'test',
  73. ]:
  74. self.assertTrue(ctx.verify("test", hash))
  75. def test_ldap_mysql_context(self):
  76. ctx = apps.mysql_context
  77. for hash in [
  78. '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29',
  79. '378b243e220ca493',
  80. ]:
  81. self.assertTrue(ctx.verify("test", hash))
  82. def test_postgres_context(self):
  83. ctx = apps.postgres_context
  84. hash = 'md55d9c68c6c50ed3d02a2fcf54f63993b6'
  85. self.assertTrue(ctx.verify("test", hash, user='user'))
  86. def test_phppass_context(self):
  87. ctx = apps.phpass_context
  88. for hash in [
  89. '$P$8Ja1vJsKa5qyy/b3mCJGXM7GyBnt6..',
  90. '$H$8b95CoYQnQ9Y6fSTsACyphNh5yoM02.',
  91. '_cD..aBxeRhYFJvtUvsI',
  92. ]:
  93. self.assertTrue(ctx.verify("test", hash))
  94. h1 = "$2a$04$yjDgE74RJkeqC0/1NheSSOrvKeu9IbKDpcQf/Ox3qsrRS/Kw42qIS"
  95. if hashmod.bcrypt.has_backend():
  96. self.assertTrue(ctx.verify("test", h1))
  97. self.assertEqual(ctx.default_scheme(), "bcrypt")
  98. self.assertEqual(ctx.handler().name, "bcrypt")
  99. else:
  100. self.assertEqual(ctx.identify(h1), "bcrypt")
  101. self.assertEqual(ctx.default_scheme(), "phpass")
  102. self.assertEqual(ctx.handler().name, "phpass")
  103. def test_phpbb3_context(self):
  104. ctx = apps.phpbb3_context
  105. for hash in [
  106. '$P$8Ja1vJsKa5qyy/b3mCJGXM7GyBnt6..',
  107. '$H$8b95CoYQnQ9Y6fSTsACyphNh5yoM02.',
  108. ]:
  109. self.assertTrue(ctx.verify("test", hash))
  110. self.assertTrue(ctx.hash("test").startswith("$H$"))
  111. def test_roundup_context(self):
  112. ctx = apps.roundup_context
  113. for hash in [
  114. '{PBKDF2}9849$JMTYu3eOUSoFYExprVVqbQ$N5.gV.uR1.BTgLSvi0qyPiRlGZ0',
  115. '{SHA}a94a8fe5ccb19ba61c4c0873d391e987982fbbd3',
  116. '{CRYPT}dptOmKDriOGfU',
  117. '{plaintext}test',
  118. ]:
  119. self.assertTrue(ctx.verify("test", hash))
  120. #=============================================================================
  121. # eof
  122. #=============================================================================