Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 8.3KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import string
  2. import sys
  3. import pythoncom
  4. import win32api
  5. from win32com.adsi import *
  6. verbose_level = 0
  7. server = "" # Must have trailing /
  8. local_name = win32api.GetComputerName()
  9. def DumpRoot():
  10. "Dumps the root DSE"
  11. path = "LDAP://%srootDSE" % server
  12. rootdse = ADsGetObject(path)
  13. for item in rootdse.Get("SupportedLDAPVersion"):
  14. print("%s supports ldap version %s" % (path, item))
  15. attributes = ["CurrentTime", "defaultNamingContext"]
  16. for attr in attributes:
  17. val = rootdse.Get(attr)
  18. print(" %s=%s" % (attr, val))
  19. ###############################################
  20. #
  21. # Code taken from article titled:
  22. # Reading attributeSchema and classSchema Objects
  23. def _DumpClass(child):
  24. attrs = "Abstract lDAPDisplayName schemaIDGUID schemaNamingContext attributeSyntax oMSyntax"
  25. _DumpTheseAttributes(child, string.split(attrs))
  26. def _DumpAttribute(child):
  27. attrs = "lDAPDisplayName schemaIDGUID adminDescription adminDisplayName rDNAttID defaultHidingValue defaultObjectCategory systemOnly defaultSecurityDescriptor"
  28. _DumpTheseAttributes(child, string.split(attrs))
  29. def _DumpTheseAttributes(child, attrs):
  30. for attr in attrs:
  31. try:
  32. val = child.Get(attr)
  33. except pythoncom.com_error as details:
  34. continue
  35. # ###
  36. (hr, msg, exc, arg) = details
  37. if exc and exc[2]:
  38. msg = exc[2]
  39. val = "<Error: %s>" % (msg,)
  40. if verbose_level >= 2:
  41. print(" %s: %s=%s" % (child.Class, attr, val))
  42. def DumpSchema():
  43. "Dumps the default DSE schema"
  44. # Bind to rootDSE to get the schemaNamingContext property.
  45. path = "LDAP://%srootDSE" % server
  46. rootdse = ADsGetObject(path)
  47. name = rootdse.Get("schemaNamingContext")
  48. # Bind to the actual schema container.
  49. path = "LDAP://" + server + name
  50. print("Binding to", path)
  51. ob = ADsGetObject(path)
  52. nclasses = nattr = nsub = nunk = 0
  53. # Enumerate the attribute and class objects in the schema container.
  54. for child in ob:
  55. # Find out if this is a class, attribute, or subSchema object.
  56. class_name = child.Class
  57. if class_name == "classSchema":
  58. _DumpClass(child)
  59. nclasses = nclasses + 1
  60. elif class_name == "attributeSchema":
  61. _DumpAttribute(child)
  62. nattr = nattr + 1
  63. elif class_name == "subSchema":
  64. nsub = nsub + 1
  65. else:
  66. print("Unknown class:", class_name)
  67. nunk = nunk + 1
  68. if verbose_level:
  69. print("Processed", nclasses, "classes")
  70. print("Processed", nattr, "attributes")
  71. print("Processed", nsub, "sub-schema's")
  72. print("Processed", nunk, "unknown types")
  73. def _DumpObject(ob, level=0):
  74. prefix = " " * level
  75. print("%s%s object: %s" % (prefix, ob.Class, ob.Name))
  76. # Do the directory object thing
  77. try:
  78. dir_ob = ADsGetObject(ob.ADsPath, IID_IDirectoryObject)
  79. except pythoncom.com_error:
  80. dir_ob = None
  81. if dir_ob is not None:
  82. info = dir_ob.GetObjectInformation()
  83. print("%s RDN='%s', ObjectDN='%s'" % (prefix, info.RDN, info.ObjectDN))
  84. # Create a list of names to fetch
  85. names = ["distinguishedName"]
  86. attrs = dir_ob.GetObjectAttributes(names)
  87. for attr in attrs:
  88. for val, typ in attr.Values:
  89. print("%s Attribute '%s' = %s" % (prefix, attr.AttrName, val))
  90. for child in ob:
  91. _DumpObject(child, level + 1)
  92. def DumpAllObjects():
  93. "Recursively dump the entire directory!"
  94. path = "LDAP://%srootDSE" % server
  95. rootdse = ADsGetObject(path)
  96. name = rootdse.Get("defaultNamingContext")
  97. # Bind to the actual schema container.
  98. path = "LDAP://" + server + name
  99. print("Binding to", path)
  100. ob = ADsGetObject(path)
  101. # Enumerate the attribute and class objects in the schema container.
  102. _DumpObject(ob)
  103. ##########################################################
  104. #
  105. # Code taken from article:
  106. # Example Code for Enumerating Schema Classes, Attributes, and Syntaxes
  107. # Fill a map with VT_ datatypes, to give us better names:
  108. vt_map = {}
  109. for name, val in pythoncom.__dict__.items():
  110. if name[:3] == "VT_":
  111. vt_map[val] = name
  112. def DumpSchema2():
  113. "Dumps the schema using an alternative technique"
  114. path = "LDAP://%sschema" % (server,)
  115. schema = ADsGetObject(path, IID_IADsContainer)
  116. nclass = nprop = nsyntax = 0
  117. for item in schema:
  118. item_class = string.lower(item.Class)
  119. if item_class == "class":
  120. items = []
  121. if item.Abstract:
  122. items.append("Abstract")
  123. if item.Auxiliary:
  124. items.append("Auxiliary")
  125. # if item.Structural: items.append("Structural")
  126. desc = string.join(items, ", ")
  127. import win32com.util
  128. iid_name = win32com.util.IIDToInterfaceName(item.PrimaryInterface)
  129. if verbose_level >= 2:
  130. print(
  131. "Class: Name=%s, Flags=%s, Primary Interface=%s"
  132. % (item.Name, desc, iid_name)
  133. )
  134. nclass = nclass + 1
  135. elif item_class == "property":
  136. if item.MultiValued:
  137. val_type = "Multi-Valued"
  138. else:
  139. val_type = "Single-Valued"
  140. if verbose_level >= 2:
  141. print("Property: Name=%s, %s" % (item.Name, val_type))
  142. nprop = nprop + 1
  143. elif item_class == "syntax":
  144. data_type = vt_map.get(item.OleAutoDataType, "<unknown type>")
  145. if verbose_level >= 2:
  146. print("Syntax: Name=%s, Datatype = %s" % (item.Name, data_type))
  147. nsyntax = nsyntax + 1
  148. if verbose_level >= 1:
  149. print("Processed", nclass, "classes")
  150. print("Processed", nprop, "properties")
  151. print("Processed", nsyntax, "syntax items")
  152. def DumpGC():
  153. "Dumps the GC: object (whatever that is!)"
  154. ob = ADsGetObject("GC:", IID_IADsContainer)
  155. for sub_ob in ob:
  156. print("GC ob: %s (%s)" % (sub_ob.Name, sub_ob.ADsPath))
  157. def DumpLocalUsers():
  158. "Dumps the local machine users"
  159. path = "WinNT://%s,computer" % (local_name,)
  160. ob = ADsGetObject(path, IID_IADsContainer)
  161. ob.put_Filter(["User", "Group"])
  162. for sub_ob in ob:
  163. print("User/Group: %s (%s)" % (sub_ob.Name, sub_ob.ADsPath))
  164. def DumpLocalGroups():
  165. "Dumps the local machine groups"
  166. path = "WinNT://%s,computer" % (local_name,)
  167. ob = ADsGetObject(path, IID_IADsContainer)
  168. ob.put_Filter(["Group"])
  169. for sub_ob in ob:
  170. print("Group: %s (%s)" % (sub_ob.Name, sub_ob.ADsPath))
  171. # get the members
  172. members = sub_ob.Members()
  173. for member in members:
  174. print(" Group member: %s (%s)" % (member.Name, member.ADsPath))
  175. def usage(tests):
  176. import os
  177. print("Usage: %s [-s server ] [-v] [Test ...]" % os.path.basename(sys.argv[0]))
  178. print(" -v : Verbose - print more information")
  179. print(" -s : server - execute the tests against the named server")
  180. print("where Test is one of:")
  181. for t in tests:
  182. print(t.__name__, ":", t.__doc__)
  183. print()
  184. print("If not tests are specified, all tests are run")
  185. sys.exit(1)
  186. def main():
  187. import getopt
  188. import traceback
  189. tests = []
  190. for ob in globals().values():
  191. if type(ob) == type(main) and ob.__doc__:
  192. tests.append(ob)
  193. opts, args = getopt.getopt(sys.argv[1:], "s:hv")
  194. for opt, val in opts:
  195. if opt == "-s":
  196. if val[-1] not in "\\/":
  197. val = val + "/"
  198. global server
  199. server = val
  200. if opt == "-h":
  201. usage(tests)
  202. if opt == "-v":
  203. global verbose_level
  204. verbose_level = verbose_level + 1
  205. if len(args) == 0:
  206. print("Running all tests - use '-h' to see command-line options...")
  207. dotests = tests
  208. else:
  209. dotests = []
  210. for arg in args:
  211. for t in tests:
  212. if t.__name__ == arg:
  213. dotests.append(t)
  214. break
  215. else:
  216. print("Test '%s' unknown - skipping" % arg)
  217. if not len(dotests):
  218. print("Nothing to do!")
  219. usage(tests)
  220. for test in dotests:
  221. try:
  222. test()
  223. except:
  224. print("Test %s failed" % test.__name__)
  225. traceback.print_exc()
  226. if __name__ == "__main__":
  227. main()