-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmoke_test.py
More file actions
303 lines (244 loc) · 9.29 KB
/
Copy pathsmoke_test.py
File metadata and controls
303 lines (244 loc) · 9.29 KB
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
import io
import os
import struct
import subprocess
import sys
import time
from pathlib import Path
import win32clipboard
import win32con
from PIL import Image
from paste_anywhere import CLIPS_DIR, LOG_FILE, purge_old_clips
SCRIPT_DIR = Path(__file__).parent
BRIDGE_SCRIPT = SCRIPT_DIR / "paste_anywhere.py"
CREATE_NO_WINDOW = 0x08000000
results = []
def check(name, condition):
status = "PASS" if condition else "FAIL"
print(f"[{status}] {name}")
results.append(condition)
return condition
def image_to_dib(img):
buf = io.BytesIO()
img.convert("RGB").save(buf, "BMP")
return buf.getvalue()[14:]
def open_clipboard_with_retry(attempts=10, delay=0.1):
for _ in range(attempts):
try:
win32clipboard.OpenClipboard()
return True
except Exception:
time.sleep(delay)
return False
def set_test_bitmap():
img = Image.new("RGB", (200, 100), (255, 0, 0))
dib = image_to_dib(img)
if not open_clipboard_with_retry():
raise RuntimeError("could not open clipboard to set test bitmap")
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_DIB, dib)
finally:
win32clipboard.CloseClipboard()
def set_plain_text(text):
if not open_clipboard_with_retry():
raise RuntimeError("could not open clipboard to set text")
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT, text)
finally:
win32clipboard.CloseClipboard()
def get_seq_number():
import ctypes
return ctypes.windll.user32.GetClipboardSequenceNumber()
def read_clipboard_text():
if not open_clipboard_with_retry():
raise RuntimeError("could not open clipboard to read text")
try:
return win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
finally:
win32clipboard.CloseClipboard()
def set_test_bitmap_sized(w, h, color=(0, 255, 0)):
img = Image.new("RGB", (w, h), color)
dib = image_to_dib(img)
if not open_clipboard_with_retry():
raise RuntimeError("could not open clipboard to set test bitmap")
try:
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_DIB, dib)
finally:
win32clipboard.CloseClipboard()
def list_clip_files():
if not CLIPS_DIR.exists():
return set()
return set(CLIPS_DIR.glob("clip_*.png"))
def read_log_tail(n=30):
if not LOG_FILE.exists():
return []
with open(LOG_FILE, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
return lines[-n:]
def test_duplicate_guard():
# Use a bitmap distinct from every other test's bitmap (color+size), since the
# bridge's recent-hash dedup persists for the life of the bridge process and
# would otherwise treat this as a duplicate of an earlier, unrelated test.
before = list_clip_files()
set_test_bitmap_sized(200, 100, (0, 0, 255))
time.sleep(2.5)
set_test_bitmap_sized(200, 100, (0, 0, 255))
time.sleep(2.5)
after = list_clip_files()
new_files = after - before
check(
"duplicate guard: exactly one clip file created from two identical captures",
len(new_files) == 1,
)
tail = read_log_tail(30)
logged_duplicate = any("duplicate clip, skipped" in line for line in tail)
check("duplicate guard: second capture logged as duplicate", logged_duplicate)
for f in new_files:
f.unlink(missing_ok=True)
def test_downscale():
before = list_clip_files()
set_test_bitmap_sized(3000, 400)
deadline = time.time() + 10
new_file = None
while time.time() < deadline:
time.sleep(0.3)
after = list_clip_files()
diff = after - before
if diff:
new_file = next(iter(diff))
break
check("downscale: bridge created a clip for the oversized bitmap", new_file is not None)
if new_file is not None:
time.sleep(0.3)
try:
with Image.open(new_file) as img:
img.load()
long_edge = max(img.size)
check("downscale: saved PNG long edge is 1568", long_edge == 1568)
except Exception as e:
check(f"downscale: saved PNG long edge is 1568 (error: {e})", False)
new_file.unlink(missing_ok=True)
def test_duplicate_guard_oversized():
# Coverage gap that let the pre/post-downscale hash mismatch through: an
# oversized capture gets downscaled before saving, so the duplicate guard
# must hash the same (post-downscale) bytes it later writes to the
# clipboard, or a replay of an already-processed oversized image never
# matches and gets reprocessed.
before = list_clip_files()
set_test_bitmap_sized(3000, 400, (255, 0, 255))
time.sleep(3)
set_test_bitmap_sized(3000, 400, (255, 0, 255))
time.sleep(3)
after = list_clip_files()
new_files = after - before
check(
"duplicate guard: exactly one clip file created from two identical oversized captures",
len(new_files) == 1,
)
tail = read_log_tail(30)
logged_duplicate = any("duplicate clip, skipped" in line for line in tail)
check("duplicate guard: second oversized capture logged as duplicate", logged_duplicate)
for f in new_files:
f.unlink(missing_ok=True)
def test_purge_old_clips():
CLIPS_DIR.mkdir(parents=True, exist_ok=True)
fake = CLIPS_DIR / "clip_19990101_000000.png"
Image.new("RGB", (10, 10)).save(fake, "PNG")
old_time = time.time() - (40 * 86400)
os.utime(fake, (old_time, old_time))
os.environ["PASTE_ANYWHERE_MAX_AGE_DAYS"] = "30"
os.environ["PASTE_ANYWHERE_MAX_TOTAL_MB"] = "0"
try:
purge_old_clips()
finally:
del os.environ["PASTE_ANYWHERE_MAX_AGE_DAYS"]
del os.environ["PASTE_ANYWHERE_MAX_TOTAL_MB"]
check("purge: old clip file removed by age cap", not fake.exists())
fake.unlink(missing_ok=True)
def main():
proc = subprocess.Popen(
[sys.executable, str(BRIDGE_SCRIPT)],
creationflags=CREATE_NO_WINDOW,
)
time.sleep(2)
if proc.poll() is not None:
print(
"[FAIL] bridge exited at startup - is a production instance already "
"running holding the mutex?"
)
sys.exit(1)
try:
marker_id = win32clipboard.RegisterClipboardFormat("PasteAnywhereMarker")
set_test_bitmap()
deadline = time.time() + 10
triggered = False
while time.time() < deadline:
time.sleep(0.3)
if not open_clipboard_with_retry():
continue
try:
has_hdrop = win32clipboard.IsClipboardFormatAvailable(win32con.CF_HDROP)
has_text = win32clipboard.IsClipboardFormatAvailable(win32con.CF_UNICODETEXT)
has_marker = win32clipboard.IsClipboardFormatAvailable(marker_id)
has_dib = win32clipboard.IsClipboardFormatAvailable(win32con.CF_DIB)
finally:
win32clipboard.CloseClipboard()
if has_hdrop and has_text and has_marker and has_dib:
triggered = True
break
check("bridge triggered and set CF_HDROP + CF_UNICODETEXT + marker + CF_DIB", triggered)
if not triggered:
print("[FAIL] bridge did not trigger - skipping checks that depend on it")
else:
path_str = read_clipboard_text()
saved_path = Path(path_str)
check("clipboard text is a real file path", saved_path.exists())
if saved_path.exists():
try:
with Image.open(saved_path) as img:
img.load()
check(
"saved file is a valid 200x100 PNG",
img.format == "PNG" and img.size == (200, 100),
)
except Exception as e:
check(f"saved file is a valid 200x100 PNG (error: {e})", False)
seq1 = get_seq_number()
time.sleep(3)
seq2 = get_seq_number()
check("loop prevention: clipboard sequence number unchanged after re-set", seq1 == seq2)
saved_path.unlink(missing_ok=True)
test_text = "smoke-test-plain-text-no-touch"
set_plain_text(test_text)
time.sleep(2)
if not open_clipboard_with_retry():
check("negative test: could open clipboard to verify", False)
else:
try:
has_hdrop_after = win32clipboard.IsClipboardFormatAvailable(win32con.CF_HDROP)
text_after = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
finally:
win32clipboard.CloseClipboard()
check("negative test: bridge did not add CF_HDROP to plain text", not has_hdrop_after)
check("negative test: plain text unchanged", text_after == test_text)
test_duplicate_guard()
test_downscale()
test_duplicate_guard_oversized()
finally:
proc.terminate()
try:
proc.wait(timeout=5)
except Exception:
proc.kill()
test_purge_old_clips()
if all(results):
print("ALL CHECKS PASSED")
sys.exit(0)
else:
print("SOME CHECKS FAILED")
sys.exit(1)
if __name__ == "__main__":
main()