test_win32wnet.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import unittest
  2. import win32wnet
  3. import win32api
  4. import netbios
  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.assertEquals(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.failUnless(type(val) in (int,),
  55. "Attr %r has value %r" % (attr, val))
  56. new_val = val + 1
  57. elif typ is str:
  58. if val is not None:
  59. # on py2k, must be string or unicode. py3k must be string or bytes.
  60. self.failUnless(type(val) in (str, str),
  61. "Attr %r has value %r" % (attr, val))
  62. new_val = val + " new value"
  63. else:
  64. new_val = "new value"
  65. else:
  66. self.fail("Don't know what %s is" % (typ,))
  67. # set the attribute just to make sure we can.
  68. setattr(item, attr, new_val)
  69. def testNETRESOURCE(self):
  70. nr = win32wnet.NETRESOURCE()
  71. self._checkItemAttributes(nr, NETRESOURCE_attributes)
  72. def testWNetEnumResource(self):
  73. handle = win32wnet.WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
  74. 0, None)
  75. try:
  76. while 1:
  77. items = win32wnet.WNetEnumResource(handle, 0)
  78. if len(items)==0:
  79. break
  80. for item in items:
  81. self._checkItemAttributes(item, NETRESOURCE_attributes)
  82. finally:
  83. handle.Close()
  84. def testNCB(self):
  85. ncb = win32wnet.NCB()
  86. self._checkItemAttributes(ncb, NCB_attributes)
  87. def testNetbios(self):
  88. # taken from the demo code in netbios.py
  89. ncb = win32wnet.NCB()
  90. ncb.Command = netbios.NCBENUM
  91. la_enum = netbios.LANA_ENUM()
  92. ncb.Buffer = la_enum
  93. rc = win32wnet.Netbios(ncb)
  94. self.failUnlessEqual(rc, 0)
  95. for i in range(la_enum.length):
  96. ncb.Reset()
  97. ncb.Command = netbios.NCBRESET
  98. ncb.Lana_num = netbios.byte_to_int(la_enum.lana[i])
  99. rc = Netbios(ncb)
  100. self.failUnlessEqual(rc, 0)
  101. ncb.Reset()
  102. ncb.Command = netbios.NCBASTAT
  103. ncb.Lana_num = byte_to_int(la_enum.lana[i])
  104. ncb.Callname = str2bytes("* ") # ensure bytes on py2x and 3k
  105. adapter = netbios.ADAPTER_STATUS()
  106. ncb.Buffer = adapter
  107. Netbios(ncb)
  108. # expect 6 bytes in the mac address.
  109. self.failUnless(len(adapter.adapter_address), 6)
  110. def iterConnectableShares(self):
  111. nr = win32wnet.NETRESOURCE()
  112. nr.dwScope = RESOURCE_GLOBALNET
  113. nr.dwUsage = RESOURCEUSAGE_CONTAINER
  114. nr.lpRemoteName = "\\\\" + win32api.GetComputerName()
  115. handle = win32wnet.WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
  116. 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 = [x[0].lower() for x in win32api.GetLogicalDriveStrings().split('\0') if x]
  126. handle = win32wnet.WNetOpenEnum(RESOURCE_REMEMBERED,RESOURCETYPE_DISK,0,None)
  127. try:
  128. while 1:
  129. items = win32wnet.WNetEnumResource(handle, 0)
  130. if len(items)==0:
  131. break
  132. xtra = [i.lpLocalName[0].lower() for i in items if i.lpLocalName]
  133. existing.extend(xtra)
  134. finally:
  135. handle.Close()
  136. for maybe in 'defghijklmnopqrstuvwxyz':
  137. if maybe not in existing:
  138. return maybe
  139. self.fail("All drive mappings are taken?")
  140. def testAddConnection(self):
  141. localName = self.findUnusedDriveLetter() + ':'
  142. for share in self.iterConnectableShares():
  143. share.lpLocalName = localName
  144. win32wnet.WNetAddConnection2(share)
  145. win32wnet.WNetCancelConnection2(localName, 0, 0)
  146. break
  147. def testAddConnectionOld(self):
  148. localName = self.findUnusedDriveLetter() + ':'
  149. for share in self.iterConnectableShares():
  150. win32wnet.WNetAddConnection2(share.dwType, localName, share.lpRemoteName)
  151. win32wnet.WNetCancelConnection2(localName, 0, 0)
  152. break
  153. if __name__ == '__main__':
  154. unittest.main()