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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
#!/usr/bin/env python3
"""Which pane a spawn thinks it is next to (pure stdlib, no IDA, no slosh).
The bug being pinned: the slosh backend used to work out "where do I live" from
`panes[].focused`. Exactly one pane in a slosh session is focused, it is always
in the tab currently on screen, and it belongs to whoever is looking -- so a
TUI spawned "beside the agent" landed in whatever tab the human had wandered
into, and in a session running several agents it was reliably a stranger's.
The rule now: identity comes from the environment ($SLOSH_PANE), or from our own
process ancestry against `panes[].pid`, or from a purpose we were told to look
for -- and if none of those can answer, the pane stays in the tab it was built
in rather than being placed on a guess.
slosh is faked here on purpose: these are decisions the backend makes about a
`panes` reply, so the reply is the input. tests/test_own_pane.py in the slosh
repo covers the other side (that slosh really does export $SLOSH_PANE, report
pids, and honour `beside`/`focus:false`).
"""
#: origin resolution + spawn placement, pure stdlib.
#: Read by tests/run.py (--fast skips every NEEDS_IDA file).
NEEDS_IDA = False
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from idatui import pane as P # noqa: E402
PASS = FAIL = 0
def check(name, cond, detail=""):
global PASS, FAIL
if cond:
PASS += 1
print(f"ok {name}")
else:
FAIL += 1
print(f"FAIL {name} <- {detail}")
# The session every test below reasons about: the agent is pane 1 in tab 1, the
# human is pane 2 in tab 2 and holds the session's only focus.
AGENT = {
"id": 1,
"tab_id": 1,
"pid": 4242,
"focused": False,
"purpose": "",
"alive": True,
"w": 80,
"h": 24,
}
HUMAN = {
"id": 2,
"tab_id": 2,
"pid": 5252,
"focused": True,
"purpose": "",
"alive": True,
"w": 80,
"h": 24,
}
class FakeSlosh:
"""Records every call, and answers `panes` from a list we control."""
def __init__(self, rows, spawned_id=9):
self.rows = [dict(r) for r in rows]
self.calls = []
self.spawned_id = spawned_id
def __call__(self, cmd, **params):
self.calls.append((cmd, params))
if cmd == "panes":
return {"ok": True, "panes": self.rows}
if cmd == "apply-layout":
# slosh builds the pane in a tab of its own; give it back with the
# purpose the caller declared, which is how the backend finds it.
kdl = params.get("kdl", "")
purpose = kdl.split('purpose="', 1)[-1].split('"', 1)[0]
self.rows.append(
{
"id": self.spawned_id,
"tab_id": 99,
"pid": 6262,
"focused": False,
"purpose": purpose,
"alive": True,
"w": 80,
"h": 24,
}
)
return {"ok": True}
if cmd == "move-pane":
for r in self.rows:
if r["id"] == params.get("id"):
r["tab_id"] = params.get("tab")
return {"ok": True, "tab": params.get("tab")}
return {"ok": True}
def of(self, cmd):
return [p for c, p in self.calls if c == cmd]
def with_fake(rows, env=None, ancestry=(), spawned_id=9):
"""Install a fake slosh + environment; returns (fake, restore callable)."""
fake = FakeSlosh(rows, spawned_id=spawned_id)
saved = {
"slosh": P._slosh,
"ancestry": P._proc_ancestry,
"caps": dict(P._SLOSH_CAPS),
"env": {
k: os.environ.get(k)
for k in (
"SLOSH_PANE",
"IDATUI_SLOSH_PANE",
"IDATUI_ORIGIN_PURPOSE",
"SLOSH_PANE_PURPOSE",
)
},
}
P._slosh = fake
P._proc_ancestry = lambda: list(ancestry)
P._SLOSH_CAPS.clear()
for k in saved["env"]:
os.environ.pop(k, None)
for k, v in (env or {}).items():
os.environ[k] = v
def restore():
P._slosh = saved["slosh"]
P._proc_ancestry = saved["ancestry"]
P._SLOSH_CAPS.clear()
P._SLOSH_CAPS.update(saved["caps"])
for k, v in saved["env"].items():
os.environ.pop(k, None)
if v is not None:
os.environ[k] = v
return fake, restore
def origin_with(**kw):
fake, restore = with_fake(**kw)
try:
return P._slosh_origin()
finally:
restore()
def test_origin_is_never_the_focused_pane():
o = origin_with(rows=[AGENT, HUMAN], env={"SLOSH_PANE": "1"})
check(
"$SLOSH_PANE wins over the focused pane",
o and o["id"] == 1,
f"got {o}",
)
o = origin_with(rows=[AGENT, HUMAN], ancestry=(1111, 4242, 1))
check(
"our own pid's ancestry finds our pane",
o and o["id"] == 1,
f"got {o}",
)
o = origin_with(
rows=[AGENT, HUMAN],
env={"IDATUI_ORIGIN_PURPOSE": "agent:me"},
)
check("an unset purpose does not match an empty one", o is None, f"got {o}")
tagged = dict(AGENT, purpose="agent:me")
o = origin_with(rows=[tagged, HUMAN], env={"IDATUI_ORIGIN_PURPOSE": "agent:me"})
check("a declared purpose is the last resort", o and o["id"] == 1, f"got {o}")
# What pi's slosh-status extension publishes for its own pane.
tagged = dict(AGENT, purpose="agent:pi.1234")
o = origin_with(rows=[tagged, HUMAN], env={"SLOSH_PANE_PURPOSE": "agent:pi.1234"})
check("$SLOSH_PANE_PURPOSE works too", o and o["id"] == 1, f"got {o}")
o = origin_with(rows=[AGENT, HUMAN])
check(
"with nothing to go on the answer is None, never the focused pane",
o is None,
f"got {o} (the focused pane is the human's)",
)
def test_an_override_beats_everything():
o = origin_with(
rows=[AGENT, HUMAN],
env={"IDATUI_SLOSH_PANE": "2", "SLOSH_PANE": "1"},
ancestry=(4242,),
)
check("$IDATUI_SLOSH_PANE overrides $SLOSH_PANE", o and o["id"] == 2, f"got {o}")
o = origin_with(rows=[AGENT, HUMAN], env={"SLOSH_PANE": "777"}, ancestry=(4242,))
check(
"an id that is not in the session falls through to the pid",
o and o["id"] == 1,
f"got {o}",
)
def test_a_spawn_lands_beside_us_and_takes_no_focus():
fake, restore = with_fake(rows=[AGENT, HUMAN], env={"SLOSH_PANE": "1"})
try:
got = P._pane_split(
["/bin/true"], mux="slosh", vertical=False, size=None, detached=True
)
finally:
restore()
check("the spawn returns the new pane's id", got == "9", f"got {got!r}")
layouts = fake.of("apply-layout")
check(
"the layout is applied without taking the view",
layouts and layouts[0].get("focus") is False,
f"{layouts}",
)
moves = fake.of("move-pane")
check("the pane is moved once", len(moves) == 1, f"{moves}")
if moves:
check(
"into the tab of the pane we are in, not the focused tab",
moves[0].get("tab") == AGENT["tab_id"],
f"went to tab {moves[0].get('tab')}, human is in {HUMAN['tab_id']}",
)
check(
"beside our own pane, not that tab's focus",
moves[0].get("beside") == AGENT["id"],
f"{moves[0]}",
)
check(
"and without taking focus",
moves[0].get("focus") is False,
f"{moves[0]}",
)
check(
"nothing is focused on the way out",
not fake.of("focus"),
f"{fake.of('focus')}",
)
def test_focus_is_restored_only_when_slosh_moved_it():
"""`--detached` on an older slosh: it ignores `focus`, so we put it back."""
rows = [AGENT, HUMAN]
fake, restore = with_fake(rows=rows, env={"SLOSH_PANE": "1"})
# An older slosh: no `pid` in the rows, so the backend must not pass the
# newer arguments -- and must fix up focus itself.
old = [{k: v for k, v in r.items() if k != "pid"} for r in rows]
fake.rows = old
P._slosh = fake
P._SLOSH_CAPS.clear()
try:
P._pane_split(
["/bin/true"], mux="slosh", vertical=False, size=None, detached=True
)
moves = fake.of("move-pane")
check(
"an older slosh is not sent arguments it would ignore",
moves and "beside" not in moves[0] and "focus" not in moves[0],
f"{moves}",
)
check(
"the layout is applied the old way too",
fake.of("apply-layout") and "focus" not in fake.of("apply-layout")[0],
f"{fake.of('apply-layout')}",
)
check(
"focus is not put back when it never moved",
not fake.of("focus"),
f"{fake.of('focus')}",
)
finally:
restore()
# Now the same, with the new pane holding focus afterwards: put it back.
fake2, restore2 = with_fake(rows=rows, env={"SLOSH_PANE": "1"})
fake2.rows = [{k: v for k, v in r.items() if k != "pid"} for r in rows]
P._slosh = fake2
P._SLOSH_CAPS.clear()
orig_call = fake2.__call__
def stealing(cmd, **params):
out = orig_call(cmd, **params)
if cmd == "move-pane": # slosh focuses what it moves
for r in fake2.rows:
r["focused"] = r["id"] == params.get("id")
return out
P._slosh = stealing
try:
P._pane_split(
["/bin/true"], mux="slosh", vertical=False, size=None, detached=True
)
check(
"focus is put back when slosh did move it",
[p.get("id") for p in fake2.of("focus")] == [AGENT["id"]],
f"{fake2.of('focus')}",
)
finally:
restore2()
def test_without_an_identity_it_stays_put():
fake, restore = with_fake(rows=[AGENT, HUMAN]) # nothing to go on
try:
got = P._pane_split(
["/bin/true"], mux="slosh", vertical=False, size=None, detached=True
)
finally:
restore()
check("the pane is still created", got == "9", f"got {got!r}")
check(
"but never moved into somebody else's tab",
not fake.of("move-pane"),
f"{fake.of('move-pane')}",
)
check("and focus is left alone", not fake.of("focus"), f"{fake.of('focus')}")
def main():
for name, fn in sorted(globals().items()):
if name.startswith("test_"):
fn()
print(f"\n{PASS} passed, {FAIL} failed")
return 1 if FAIL else 0
if __name__ == "__main__":
raise SystemExit(main())
|