File: connection.py

package info (click to toggle)
nyx 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,732 kB
  • sloc: python: 7,055; makefile: 7; sh: 3
file content (297 lines) | stat: -rw-r--r-- 13,024 bytes parent folder | download | duplicates (4)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
Unit tests for nyx.panel.connection.
"""

import datetime
import unittest

import stem.exit_policy
import stem.version
import nyx.panel.connection
import test

from nyx.tracker import Connection
from nyx.panel.connection import Category, LineType, Line, Entry
from test import require_curses

try:
  # added in python 3.3
  from unittest.mock import Mock, patch
except ImportError:
  from mock import Mock, patch

TIMESTAMP = 1468170303.7
CONNECTION = Connection(TIMESTAMP, False, '127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False)

DETAILS_BUILDING_CIRCUIT = """
+------------------------------------------------------------------------------+
| Building Circuit...                                                          |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
""".strip()

DETAILS_NO_CONSENSUS_DATA = """
+------------------------------------------------------------------------------+
| address: 75.119.206.243:22                                                   |
| locale: de                                                                   |
| No consensus data found                                                      |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
""".strip()

DETAILS_WHEN_PRIVATE = """
+------------------------------------------------------------------------------+
| address: <scrubbed>:22                                                       |
| locale: ??                                                                   |
| No consensus data found                                                      |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
""".strip()

DETAILS_FOR_RELAY = """
+------------------------------------------------------------------------------+
| address: 75.119.206.243:22                                                   |
| locale: de   fingerprint: B6D83EC2D9E18B0A7A33428F8CFA9C536769E209           |
| nickname: caerSidi                  orport: 9051       dirport: 9052         |
| published: 17:15 03/01/2012         os: Debian         version: 0.2.1.30     |
| flags: Fast, HSDir                                                           |
| exit policy: reject 1-65535                                                  |
| contact: spiffy_person@torproject.org                                        |
+------------------------------------------------------------------------------+
""".strip()

DETAILS_FOR_MULTIPLE_MATCHES = """
+------------------------------------------------------------------------------+
| address: 75.119.206.243:22                                                   |
| locale: de                                                                   |
| Multiple matches, possible fingerprints are:                                 |
| 1. or port: 52    fingerprint: 1F43EE37A0670301AD9CB555D94AFEC2C89FDE86      |
| 2. or port: 80    fingerprint: B6D83EC2D9E18B0A7A33428F8CFA9C536769E209      |
| 3. or port: 443   fingerprint: E0BD57A11F00041A9789577C53A1B784473669E4      |
|                                                                              |
+------------------------------------------------------------------------------+
""".strip()


class MockEntry(Entry):
  def __init__(self, lines = [], entry_type = Category.INBOUND, is_private = False):
    self._lines = lines
    self._type = entry_type
    self._is_private = is_private

  def lines(self):
    return self._lines

  def get_type(self):
    return self._type

  def is_private(self):
    return self._is_private


class MockCircuit(object):
  def __init__(self, circ_id = 7, status = 'BUILT', purpose = 'GENERAL', path = None):
    self.id = circ_id
    self.status = status
    self.purpose = purpose

    if path:
      self.path = path
    else:
      self.path = [
        ('1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', 'Unnamed'),
        ('B6D83EC2D9E18B0A7A33428F8CFA9C536769E209', 'moria1'),
        ('E0BD57A11F00041A9789577C53A1B784473669E4', 'caerSidi'),
      ]


def line(entry = MockEntry(), line_type = LineType.CONNECTION, connection = CONNECTION, circ = MockCircuit(), fingerprint = '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86', nickname = 'Unnamed', locale = 'de'):
  return Line(entry, line_type, connection, circ, fingerprint, nickname, locale)


class TestConnectionPanel(unittest.TestCase):
  @require_curses
  def test_draw_title(self):
    rendered = test.render(nyx.panel.connection._draw_title, [], True)
    self.assertEqual('Connection Details:', rendered.content)

    rendered = test.render(nyx.panel.connection._draw_title, [], False)
    self.assertEqual('Connections:', rendered.content)

    entries = [MockEntry(entry_type = category) for category in (Category.INBOUND, Category.INBOUND, Category.OUTBOUND, Category.INBOUND, Category.CONTROL)]

    rendered = test.render(nyx.panel.connection._draw_title, entries, False)
    self.assertEqual('Connections (3 inbound, 1 outbound, 1 control):', rendered.content)

  @require_curses
  def test_draw_details_incomplete_circuit(self):
    selected = line(line_type = LineType.CIRCUIT_HEADER, circ = MockCircuit(status = 'EXTENDING'))

    rendered = test.render(nyx.panel.connection._draw_details, selected)
    self.assertEqual(DETAILS_BUILDING_CIRCUIT, rendered.content)

  @require_curses
  @patch('nyx.tracker.get_consensus_tracker')
  def test_draw_details_no_consensus_data(self, consensus_tracker_mock):
    consensus_tracker_mock().get_relay_fingerprints.return_value = None

    rendered = test.render(nyx.panel.connection._draw_details, line())
    self.assertEqual(DETAILS_NO_CONSENSUS_DATA, rendered.content)

  @require_curses
  @patch('nyx.tracker.get_consensus_tracker')
  def test_draw_details_when_private(self, consensus_tracker_mock):
    consensus_tracker_mock().get_relay_fingerprints.return_value = None
    selected = line(entry = MockEntry(is_private = True))

    rendered = test.render(nyx.panel.connection._draw_details, selected)
    self.assertEqual(DETAILS_WHEN_PRIVATE, rendered.content)

  @require_curses
  @patch('nyx.panel.connection.tor_controller')
  @patch('nyx.tracker.get_consensus_tracker')
  def test_draw_details_for_relay(self, consensus_tracker_mock, tor_controller_mock):
    router_status_entry = Mock()
    router_status_entry.or_port = 9051
    router_status_entry.dir_port = 9052
    router_status_entry.nickname = 'caerSidi'
    router_status_entry.flags = ['Fast', 'HSDir']
    router_status_entry.published = datetime.datetime(2012, 3, 1, 17, 15, 27)

    tor_controller_mock().get_network_status.return_value = router_status_entry

    server_descriptor = Mock()
    server_descriptor.exit_policy = stem.exit_policy.ExitPolicy('reject *:*')
    server_descriptor.tor_version = stem.version.Version('0.2.1.30')
    server_descriptor.operating_system = 'Debian'
    server_descriptor.contact = 'spiffy_person@torproject.org'

    tor_controller_mock().get_server_descriptor.return_value = server_descriptor

    consensus_tracker_mock().get_relay_fingerprints.return_value = {
      22: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'
    }

    rendered = test.render(nyx.panel.connection._draw_details, line())
    self.assertEqual(DETAILS_FOR_RELAY, rendered.content)

  @require_curses
  @patch('nyx.tracker.get_consensus_tracker')
  def test_draw_details_with_multiple_matches(self, consensus_tracker_mock):
    consensus_tracker_mock().get_relay_fingerprints.return_value = {
      52: '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86',
      80: 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209',
      443: 'E0BD57A11F00041A9789577C53A1B784473669E4',
    }

    rendered = test.render(nyx.panel.connection._draw_details, line())
    self.assertEqual(DETAILS_FOR_MULTIPLE_MATCHES, rendered.content)

  @require_curses
  @patch('nyx.panel.connection.tor_controller')
  def test_draw_line(self, tor_controller_mock):
    tor_controller_mock().is_geoip_unavailable.return_value = False
    tor_controller_mock().get_info.return_value = '82.121.9.9'

    test_data = ((
      line(),
      ' 75.119.206.243:22 (de)      -->  82.121.9.9:3531              15.4s (INBOUND)',
    ), (
      line(entry = MockEntry(entry_type = Category.CIRCUIT), line_type = LineType.CIRCUIT_HEADER),
      ' 82.121.9.9             -->  75.119.206.243:22 (de)            15.4s (CIRCUIT)',
    ), (
      line(line_type = LineType.CIRCUIT, fingerprint = '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86'),
      ' |  82.121.9.9                                                    1 / Guard',
    ), (
      line(line_type = LineType.CIRCUIT, fingerprint = 'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209'),
      ' |  82.121.9.9                                                    2 / Middle',
    ), (
      line(line_type = LineType.CIRCUIT, fingerprint = 'E0BD57A11F00041A9789577C53A1B784473669E4'),
      ' +- 82.121.9.9                                                    3 / End',
    ))

    for test_line, expected in test_data:
      rendered = test.render(nyx.panel.connection._draw_line, 0, 0, test_line, False, 80, TIMESTAMP + 15.4)
      self.assertEqual(expected, rendered.content)

  @require_curses
  @patch('nyx.panel.connection.tor_controller')
  def test_draw_address_column(self, tor_controller_mock):
    tor_controller_mock().is_geoip_unavailable.return_value = False
    tor_controller_mock().get_info.return_value = '82.121.9.9'

    test_data = ((
      line(),
      '75.119.206.243:22 (de)      -->  82.121.9.9:3531',
    ), (
      line(entry = MockEntry(entry_type = Category.EXIT)),
      '82.121.9.9:3531        -->  75.119.206.243:22 (SSH)',
    ), (
      line(line_type = LineType.CIRCUIT_HEADER, circ = MockCircuit(status = 'EXTENDING')),
      'Building...                 -->  82.121.9.9',
    ), (
      line(line_type = LineType.CIRCUIT),
      '82.121.9.9',
    ))

    for test_line, expected in test_data:
      rendered = test.render(nyx.panel.connection._draw_address_column, 0, 0, test_line, ())
      self.assertEqual(expected, rendered.content)

  @require_curses
  @patch('nyx.tracker.get_port_usage_tracker')
  def test_draw_line_details(self, port_usage_tracker_mock):
    process = Mock()
    process.name = 'firefox'
    process.pid = 722

    port_usage_tracker_mock().fetch.return_value = process

    test_data = ((
      line(),
      '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86  Unnamed',
    ), (
      line(line_type = LineType.CIRCUIT_HEADER),
      'Purpose: General, Circuit ID: 7',
    ), (
      line(entry = MockEntry(entry_type = Category.CONTROL)),
      'firefox (722)',
    ))

    for test_line, expected in test_data:
      rendered = test.render(nyx.panel.connection._draw_line_details, 0, 0, test_line, 80, ())
      self.assertEqual(expected, rendered.content)

  @require_curses
  def test_draw_right_column(self):
    rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, line(), TIMESTAMP + 62, ())
    self.assertEqual('  1.0m (INBOUND)', rendered.content)

    legacy_connection = Connection(TIMESTAMP, True, '127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False)
    test_line = line(entry = MockEntry(entry_type = Category.CONTROL), connection = legacy_connection)

    rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 68, ())
    self.assertEqual('+ 1.1m (CONTROL)', rendered.content)

    test_data = {
      '1F43EE37A0670301AD9CB555D94AFEC2C89FDE86': '    1 / Guard',
      'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209': '    2 / Middle',
      'E0BD57A11F00041A9789577C53A1B784473669E4': '    3 / End',
    }

    for fp, expected in test_data.items():
      test_line = line(line_type = LineType.CIRCUIT, fingerprint = fp)

      rendered = test.render(nyx.panel.connection._draw_right_column, 0, 0, test_line, TIMESTAMP + 62, ())
      self.assertEqual(expected, rendered.content)