test_spwd.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. import unittest
  3. from test import test_support
  4. spwd = test_support.import_module('spwd')
  5. @unittest.skipUnless(hasattr(os, 'geteuid') and os.geteuid() == 0,
  6. 'root privileges required')
  7. class TestSpwdRoot(unittest.TestCase):
  8. def test_getspall(self):
  9. entries = spwd.getspall()
  10. self.assertIsInstance(entries, list)
  11. for entry in entries:
  12. self.assertIsInstance(entry, spwd.struct_spwd)
  13. def test_getspnam(self):
  14. entries = spwd.getspall()
  15. if not entries:
  16. self.skipTest('empty shadow password database')
  17. random_name = entries[0].sp_nam
  18. entry = spwd.getspnam(random_name)
  19. self.assertIsInstance(entry, spwd.struct_spwd)
  20. self.assertEqual(entry.sp_nam, random_name)
  21. self.assertEqual(entry.sp_nam, entry[0])
  22. self.assertIsInstance(entry.sp_pwd, str)
  23. self.assertEqual(entry.sp_pwd, entry[1])
  24. self.assertIsInstance(entry.sp_lstchg, int)
  25. self.assertEqual(entry.sp_lstchg, entry[2])
  26. self.assertIsInstance(entry.sp_min, int)
  27. self.assertEqual(entry.sp_min, entry[3])
  28. self.assertIsInstance(entry.sp_max, int)
  29. self.assertEqual(entry.sp_max, entry[4])
  30. self.assertIsInstance(entry.sp_warn, int)
  31. self.assertEqual(entry.sp_warn, entry[5])
  32. self.assertIsInstance(entry.sp_inact, int)
  33. self.assertEqual(entry.sp_inact, entry[6])
  34. self.assertIsInstance(entry.sp_expire, int)
  35. self.assertEqual(entry.sp_expire, entry[7])
  36. self.assertIsInstance(entry.sp_flag, int)
  37. self.assertEqual(entry.sp_flag, entry[8])
  38. with self.assertRaises(KeyError) as cx:
  39. spwd.getspnam('invalid user name')
  40. self.assertEqual(str(cx.exception), "'getspnam(): name not found'")
  41. self.assertRaises(TypeError, spwd.getspnam)
  42. self.assertRaises(TypeError, spwd.getspnam, 0)
  43. self.assertRaises(TypeError, spwd.getspnam, random_name, 0)
  44. if test_support.have_unicode:
  45. try:
  46. unicode_name = unicode(random_name)
  47. except UnicodeDecodeError:
  48. pass
  49. else:
  50. self.assertEqual(spwd.getspnam(unicode_name), entry)
  51. def test_main():
  52. test_support.run_unittest(TestSpwdRoot)
  53. if __name__ == "__main__":
  54. test_main()