test_security.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Tests for the win32security module.
  2. import sys, os
  3. import unittest
  4. import winerror
  5. from pywin32_testutil import testmain, TestSkipped, ob2memory
  6. import win32api, win32con, win32security, ntsecuritycon, pywintypes
  7. class SecurityTests(unittest.TestCase):
  8. def setUp(self):
  9. self.pwr_sid=win32security.LookupAccountName('','Power Users')[0]
  10. try:
  11. self.admin_sid=win32security.LookupAccountName('','Administrator')[0]
  12. except pywintypes.error as exc:
  13. # in automation we see:
  14. # pywintypes.error: (1332, 'LookupAccountName', 'No mapping between account names and security IDs was done.')
  15. if exc.winerror != winerror.ERROR_NONE_MAPPED:
  16. raise
  17. self.admin_sid = None
  18. def tearDown(self):
  19. pass
  20. def testEqual(self):
  21. if self.admin_sid is None:
  22. raise TestSkipped("No 'Administrator' account is available")
  23. self.failUnlessEqual(win32security.LookupAccountName('','Administrator')[0],
  24. win32security.LookupAccountName('','Administrator')[0])
  25. def testNESID(self):
  26. self.failUnless(self.pwr_sid==self.pwr_sid)
  27. if self.admin_sid:
  28. self.failUnless(self.pwr_sid!=self.admin_sid)
  29. def testNEOther(self):
  30. self.failUnless(self.pwr_sid!=None)
  31. self.failUnless(None!=self.pwr_sid)
  32. self.failIf(self.pwr_sid==None)
  33. self.failIf(None==self.pwr_sid)
  34. self.failIfEqual(None, self.pwr_sid)
  35. def testSIDInDict(self):
  36. d = dict(foo=self.pwr_sid)
  37. self.failUnlessEqual(d['foo'], self.pwr_sid)
  38. def testBuffer(self):
  39. if self.admin_sid is None:
  40. raise TestSkipped("No 'Administrator' account is available")
  41. self.failUnlessEqual(ob2memory(win32security.LookupAccountName('','Administrator')[0]),
  42. ob2memory(win32security.LookupAccountName('','Administrator')[0]))
  43. def testMemory(self):
  44. pwr_sid = self.pwr_sid
  45. admin_sid = self.admin_sid
  46. sd1=win32security.SECURITY_DESCRIPTOR()
  47. sd2=win32security.SECURITY_DESCRIPTOR()
  48. sd3=win32security.SECURITY_DESCRIPTOR()
  49. dacl=win32security.ACL()
  50. dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_READ,pwr_sid)
  51. if admin_sid is not None:
  52. dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_ALL,admin_sid)
  53. sd4=win32security.SECURITY_DESCRIPTOR()
  54. sacl=win32security.ACL()
  55. if admin_sid is not None:
  56. sacl.AddAuditAccessAce(win32security.ACL_REVISION,win32con.DELETE,admin_sid,1,1)
  57. sacl.AddAuditAccessAce(win32security.ACL_REVISION,win32con.GENERIC_ALL,pwr_sid,1,1)
  58. for x in range(0,200000):
  59. if admin_sid is not None:
  60. sd1.SetSecurityDescriptorOwner(admin_sid,0)
  61. sd2.SetSecurityDescriptorGroup(pwr_sid,0)
  62. sd3.SetSecurityDescriptorDacl(1,dacl,0)
  63. sd4.SetSecurityDescriptorSacl(1,sacl,0)
  64. class DomainTests(unittest.TestCase):
  65. def setUp(self):
  66. self.ds_handle = None
  67. try:
  68. # saving the handle means the other test itself should bind faster.
  69. self.ds_handle = win32security.DsBind()
  70. except win32security.error as exc:
  71. if exc.winerror != winerror.ERROR_NO_SUCH_DOMAIN:
  72. raise
  73. raise TestSkipped(exc)
  74. def tearDown(self):
  75. if self.ds_handle is not None:
  76. self.ds_handle.close()
  77. class TestDS(DomainTests):
  78. def testDsGetDcName(self):
  79. # Not sure what we can actually test here! At least calling it
  80. # does something :)
  81. win32security.DsGetDcName()
  82. def testDsListServerInfo(self):
  83. # again, not checking much, just exercising the code.
  84. h=win32security.DsBind()
  85. for (status, ignore, site) in win32security.DsListSites(h):
  86. for (status, ignore, server) in win32security.DsListServersInSite(h, site):
  87. info = win32security.DsListInfoForServer(h, server)
  88. for (status, ignore, domain) in win32security.DsListDomainsInSite(h, site):
  89. pass
  90. def testDsCrackNames(self):
  91. h = win32security.DsBind()
  92. fmt_offered = ntsecuritycon.DS_FQDN_1779_NAME
  93. name = win32api.GetUserNameEx(fmt_offered)
  94. result = win32security.DsCrackNames(h, 0, fmt_offered, fmt_offered, (name,))
  95. self.failUnlessEqual(name, result[0][2])
  96. def testDsCrackNamesSyntax(self):
  97. # Do a syntax check only - that allows us to avoid binding.
  98. # But must use DS_CANONICAL_NAME (or _EX)
  99. expected = win32api.GetUserNameEx(win32api.NameCanonical)
  100. fmt_offered = ntsecuritycon.DS_FQDN_1779_NAME
  101. name = win32api.GetUserNameEx(fmt_offered)
  102. result = win32security.DsCrackNames(None, ntsecuritycon.DS_NAME_FLAG_SYNTACTICAL_ONLY,
  103. fmt_offered, ntsecuritycon.DS_CANONICAL_NAME,
  104. (name,))
  105. self.failUnlessEqual(expected, result[0][2])
  106. class TestTranslate(DomainTests):
  107. def _testTranslate(self, fmt_from, fmt_to):
  108. name = win32api.GetUserNameEx(fmt_from)
  109. expected = win32api.GetUserNameEx(fmt_to)
  110. got = win32security.TranslateName(name, fmt_from, fmt_to)
  111. self.failUnlessEqual(got, expected)
  112. def testTranslate1(self):
  113. self._testTranslate(win32api.NameFullyQualifiedDN, win32api.NameSamCompatible)
  114. def testTranslate2(self):
  115. self._testTranslate(win32api.NameSamCompatible, win32api.NameFullyQualifiedDN)
  116. def testTranslate3(self):
  117. self._testTranslate(win32api.NameFullyQualifiedDN, win32api.NameUniqueId)
  118. def testTranslate4(self):
  119. self._testTranslate(win32api.NameUniqueId, win32api.NameFullyQualifiedDN)
  120. if __name__=='__main__':
  121. testmain()