| 1 |
# Copyright (C) 2003-2009 Robey Pointer <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of paramiko. |
| 4 |
# |
| 5 |
# Paramiko is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU Lesser General Public License as published by the Free |
| 7 |
# Software Foundation; either version 2.1 of the License, or (at your option) |
| 8 |
# any later version. |
| 9 |
# |
| 10 |
# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 13 |
# details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with Paramiko; if not, write to the Free Software Foundation, Inc., |
| 17 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
|
| 19 |
""" |
| 20 |
Some unit tests for the BufferedFile abstraction. |
| 21 |
""" |
| 22 |
|
| 23 |
import unittest |
| 24 |
from io import BytesIO |
| 25 |
|
| 26 |
from paramiko.common import linefeed_byte, crlf, cr_byte |
| 27 |
from paramiko.file import BufferedFile |
| 28 |
|
| 29 |
from ._util import needs_builtin |
| 30 |
|
| 31 |
|
| 32 |
class LoopbackFile(BufferedFile): |
| 33 |
""" |
| 34 |
BufferedFile object that you can write data into, and then read it back. |
| 35 |
""" |
| 36 |
|
| 37 |
def __init__(self, mode="r", bufsize=-1): |
| 38 |
BufferedFile.__init__(self) |
| 39 |
self._set_mode(mode, bufsize) |
| 40 |
self.buffer = BytesIO() |
| 41 |
self.offset = 0 |
| 42 |
|
| 43 |
def _read(self, size): |
| 44 |
data = self.buffer.getvalue()[self.offset : self.offset + size] |
| 45 |
self.offset += len(data) |
| 46 |
return data |
| 47 |
|
| 48 |
def _write(self, data): |
| 49 |
self.buffer.write(data) |
| 50 |
return len(data) |
| 51 |
|
| 52 |
|
| 53 |
class BufferedFileTest(unittest.TestCase): |
| 54 |
def test_simple(self): |
| 55 |
f = LoopbackFile("r") |
| 56 |
try: |
| 57 |
f.write(b"hi") |
| 58 |
self.assertTrue(False, "no exception on write to read-only file") |
| 59 |
except: |
| 60 |
pass |
| 61 |
f.close() |
| 62 |
|
| 63 |
f = LoopbackFile("w") |
| 64 |
try: |
| 65 |
f.read(1) |
| 66 |
self.assertTrue(False, "no exception to read from write-only file") |
| 67 |
except: |
| 68 |
pass |
| 69 |
f.close() |
| 70 |
|
| 71 |
def test_readline(self): |
| 72 |
f = LoopbackFile("r+U") |
| 73 |
f.write( |
| 74 |
b"First line.\nSecond line.\r\nThird line.\n" |
| 75 |
+ b"Fourth line.\nFinal line non-terminated." |
| 76 |
) |
| 77 |
|
| 78 |
self.assertEqual(f.readline(), "First line.\n") |
| 79 |
# universal newline mode should convert this linefeed: |
| 80 |
self.assertEqual(f.readline(), "Second line.\n") |
| 81 |
# truncated line: |
| 82 |
self.assertEqual(f.readline(7), "Third l") |
| 83 |
self.assertEqual(f.readline(), "ine.\n") # codespell:ignore |
| 84 |
# newline should be detected and only the fourth line returned |
| 85 |
self.assertEqual(f.readline(39), "Fourth line.\n") |
| 86 |
self.assertEqual(f.readline(), "Final line non-terminated.") |
| 87 |
self.assertEqual(f.readline(), "") |
| 88 |
f.close() |
| 89 |
try: |
| 90 |
f.readline() |
| 91 |
self.assertTrue(False, "no exception on readline of closed file") |
| 92 |
except IOError: |
| 93 |
pass |
| 94 |
self.assertTrue(linefeed_byte in f.newlines) |
| 95 |
self.assertTrue(crlf in f.newlines) |
| 96 |
self.assertTrue(cr_byte not in f.newlines) |
| 97 |
|
| 98 |
def test_lf(self): |
| 99 |
""" |
| 100 |
try to trick the linefeed detector. |
| 101 |
""" |
| 102 |
f = LoopbackFile("r+U") |
| 103 |
f.write(b"First line.\r") |
| 104 |
self.assertEqual(f.readline(), "First line.\n") |
| 105 |
f.write(b"\nSecond.\r\n") |
| 106 |
self.assertEqual(f.readline(), "Second.\n") |
| 107 |
f.close() |
| 108 |
self.assertEqual(f.newlines, crlf) |
| 109 |
|
| 110 |
def test_write(self): |
| 111 |
""" |
| 112 |
verify that write buffering is on. |
| 113 |
""" |
| 114 |
f = LoopbackFile("r+", 1) |
| 115 |
f.write(b"Complete line.\nIncomplete line.") |
| 116 |
self.assertEqual(f.readline(), "Complete line.\n") |
| 117 |
self.assertEqual(f.readline(), "") |
| 118 |
f.write("..\n") |
| 119 |
self.assertEqual(f.readline(), "Incomplete line...\n") |
| 120 |
f.close() |
| 121 |
|
| 122 |
def test_flush(self): |
| 123 |
""" |
| 124 |
verify that flush will force a write. |
| 125 |
""" |
| 126 |
f = LoopbackFile("r+", 512) |
| 127 |
f.write("Not\nquite\n512 bytes.\n") |
| 128 |
self.assertEqual(f.read(1), b"") |
| 129 |
f.flush() |
| 130 |
self.assertEqual(f.read(6), b"Not\nqu") |
| 131 |
self.assertEqual(f.read(4), b"ite\n") |
| 132 |
self.assertEqual(f.read(5), b"512 b") |
| 133 |
self.assertEqual(f.read(9), b"ytes.\n") |
| 134 |
self.assertEqual(f.read(3), b"") |
| 135 |
f.close() |
| 136 |
|
| 137 |
def test_buffering_flushes(self): |
| 138 |
""" |
| 139 |
verify that flushing happens automatically on buffer crossing. |
| 140 |
""" |
| 141 |
f = LoopbackFile("r+", 16) |
| 142 |
f.write(b"Too small.") |
| 143 |
self.assertEqual(f.read(4), b"") |
| 144 |
f.write(b" ") |
| 145 |
self.assertEqual(f.read(4), b"") |
| 146 |
f.write(b"Enough.") |
| 147 |
self.assertEqual(f.read(20), b"Too small. Enough.") |
| 148 |
f.close() |
| 149 |
|
| 150 |
def test_read_all(self): |
| 151 |
""" |
| 152 |
verify that read(-1) returns everything left in the file. |
| 153 |
""" |
| 154 |
f = LoopbackFile("r+", 16) |
| 155 |
f.write(b"The first thing you need to do is open your eyes. ") |
| 156 |
f.write(b"Then, you need to close them again.\n") |
| 157 |
s = f.read(-1) |
| 158 |
self.assertEqual( |
| 159 |
s, |
| 160 |
b"The first thing you need to do is open your eyes. Then, you " |
| 161 |
+ b"need to close them again.\n", |
| 162 |
) |
| 163 |
f.close() |
| 164 |
|
| 165 |
def test_readable(self): |
| 166 |
f = LoopbackFile("r") |
| 167 |
self.assertTrue(f.readable()) |
| 168 |
self.assertFalse(f.writable()) |
| 169 |
self.assertFalse(f.seekable()) |
| 170 |
f.close() |
| 171 |
|
| 172 |
def test_writable(self): |
| 173 |
f = LoopbackFile("w") |
| 174 |
self.assertTrue(f.writable()) |
| 175 |
self.assertFalse(f.readable()) |
| 176 |
self.assertFalse(f.seekable()) |
| 177 |
f.close() |
| 178 |
|
| 179 |
def test_readinto(self): |
| 180 |
data = bytearray(5) |
| 181 |
f = LoopbackFile("r+") |
| 182 |
f._write(b"hello") |
| 183 |
f.readinto(data) |
| 184 |
self.assertEqual(data, b"hello") |
| 185 |
f.close() |
| 186 |
|
| 187 |
def test_write_bad_type(self): |
| 188 |
with LoopbackFile("wb") as f: |
| 189 |
self.assertRaises(TypeError, f.write, object()) |
| 190 |
|
| 191 |
def test_write_unicode_as_binary(self): |
| 192 |
text = "\xa7 why is writing text to a binary file allowed?\n" |
| 193 |
with LoopbackFile("rb+") as f: |
| 194 |
f.write(text) |
| 195 |
self.assertEqual(f.read(), text.encode("utf-8")) |
| 196 |
|
| 197 |
@needs_builtin("memoryview") |
| 198 |
def test_write_bytearray(self): |
| 199 |
with LoopbackFile("rb+") as f: |
| 200 |
f.write(bytearray(12)) |
| 201 |
self.assertEqual(f.read(), 12 * b"\0") |
| 202 |
|
| 203 |
@needs_builtin("buffer") |
| 204 |
def test_write_buffer(self): |
| 205 |
data = 3 * b"pretend giant block of data\n" |
| 206 |
offsets = range(0, len(data), 8) |
| 207 |
with LoopbackFile("rb+") as f: |
| 208 |
for offset in offsets: |
| 209 |
f.write(buffer(data, offset, 8)) # noqa |
| 210 |
self.assertEqual(f.read(), data) |
| 211 |
|
| 212 |
@needs_builtin("memoryview") |
| 213 |
def test_write_memoryview(self): |
| 214 |
data = 3 * b"pretend giant block of data\n" |
| 215 |
offsets = range(0, len(data), 8) |
| 216 |
with LoopbackFile("rb+") as f: |
| 217 |
view = memoryview(data) |
| 218 |
for offset in offsets: |
| 219 |
f.write(view[offset : offset + 8]) |
| 220 |
self.assertEqual(f.read(), data) |
| 221 |
|
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
from unittest import main |
| 225 |
|
| 226 |
main() |