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
|
# test_backend_socket -- Test nxt.backend.socket module
# Copyright (C) 2021 Nicolas Schodet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
from unittest.mock import Mock, call, patch
import pytest
import nxt.backend.socket
@pytest.fixture
def mdev():
dev = Mock(
spec_set=(
"connect",
"send",
"recv",
"close",
)
)
return dev
@pytest.fixture
def msocket(mdev):
with patch("nxt.backend.socket.socket") as socket:
socket.socket.return_value = mdev
yield socket
def test_socket(msocket, mdev):
# Instantiate backend.
backend = nxt.backend.socket.get_backend()
# Find brick.
mdev.recv.return_value = b"usb"
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 1
brick = bricks[0]
assert brick._sock.type == "ipusb"
assert msocket.socket.called
assert mdev.connect.called
assert mdev.send.call_args == call(b"\x98")
sock = brick._sock
# str.
assert str(sock) == "Socket (localhost:2727)"
# Send.
some_bytes = bytes.fromhex("01020304")
sock.send(some_bytes)
assert mdev.send.call_args == call(some_bytes)
# Recv.
mdev.recv.return_value = some_bytes
r = sock.recv()
assert r == some_bytes
assert mdev.recv.called
# Close.
brick.close()
assert mdev.send.call_args == call(b"\x99")
assert mdev.close.called
# Duplicated close.
sock.close()
def test_socket_cant_connect(msocket, mdev):
backend = nxt.backend.socket.get_backend()
mdev.connect.side_effect = [ConnectionRefusedError]
bricks = list(backend.find(blah="blah"))
assert len(bricks) == 0
@pytest.mark.nxt("socket")
def test_socket_real():
# Instantiate backend.
backend = nxt.backend.socket.get_backend()
# Find brick.
bricks = list(backend.find())
assert len(bricks) > 0, "no NXT found"
brick = bricks[0]
sock = brick._sock
# str.
assert str(sock) == "Socket (localhost:2727)"
# Send.
sock.send(bytes.fromhex("019b"))
# Recv.
r = sock.recv()
assert r.startswith(bytes.fromhex("029b00"))
# Close.
brick.close()
|