Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc804050

Browse files
committed
More work on#2
100% coverage on streams module now
1 parent4ae497e commitc804050

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

‎compoundfiles/streams.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ def read(self, n=-1):
163163
i=0
164164
whilei<n:
165165
buf=self.read1(n-i)
166-
ifnotbuf:
167-
break
166+
assertbuf
168167
result[i:i+len(buf)]=buf
169168
i+=len(buf)
170169
returnbytes(result)
@@ -227,8 +226,8 @@ def __init__(self, parent, start, length=None):
227226
iflengthisnotNoneandlength>max_length:
228227
warnings.warn(
229228
CompoundFileDirSizeWarning(
230-
'length (%d) of stream at sector %d exceedsmax'% (
231-
length,start,max_length)))
229+
'length (%d) of stream at sector %d exceeds'
230+
'max (%d)'% (length,start,max_length)))
232231
self._length=min(max_length,lengthormax_length)
233232
self._set_pos(0)
234233

‎tests/example2.dat

7.5 KB
Binary file not shown.
File renamed without changes.

‎tests/invalid_dir_size2.dat

3.5 KB
Binary file not shown.

‎tests/invalid_fat_loop.dat

3 KB
Binary file not shown.

‎tests/test_function.py

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
str=type('')
3232

3333

34+
importio
3435
importcompoundfilesascf
3536
importpytest
3637
importwarnings
@@ -66,7 +67,7 @@ def verify_example(doc, contents=None):
6667
DirEntry('Storage 1',False,1),
6768
DirEntry('Storage 1/Stream 1',True,544),
6869
)
69-
verify_contents(doc,contents)
70+
verify_contents(doc,contents)
7071

7172

7273
deftest_function_sample1_doc():
@@ -214,19 +215,70 @@ def test_invalid_dir_misc():
214215

215216
deftest_invalid_dir_size():
216217
withwarnings.catch_warnings(record=True)asw:
217-
# Same file as example.dat with size of Stream 1 corrupted; library
218-
# re-writes size to be within 32-bits
219-
doc=cf.CompoundFileReader('tests/invalid_dir_size.dat')
218+
# Same file as example.dat with size of Stream 1 corrupted (> 32-bits);
219+
#libraryre-writes size to be within 32-bits
220+
doc=cf.CompoundFileReader('tests/invalid_dir_size1.dat')
220221
assertissubclass(w[0].category,cf.CompoundFileDirSizeWarning)
221222
assertissubclass(w[1].category,cf.CompoundFileDirSizeWarning)
222223
assertlen(w)==2
223224
verify_example(doc, (
224225
DirEntry('Storage 1',False,1),
225-
DirEntry('Storage 1/Stream 1',False,0xFFFFFFFF),
226+
DirEntry('Storage 1/Stream 1',True,0xFFFFFFFF),
226227
))
227228

228229
deftest_invalid_dir_loop():
229230
withpytest.raises(cf.CompoundFileDirLoopError):
231+
# Same as example.dat but with Stream 1's left pointer corrupted to
232+
# point to the Root Entry
230233
doc=cf.CompoundFileReader('tests/invalid_dir_loop.dat')
231234

235+
deftest_invalid_fat_loop():
236+
withpytest.raises(cf.CompoundFileNormalLoopError):
237+
# Sample as example.dat but with Stream 1's FAT entry corrupted to
238+
# point to itself
239+
doc=cf.CompoundFileReader('tests/invalid_fat_loop.dat')
240+
241+
deftest_stream_attr():
242+
withcf.CompoundFileReader('tests/example.dat')asdoc:
243+
withdoc.open('Storage 1/Stream 1')asf:
244+
assertf.readable()
245+
assertnotf.writable()
246+
assertf.seekable()
247+
248+
deftest_stream_seek():
249+
withcf.CompoundFileReader('tests/example.dat')asdoc:
250+
withdoc.open('Storage 1/Stream 1')asf:
251+
assertf.seek(0,io.SEEK_END)==544
252+
assertf.seek(0,io.SEEK_CUR)==544
253+
withpytest.raises(ValueError):
254+
f.seek(-1)
255+
256+
deftest_stream_read():
257+
withcf.CompoundFileReader('tests/example2.dat')asdoc:
258+
# Same file as example.dat with an additional Stream 2 which is 4112
259+
# bytes long (too long for mini FAT)
260+
withdoc.open('Storage 1/Stream 1')asf:
261+
assertlen(f.read())==544
262+
f.seek(0)
263+
assertlen(f.read(1024))==544
264+
f.seek(0)
265+
assertlen(f.read1())==64
266+
f.seek(0,io.SEEK_END)
267+
assertf.read1()==b''
268+
withdoc.open('Storage 1/Stream 2')asf:
269+
assertlen(f.read())==4112
270+
f.seek(0)
271+
assertlen(f.read1())==512
272+
f.seek(0,io.SEEK_END)
273+
assertf.read1()==b''
232274

275+
deftest_stream_read_broken_size():
276+
withcf.CompoundFileReader('tests/invalid_dir_size2.dat')asdoc:
277+
# Same file as example.dat with size of Stream 1 corrupted to 3072
278+
# bytes (small enough to fit in the mini FAT but too large for the
279+
# actual data which is 544 bytes), and additional Stream 2 which has
280+
# corrupted size 8192 bytes (actual size 512 bytes)
281+
withdoc.open('Storage 1/Stream 1')asf:
282+
assertf.seek(0,io.SEEK_END)==576
283+
withdoc.open('Storage 1/Stream 2')asf:
284+
assertf.seek(0,io.SEEK_END)==512

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp