home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2761 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.2 KB  |  148 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import pythoncom
  5. import win32com.server.util as win32com
  6. import win32com.test.util as win32com
  7. import unittest
  8.  
  9. class Persists:
  10.     _public_methods_ = [
  11.         'GetClassID',
  12.         'IsDirty',
  13.         'Load',
  14.         'Save',
  15.         'GetSizeMax',
  16.         'InitNew']
  17.     _com_interfaces_ = [
  18.         pythoncom.IID_IPersistStreamInit]
  19.     
  20.     def __init__(self):
  21.         self.data = 'abcdefg'
  22.         self.dirty = 1
  23.  
  24.     
  25.     def GetClassID(self):
  26.         return pythoncom.IID_NULL
  27.  
  28.     
  29.     def IsDirty(self):
  30.         return self.dirty
  31.  
  32.     
  33.     def Load(self, stream):
  34.         self.data = stream.Read(26)
  35.  
  36.     
  37.     def Save(self, stream, clearDirty):
  38.         stream.Write(self.data)
  39.         if clearDirty:
  40.             self.dirty = 0
  41.         
  42.  
  43.     
  44.     def GetSizeMax(self):
  45.         return 1024
  46.  
  47.     
  48.     def InitNew(self):
  49.         pass
  50.  
  51.  
  52.  
  53. class Stream:
  54.     _public_methods_ = [
  55.         'Read',
  56.         'Write',
  57.         'Seek']
  58.     _com_interfaces_ = [
  59.         pythoncom.IID_IStream]
  60.     
  61.     def __init__(self, data):
  62.         self.data = data
  63.         self.index = 0
  64.  
  65.     
  66.     def Read(self, amount):
  67.         result = self.data[self.index:self.index + amount]
  68.         self.index = self.index + amount
  69.         return result
  70.  
  71.     
  72.     def Write(self, data):
  73.         self.data = data
  74.         self.index = 0
  75.         return len(data)
  76.  
  77.     
  78.     def Seek(self, dist, origin):
  79.         if origin == pythoncom.STREAM_SEEK_SET:
  80.             self.index = dist
  81.         elif origin == pythoncom.STREAM_SEEK_CUR:
  82.             self.index = self.index + dist
  83.         elif origin == pythoncom.STREAM_SEEK_END:
  84.             self.index = len(self.data) + dist
  85.         else:
  86.             raise ValueError, 'Unknown Seek type: ' + str(origin)
  87.         if (origin == pythoncom.STREAM_SEEK_SET).index < 0:
  88.             self.index = 0
  89.         else:
  90.             self.index = min(self.index, len(self.data))
  91.         return self.index
  92.  
  93.  
  94.  
  95. class BadStream(Stream):
  96.     
  97.     def Read(self, amount):
  98.         return 'x' * (amount + 1)
  99.  
  100.  
  101.  
  102. class StreamTest(win32com.test.util.TestCase):
  103.     
  104.     def _readWrite(self, data, write_stream, read_stream = None):
  105.         if read_stream is None:
  106.             read_stream = write_stream
  107.         
  108.         write_stream.Write(data)
  109.         read_stream.Seek(0, pythoncom.STREAM_SEEK_SET)
  110.         got = read_stream.Read(len(data))
  111.         self.assertEqual(data, got)
  112.         read_stream.Seek(1, pythoncom.STREAM_SEEK_SET)
  113.         got = read_stream.Read(len(data) - 2)
  114.         self.assertEqual(data[1:-1], got)
  115.  
  116.     
  117.     def testit(self):
  118.         mydata = 'abcdefghijklmnopqrstuvwxyz'
  119.         s = Stream(mydata)
  120.         p = Persists()
  121.         p.Load(s)
  122.         p.Save(s, 0)
  123.         self.assertEqual(s.data, mydata)
  124.         s2 = win32com.server.util.wrap(s, pythoncom.IID_IStream)
  125.         p2 = win32com.server.util.wrap(p, pythoncom.IID_IPersistStreamInit)
  126.         self._readWrite(mydata, s, s)
  127.         self._readWrite(mydata, s, s2)
  128.         self._readWrite(mydata, s2, s)
  129.         self._readWrite(mydata, s2, s2)
  130.         self._readWrite('string with\x00a NULL', s2, s2)
  131.         s.Write(mydata)
  132.         p2.Load(s2)
  133.         p2.Save(s2, 0)
  134.         self.assertEqual(s.data, mydata)
  135.         (records, old_log) = win32com.test.util.setup_test_logger()
  136.         badstream = BadStream('Check for buffer overflow')
  137.         badstream2 = win32com.server.util.wrap(badstream, pythoncom.IID_IStream)
  138.         self.assertRaises(pythoncom.com_error, badstream2.Read, 10)
  139.         win32com.test.util.restore_test_logger(old_log)
  140.         self.assertEqual(len(records), 2)
  141.         self.failUnless(records[0].msg.startswith('pythoncom error'))
  142.         self.failUnless(records[1].msg.startswith('pythoncom error'))
  143.  
  144.  
  145. if __name__ == '__main__':
  146.     unittest.main()
  147.  
  148.