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.

testStreams.py 4.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import unittest
  2. import pythoncom
  3. import win32com.server.util
  4. import win32com.test.util
  5. from pywin32_testutil import str2bytes
  6. class Persists:
  7. _public_methods_ = [
  8. "GetClassID",
  9. "IsDirty",
  10. "Load",
  11. "Save",
  12. "GetSizeMax",
  13. "InitNew",
  14. ]
  15. _com_interfaces_ = [pythoncom.IID_IPersistStreamInit]
  16. def __init__(self):
  17. self.data = str2bytes("abcdefg")
  18. self.dirty = 1
  19. def GetClassID(self):
  20. return pythoncom.IID_NULL
  21. def IsDirty(self):
  22. return self.dirty
  23. def Load(self, stream):
  24. self.data = stream.Read(26)
  25. def Save(self, stream, clearDirty):
  26. stream.Write(self.data)
  27. if clearDirty:
  28. self.dirty = 0
  29. def GetSizeMax(self):
  30. return 1024
  31. def InitNew(self):
  32. pass
  33. class Stream:
  34. _public_methods_ = ["Read", "Write", "Seek"]
  35. _com_interfaces_ = [pythoncom.IID_IStream]
  36. def __init__(self, data):
  37. self.data = data
  38. self.index = 0
  39. def Read(self, amount):
  40. result = self.data[self.index : self.index + amount]
  41. self.index = self.index + amount
  42. return result
  43. def Write(self, data):
  44. self.data = data
  45. self.index = 0
  46. return len(data)
  47. def Seek(self, dist, origin):
  48. if origin == pythoncom.STREAM_SEEK_SET:
  49. self.index = dist
  50. elif origin == pythoncom.STREAM_SEEK_CUR:
  51. self.index = self.index + dist
  52. elif origin == pythoncom.STREAM_SEEK_END:
  53. self.index = len(self.data) + dist
  54. else:
  55. raise ValueError("Unknown Seek type: " + str(origin))
  56. if self.index < 0:
  57. self.index = 0
  58. else:
  59. self.index = min(self.index, len(self.data))
  60. return self.index
  61. class BadStream(Stream):
  62. """PyGStream::Read could formerly overflow buffer if the python implementation
  63. returned more data than requested.
  64. """
  65. def Read(self, amount):
  66. return str2bytes("x") * (amount + 1)
  67. class StreamTest(win32com.test.util.TestCase):
  68. def _readWrite(self, data, write_stream, read_stream=None):
  69. if read_stream is None:
  70. read_stream = write_stream
  71. write_stream.Write(data)
  72. read_stream.Seek(0, pythoncom.STREAM_SEEK_SET)
  73. got = read_stream.Read(len(data))
  74. self.assertEqual(data, got)
  75. read_stream.Seek(1, pythoncom.STREAM_SEEK_SET)
  76. got = read_stream.Read(len(data) - 2)
  77. self.assertEqual(data[1:-1], got)
  78. def testit(self):
  79. mydata = str2bytes("abcdefghijklmnopqrstuvwxyz")
  80. # First test the objects just as Python objects...
  81. s = Stream(mydata)
  82. p = Persists()
  83. p.Load(s)
  84. p.Save(s, 0)
  85. self.assertEqual(s.data, mydata)
  86. # Wrap the Python objects as COM objects, and make the calls as if
  87. # they were non-Python COM objects.
  88. s2 = win32com.server.util.wrap(s, pythoncom.IID_IStream)
  89. p2 = win32com.server.util.wrap(p, pythoncom.IID_IPersistStreamInit)
  90. self._readWrite(mydata, s, s)
  91. self._readWrite(mydata, s, s2)
  92. self._readWrite(mydata, s2, s)
  93. self._readWrite(mydata, s2, s2)
  94. self._readWrite(str2bytes("string with\0a NULL"), s2, s2)
  95. # reset the stream
  96. s.Write(mydata)
  97. p2.Load(s2)
  98. p2.Save(s2, 0)
  99. self.assertEqual(s.data, mydata)
  100. def testseek(self):
  101. s = Stream(str2bytes("yo"))
  102. s = win32com.server.util.wrap(s, pythoncom.IID_IStream)
  103. # we used to die in py3k passing a value > 32bits
  104. s.Seek(0x100000000, pythoncom.STREAM_SEEK_SET)
  105. def testerrors(self):
  106. # setup a test logger to capture tracebacks etc.
  107. records, old_log = win32com.test.util.setup_test_logger()
  108. ## check for buffer overflow in Read method
  109. badstream = BadStream("Check for buffer overflow")
  110. badstream2 = win32com.server.util.wrap(badstream, pythoncom.IID_IStream)
  111. self.assertRaises(pythoncom.com_error, badstream2.Read, 10)
  112. win32com.test.util.restore_test_logger(old_log)
  113. # there's 1 error here
  114. self.assertEqual(len(records), 1)
  115. self.assertTrue(records[0].msg.startswith("pythoncom error"))
  116. if __name__ == "__main__":
  117. unittest.main()