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
|
# deck-it - tests for obsclient
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import base64
import hashlib
import unittest
import deckit.obsclient as oc
class Auth(unittest.TestCase):
def test_matches_protocol_formula(self):
pw, salt, challenge = "supersecret", "salty", "chall"
step1 = base64.b64encode(hashlib.sha256((pw + salt).encode()).digest()).decode()
expected = base64.b64encode(hashlib.sha256((step1 + challenge).encode()).digest()).decode()
self.assertEqual(oc.auth_string(pw, salt, challenge), expected)
self.assertNotEqual(oc.auth_string("other", salt, challenge), expected)
class Disconnected(unittest.TestCase):
def test_request_raises_when_closed(self):
with self.assertRaises(oc.ModuleError):
oc.ObsClient(host="127.0.0.1", port=4455, password="").request("GetVersion")
class RecvJson(unittest.TestCase):
def test_empty_reply_is_a_clear_error(self):
c = oc.ObsClient()
c.ws = type("W", (), {"recv": lambda self: ""})()
with self.assertRaisesRegex(oc.ModuleError, "closed"):
c._recv_json()
def test_non_json_reply_is_a_clear_error(self):
c = oc.ObsClient()
c.ws = type("W", (), {"recv": lambda self: "HTTP/1.1 400"})()
with self.assertRaisesRegex(oc.ModuleError, "non-JSON"):
c._recv_json()
class RecvLoop(unittest.TestCase):
def test_a_recv_timeout_does_not_end_the_loop(self):
import websocket
class FakeWS:
calls = 0
def recv(self):
FakeWS.calls += 1
if FakeWS.calls <= 3:
raise websocket.WebSocketTimeoutException("idle")
raise RuntimeError("closed")
c = oc.ObsClient()
c.ws = FakeWS()
c.connected = True
c._recv_loop()
# It kept calling recv past three idle timeouts; a timeout must not
# look like a closed socket.
self.assertEqual(FakeWS.calls, 4)
if __name__ == "__main__":
unittest.main()
|