|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import socket as _socket |
| 4 | + |
| 5 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) |
| 6 | + |
| 7 | +import netSplit |
| 8 | +from netSplit import socket as NetSocket |
| 9 | + |
| 10 | + |
| 11 | +class SeqClient: |
| 12 | + def __init__(self, seq): |
| 13 | + self.seq = list(seq) |
| 14 | + self.sent = [] |
| 15 | + self.closed = False |
| 16 | + |
| 17 | + def recv(self, n): |
| 18 | + if not self.seq: |
| 19 | + return b'' |
| 20 | + return self.seq.pop(0) |
| 21 | + |
| 22 | + def sendall(self, data): |
| 23 | + self.sent.append(data) |
| 24 | + |
| 25 | + def close(self): |
| 26 | + self.closed = True |
| 27 | + |
| 28 | + def settimeout(self, t): |
| 29 | + pass |
| 30 | + |
| 31 | + def setblocking(self, b): |
| 32 | + pass |
| 33 | + |
| 34 | + def getpeername(self): |
| 35 | + return ('127.0.0.1', 1) |
| 36 | + |
| 37 | + |
| 38 | +def _stub_proxy(self, cs, sock): |
| 39 | + # mark that backend was connected and return success |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +class FakeSock: |
| 44 | + """A minimal fake socket used to avoid real network connects in tests.""" |
| 45 | + def __init__(self, *a, **k): |
| 46 | + self.connected = False |
| 47 | + |
| 48 | + def settimeout(self, t): |
| 49 | + pass |
| 50 | + |
| 51 | + def connect(self, addr): |
| 52 | + # pretend we connected; don't actually touch the network |
| 53 | + self.connected = True |
| 54 | + |
| 55 | + def sendall(self, data): |
| 56 | + pass |
| 57 | + |
| 58 | + def close(self): |
| 59 | + pass |
| 60 | + |
| 61 | + def setblocking(self, b): |
| 62 | + pass |
| 63 | + |
| 64 | + |
| 65 | +def test_version_rest_combines_and_proceeds(monkeypatch): |
| 66 | + # split VERSION into prefix + rest so the code executes 'ver = initial + rest' |
| 67 | + v = netSplit.VERSION |
| 68 | + prefix = v[:1].encode() |
| 69 | + rest = v[1:].encode() |
| 70 | + |
| 71 | + # sequence of what client will send to the server via recv() |
| 72 | + # initial (prefix), then rest for recv_exact, then ack 'OK', then marker 'i' + 4-byte id |
| 73 | + seq = [prefix, rest, b'OK', b'i', (0).to_bytes(4, 'big')] |
| 74 | + client = SeqClient(seq) |
| 75 | + |
| 76 | + # set a config so len(config) is available and target server exists |
| 77 | + netSplit.config = {0: {'host': '127.0.0.1', 'port': 1}} |
| 78 | + |
| 79 | + # stub the backend proxying so no network is used |
| 80 | + monkeypatch.setattr(netSplit.socket, '_proxy_between', _stub_proxy) |
| 81 | + # prevent real socket.connect() from being called |
| 82 | + monkeypatch.setattr(netSplit._socket, 'socket', FakeSock) |
| 83 | + |
| 84 | + listener = NetSocket(_socket.AF_INET, _socket.SOCK_STREAM) |
| 85 | + res = listener.handle_client(client, ('127.0.0.1', 0)) |
| 86 | + assert res is None or isinstance(res, str) |
| 87 | + |
| 88 | + |
| 89 | +def test_selection_by_name_success(monkeypatch): |
| 90 | + name = 'myserver' |
| 91 | + # client sends full VERSION, then 'OK' ack, then 's' marker + name length + name |
| 92 | + seq = [netSplit.VERSION.encode(), b'OK', b's', len(name.encode()).to_bytes(4, 'big'), name.encode()] |
| 93 | + client = SeqClient(seq) |
| 94 | + |
| 95 | + netSplit.config = {name: {'host': '127.0.0.1', 'port': 1}} |
| 96 | + |
| 97 | + # stub proxy to avoid real sockets |
| 98 | + monkeypatch.setattr(netSplit.socket, '_proxy_between', _stub_proxy) |
| 99 | + monkeypatch.setattr(netSplit._socket, 'socket', FakeSock) |
| 100 | + |
| 101 | + listener = NetSocket(_socket.AF_INET, _socket.SOCK_STREAM) |
| 102 | + res = listener.handle_client(client, ('127.0.0.1', 0)) |
| 103 | + assert res is None or isinstance(res, str) |
| 104 | + |
| 105 | + |
| 106 | +def test_legacy_list_index_success(monkeypatch): |
| 107 | + # legacy marker: send a first byte that's not 'i' or 's', then 3 bytes follow |
| 108 | + sid = 0 |
| 109 | + raw = sid.to_bytes(4, 'big') |
| 110 | + seq = [netSplit.VERSION.encode(), b'OK', raw[:1], raw[1:]] |
| 111 | + client = SeqClient(seq) |
| 112 | + |
| 113 | + netSplit.config = [{'host': '127.0.0.1', 'port': 1}] |
| 114 | + |
| 115 | + monkeypatch.setattr(netSplit.socket, '_proxy_between', _stub_proxy) |
| 116 | + monkeypatch.setattr(netSplit._socket, 'socket', FakeSock) |
| 117 | + |
| 118 | + listener = NetSocket(_socket.AF_INET, _socket.SOCK_STREAM) |
| 119 | + res = listener.handle_client(client, ('127.0.0.1', 0)) |
| 120 | + assert res is None or isinstance(res, str) |
0 commit comments