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_win32pipe.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import threading
  2. import time
  3. import unittest
  4. import pywintypes
  5. import win32con
  6. import win32event
  7. import win32file
  8. import win32pipe
  9. import winerror
  10. from pywin32_testutil import str2bytes # py3k-friendly helper
  11. class PipeTests(unittest.TestCase):
  12. pipename = "\\\\.\\pipe\\python_test_pipe"
  13. def _serverThread(self, pipe_handle, event, wait_time):
  14. # just do one connection and terminate.
  15. hr = win32pipe.ConnectNamedPipe(pipe_handle)
  16. self.assertTrue(
  17. hr in (0, winerror.ERROR_PIPE_CONNECTED), "Got error code 0x%x" % (hr,)
  18. )
  19. hr, got = win32file.ReadFile(pipe_handle, 100)
  20. self.assertEqual(got, str2bytes("foo\0bar"))
  21. time.sleep(wait_time)
  22. win32file.WriteFile(pipe_handle, str2bytes("bar\0foo"))
  23. pipe_handle.Close()
  24. event.set()
  25. def startPipeServer(self, event, wait_time=0):
  26. openMode = win32pipe.PIPE_ACCESS_DUPLEX
  27. pipeMode = win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT
  28. sa = pywintypes.SECURITY_ATTRIBUTES()
  29. sa.SetSecurityDescriptorDacl(1, None, 0)
  30. pipe_handle = win32pipe.CreateNamedPipe(
  31. self.pipename,
  32. openMode,
  33. pipeMode,
  34. win32pipe.PIPE_UNLIMITED_INSTANCES,
  35. 0,
  36. 0,
  37. 2000,
  38. sa,
  39. )
  40. threading.Thread(
  41. target=self._serverThread, args=(pipe_handle, event, wait_time)
  42. ).start()
  43. def testCallNamedPipe(self):
  44. event = threading.Event()
  45. self.startPipeServer(event)
  46. got = win32pipe.CallNamedPipe(
  47. self.pipename, str2bytes("foo\0bar"), 1024, win32pipe.NMPWAIT_WAIT_FOREVER
  48. )
  49. self.assertEqual(got, str2bytes("bar\0foo"))
  50. event.wait(5)
  51. self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
  52. def testTransactNamedPipeBlocking(self):
  53. event = threading.Event()
  54. self.startPipeServer(event)
  55. open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
  56. hpipe = win32file.CreateFile(
  57. self.pipename,
  58. open_mode,
  59. 0, # no sharing
  60. None, # default security
  61. win32con.OPEN_EXISTING,
  62. 0, # win32con.FILE_FLAG_OVERLAPPED,
  63. None,
  64. )
  65. # set to message mode.
  66. win32pipe.SetNamedPipeHandleState(
  67. hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None
  68. )
  69. hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), 1024, None)
  70. self.assertEqual(got, str2bytes("bar\0foo"))
  71. event.wait(5)
  72. self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
  73. def testTransactNamedPipeBlockingBuffer(self):
  74. # Like testTransactNamedPipeBlocking, but a pre-allocated buffer is
  75. # passed (not really that useful, but it exercises the code path)
  76. event = threading.Event()
  77. self.startPipeServer(event)
  78. open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
  79. hpipe = win32file.CreateFile(
  80. self.pipename,
  81. open_mode,
  82. 0, # no sharing
  83. None, # default security
  84. win32con.OPEN_EXISTING,
  85. 0, # win32con.FILE_FLAG_OVERLAPPED,
  86. None,
  87. )
  88. # set to message mode.
  89. win32pipe.SetNamedPipeHandleState(
  90. hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None
  91. )
  92. buffer = win32file.AllocateReadBuffer(1024)
  93. hr, got = win32pipe.TransactNamedPipe(
  94. hpipe, str2bytes("foo\0bar"), buffer, None
  95. )
  96. self.assertEqual(got, str2bytes("bar\0foo"))
  97. event.wait(5)
  98. self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
  99. def testTransactNamedPipeAsync(self):
  100. event = threading.Event()
  101. overlapped = pywintypes.OVERLAPPED()
  102. overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)
  103. self.startPipeServer(event, 0.5)
  104. open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
  105. hpipe = win32file.CreateFile(
  106. self.pipename,
  107. open_mode,
  108. 0, # no sharing
  109. None, # default security
  110. win32con.OPEN_EXISTING,
  111. win32con.FILE_FLAG_OVERLAPPED,
  112. None,
  113. )
  114. # set to message mode.
  115. win32pipe.SetNamedPipeHandleState(
  116. hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None
  117. )
  118. buffer = win32file.AllocateReadBuffer(1024)
  119. hr, got = win32pipe.TransactNamedPipe(
  120. hpipe, str2bytes("foo\0bar"), buffer, overlapped
  121. )
  122. self.assertEqual(hr, winerror.ERROR_IO_PENDING)
  123. nbytes = win32file.GetOverlappedResult(hpipe, overlapped, True)
  124. got = buffer[:nbytes]
  125. self.assertEqual(got, str2bytes("bar\0foo"))
  126. event.wait(5)
  127. self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
  128. if __name__ == "__main__":
  129. unittest.main()