|
| 1 | +#!/usr/bin/env python |
| 2 | +# vim: set et sw=4 sts=4 fileencoding=utf-8: |
| 3 | +# |
| 4 | +# A library for reading Microsoft's OLE Compound Document format |
| 5 | +# Copyright (c) 2014 Dave Hughes <dave@waveform.org.uk> |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +from __future__import ( |
| 26 | +unicode_literals, |
| 27 | +absolute_import, |
| 28 | +print_function, |
| 29 | +division, |
| 30 | + ) |
| 31 | +str=type('') |
| 32 | + |
| 33 | + |
| 34 | +importio |
| 35 | +importmmap |
| 36 | +importcompoundfiles.mmapasfake_mmap |
| 37 | +importpytest |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope='session') |
| 41 | +defsource(): |
| 42 | +returnio.open('tests/mmap.dat','rb') |
| 43 | + |
| 44 | +@pytest.fixture(params=(False,True)) |
| 45 | +deftest_map(request,source): |
| 46 | +# Run all tests against a real memory map, and our fake memory map, both |
| 47 | +# covering the same underlying file (which just contains a..z), to ensure |
| 48 | +# that the emulated behaviour matches the real implementation |
| 49 | +ifrequest.param: |
| 50 | +returnfake_mmap.FakeMemoryMap(source) |
| 51 | +else: |
| 52 | +returnmmap.mmap(source.fileno(),0,access=mmap.ACCESS_READ) |
| 53 | + |
| 54 | + |
| 55 | +deftest_read_only(test_map): |
| 56 | +withpytest.raises(TypeError): |
| 57 | +test_map.write(b'foo') |
| 58 | +withpytest.raises(TypeError): |
| 59 | +test_map.write_byte(b'f') |
| 60 | +withpytest.raises(TypeError): |
| 61 | +test_map[:6]=b'foobar' |
| 62 | +withpytest.raises(TypeError): |
| 63 | +test_map.move(5,10,4) |
| 64 | +withpytest.raises(TypeError): |
| 65 | +test_map.resize(1000) |
| 66 | +# This doesn't raise an error ... even with a read-only mmap! |
| 67 | +test_map.flush() |
| 68 | + |
| 69 | +deftest_len(test_map): |
| 70 | +assertlen(test_map)==26 |
| 71 | + |
| 72 | +deftest_indexing(test_map): |
| 73 | +asserttest_map[0]==b'a' |
| 74 | +asserttest_map[4]==b'e' |
| 75 | +asserttest_map[-1]==b'z' |
| 76 | +asserttest_map[-3]==b'x' |
| 77 | +withpytest.raises(IndexError): |
| 78 | +test_map[30] |
| 79 | +withpytest.raises(IndexError): |
| 80 | +test_map[-30] |
| 81 | + |
| 82 | +deftest_slicing(test_map): |
| 83 | +asserttest_map[:0]==b'' |
| 84 | +asserttest_map[:1]==b'a' |
| 85 | +asserttest_map[:5]==b'abcde' |
| 86 | +asserttest_map[1:5]==b'bcde' |
| 87 | +asserttest_map[1:5:2]==b'bd' |
| 88 | +asserttest_map[2:5:3]==b'c' |
| 89 | +asserttest_map[:-3]==b'abcdefghijklmnopqrstuvw' |
| 90 | +asserttest_map[0:]==b'abcdefghijklmnopqrstuvwxyz' |
| 91 | +asserttest_map[-3:]==b'xyz' |
| 92 | +asserttest_map[-3::1]==b'xyz' |
| 93 | +asserttest_map[-3::2]==b'xz' |
| 94 | +asserttest_map[-5:-1:2]==b'vx' |
| 95 | + |
| 96 | +deftest_negative_slicing(test_map): |
| 97 | +asserttest_map[::-1]==b''.join(reversed(test_map[:])) |
| 98 | +asserttest_map[::-2]==b'zxvtrpnljhfdb' |
| 99 | +asserttest_map[-2::-2]==b'ywusqomkigeca' |
| 100 | +asserttest_map[5:10:-1]==b'' |
| 101 | +asserttest_map[10:5:-1]==b'kjihg' |
| 102 | + |
| 103 | +deftest_bad_slice(test_map): |
| 104 | +withpytest.raises(ValueError): |
| 105 | +test_map[5:10:0] |
| 106 | + |
| 107 | +deftest_contains(test_map): |
| 108 | +# See comments in FakeMemoryMap.__contains__ to understand this! |
| 109 | +assertb'a'intest_map |
| 110 | +assertb'abc'notintest_map |
| 111 | +assertb'd'intest_map |
| 112 | +assertb'def'notintest_map |
| 113 | +assertb'vwxyz'notintest_map |
| 114 | +assertb'blah'notintest_map |
| 115 | + |
| 116 | +deftest_find(test_map): |
| 117 | +asserttest_map.find(b'abc')==0 |
| 118 | +asserttest_map.find(b'abc',5)==-1 |
| 119 | +asserttest_map.find(b'xyz')==23 |
| 120 | +asserttest_map.find(b'xyz',0,-1)==-1 |
| 121 | +asserttest_map.find(b'foobar')==-1 |
| 122 | + |
| 123 | +deftest_rfind(test_map): |
| 124 | +asserttest_map.rfind(b'abc')==0 |
| 125 | +asserttest_map.rfind(b'abc',5)==-1 |
| 126 | +asserttest_map.rfind(b'xyz')==23 |
| 127 | +asserttest_map.rfind(b'xyz',0,-1)==-1 |
| 128 | +asserttest_map.rfind(b'foobar')==-1 |
| 129 | + |
| 130 | +deftest_seek_n_read(test_map): |
| 131 | +test_map.seek(0) |
| 132 | +asserttest_map.read(26)==b'abcdefghijklmnopqrstuvwxyz' |
| 133 | +test_map.seek(0) |
| 134 | +asserttest_map.read(5)==b'abcde' |
| 135 | +asserttest_map.read(2)==b'fg' |
| 136 | +test_map.seek(-3,io.SEEK_END) |
| 137 | +asserttest_map.read(-1)==b'xyz' |
| 138 | +asserttest_map.read(-1)==b'' |
| 139 | +test_map.seek(-3,io.SEEK_CUR) |
| 140 | +asserttest_map.read_byte()==b'x' |
| 141 | +asserttest_map.read_byte()==b'y' |
| 142 | +asserttest_map.read_byte()==b'z' |
| 143 | +test_map.seek(0,io.SEEK_SET) |
| 144 | +asserttest_map.readline()==b'abcdefghijklmnopqrstuvwxyz' |
| 145 | + |
| 146 | +deftest_seek_n_tell(test_map): |
| 147 | +test_map.seek(0) |
| 148 | +asserttest_map.tell()==0 |
| 149 | +test_map.seek(0,io.SEEK_END) |
| 150 | +asserttest_map.tell()==26 |
| 151 | +test_map.seek(-3,io.SEEK_CUR) |
| 152 | +asserttest_map.tell()==23 |
| 153 | +test_map.seek(0,io.SEEK_SET) |
| 154 | +asserttest_map.tell()==0 |