-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathtest_utils.py
More file actions
88 lines (79 loc) · 3.13 KB
/
test_utils.py
File metadata and controls
88 lines (79 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# This file is part of ssh2-python.
# Copyright (C) 2017-2025 Panos Kittenis
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation, version 2.1.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import unittest
from ssh2.utils import find_eol
class UtilsTest(unittest.TestCase):
def test_find_eol_no_lines(self):
buf = b"a buffer"
linepos, new_line_pos = find_eol(buf, 0)
self.assertEqual(linepos, -1)
self.assertEqual(new_line_pos, 0)
def test_read_line(self):
lines = [b'a line', b'another line', b'third']
buf = b"\n".join(lines)
pos = 0
line_num = 0
linesep, new_line_pos = find_eol(buf, 0)
self.assertTrue(linesep > 0)
self.assertTrue(linesep < len(buf))
while pos < len(buf):
if linesep < 0:
break
end_of_line = pos + linesep
line = buf[pos:end_of_line]
self.assertEqual(lines[line_num], line)
pos += linesep + new_line_pos
line_num += 1
linesep, new_line_pos = find_eol(buf, pos)
line = buf[pos:]
self.assertEqual(lines[line_num], line)
def test_read_line_crnl(self):
lines = [b'a line', b'another line', b'third']
buf = b"\r\n".join(lines)
pos = 0
line_num = 0
linesep, new_line_pos = find_eol(buf, 0)
self.assertTrue(linesep > 0)
self.assertTrue(linesep < len(buf))
while pos < len(buf):
if linesep < 0:
break
end_of_line = pos + linesep
line = buf[pos:end_of_line]
self.assertEqual(lines[line_num], line)
pos += linesep + new_line_pos
line_num += 1
linesep, new_line_pos = find_eol(buf, pos)
line = buf[pos:]
self.assertEqual(lines[line_num], line)
def test_read_line_cr_only(self):
lines = [b'a line', b'another line', b'third']
buf = b"\r".join(lines)
linesep, new_line_pos = find_eol(buf, 0)
self.assertEqual(linesep, -1)
def test_read_line_bad_data(self):
linesep, new_line_pos = find_eol(b"", 0)
self.assertEqual(linesep, -1)
self.assertEqual(new_line_pos, 0)
linesep, new_line_pos = find_eol(b"\n", 0)
self.assertEqual(linesep, 0)
self.assertEqual(new_line_pos, 1)
linesep, new_line_pos = find_eol(b"\r\n", 0)
self.assertEqual(linesep, 0)
self.assertEqual(new_line_pos, 2)
linesep, new_line_pos = find_eol(b"\r", 0)
self.assertEqual(linesep, -1)
self.assertEqual(new_line_pos, 0)