Start working on #888 (new IPC path).

This commit is contained in:
Florian Bruhin 2015-09-06 19:50:03 +02:00
parent bfd8faafef
commit 6a0994038e
2 changed files with 53 additions and 17 deletions

View File

@ -30,7 +30,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
from PyQt5.QtNetwork import QLocalSocket, QLocalServer, QAbstractSocket from PyQt5.QtNetwork import QLocalSocket, QLocalServer, QAbstractSocket
import qutebrowser import qutebrowser
from qutebrowser.utils import log, usertypes, error, objreg from qutebrowser.utils import log, usertypes, error, objreg, standarddir
CONNECT_TIMEOUT = 100 CONNECT_TIMEOUT = 100
@ -39,15 +39,28 @@ READ_TIMEOUT = 5000
PROTOCOL_VERSION = 1 PROTOCOL_VERSION = 1
def _get_socketname(basedir, user=None): def _get_socketname(basedir, runtime_dir, legacy=True, user=None):
"""Get a socketname to use.""" """Get a socketname to use."""
if user is None: if user is None:
user = getpass.getuser() user = getpass.getuser()
parts = ['qutebrowser', user]
if basedir is not None: if basedir is None:
basedir_md5 = None
else:
md5 = hashlib.md5(basedir.encode('utf-8')) md5 = hashlib.md5(basedir.encode('utf-8'))
parts.append(md5.hexdigest()) basedir_md5 = md5.hexdigest()
return '-'.join(parts)
if legacy or os.name == 'nt':
parts = ['qutebrowser', user]
if basedir_md5 is not None:
parts.append(basedir_md5)
return '-'.join(parts)
else:
parts = ['qutebrowser-ipc']
if basedir_md5 is not None:
parts.append(basedir_md5)
return os.path.join(runtime_dir, '-'.join(parts))
class Error(Exception): class Error(Exception):
@ -374,14 +387,14 @@ def send_or_listen(args):
The IPCServer instance if no running instance was detected. The IPCServer instance if no running instance was detected.
None if an instance was running and received our request. None if an instance was running and received our request.
""" """
socketname = _get_socketname(args.basedir) socketname = _get_socketname(args.basedir, standarddir.runtime())
try: try:
try: try:
sent = send_to_running_instance(socketname, args.command) sent = send_to_running_instance(socketname, args.command)
if sent: if sent:
return None return None
log.init.debug("Starting IPC server...") log.init.debug("Starting IPC server...")
server = IPCServer(_get_socketname(args.basedir)) server = IPCServer(socketname)
server.listen() server.listen()
objreg.register('ipc-server', server) objreg.register('ipc-server', server)
return server return server

View File

@ -70,6 +70,12 @@ def qlocalsocket(qapp):
socket.waitForDisconnected(1000) socket.waitForDisconnected(1000)
@pytest.fixture(autouse=True)
def fake_runtime_dir(monkeypatch, tmpdir):
monkeypatch.setenv('XDG_RUNTIME_DIR', str(tmpdir))
return str(tmpdir)
class FakeSocket(QObject): class FakeSocket(QObject):
"""A stub for a QLocalSocket. """A stub for a QLocalSocket.
@ -159,16 +165,21 @@ def test_getpass_getuser():
assert getpass.getuser() assert getpass.getuser()
@pytest.mark.parametrize('username, basedir, expected', [ @pytest.mark.parametrize('username, basedir, legacy, expected', [
('florian', None, 'qutebrowser-florian'), ('florian', None, True, 'qutebrowser-florian'),
('florian', '/x', 'qutebrowser-florian-cc8755609ad61864910f145119713de9'), ('florian', '/x', True,
'qutebrowser-florian-cc8755609ad61864910f145119713de9'),
(None, None, True, 'qutebrowser-{}'.format(getpass.getuser())),
('florian', None, False, '/runtimedir/qutebrowser-ipc'),
('florian', '/x', False,
'/runtimedir/qutebrowser-ipc-cc8755609ad61864910f145119713de9'),
(None, None, False, '/runtimedir/qutebrowser-ipc'),
]) ])
def test_get_socketname(username, basedir, expected): def test_get_socketname(username, basedir, legacy, expected):
assert ipc._get_socketname(basedir, user=username) == expected socketname = ipc._get_socketname(basedir, '/runtimedir', legacy=legacy,
user=username)
assert socketname == expected
def test_get_socketname_no_user():
assert ipc._get_socketname(None).startswith('qutebrowser-')
class TestExceptions: class TestExceptions:
@ -622,3 +633,15 @@ class TestSendOrListen:
'string (error 4)', 'string (error 4)',
] ]
assert msgs[-5:] == error_msgs assert msgs[-5:] == error_msgs
def test_long_username(fake_runtime_dir):
"""See https://github.com/The-Compiler/qutebrowser/issues/888."""
name = ipc._get_socketname(basedir='/foo',
runtime_dir=fake_runtime_dir,
user='alexandercogneau')
server = ipc.IPCServer(name)
try:
server.listen()
finally:
server.shutdown()