1 2from filelike.wrappers import PadToBlockSize, UnPadToBlockSize 3from filelike import tests, NotSeekableError 4 5from StringIO import StringIO 6 7 8class Test_PadToBlockSize5(tests.Test_ReadWriteSeek): 9 """Testcases for PadToBlockSize with blocksize=5.""" 10 11 contents = "this is some sample textZ" 12 empty_contents = "ZXXXX" 13 text_plain = ["Zhis is sample texty"] 14 text_padded = ["Zhis is sample textyZXXXX"] 15 blocksize = 5 16 17 def makeFile(self,contents,mode): 18 # Careful here - 'contents' should be the contents of the returned 19 # file, and is therefore expected to contain the padding. But for 20 # easy testing we allow it to omit the padding and be used directly 21 # in the underlying StringIO object. 22 idx = contents.rfind("Z") 23 if idx < 0: 24 idx = len(contents) 25 s = StringIO(contents[:idx]) 26 f = PadToBlockSize(s,self.blocksize,mode=mode) 27 def getvalue(): 28 val = s.getvalue() + "Z" 29 if len(val) % self.blocksize != 0: 30 val = val + (self.blocksize - (len(val) % self.blocksize))*"X" 31 return val 32 f.getvalue = getvalue 33 return f 34 35 def test_padding(self): 36 for (plain,padded) in zip(self.text_plain,self.text_padded): 37 f = self.makeFile(padded,"rw") 38 self.assert_(len(padded) % self.blocksize == 0) 39 self.assertEquals(f._fileobj.getvalue(),plain) 40 41 def test_write_zeds(self): 42 f = self.makeFile("","w") 43 txt = "test data Z with lots of Z's embedded in it Z" 44 f.write("test data Z w") 45 f.write("ith lots of Z's e") 46 f.write("mbedded in it Z") 47 f.write(f._padding(txt)) 48 f.flush() 49 self.assertEquals(f._fileobj.getvalue(),txt) 50 51 def test_write_at_end(self): 52 pass 53 54 55class Test_PadToBlockSize7(Test_PadToBlockSize5): 56 """Testcases for PadToBlockSize with blocksize=7.""" 57 58 contents = "this is som\n sample textZXXX" 59 empty_contents = "ZXXXXXX" 60 text_plain = ["Zhis is sample texty"] 61 text_padded = ["Zhis is sample textyZ"] 62 blocksize = 7 63 64 65class Test_PadToBlockSize16(Test_PadToBlockSize5): 66 """Testcases for PadToBlockSize with blocksize=16.""" 67 68 contents = "This is Zome Zample TeZTZXXXXXXX" 69 empty_contents = "ZXXXXXXXXXXXXXXX" 70 text_plain = ["short"] 71 text_padded = ["shortZXXXXXXXXXX"] 72 blocksize = 16 73 74 75class Test_UnPadToBlockSize5(tests.Test_ReadWriteSeek): 76 """Testcases for UnPadToBlockSize with blocksize=5.""" 77 78 contents = "this is some sample text" 79 text_plain = ["Zhis is sample texty"] 80 text_padded = ["Zhis is sample textyZXXXX"] 81 blocksize = 5 82 83 def makeFile(self,contents,mode): 84 f = UnPadToBlockSize(StringIO(""),self.blocksize,mode=mode) 85 s = StringIO(contents + f._padding(contents)) 86 f._fileobj = s 87 def getvalue(): 88 val = s.getvalue() 89 idx = val.rfind("Z") 90 return val[:idx] 91 f.getvalue = getvalue 92 return f 93 94 def test_padding(self): 95 for (plain,padded) in zip(self.text_plain,self.text_padded): 96 f = self.makeFile(plain,"rw") 97 self.assertEquals(f._fileobj.getvalue(),padded) 98 99 def test_write_zeds(self): 100 f = self.makeFile("","w") 101 txt = "test data Z with lots of Z's embedded in it Z" 102 f.write("test data Z w") 103 f.write("ith lots of Z's e") 104 f.write("mbedded in it Z") 105 f.flush() 106 self.assertEquals(f._fileobj.getvalue(),txt + f._padding(txt)) 107 108 def test_read_zeds(self): 109 f = self.makeFile("","r") 110 txt = "test data Z with lots of Z's embedded in it Z" 111 f._fileobj = StringIO(txt + f._padding(txt)) 112 self.assertEquals(f.read(),txt) 113 114 115class Test_UnPadToBlockSize7(Test_UnPadToBlockSize5): 116 """Testcases for UnPadToBlockSize with blocksize=7.""" 117 118 contents = "this is som\n sample text" 119 text_plain = ["Zhis is sample texty"] 120 text_padded = ["Zhis is sample textyZ"] 121 blocksize = 7 122 123 124class Test_UnPadToBlockSize8(Test_UnPadToBlockSize5): 125 """Testcases for UnPadToBlockSize with blocksize=7.""" 126 127 contents = "this text is a multiple of eight" 128 text_plain = ["Zhis is sample texty"] 129 text_padded = ["Zhis is sample textyZXXX"] 130 blocksize = 8 131 132 133class Test_UnPadToBlockSize16(Test_UnPadToBlockSize5): 134 """Testcases for UnPadToBlockSize with blocksize=16.""" 135 136 contents = "This is Zome Zample TeZTZ" 137 text_plain = ["short"] 138 text_padded = ["shortZXXXXXXXXXX"] 139 blocksize = 16 140 141