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_win32wnet.py 5.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import unittest
  2. import netbios
  3. import win32api
  4. import win32wnet
  5. from pywin32_testutil import str2bytes
  6. RESOURCE_CONNECTED = 0x00000001
  7. RESOURCE_GLOBALNET = 0x00000002
  8. RESOURCE_REMEMBERED = 0x00000003
  9. RESOURCE_RECENT = 0x00000004
  10. RESOURCE_CONTEXT = 0x00000005
  11. RESOURCETYPE_ANY = 0x00000000
  12. RESOURCETYPE_DISK = 0x00000001
  13. RESOURCETYPE_PRINT = 0x00000002
  14. RESOURCETYPE_RESERVED = 0x00000008
  15. RESOURCETYPE_UNKNOWN = 0xFFFFFFFF
  16. RESOURCEUSAGE_CONNECTABLE = 0x00000001
  17. RESOURCEUSAGE_CONTAINER = 0x00000002
  18. RESOURCEDISPLAYTYPE_GENERIC = 0x00000000
  19. RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001
  20. RESOURCEDISPLAYTYPE_SERVER = 0x00000002
  21. RESOURCEDISPLAYTYPE_SHARE = 0x00000003
  22. NETRESOURCE_attributes = [
  23. ("dwScope", int),
  24. ("dwType", int),
  25. ("dwDisplayType", int),
  26. ("dwUsage", int),
  27. ("lpLocalName", str),
  28. ("lpRemoteName", str),
  29. ("lpComment", str),
  30. ("lpProvider", str),
  31. ]
  32. NCB_attributes = [
  33. ("Command", int),
  34. ("Retcode", int),
  35. ("Lsn", int),
  36. ("Num", int),
  37. # ("Bufflen", int), - read-only
  38. ("Callname", str),
  39. ("Name", str),
  40. ("Rto", int),
  41. ("Sto", int),
  42. ("Lana_num", int),
  43. ("Cmd_cplt", int),
  44. ("Event", int),
  45. ("Post", int),
  46. ]
  47. class TestCase(unittest.TestCase):
  48. def testGetUser(self):
  49. self.assertEqual(win32api.GetUserName(), win32wnet.WNetGetUser())
  50. def _checkItemAttributes(self, item, attrs):
  51. for attr, typ in attrs:
  52. val = getattr(item, attr)
  53. if typ is int:
  54. self.assertTrue(
  55. type(val) in (int,), "Attr %r has value %r" % (attr, val)
  56. )
  57. new_val = val + 1
  58. elif typ is str:
  59. if val is not None:
  60. # on py2k, must be string or unicode. py3k must be string or bytes.
  61. self.assertTrue(
  62. type(val) in (str, str), "Attr %r has value %r" % (attr, val)
  63. )
  64. new_val = val + " new value"
  65. else:
  66. new_val = "new value"
  67. else:
  68. self.fail("Don't know what %s is" % (typ,))
  69. # set the attribute just to make sure we can.
  70. setattr(item, attr, new_val)
  71. def testNETRESOURCE(self):
  72. nr = win32wnet.NETRESOURCE()
  73. self._checkItemAttributes(nr, NETRESOURCE_attributes)
  74. def testWNetEnumResource(self):
  75. handle = win32wnet.WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, None)
  76. try:
  77. while 1:
  78. items = win32wnet.WNetEnumResource(handle, 0)
  79. if len(items) == 0:
  80. break
  81. for item in items:
  82. self._checkItemAttributes(item, NETRESOURCE_attributes)
  83. finally:
  84. handle.Close()
  85. def testNCB(self):
  86. ncb = win32wnet.NCB()
  87. self._checkItemAttributes(ncb, NCB_attributes)
  88. def testNetbios(self):
  89. # taken from the demo code in netbios.py
  90. ncb = win32wnet.NCB()
  91. ncb.Command = netbios.NCBENUM
  92. la_enum = netbios.LANA_ENUM()
  93. ncb.Buffer = la_enum
  94. rc = win32wnet.Netbios(ncb)
  95. self.assertEqual(rc, 0)
  96. for i in range(la_enum.length):
  97. ncb.Reset()
  98. ncb.Command = netbios.NCBRESET
  99. ncb.Lana_num = netbios.byte_to_int(la_enum.lana[i])
  100. rc = Netbios(ncb)
  101. self.assertEqual(rc, 0)
  102. ncb.Reset()
  103. ncb.Command = netbios.NCBASTAT
  104. ncb.Lana_num = byte_to_int(la_enum.lana[i])
  105. ncb.Callname = str2bytes("* ") # ensure bytes on py2x and 3k
  106. adapter = netbios.ADAPTER_STATUS()
  107. ncb.Buffer = adapter
  108. Netbios(ncb)
  109. # expect 6 bytes in the mac address.
  110. self.assertTrue(len(adapter.adapter_address), 6)
  111. def iterConnectableShares(self):
  112. nr = win32wnet.NETRESOURCE()
  113. nr.dwScope = RESOURCE_GLOBALNET
  114. nr.dwUsage = RESOURCEUSAGE_CONTAINER
  115. nr.lpRemoteName = "\\\\" + win32api.GetComputerName()
  116. handle = win32wnet.WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, nr)
  117. while 1:
  118. items = win32wnet.WNetEnumResource(handle, 0)
  119. if len(items) == 0:
  120. break
  121. for item in items:
  122. if item.dwDisplayType == RESOURCEDISPLAYTYPE_SHARE:
  123. yield item
  124. def findUnusedDriveLetter(self):
  125. existing = [
  126. x[0].lower() for x in win32api.GetLogicalDriveStrings().split("\0") if x
  127. ]
  128. handle = win32wnet.WNetOpenEnum(RESOURCE_REMEMBERED, RESOURCETYPE_DISK, 0, None)
  129. try:
  130. while 1:
  131. items = win32wnet.WNetEnumResource(handle, 0)
  132. if len(items) == 0:
  133. break
  134. xtra = [i.lpLocalName[0].lower() for i in items if i.lpLocalName]
  135. existing.extend(xtra)
  136. finally:
  137. handle.Close()
  138. for maybe in "defghijklmnopqrstuvwxyz":
  139. if maybe not in existing:
  140. return maybe
  141. self.fail("All drive mappings are taken?")
  142. def testAddConnection(self):
  143. localName = self.findUnusedDriveLetter() + ":"
  144. for share in self.iterConnectableShares():
  145. share.lpLocalName = localName
  146. win32wnet.WNetAddConnection2(share)
  147. win32wnet.WNetCancelConnection2(localName, 0, 0)
  148. break
  149. def testAddConnectionOld(self):
  150. localName = self.findUnusedDriveLetter() + ":"
  151. for share in self.iterConnectableShares():
  152. win32wnet.WNetAddConnection2(share.dwType, localName, share.lpRemoteName)
  153. win32wnet.WNetCancelConnection2(localName, 0, 0)
  154. break
  155. if __name__ == "__main__":
  156. unittest.main()