-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
183 lines (140 loc) · 5.29 KB
/
test.py
File metadata and controls
183 lines (140 loc) · 5.29 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
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
###
# Copyright (c) 2026, Barry KW Suridge
# All rights reserved.
#
#
###
import unittest
import socket
import tempfile
from pathlib import Path
from supybot import test as supybot_test
try:
from . import plugin
except ImportError: # pragma: no cover - allows direct pytest execution.
import plugin
class LocalControlTestCase(supybot_test.PluginTestCase):
__test__ = False
plugins = ("LocalControl",)
class TestLocalControlModule(unittest.TestCase):
def test_plugin_class_is_available(self):
self.assertIs(plugin.Class, plugin.LocalControl)
def test_restrict_socket_permissions_sets_owner_only_mode(self):
with tempfile.TemporaryDirectory() as tmpdir:
socket_path = Path(tmpdir) / "localcontrol.sock"
socket_path.touch()
plugin._restrict_socket_permissions(socket_path)
self.assertEqual(socket_path.stat().st_mode & 0o777, plugin.SOCKET_MODE)
def test_idle_client_times_out(self):
class IdleClient:
def __init__(self):
self.timeout = None
self.sent = b""
self.closed = False
def settimeout(self, timeout):
self.timeout = timeout
def recv(self, size):
raise socket.timeout()
def sendall(self, data):
self.sent += data
def close(self):
self.closed = True
local_control = object.__new__(plugin.LocalControl)
local_control.registryValue = lambda name: False
client = IdleClient()
local_control._handle_client(client)
self.assertEqual(client.timeout, plugin.CLIENT_TIMEOUT_SECONDS)
self.assertEqual(client.sent, b"Error: request timed out\n")
self.assertTrue(client.closed)
def test_dispatch_uses_lock_around_irc_monkeypatch(self):
class Client:
def __init__(self):
self.sent = b""
self.closed = False
def settimeout(self, timeout):
pass
def recv(self, size):
return b"sysinfo\n"
def sendall(self, data):
self.sent += data
def close(self):
self.closed = True
class Irc:
nick = "TestBot"
def sendMsg(self, msg):
pass
def queueMsg(self, msg):
pass
class DispatchLock:
def __init__(self):
self.inside = False
self.entered = False
def __enter__(self):
self.inside = True
self.entered = True
def __exit__(self, exc_type, exc_value, traceback):
self.inside = False
local_control = object.__new__(plugin.LocalControl)
local_control.registryValue = lambda name: False
local_control._dispatch_lock = DispatchLock()
dispatch_states = []
def dispatch(irc, command, args, synthetic_prefix):
dispatch_states.append(local_control._dispatch_lock.inside)
local_control._dispatch = dispatch
original_ircs = plugin.world.ircs
plugin.world.ircs = [Irc()]
try:
local_control._handle_client(Client())
finally:
plugin.world.ircs = original_ircs
self.assertTrue(local_control._dispatch_lock.entered)
self.assertEqual(dispatch_states, [True])
def test_socket_request_logging_uses_safe_summary_by_default(self):
lines = []
original_info = plugin.log.info
local_control = object.__new__(plugin.LocalControl)
local_control.registryValue = lambda name: {
"socketRequestLogging": True,
"socketRequestFullCommandLogging": False,
}[name]
try:
plugin.log.info = lines.append
local_control._log_socket_request(
1,
"config networks.Libera.sasl.password hunter2",
"ok",
replies=1,
started=0.0,
)
finally:
plugin.log.info = original_info
self.assertEqual(len(lines), 1)
self.assertIn('command="config" argc=2', lines[0])
self.assertNotIn("hunter2", lines[0])
self.assertNotIn("full_cmd", lines[0])
def test_full_command_logging_redacts_sensitive_values(self):
lines = []
original_info = plugin.log.info
local_control = object.__new__(plugin.LocalControl)
local_control.registryValue = lambda name: {
"socketRequestLogging": True,
"socketRequestFullCommandLogging": True,
}[name]
try:
plugin.log.info = lines.append
local_control._log_socket_request(
2,
"config networks.Libera.sasl.password hunter2 api_key=abc123 token xyz",
"ok",
replies=1,
started=0.0,
)
finally:
plugin.log.info = original_info
self.assertEqual(len(lines), 1)
self.assertIn('command="config" argc=5', lines[0])
self.assertIn("full_cmd=", lines[0])
self.assertNotIn("hunter2", lines[0])
self.assertNotIn("abc123", lines[0])
self.assertNotIn("xyz", lines[0])
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: