Example #1
0
def test_parsing_msg():
    """Test parsing of messages."""
    parse_search = credential.parse_ddp_response(
        get_ddp_search_message().encode("utf-8")
    )
    assert parse_search == credential.PARSE_TYPE_SEARCH
    wakeup_response = get_ddp_wake_message(MOCK_CREDS).encode("utf-8")
    parse_wakeup = credential.parse_ddp_response(wakeup_response)
    assert parse_wakeup == credential.PARSE_TYPE_WAKEUP
    assert MOCK_CREDS == credential.get_creds(wakeup_response)
Example #2
0
def test_creds_handle_search():
    """Test Service handles search message."""
    mock_response = (get_ddp_search_message().encode(), MOCK_ADDRESS)
    creds = init_creds()
    mock_search = credential.get_ddp_message(
        credential.STANDBY, creds.response
    ).encode()
    creds.sock = MagicMock()
    creds.sock.recvfrom = MagicMock(return_value=mock_response)
    result = creds.listen(0.1)
    creds.sock.sendto.assert_called_with(mock_search, MOCK_ADDRESS)
    assert result is None
Example #3
0
def test_send_recv_msg_no_socket():
    """Test that socket is generated if sock is None."""
    msg = ddp.get_ddp_search_message()
    mock_sock = MagicMock()
    mock_sock.recvfrom.return_value = (
        MOCK_DDP_RESPONSE.encode(),
        (MOCK_HOST, MOCK_RANDOM_PORT),
    )
    with patch(
            "pyps4_2ndscreen.ddp.get_socket",
            return_value=mock_sock,
    ), patch("pyps4_2ndscreen.ddp.select.select",
             return_value=([mock_sock], [MagicMock()], [MagicMock()])):
        ddp._send_recv_msg(MOCK_HOST, msg, sock=None)
        assert len(mock_sock.sendto.mock_calls) == 1
        assert len(mock_sock.recvfrom.mock_calls) == 1
Example #4
0
def test_creds_errors():
    """Test Service error handling."""
    creds = init_creds()
    creds.sock = MagicMock()
    creds.sock.recvfrom = MagicMock(side_effect=credential.socket.error())
    with pytest.raises(credential.CredentialTimeout):
        result = creds.listen(0.1)
        assert result is None

    mock_response = (get_ddp_search_message().encode(), MOCK_ADDRESS)
    creds = init_creds()
    creds.sock = MagicMock()
    creds.sock.recvfrom = MagicMock(return_value=mock_response)
    creds.sock.sendto = MagicMock(side_effect=credential.socket.error())
    result = creds.listen(0.1)
    assert result is None

    # Test invalid msg.
    mock_response = (b"\x00", MOCK_ADDRESS)
    creds = init_creds()
    creds.sock = MagicMock()
    creds.sock.recvfrom = MagicMock(return_value=mock_response)
    result = creds.listen(0.1)
    assert result is None

    creds = init_creds()
    creds.sock = MagicMock()
    creds.sock.recvfrom = MagicMock(side_effect=KeyboardInterrupt)
    result = creds.listen(0.1)
    assert result is None

    with patch(
        "pyps4_2ndscreen.credential.socket.socket", side_effect=credential.socket.error
    ):
        creds = init_creds()
        creds.listen()
        assert creds.sock is None

    with patch(
        "pyps4_2ndscreen.credential.socket.socket.bind",
        side_effect=credential.socket.error,
    ):
        creds = init_creds()
        creds.listen()
        assert creds.sock is None
Example #5
0
def test_ddp_messages():
    """Test that DDP messages to send are correct."""
    msg = ddp.get_ddp_search_message()
    msg = msg.split("\n")
    for item in msg:
        assert item in MOCK_DDP_MESSAGE.format(ddp.DDP_TYPE_SEARCH,
                                               MOCK_DDP_VER)

    cred_data = MOCK_DDP_CRED_DATA.format(MOCK_CREDS, MOCK_DDP_VER)

    msg = ddp.get_ddp_launch_message(MOCK_CREDS)
    msg = msg.split("\n")
    for item in msg:
        assert item in MOCK_DDP_MESSAGE.format(ddp.DDP_TYPE_LAUNCH, cred_data)

    msg = ddp.get_ddp_wake_message(MOCK_CREDS)
    msg = msg.split("\n")
    for item in msg:
        assert item in MOCK_DDP_MESSAGE.format(ddp.DDP_TYPE_WAKEUP, cred_data)
Example #6
0
def test_search():
    """Test Search."""
    mock_sock = MagicMock()
    mock_sock.recvfrom.return_value = (
        MOCK_DDP_RESPONSE.encode(),
        (MOCK_HOST, MOCK_RANDOM_PORT),
    )
    with patch(
            "pyps4_2ndscreen.ddp.select.select",
            return_value=([mock_sock], [MagicMock()], [MagicMock()]),
    ):
        mock_result = ddp.search(MOCK_HOST, sock=mock_sock)[0]
        assert MOCK_HOST in mock_result.values()

    # Test search msg sent to broadcast IP if host is None.
    with patch(
            "pyps4_2ndscreen.ddp.select.select",
            return_value=([mock_sock], [MagicMock()], [MagicMock()]),
    ), patch("pyps4_2ndscreen.ddp._send_msg") as mock_send:
        mock_result = ddp.search(host=None, sock=mock_sock)[0]
        assert MOCK_HOST in mock_result.values()
        args, _ = mock_send.call_args
        assert ddp.BROADCAST_IP == args[0]
        assert ddp.get_ddp_search_message() == args[1]
Example #7
0
def test_unspecified_socket_no_close():
    """Test that not closing unspecified socket raises error."""
    msg = ddp.get_ddp_search_message()
    with pytest.raises(ValueError):
        ddp._send_recv_msg(MOCK_HOST, msg, sock=None, close=False)
Example #8
0
def test_parse_ddp_msg_search():
    """Test handle of parsing search message."""
    msg = ddp.get_ddp_search_message()
    data = ddp.parse_ddp_response(msg)
    assert not data