test_bambu_ftp.py 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. """Comprehensive FTP test suite for BambuFTPClient.
  2. Tests against a real mock implicit FTPS server, covering:
  3. - Connection (auth, SSL modes, timeout, caching)
  4. - File listing
  5. - Download (bytes, to_file, 0-byte regression)
  6. - Upload (chunked transfer, progress, error codes)
  7. - Delete
  8. - File size
  9. - Storage info (AVBL, directory scan)
  10. - Model-specific behavior (X1C prot_p, A1 prot_c fallback)
  11. - Async wrappers
  12. - Failure injection scenarios (regressions for 0.1.8 bugs)
  13. """
  14. import asyncio
  15. import logging
  16. import socket
  17. import threading
  18. import time
  19. from pathlib import Path
  20. from unittest.mock import patch
  21. import pytest
  22. from backend.app.services import bambu_ftp
  23. from backend.app.services.bambu_ftp import (
  24. BambuFTPClient,
  25. FileNotOnPrinterError,
  26. cache_3mf_download,
  27. clear_3mf_cache,
  28. delete_file_async,
  29. download_file_async,
  30. download_file_try_paths_async,
  31. get_cached_3mf,
  32. list_files_async,
  33. normalize_3mf_name,
  34. upload_file_async,
  35. with_ftp_retry,
  36. )
  37. # Brief delay to allow pyftpdlib to flush uploaded files to disk.
  38. # Needed because upload_file() skips voidresp() for all models,
  39. # so the server may still be processing the data channel close event.
  40. _UPLOAD_FLUSH_DELAY = 0.3
  41. # ---------------------------------------------------------------------------
  42. # TestConnection
  43. # ---------------------------------------------------------------------------
  44. class TestConnection:
  45. """Tests for FTP connect/disconnect behavior."""
  46. def test_connect_success(self, ftp_client_factory):
  47. """Successful implicit FTPS connection and login."""
  48. client = ftp_client_factory()
  49. assert client.connect() is True
  50. client.disconnect()
  51. def test_connect_wrong_access_code(self, ftp_client_factory):
  52. """Wrong access code returns False."""
  53. client = ftp_client_factory(access_code="wrongcode")
  54. assert client.connect() is False
  55. def test_connect_unreachable_host(self, ftp_server):
  56. """Unreachable host returns False."""
  57. client = BambuFTPClient(
  58. ip_address="192.0.2.1", # TEST-NET, guaranteed unreachable
  59. access_code="12345678",
  60. timeout=1.0,
  61. printer_model="X1C",
  62. )
  63. client.FTP_PORT = ftp_server.port
  64. assert client.connect() is False
  65. def test_connect_timeout(self, ftp_server):
  66. """Very short timeout triggers timeout error."""
  67. client = BambuFTPClient(
  68. ip_address="192.0.2.1",
  69. access_code="12345678",
  70. timeout=0.001, # Extremely short
  71. printer_model="X1C",
  72. )
  73. client.FTP_PORT = ftp_server.port
  74. assert client.connect() is False
  75. def test_disconnect_clean(self, ftp_client_factory):
  76. """Clean disconnect after successful connect."""
  77. client = ftp_client_factory()
  78. client.connect()
  79. client.disconnect()
  80. assert client._ftp is None
  81. def test_disconnect_without_connect(self, ftp_client_factory):
  82. """Disconnect without connect does not raise."""
  83. client = ftp_client_factory()
  84. client.disconnect() # Should not raise
  85. assert client._ftp is None
  86. def test_x1c_uses_prot_p(self, ftp_client_factory):
  87. """X1C model connects with prot_p (protected data channel)."""
  88. client = ftp_client_factory(printer_model="X1C")
  89. assert client.connect() is True
  90. assert client._should_use_prot_c() is False
  91. client.disconnect()
  92. def test_a1_defaults_prot_p(self, ftp_client_factory):
  93. """A1 model defaults to prot_p when no cache exists."""
  94. client = ftp_client_factory(printer_model="A1")
  95. assert client._should_use_prot_c() is False
  96. assert client.connect() is True
  97. client.disconnect()
  98. def test_a1_force_prot_c(self, ftp_client_factory):
  99. """A1 model with force_prot_c uses clear data channel."""
  100. client = ftp_client_factory(printer_model="A1", force_prot_c=True)
  101. assert client._should_use_prot_c() is True
  102. assert client.connect() is True
  103. client.disconnect()
  104. def test_cached_mode_respected(self, ftp_client_factory):
  105. """Cached mode is used on subsequent connections."""
  106. BambuFTPClient.cache_mode("127.0.0.1", "prot_c")
  107. client = ftp_client_factory(printer_model="A1")
  108. assert client._should_use_prot_c() is True
  109. assert client.connect() is True
  110. client.disconnect()
  111. # ---------------------------------------------------------------------------
  112. # TestDisconnectServerGone — isolated class because server.stop() calls
  113. # close_all() which nukes all asyncore sockets globally.
  114. # ---------------------------------------------------------------------------
  115. class TestDisconnectServerGone:
  116. """Test disconnect behavior when the server has stopped."""
  117. def test_disconnect_after_server_gone(self, ftp_certs, tmp_path):
  118. """Disconnect after server has stopped does not raise.
  119. disconnect() catches OSError, ftplib.Error, and EOFError so that
  120. best-effort cleanup never propagates exceptions to the caller.
  121. """
  122. from backend.tests.unit.services.mock_ftp_server import (
  123. MockBambuFTPServer,
  124. )
  125. from .conftest import _find_free_port
  126. cert_path, key_path = ftp_certs
  127. port = _find_free_port()
  128. server = MockBambuFTPServer("127.0.0.1", port, str(tmp_path), cert_path, key_path)
  129. server.start()
  130. client = BambuFTPClient("127.0.0.1", "12345678", timeout=5.0)
  131. client.FTP_PORT = port
  132. client.connect()
  133. server.stop()
  134. # Should not raise — disconnect() catches all connection errors
  135. client.disconnect()
  136. assert client._ftp is None
  137. # ---------------------------------------------------------------------------
  138. # TestListFiles
  139. # ---------------------------------------------------------------------------
  140. class TestListFiles:
  141. """Tests for directory listing."""
  142. def test_list_empty_directory(self, ftp_client_factory):
  143. """Listing an empty directory returns empty list."""
  144. client = ftp_client_factory()
  145. client.connect()
  146. files = client.list_files("/cache")
  147. assert files == []
  148. client.disconnect()
  149. def test_list_directory_with_files(self, ftp_client_factory, ftp_server):
  150. """Files in directory are listed correctly."""
  151. ftp_server.add_file("cache/test.3mf", b"x" * 1024)
  152. ftp_server.add_file("cache/test2.gcode", b"y" * 512)
  153. client = ftp_client_factory()
  154. client.connect()
  155. files = client.list_files("/cache")
  156. names = {f["name"] for f in files}
  157. assert "test.3mf" in names
  158. assert "test2.gcode" in names
  159. client.disconnect()
  160. def test_directories_marked(self, ftp_client_factory, ftp_server):
  161. """Subdirectories are identified with is_directory=True."""
  162. ftp_server.add_directory("model/subdir")
  163. client = ftp_client_factory()
  164. client.connect()
  165. files = client.list_files("/model")
  166. dirs = [f for f in files if f["is_directory"]]
  167. assert len(dirs) >= 1
  168. assert dirs[0]["name"] == "subdir"
  169. client.disconnect()
  170. def test_nonexistent_path_returns_empty(self, ftp_client_factory):
  171. """Listing a nonexistent path returns empty list."""
  172. client = ftp_client_factory()
  173. client.connect()
  174. files = client.list_files("/nonexistent/path")
  175. assert files == []
  176. client.disconnect()
  177. def test_file_sizes_and_paths(self, ftp_client_factory, ftp_server):
  178. """File sizes and full paths are parsed correctly."""
  179. ftp_server.add_file("cache/sized.bin", b"a" * 2048)
  180. client = ftp_client_factory()
  181. client.connect()
  182. files = client.list_files("/cache")
  183. sized = [f for f in files if f["name"] == "sized.bin"]
  184. assert len(sized) == 1
  185. assert sized[0]["size"] == 2048
  186. assert sized[0]["path"] == "/cache/sized.bin"
  187. client.disconnect()
  188. # ---------------------------------------------------------------------------
  189. # TestDownload
  190. # ---------------------------------------------------------------------------
  191. class TestDownload:
  192. """Tests for file download operations."""
  193. def test_download_file_returns_bytes(self, ftp_client_factory, ftp_server):
  194. """download_file() returns file content as bytes."""
  195. content = b"Hello FTP World!"
  196. ftp_server.add_file("cache/hello.txt", content)
  197. client = ftp_client_factory()
  198. client.connect()
  199. result = client.download_file("/cache/hello.txt")
  200. assert result == content
  201. client.disconnect()
  202. def test_download_file_missing(self, ftp_client_factory):
  203. """download_file() returns None for missing file."""
  204. client = ftp_client_factory()
  205. client.connect()
  206. result = client.download_file("/cache/does_not_exist.txt")
  207. assert result is None
  208. client.disconnect()
  209. def test_download_to_file_writes_to_disk(self, ftp_client_factory, ftp_server, tmp_path):
  210. """download_to_file() writes content to local filesystem."""
  211. content = b"Downloaded content"
  212. ftp_server.add_file("cache/dl.bin", content)
  213. local = tmp_path / "output" / "dl.bin"
  214. client = ftp_client_factory()
  215. client.connect()
  216. result = client.download_to_file("/cache/dl.bin", local)
  217. assert result is True
  218. assert local.read_bytes() == content
  219. client.disconnect()
  220. def test_download_to_file_creates_parent_dirs(self, ftp_client_factory, ftp_server, tmp_path):
  221. """download_to_file() creates parent directories automatically."""
  222. ftp_server.add_file("cache/nested.txt", b"nested content")
  223. local = tmp_path / "deep" / "nested" / "path" / "nested.txt"
  224. client = ftp_client_factory()
  225. client.connect()
  226. result = client.download_to_file("/cache/nested.txt", local)
  227. assert result is True
  228. assert local.exists()
  229. client.disconnect()
  230. def test_zero_byte_download_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  231. """0-byte download returns False and cleans up (regression test)."""
  232. ftp_server.add_file("cache/empty.bin", b"")
  233. local = tmp_path / "empty.bin"
  234. client = ftp_client_factory()
  235. client.connect()
  236. result = client.download_to_file("/cache/empty.bin", local)
  237. assert result is False
  238. assert not local.exists()
  239. client.disconnect()
  240. def test_download_to_file_prefers_fresh_server_size_over_stale_client_hint(
  241. self, ftp_client_factory, ftp_server, tmp_path
  242. ):
  243. """The immediate printer SIZE result overrides a stale browser hint."""
  244. ftp_server.add_file("cache/short.bin", b"short")
  245. local = tmp_path / "short.bin"
  246. client = ftp_client_factory()
  247. client.connect()
  248. try:
  249. assert client.download_to_file("/cache/short.bin", local, expected_size=100) is True
  250. assert local.read_bytes() == b"short"
  251. finally:
  252. client.disconnect()
  253. def test_download_to_file_enforces_actual_byte_limit(self, ftp_client_factory, ftp_server, tmp_path):
  254. """Untrusted size hints cannot let a transfer exceed the server-side cap."""
  255. ftp_server.add_file("cache/oversize.bin", b"too large")
  256. local = tmp_path / "oversize.bin"
  257. client = ftp_client_factory()
  258. client.connect()
  259. try:
  260. with pytest.raises(bambu_ftp.DownloadLimitExceeded):
  261. client.download_to_file("/cache/oversize.bin", local, max_bytes=3)
  262. assert not local.exists()
  263. finally:
  264. client.disconnect()
  265. def test_download_to_file_uses_server_size_when_client_hint_is_omitted(self, tmp_path):
  266. """A clean short RETR is rejected using SIZE, without trusting a caller hint."""
  267. local = tmp_path / "server-sized.bin"
  268. client = BambuFTPClient("127.0.0.1", "12345678")
  269. class FakeFTP:
  270. def size(self, _remote_path):
  271. return 100
  272. def retrbinary(self, _command, callback):
  273. callback(b"short")
  274. client._ftp = FakeFTP()
  275. assert client.download_to_file("/cache/server-sized.bin", local) is False
  276. assert not local.exists()
  277. def test_download_to_file_falls_back_to_client_hint_when_size_is_unsupported(self, tmp_path):
  278. local = tmp_path / "hint-sized.bin"
  279. client = BambuFTPClient("127.0.0.1", "12345678")
  280. class FakeFTP:
  281. def size(self, _remote_path):
  282. raise bambu_ftp.ftplib.error_perm("502 SIZE unsupported")
  283. def retrbinary(self, _command, callback):
  284. callback(b"short")
  285. client._ftp = FakeFTP()
  286. assert client.download_to_file("/cache/hint-sized.bin", local, expected_size=100) is False
  287. assert not local.exists()
  288. def test_download_to_file_missing_raises_not_on_printer(self, ftp_client_factory, tmp_path):
  289. """Missing file raises FileNotOnPrinterError so callers can short-circuit
  290. the retry loop — 550 means the file isn't there and retrying won't help."""
  291. from backend.app.services.bambu_ftp import FileNotOnPrinterError
  292. local = tmp_path / "missing.bin"
  293. client = ftp_client_factory()
  294. client.connect()
  295. try:
  296. with pytest.raises(FileNotOnPrinterError):
  297. client.download_to_file("/cache/no_such_file.bin", local)
  298. finally:
  299. client.disconnect()
  300. def test_download_large_file(self, ftp_client_factory, ftp_server):
  301. """Large file download (>1MB) works correctly."""
  302. large_content = b"X" * (1024 * 1024 + 500) # ~1MB + 500 bytes
  303. ftp_server.add_file("cache/large.bin", large_content)
  304. client = ftp_client_factory()
  305. client.connect()
  306. result = client.download_file("/cache/large.bin")
  307. assert result == large_content
  308. client.disconnect()
  309. def test_download_not_connected(self):
  310. """download_file() returns None when not connected."""
  311. client = BambuFTPClient("127.0.0.1", "12345678")
  312. assert client.download_file("/cache/test.bin") is None
  313. # ---------------------------------------------------------------------------
  314. # TestUpload
  315. # ---------------------------------------------------------------------------
  316. class TestUpload:
  317. """Tests for file upload operations."""
  318. def test_upload_success(self, ftp_client_factory, ftp_server, tmp_path):
  319. """Successful upload via transfercmd (not storbinary)."""
  320. content = b"Upload test content"
  321. local = tmp_path / "upload.3mf"
  322. local.write_bytes(content)
  323. client = ftp_client_factory()
  324. client.connect()
  325. result = client.upload_file(local, "/cache/upload.3mf")
  326. assert result is True
  327. client.disconnect()
  328. # Verify via fresh connection (upload_file skips voidresp() for all
  329. # models, so the original session can't be reused for download)
  330. time.sleep(_UPLOAD_FLUSH_DELAY)
  331. client2 = ftp_client_factory()
  332. client2.connect()
  333. downloaded = client2.download_file("/cache/upload.3mf")
  334. assert downloaded == content
  335. client2.disconnect()
  336. def test_upload_progress_callback(self, ftp_client_factory, ftp_server, tmp_path):
  337. """Progress callback receives updates during upload."""
  338. content = b"P" * 2048
  339. local = tmp_path / "progress.bin"
  340. local.write_bytes(content)
  341. progress_calls = []
  342. def on_progress(uploaded, total):
  343. progress_calls.append((uploaded, total))
  344. client = ftp_client_factory()
  345. client.connect()
  346. client.upload_file(local, "/cache/progress.bin", on_progress)
  347. assert len(progress_calls) >= 1
  348. # Last call should report full file uploaded
  349. assert progress_calls[-1][0] == len(content)
  350. assert progress_calls[-1][1] == len(content)
  351. client.disconnect()
  352. def test_upload_not_connected(self, tmp_path):
  353. """Upload when not connected returns False."""
  354. local = tmp_path / "test.bin"
  355. local.write_bytes(b"data")
  356. client = BambuFTPClient("127.0.0.1", "12345678")
  357. assert client.upload_file(local, "/cache/test.bin") is False
  358. def test_upload_553_no_sd_card(self, ftp_client_factory, ftp_server, tmp_path):
  359. """553 error (no SD card) returns False."""
  360. ftp_server.inject_failure("STOR", 553, "Could not create file.")
  361. local = tmp_path / "test.bin"
  362. local.write_bytes(b"data")
  363. client = ftp_client_factory()
  364. client.connect()
  365. result = client.upload_file(local, "/cache/test.bin")
  366. assert result is False
  367. client.disconnect()
  368. def test_upload_550_permission_denied(self, ftp_client_factory, ftp_server, tmp_path):
  369. """550 error (permission denied) returns False."""
  370. ftp_server.inject_failure("STOR", 550, "Permission denied.")
  371. local = tmp_path / "test.bin"
  372. local.write_bytes(b"data")
  373. client = ftp_client_factory()
  374. client.connect()
  375. result = client.upload_file(local, "/cache/test.bin")
  376. assert result is False
  377. client.disconnect()
  378. def test_upload_552_storage_full(self, ftp_client_factory, ftp_server, tmp_path):
  379. """552 error (storage full) returns False."""
  380. ftp_server.inject_failure("STOR", 552, "Storage quota exceeded.")
  381. local = tmp_path / "test.bin"
  382. local.write_bytes(b"data")
  383. client = ftp_client_factory()
  384. client.connect()
  385. result = client.upload_file(local, "/cache/test.bin")
  386. assert result is False
  387. client.disconnect()
  388. @pytest.mark.parametrize(
  389. ("sizes", "uploaded", "logged_error"),
  390. [
  391. ([1024], True, False), # complete at the first read
  392. ([None], True, False), # unreadable: nothing to judge by, proceeds as before
  393. # #3538: an A1 still writing the file out reads short at first,
  394. # then reaches the full size.
  395. ([0, 0, 512, 1024], True, False),
  396. # Never complete: no print command for it.
  397. ([100], False, True),
  398. ],
  399. )
  400. def test_upload_without_a_226_is_checked_on_a_new_connection(
  401. self, ftp_client_factory, ftp_server, tmp_path, caplog, sizes, uploaded, logged_error
  402. ):
  403. """No 226 within the wait (an H2D can take 30+ s) still lets the upload
  404. proceed once its size is read back on a fresh connection, since the
  405. old one may be out of step. A short size is read again until it
  406. matches; one that never does fails the upload so it is retried."""
  407. import logging
  408. import backend.app.services.bambu_ftp as ftp_mod
  409. local = tmp_path / "test.bin"
  410. local.write_bytes(b"data" * 256)
  411. client = ftp_client_factory()
  412. client.connect()
  413. client.UNCONFIRMED_SIZE_POLL_INTERVAL = 0
  414. client.UNCONFIRMED_SIZE_POLL_TIMEOUT = 0.2
  415. def never_answers():
  416. raise TimeoutError("timed out")
  417. client._ftp.voidresp = never_answers
  418. checked: list[str] = []
  419. def fake_size(c, remote_path):
  420. checked.append(remote_path)
  421. return sizes[min(len(checked), len(sizes)) - 1]
  422. with (
  423. patch.object(ftp_mod, "_size_on_new_connection", fake_size),
  424. caplog.at_level(logging.INFO, logger="backend.app.services.bambu_ftp"),
  425. ):
  426. assert client.upload_file(local, "/cache/test.bin") is uploaded
  427. client.disconnect()
  428. assert set(checked) == {"/cache/test.bin"}
  429. if uploaded:
  430. assert len(checked) == len(sizes)
  431. else:
  432. assert client.last_failure.kind is ftp_mod.FtpFailureKind.NETWORK
  433. errors = [r for r in caplog.records if r.levelno >= logging.ERROR]
  434. assert bool(errors) is logged_error
  435. def test_the_read_back_reads_the_size_on_its_own_connection(self, ftp_client_factory, ftp_server, tmp_path):
  436. import backend.app.services.bambu_ftp as ftp_mod
  437. local = tmp_path / "r.bin"
  438. local.write_bytes(b"x" * 300)
  439. client = ftp_client_factory()
  440. client.connect()
  441. assert client.upload_file(local, "/cache/r.bin") is True
  442. assert ftp_mod._size_on_new_connection(client, "/cache/r.bin") == 300
  443. client.disconnect()
  444. def test_a_refused_read_back_does_not_pause_the_printer(self, ftp_client_factory, ftp_server):
  445. """A second session can be refused while the first still waits; that
  446. must not open the cool-off that skips the printer's archive downloads."""
  447. import ssl
  448. import backend.app.services.bambu_ftp as ftp_mod
  449. client = ftp_client_factory()
  450. with patch.object(ftp_mod.ImplicitFTP_TLS, "connect", side_effect=ssl.SSLError("refused")):
  451. assert ftp_mod._size_on_new_connection(client, "/cache/r.bin") is None
  452. assert not ftp_mod.BambuFTPClient.handshake_blocked("127.0.0.1")
  453. def test_upload_426_with_intact_file_proceeds(self, ftp_client_factory, ftp_server, tmp_path):
  454. """Some P2S firmware revisions return 426 on voidresp() even when the
  455. file landed fully (TLS data-channel close races the 226). #1417
  456. follow-up — verify via SIZE: when server size matches, proceed with
  457. a warning instead of failing the dispatch.
  458. Pre-#1417 the catch raised unconditionally and the reporter saw 11
  459. retries fail in a row even though every upload was actually
  460. succeeding on the printer side (v0.2.4.1 worked because the prior
  461. proceed-with-warning branch tolerated the noise).
  462. """
  463. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  464. local = tmp_path / "test.bin"
  465. local.write_bytes(b"data" * 256) # 1024 bytes
  466. client = ftp_client_factory()
  467. client.connect()
  468. def raise_426():
  469. raise ftplib.error_temp("426 Failure reading network stream.")
  470. def fake_size(_path):
  471. # Real P2S firmware: voidresp returns 426 but the file IS on
  472. # the SD card at its full size. Mock can't reproduce both
  473. # halves naturally because pyftpdlib only flushes on a clean
  474. # voidresp, so we inject SIZE explicitly to model the
  475. # printer-side state the user observes.
  476. return 1024
  477. client._ftp.voidresp = raise_426
  478. client._ftp.size = fake_size
  479. result = client.upload_file(local, "/cache/test.bin")
  480. assert result is True, "intact file (SIZE match) tolerates 426 noise"
  481. client.disconnect()
  482. def test_upload_426_with_intact_file_logs_at_info_not_warning(
  483. self, ftp_client_factory, ftp_server, tmp_path, caplog
  484. ):
  485. """A verified-intact 426 is how Bambu FTPS normally ends a transfer,
  486. not a fault, so it must not be a WARNING (#2987).
  487. It fired 54 times in one support bundle, every one followed by a
  488. completed upload, and buried the 26 TLS handshake failures in the same
  489. log that actually cost the reporter two prints. The truncated case below
  490. is still an error -- this only moves the one we have already verified.
  491. """
  492. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  493. import logging
  494. local = tmp_path / "test.bin"
  495. local.write_bytes(b"data" * 256) # 1024 bytes
  496. client = ftp_client_factory()
  497. client.connect()
  498. def raise_426():
  499. raise ftplib.error_temp("426 Failure reading network stream.")
  500. client._ftp.voidresp = raise_426
  501. client._ftp.size = lambda _path: 1024
  502. with caplog.at_level(logging.INFO, logger="backend.app.services.bambu_ftp"):
  503. assert client.upload_file(local, "/cache/test.bin") is True
  504. client.disconnect()
  505. intact = [r for r in caplog.records if "file is intact on the" in r.getMessage()]
  506. assert intact, "the proceed path must still say why it proceeded"
  507. assert [r.levelno for r in intact] == [logging.INFO] * len(intact)
  508. def test_upload_426_with_truncated_file_still_logs_an_error(self, ftp_client_factory, ftp_server, tmp_path, caplog):
  509. """The half that must stay loud: bytes that did not verify."""
  510. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  511. import logging
  512. local = tmp_path / "test.bin"
  513. local.write_bytes(b"data" * 256)
  514. client = ftp_client_factory()
  515. client.connect()
  516. def raise_426():
  517. raise ftplib.error_temp("426 Failure reading network stream.")
  518. client._ftp.voidresp = raise_426
  519. client._ftp.size = lambda _path: 100
  520. with caplog.at_level(logging.INFO, logger="backend.app.services.bambu_ftp"):
  521. assert client.upload_file(local, "/cache/test.bin") is False
  522. client.disconnect()
  523. rejected = [r for r in caplog.records if "rejected by printer" in r.getMessage()]
  524. assert rejected and all(r.levelno == logging.ERROR for r in rejected)
  525. def test_upload_426_with_truncated_file_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  526. """The original #1401 fix is preserved: when SIZE confirms the file
  527. isn't on the server at full size (or SIZE itself fails), the upload
  528. must fail so the dispatcher doesn't send a print command for a
  529. partial 3MF."""
  530. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  531. local = tmp_path / "test.bin"
  532. local.write_bytes(b"data" * 256)
  533. client = ftp_client_factory()
  534. client.connect()
  535. def raise_426():
  536. raise ftplib.error_temp("426 Failure reading network stream.")
  537. # Make SIZE report a smaller value — file is genuinely truncated.
  538. def fake_size(_path):
  539. return 100
  540. client._ftp.voidresp = raise_426
  541. client._ftp.size = fake_size
  542. result = client.upload_file(local, "/cache/test.bin")
  543. assert result is False, "truncated file (SIZE mismatch) must fail"
  544. client.disconnect()
  545. def test_upload_426_with_size_check_failing_returns_false(self, ftp_client_factory, ftp_server, tmp_path):
  546. """If SIZE itself fails (e.g. server too broken to answer), assume
  547. the worst and fail — better a retry than a print on a partial file.
  548. """
  549. import ftplib # nosec B402 — tests need the real ftplib to construct mock 426 responses
  550. local = tmp_path / "test.bin"
  551. local.write_bytes(b"data" * 256)
  552. client = ftp_client_factory()
  553. client.connect()
  554. def raise_426():
  555. raise ftplib.error_temp("426 Failure reading network stream.")
  556. def raise_size(_path):
  557. raise ftplib.error_perm("550 File not found.")
  558. client._ftp.voidresp = raise_426
  559. client._ftp.size = raise_size
  560. result = client.upload_file(local, "/cache/test.bin")
  561. assert result is False
  562. client.disconnect()
  563. def test_upload_large_chunked(self, ftp_client_factory, ftp_server, tmp_path):
  564. """Large file upload in chunks completes without error.
  565. Uses 2.5MB to trigger multiple chunks with 64KB CHUNK_SIZE.
  566. Content verification skipped because upload_file() skips
  567. voidresp() for all models, so the server may still be flushing
  568. when we check. The upload result=True confirms the client sent
  569. all chunks without error.
  570. """
  571. content = b"C" * (1024 * 1024 * 2 + 512 * 1024)
  572. local = tmp_path / "large.bin"
  573. local.write_bytes(content)
  574. progress_calls = []
  575. def on_progress(uploaded, total):
  576. progress_calls.append((uploaded, total))
  577. client = ftp_client_factory()
  578. client.connect()
  579. result = client.upload_file(local, "/cache/large.bin", on_progress)
  580. assert result is True
  581. # Verify many chunks were sent (2.5MB / 64KB = 40 chunks)
  582. assert len(progress_calls) >= 38
  583. assert progress_calls[-1][0] == len(content)
  584. client.disconnect()
  585. # ---------------------------------------------------------------------------
  586. # TestDelete
  587. # ---------------------------------------------------------------------------
  588. class TestDelete:
  589. """Tests for file deletion."""
  590. def test_delete_success(self, ftp_client_factory, ftp_server):
  591. """Successful file deletion."""
  592. from backend.app.services.bambu_ftp import DeleteResult
  593. ftp_server.add_file("cache/to_delete.bin", b"delete me")
  594. client = ftp_client_factory()
  595. client.connect()
  596. result = client.delete_file("/cache/to_delete.bin")
  597. assert result == DeleteResult.DELETED
  598. assert not ftp_server.file_exists("cache/to_delete.bin")
  599. client.disconnect()
  600. def test_delete_not_found(self, ftp_client_factory):
  601. """Deleting a nonexistent file returns NOT_FOUND (550, #1721)."""
  602. from backend.app.services.bambu_ftp import DeleteResult
  603. client = ftp_client_factory()
  604. client.connect()
  605. result = client.delete_file("/cache/no_such_file.bin")
  606. assert result == DeleteResult.NOT_FOUND
  607. client.disconnect()
  608. def test_delete_not_connected(self):
  609. """Delete when not connected returns FAILED."""
  610. from backend.app.services.bambu_ftp import DeleteResult
  611. client = BambuFTPClient("127.0.0.1", "12345678")
  612. assert client.delete_file("/cache/test.bin") == DeleteResult.FAILED
  613. # ---------------------------------------------------------------------------
  614. # TestFileSize
  615. # ---------------------------------------------------------------------------
  616. class TestFileSize:
  617. """Tests for get_file_size."""
  618. def test_file_size_correct(self, ftp_client_factory, ftp_server):
  619. """Returns correct file size."""
  620. ftp_server.add_file("cache/sized.bin", b"a" * 4096)
  621. client = ftp_client_factory()
  622. client.connect()
  623. size = client.get_file_size("/cache/sized.bin")
  624. assert size == 4096
  625. client.disconnect()
  626. def test_file_size_missing(self, ftp_client_factory):
  627. """Returns None for missing file."""
  628. client = ftp_client_factory()
  629. client.connect()
  630. size = client.get_file_size("/cache/no_file.bin")
  631. assert size is None
  632. client.disconnect()
  633. def test_file_size_not_connected(self):
  634. """Returns None when not connected."""
  635. client = BambuFTPClient("127.0.0.1", "12345678")
  636. assert client.get_file_size("/cache/test.bin") is None
  637. # ---------------------------------------------------------------------------
  638. # TestStorageInfo
  639. # ---------------------------------------------------------------------------
  640. class TestStorageInfo:
  641. """Tests for storage info and diagnostics."""
  642. def test_avbl_parsed(self, ftp_client_factory, ftp_server):
  643. """AVBL response is parsed for free_bytes."""
  644. ftp_server.set_avbl_bytes(5000000000)
  645. client = ftp_client_factory()
  646. client.connect()
  647. info = client.get_storage_info()
  648. assert info is not None
  649. assert info["free_bytes"] == 5000000000
  650. client.disconnect()
  651. def test_used_bytes_from_scan(self, ftp_client_factory, ftp_server):
  652. """used_bytes calculated from directory scan."""
  653. ftp_server.add_file("cache/file1.bin", b"a" * 1000)
  654. ftp_server.add_file("cache/file2.bin", b"b" * 2000)
  655. client = ftp_client_factory()
  656. client.connect()
  657. info = client.get_storage_info()
  658. assert info is not None
  659. assert info["used_bytes"] >= 3000 # At least these two files
  660. client.disconnect()
  661. def test_storage_info_not_connected(self):
  662. """Returns None when not connected."""
  663. client = BambuFTPClient("127.0.0.1", "12345678")
  664. assert client.get_storage_info() is None
  665. # ---------------------------------------------------------------------------
  666. # TestModelSpecificBehavior
  667. # ---------------------------------------------------------------------------
  668. class TestModelSpecificBehavior:
  669. """Tests for printer model-specific FTP behavior."""
  670. def test_x1c_upload(self, ftp_client_factory, ftp_server, tmp_path):
  671. """X1C upload with session reuse succeeds."""
  672. content = b"X1C upload data"
  673. local = tmp_path / "x1c.3mf"
  674. local.write_bytes(content)
  675. client = ftp_client_factory(printer_model="X1C")
  676. client.connect()
  677. result = client.upload_file(local, "/cache/x1c.3mf")
  678. assert result is True
  679. client.disconnect()
  680. # Verify via fresh connection
  681. time.sleep(_UPLOAD_FLUSH_DELAY)
  682. client2 = ftp_client_factory(printer_model="X1C")
  683. client2.connect()
  684. downloaded = client2.download_file("/cache/x1c.3mf")
  685. assert downloaded == content
  686. client2.disconnect()
  687. def test_a1_upload_prot_c(self, ftp_client_factory, ftp_server, tmp_path):
  688. """A1 model upload with prot_c succeeds."""
  689. content = b"A1 upload data"
  690. local = tmp_path / "a1.3mf"
  691. local.write_bytes(content)
  692. client = ftp_client_factory(printer_model="A1", force_prot_c=True)
  693. client.connect()
  694. result = client.upload_file(local, "/cache/a1.3mf")
  695. assert result is True
  696. client.disconnect()
  697. # Verify via fresh connection
  698. time.sleep(_UPLOAD_FLUSH_DELAY)
  699. client2 = ftp_client_factory(printer_model="A1", force_prot_c=True)
  700. client2.connect()
  701. downloaded = client2.download_file("/cache/a1.3mf")
  702. assert downloaded == content
  703. client2.disconnect()
  704. def test_a1_mini_upload(self, ftp_client_factory, ftp_server, tmp_path):
  705. """A1 Mini model upload succeeds."""
  706. content = b"A1 Mini data"
  707. local = tmp_path / "a1mini.3mf"
  708. local.write_bytes(content)
  709. client = ftp_client_factory(printer_model="A1 Mini", force_prot_c=True)
  710. client.connect()
  711. result = client.upload_file(local, "/cache/a1mini.3mf")
  712. assert result is True
  713. client.disconnect()
  714. def test_p1s_upload(self, ftp_client_factory, ftp_server, tmp_path):
  715. """P1S model upload with session reuse succeeds."""
  716. content = b"P1S upload data"
  717. local = tmp_path / "p1s.3mf"
  718. local.write_bytes(content)
  719. client = ftp_client_factory(printer_model="P1S")
  720. client.connect()
  721. result = client.upload_file(local, "/cache/p1s.3mf")
  722. assert result is True
  723. client.disconnect()
  724. def test_unknown_model_defaults_prot_p(self, ftp_client_factory):
  725. """Unknown model defaults to prot_p."""
  726. client = ftp_client_factory(printer_model="FuturePrinter3000")
  727. assert client._should_use_prot_c() is False
  728. assert client.connect() is True
  729. client.disconnect()
  730. def test_mode_cache_persists_and_clears(self, ftp_client_factory):
  731. """Mode cache works within a test and clears between tests."""
  732. # Cache should be empty at start (autouse fixture clears it)
  733. assert BambuFTPClient._mode_cache == {}
  734. # Connect and cache a mode
  735. BambuFTPClient.cache_mode("127.0.0.1", "prot_p")
  736. assert BambuFTPClient._mode_cache["127.0.0.1"] == "prot_p"
  737. # New client for same IP uses cached mode
  738. client = ftp_client_factory(printer_model="A1")
  739. assert client._get_cached_mode() == "prot_p"
  740. assert client._should_use_prot_c() is False
  741. client.disconnect()
  742. # ---------------------------------------------------------------------------
  743. # TestAsyncWrappers
  744. # ---------------------------------------------------------------------------
  745. class TestAsyncWrappers:
  746. """Tests for async wrapper functions using patch_ftp_port fixture."""
  747. @pytest.mark.asyncio
  748. async def test_upload_file_async_success(self, patch_ftp_port, tmp_path):
  749. """upload_file_async succeeds for X1C."""
  750. content = b"async upload"
  751. local = tmp_path / "async_up.3mf"
  752. local.write_bytes(content)
  753. result = await upload_file_async(
  754. "127.0.0.1",
  755. "12345678",
  756. local,
  757. "/cache/async_up.3mf",
  758. timeout=30.0,
  759. printer_model="X1C",
  760. )
  761. assert result is True
  762. @pytest.mark.asyncio
  763. async def test_upload_file_async_a1_fallback(self, patch_ftp_port, tmp_path):
  764. """upload_file_async tries prot_p then falls back to prot_c for A1."""
  765. content = b"a1 async upload"
  766. local = tmp_path / "a1_async.3mf"
  767. local.write_bytes(content)
  768. # For A1 models, if prot_p succeeds we get True.
  769. # If prot_p fails, it tries prot_c. Either way should succeed
  770. # against our mock server which accepts both.
  771. result = await upload_file_async(
  772. "127.0.0.1",
  773. "12345678",
  774. local,
  775. "/cache/a1_async.3mf",
  776. timeout=30.0,
  777. printer_model="A1",
  778. )
  779. assert result is True
  780. @pytest.mark.asyncio
  781. async def test_download_file_async_success(self, patch_ftp_port, tmp_path):
  782. """download_file_async succeeds."""
  783. server = patch_ftp_port
  784. content = b"async download content"
  785. server.add_file("cache/async_dl.bin", content)
  786. local = tmp_path / "async_dl.bin"
  787. result = await download_file_async(
  788. "127.0.0.1",
  789. "12345678",
  790. "/cache/async_dl.bin",
  791. local,
  792. timeout=30.0,
  793. printer_model="X1C",
  794. )
  795. assert result is True
  796. assert local.read_bytes() == content
  797. @pytest.mark.asyncio
  798. async def test_download_file_async_a1_fallback(self, patch_ftp_port, tmp_path):
  799. """download_file_async falls back for A1 models."""
  800. server = patch_ftp_port
  801. server.add_file("cache/a1_dl.bin", b"a1 data")
  802. local = tmp_path / "a1_dl.bin"
  803. result = await download_file_async(
  804. "127.0.0.1",
  805. "12345678",
  806. "/cache/a1_dl.bin",
  807. local,
  808. timeout=30.0,
  809. printer_model="A1",
  810. )
  811. assert result is True
  812. @pytest.mark.asyncio
  813. async def test_download_file_async_timeout_salvages_completed_zombie(self, tmp_path, monkeypatch):
  814. """Executor thread that completes after wait_for timeout is salvaged.
  815. asyncio.wait_for cannot cancel run_in_executor threads, so the FTP
  816. download may still complete after we give up waiting. If the thread
  817. genuinely finished (signalled via completion["success"] and the file
  818. is on disk), download_file_async should return True rather than False.
  819. Regression for #972: A1 user with 14 MB 3MF hit the hardcoded 60s
  820. timeout, but the download thread finished ~45s later. The successful
  821. file was written to disk but the async wrapper returned False, so the
  822. archive was created as a fallback with no 3MF data.
  823. """
  824. from backend.app.services import bambu_ftp
  825. # Clear mode cache so prot_p path is exercised.
  826. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  827. local = tmp_path / "zombie.bin"
  828. expected_content = b"late arrival but complete"
  829. class FakeClient:
  830. """Connects instantly, download_to_file sleeps past wait_for's
  831. timeout then writes the file and returns True."""
  832. def __init__(self, *args, **kwargs):
  833. pass
  834. def connect(self):
  835. return True
  836. def download_to_file(self, remote_path, local_path, **_kwargs):
  837. time.sleep(0.4) # longer than wait_for timeout=0.1
  838. local_path.write_bytes(expected_content)
  839. return True
  840. def disconnect(self):
  841. pass
  842. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  843. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  844. monkeypatch.setattr(FakeClient, "A1_MODELS", {"A1"}, raising=False)
  845. def _noop_cache(ip, mode):
  846. pass
  847. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(_noop_cache), raising=False)
  848. result = await download_file_async(
  849. "127.0.0.1",
  850. "12345678",
  851. "/cache/zombie.bin",
  852. local,
  853. timeout=0.1,
  854. printer_model="X1C",
  855. )
  856. assert result is True
  857. assert local.read_bytes() == expected_content
  858. @pytest.mark.asyncio
  859. async def test_download_file_async_timeout_no_salvage_when_incomplete(self, tmp_path, monkeypatch):
  860. """Timeout returns False when thread has not signalled success.
  861. A partial file on disk (mid-retrbinary) must NOT be mistaken for a
  862. completed download — only the thread's explicit success flag permits
  863. salvage.
  864. """
  865. from backend.app.services import bambu_ftp
  866. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  867. local = tmp_path / "partial.bin"
  868. class FakeClient:
  869. def __init__(self, *args, **kwargs):
  870. pass
  871. def connect(self):
  872. return True
  873. def download_to_file(self, remote_path, local_path, **kwargs):
  874. # Simulate an in-progress partial write that never completes
  875. # until the async timeout asks the worker to unwind.
  876. local_path.write_bytes(b"partial...")
  877. while not kwargs["cancel_event"].is_set():
  878. time.sleep(0.01)
  879. local_path.unlink(missing_ok=True)
  880. raise bambu_ftp.DownloadCancelled(remote_path)
  881. def disconnect(self):
  882. pass
  883. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  884. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  885. monkeypatch.setattr(FakeClient, "A1_MODELS", set(), raising=False)
  886. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  887. result = await download_file_async(
  888. "127.0.0.1",
  889. "12345678",
  890. "/cache/partial.bin",
  891. local,
  892. timeout=0.1,
  893. printer_model="X1C",
  894. )
  895. assert result is False
  896. @pytest.mark.asyncio
  897. async def test_download_file_async_timeout_waits_for_slow_zombie(self, tmp_path, monkeypatch):
  898. """A zombie that completes within the 30s grace window is salvaged.
  899. Regression for #1014: on slow WiFi, download_to_file can overshoot the
  900. user's ftp_timeout by 10–30 s without being stuck. The old fixed 0.5 s
  901. post-timeout sleep was too short — it gave up and started attempt 2
  902. while attempt 1's zombie thread kept running, and by the time the zombie
  903. wrote the file to disk with a success flag, attempt 2 had already
  904. reported failure (its own completion dict was still False). The async
  905. wrapper now waits up to min(timeout, 30 s) for the worker thread to
  906. finish before returning, so a slow-but-progressing download salvages.
  907. """
  908. from backend.app.services import bambu_ftp
  909. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  910. local = tmp_path / "slow_zombie.bin"
  911. expected_content = b"finished during grace window"
  912. class FakeClient:
  913. """Mimics a slow FTP: wait_for gives up at 1.0 s but RETR takes
  914. 1.5 s total. Old 0.5 s fixed sleep would have bailed (0.5 < 0.5
  915. extra); new grace = max(min(1.0, 30), 0.5) = 1.0 s covers the
  916. remaining 0.5 s so salvage succeeds."""
  917. def __init__(self, *args, **kwargs):
  918. pass
  919. def connect(self):
  920. return True
  921. def download_to_file(self, remote_path, local_path, **_kwargs):
  922. time.sleep(1.5) # wait_for times out at 1.0 s; zombie finishes 0.5 s later
  923. local_path.write_bytes(expected_content)
  924. return True
  925. def disconnect(self):
  926. pass
  927. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  928. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  929. monkeypatch.setattr(FakeClient, "A1_MODELS", set(), raising=False)
  930. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  931. result = await download_file_async(
  932. "127.0.0.1",
  933. "12345678",
  934. "/cache/slow_zombie.bin",
  935. local,
  936. timeout=1.0,
  937. printer_model="X1C",
  938. )
  939. assert result is True
  940. assert local.read_bytes() == expected_content
  941. @pytest.mark.asyncio
  942. async def test_download_file_async_cancellation_waits_for_worker_cleanup(self, tmp_path, monkeypatch):
  943. """Task cancellation returns only after the FTP worker has unwound."""
  944. from backend.app.services import bambu_ftp
  945. bambu_ftp.BambuFTPClient._mode_cache.pop("127.0.0.1", None)
  946. local = tmp_path / "cancelled.bin"
  947. started = threading.Event()
  948. finished = threading.Event()
  949. class FakeClient:
  950. def __init__(self, *args, **kwargs):
  951. pass
  952. def connect(self):
  953. return True
  954. def download_to_file(self, remote_path, local_path, **kwargs):
  955. local_path.write_bytes(b"partial")
  956. started.set()
  957. try:
  958. while not kwargs["cancel_event"].is_set():
  959. time.sleep(0.01)
  960. local_path.unlink(missing_ok=True)
  961. raise bambu_ftp.DownloadCancelled(remote_path)
  962. finally:
  963. finished.set()
  964. def disconnect(self):
  965. pass
  966. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  967. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  968. monkeypatch.setattr(FakeClient, "A1_MODELS", set(), raising=False)
  969. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  970. task = asyncio.create_task(
  971. download_file_async(
  972. "127.0.0.1",
  973. "12345678",
  974. "/cache/cancelled.bin",
  975. local,
  976. timeout=30.0,
  977. printer_model="X1C",
  978. )
  979. )
  980. assert await asyncio.to_thread(started.wait, 1.0)
  981. task.cancel()
  982. with pytest.raises(asyncio.CancelledError):
  983. await task
  984. assert finished.is_set()
  985. assert not local.exists()
  986. @pytest.mark.asyncio
  987. async def test_download_file_try_paths_first_succeeds(self, patch_ftp_port, tmp_path):
  988. """download_file_try_paths_async succeeds on first path."""
  989. server = patch_ftp_port
  990. server.add_file("cache/try1.bin", b"first path")
  991. local = tmp_path / "try.bin"
  992. result = await download_file_try_paths_async(
  993. "127.0.0.1",
  994. "12345678",
  995. ["/cache/try1.bin", "/cache/try2.bin"],
  996. local,
  997. printer_model="X1C",
  998. )
  999. # The path, not a flag: the caller logs which candidate served the file
  1000. # so a stale same-named copy is diagnosable (#1820).
  1001. assert result == "/cache/try1.bin"
  1002. assert local.read_bytes() == b"first path"
  1003. @pytest.mark.asyncio
  1004. async def test_download_file_try_paths_fallback(self, patch_ftp_port, tmp_path):
  1005. """download_file_try_paths_async falls back to second path."""
  1006. server = patch_ftp_port
  1007. server.add_file("cache/second.bin", b"second path")
  1008. local = tmp_path / "fallback.bin"
  1009. result = await download_file_try_paths_async(
  1010. "127.0.0.1",
  1011. "12345678",
  1012. ["/cache/missing.bin", "/cache/second.bin"],
  1013. local,
  1014. printer_model="X1C",
  1015. )
  1016. assert result == "/cache/second.bin"
  1017. assert local.read_bytes() == b"second path"
  1018. @pytest.mark.asyncio
  1019. async def test_list_files_async_success(self, patch_ftp_port):
  1020. """list_files_async returns file list."""
  1021. server = patch_ftp_port
  1022. server.add_file("cache/listed.bin", b"data")
  1023. result = await list_files_async(
  1024. "127.0.0.1",
  1025. "12345678",
  1026. "/cache",
  1027. timeout=30.0,
  1028. printer_model="X1C",
  1029. )
  1030. names = {f["name"] for f in result}
  1031. assert "listed.bin" in names
  1032. @pytest.mark.asyncio
  1033. async def test_delete_file_async_success(self, patch_ftp_port):
  1034. """delete_file_async deletes a file."""
  1035. from backend.app.services.bambu_ftp import DeleteResult
  1036. server = patch_ftp_port
  1037. server.add_file("cache/to_async_del.bin", b"delete me")
  1038. result = await delete_file_async(
  1039. "127.0.0.1",
  1040. "12345678",
  1041. "/cache/to_async_del.bin",
  1042. printer_model="X1C",
  1043. )
  1044. assert result == DeleteResult.DELETED
  1045. assert not server.file_exists("cache/to_async_del.bin")
  1046. @pytest.mark.asyncio
  1047. async def test_delete_file_async_not_found(self, patch_ftp_port):
  1048. """delete_file_async distinguishes 550 from real failure (#1721)."""
  1049. from backend.app.services.bambu_ftp import DeleteResult
  1050. result = await delete_file_async(
  1051. "127.0.0.1",
  1052. "12345678",
  1053. "/cache/never_existed.bin",
  1054. printer_model="X1C",
  1055. )
  1056. assert result == DeleteResult.NOT_FOUND
  1057. # ---------------------------------------------------------------------------
  1058. # TestFailureScenarios
  1059. # ---------------------------------------------------------------------------
  1060. class TestFailureScenarios:
  1061. """Regression tests for known FTP failure modes."""
  1062. def test_550_caught_by_broad_except(self, ftp_client_factory, ftp_server, tmp_path):
  1063. """550 error_perm is caught by (OSError, ftplib.Error) handler.
  1064. Regression: error_perm is a subclass of ftplib.Error, so the
  1065. broad except clause in upload_file catches it correctly.
  1066. """
  1067. ftp_server.inject_failure("STOR", 550, "Permission denied.")
  1068. local = tmp_path / "test.bin"
  1069. local.write_bytes(b"data")
  1070. client = ftp_client_factory()
  1071. client.connect()
  1072. result = client.upload_file(local, "/cache/test.bin")
  1073. assert result is False
  1074. client.disconnect()
  1075. def test_zero_byte_download_detected(self, ftp_client_factory, ftp_server, tmp_path):
  1076. """0-byte download is detected and file is cleaned up.
  1077. Regression: Prior to fix, 0-byte downloads were reported as success.
  1078. """
  1079. ftp_server.add_file("cache/zero.bin", b"")
  1080. local = tmp_path / "zero.bin"
  1081. client = ftp_client_factory()
  1082. client.connect()
  1083. result = client.download_to_file("/cache/zero.bin", local)
  1084. assert result is False
  1085. assert not local.exists()
  1086. client.disconnect()
  1087. def test_connection_refused_handled(self):
  1088. """Connection refused is handled gracefully."""
  1089. client = BambuFTPClient("127.0.0.1", "12345678", timeout=2.0)
  1090. client.FTP_PORT = 1 # Almost certainly not listening
  1091. assert client.connect() is False
  1092. def test_auth_failure_530(self, ftp_client_factory, ftp_server):
  1093. """530 authentication failure returns False."""
  1094. ftp_server.inject_failure("PASS", 530, "Login incorrect.")
  1095. client = ftp_client_factory()
  1096. result = client.connect()
  1097. assert result is False
  1098. def test_retr_550_handled(self, ftp_client_factory, ftp_server):
  1099. """RETR 550 (file not found) returns None."""
  1100. ftp_server.inject_failure("RETR", 550, "File not found.")
  1101. ftp_server.add_file("cache/exists.bin", b"data")
  1102. client = ftp_client_factory()
  1103. client.connect()
  1104. result = client.download_file("/cache/exists.bin")
  1105. assert result is None
  1106. client.disconnect()
  1107. def test_cwd_550_handled(self, ftp_client_factory, ftp_server):
  1108. """CWD 550 is handled in list_files."""
  1109. ftp_server.inject_failure("CWD", 550, "Directory not found.")
  1110. client = ftp_client_factory()
  1111. client.connect()
  1112. result = client.list_files("/nonexistent")
  1113. assert result == []
  1114. client.disconnect()
  1115. def test_stor_553_handled(self, ftp_client_factory, ftp_server, tmp_path):
  1116. """STOR 553 (no SD card) handled gracefully."""
  1117. ftp_server.inject_failure("STOR", 553, "Could not create file.")
  1118. local = tmp_path / "test.bin"
  1119. local.write_bytes(b"test")
  1120. client = ftp_client_factory()
  1121. client.connect()
  1122. result = client.upload_file(local, "/cache/test.bin")
  1123. assert result is False
  1124. client.disconnect()
  1125. def test_failure_injection_count_decrements(self, ftp_client_factory, ftp_server):
  1126. """Failure injection with count decrements and eventually succeeds."""
  1127. ftp_server.add_file("cache/retry.bin", b"data after retry")
  1128. ftp_server.inject_failure("RETR", 550, "Temporary error.", count=1)
  1129. client = ftp_client_factory()
  1130. client.connect()
  1131. # First attempt fails
  1132. result1 = client.download_file("/cache/retry.bin")
  1133. assert result1 is None
  1134. # Second attempt succeeds (failure count exhausted)
  1135. result2 = client.download_file("/cache/retry.bin")
  1136. assert result2 == b"data after retry"
  1137. client.disconnect()
  1138. def test_upload_skips_voidresp(self, ftp_client_factory, ftp_server, tmp_path):
  1139. """Upload returns True without calling voidresp() for any model.
  1140. voidresp() is skipped for all models: A1 printers hang on it,
  1141. H2D printers delay the 226 response by 30+ seconds, and X1C/P1S
  1142. gain nothing from waiting. The file is on the SD card once
  1143. sendall() returns.
  1144. """
  1145. content = b"voidresp test data"
  1146. local = tmp_path / "voidresp_test.3mf"
  1147. local.write_bytes(content)
  1148. for model in ("X1C", "A1", "H2D", None):
  1149. client = ftp_client_factory(printer_model=model)
  1150. client.connect()
  1151. result = client.upload_file(local, "/cache/voidresp_test.3mf")
  1152. assert result is True, f"Upload failed for model={model}"
  1153. client.disconnect()
  1154. # Verify the file is actually on the server
  1155. time.sleep(_UPLOAD_FLUSH_DELAY)
  1156. client2 = ftp_client_factory()
  1157. client2.connect()
  1158. downloaded = client2.download_file("/cache/voidresp_test.3mf")
  1159. assert downloaded == content, f"Content mismatch for model={model}"
  1160. client2.disconnect()
  1161. # ---------------------------------------------------------------------------
  1162. # Short-circuit retries on 550 (#972)
  1163. # ---------------------------------------------------------------------------
  1164. class TestFileNotOnPrinterShortCircuit:
  1165. """FileNotOnPrinterError must bypass the retry budget.
  1166. Before this fix, a 3MF path that wasn't on the printer (550) cost
  1167. `ftp_retry_count + 1` attempts × `ftp_retry_delay` seconds per candidate
  1168. path. With ftp_retry_count=10 and four candidate paths, that's ~22 min
  1169. of dead retries before the real path is tried. #972 in the wild showed
  1170. 48 min of retrying paths that didn't exist.
  1171. """
  1172. async def test_with_ftp_retry_propagates_file_not_on_printer_without_retrying(self):
  1173. """with_ftp_retry raises FileNotOnPrinterError on first attempt.
  1174. Verifies non_retry_exceptions short-circuits before the retry loop
  1175. has a chance to sleep and try again.
  1176. """
  1177. attempts = {"n": 0}
  1178. async def always_missing(*_args, **_kwargs):
  1179. attempts["n"] += 1
  1180. raise FileNotOnPrinterError("/cache/absent.3mf: 550")
  1181. with pytest.raises(FileNotOnPrinterError):
  1182. await with_ftp_retry(
  1183. always_missing,
  1184. max_retries=10,
  1185. retry_delay=0.01,
  1186. operation_name="test 550 short-circuit",
  1187. non_retry_exceptions=(FileNotOnPrinterError,),
  1188. )
  1189. assert attempts["n"] == 1, "550 must not trigger any retry"
  1190. async def test_with_ftp_retry_still_retries_transient_errors(self):
  1191. """Non-550 exceptions continue to retry up to max_retries + 1."""
  1192. attempts = {"n": 0}
  1193. async def flaky(*_args, **_kwargs):
  1194. attempts["n"] += 1
  1195. raise TimeoutError("transient")
  1196. result = await with_ftp_retry(
  1197. flaky,
  1198. max_retries=2,
  1199. retry_delay=0.01,
  1200. operation_name="test transient retries",
  1201. non_retry_exceptions=(FileNotOnPrinterError,),
  1202. )
  1203. assert result is None
  1204. assert attempts["n"] == 3, "Transient errors should retry to exhaustion"
  1205. def test_download_to_file_raises_on_missing_path(self, ftp_client_factory, tmp_path):
  1206. """download_to_file surfaces 550 as FileNotOnPrinterError end-to-end
  1207. against the real mock FTPS server, not just a hand-rolled mock."""
  1208. local = tmp_path / "never_downloaded.3mf"
  1209. client = ftp_client_factory()
  1210. client.connect()
  1211. try:
  1212. with pytest.raises(FileNotOnPrinterError):
  1213. client.download_to_file("/cache/does_not_exist.3mf", local)
  1214. finally:
  1215. client.disconnect()
  1216. assert not local.exists(), "Partial file must be cleaned up on 550"
  1217. # ---------------------------------------------------------------------------
  1218. # 3MF download cache (#972)
  1219. # ---------------------------------------------------------------------------
  1220. class TestThreeMFCache:
  1221. """Cover endpoint and archive flow share downloaded 3MF bytes via this
  1222. cache. Tests isolate themselves with clear_3mf_cache(delete_files=False)
  1223. so they don't clobber each other."""
  1224. def setup_method(self):
  1225. clear_3mf_cache(delete_files=False)
  1226. def teardown_method(self):
  1227. clear_3mf_cache(delete_files=False)
  1228. def test_normalize_collapses_filename_variants(self):
  1229. """Bambu names vary (.3mf, .gcode.3mf, with spaces) — they all map
  1230. to the same cache slot so both flows agree on the key."""
  1231. canonical = normalize_3mf_name("Broly_Legendary.gcode.3mf")
  1232. assert normalize_3mf_name("Broly_Legendary.3mf") == canonical
  1233. assert normalize_3mf_name("Broly_Legendary") == canonical
  1234. # Bambu Studio rewrites spaces to underscores on upload — treat as equal
  1235. assert normalize_3mf_name("Broly Legendary") == canonical
  1236. # Case is also collapsed so keys match across capitalizations
  1237. assert normalize_3mf_name("BROLY_LEGENDARY.3MF") == canonical
  1238. def test_cache_hit_returns_stored_path(self, tmp_path):
  1239. """get_cached_3mf returns the same Path that was put in."""
  1240. f = tmp_path / "Broly.gcode.3mf"
  1241. f.write_bytes(b"fake 3mf content")
  1242. cache_3mf_download(1, "Broly.gcode.3mf", f)
  1243. assert get_cached_3mf(1, "Broly.gcode.3mf") == f
  1244. def test_cache_lookup_uses_normalized_name(self, tmp_path):
  1245. """Caching under .gcode.3mf and querying with bare name still hits."""
  1246. f = tmp_path / "Broly.gcode.3mf"
  1247. f.write_bytes(b"x")
  1248. cache_3mf_download(1, "Broly.gcode.3mf", f)
  1249. assert get_cached_3mf(1, "Broly.3mf") == f
  1250. assert get_cached_3mf(1, "Broly") == f
  1251. def test_cache_miss_on_different_printer(self, tmp_path):
  1252. """Printer id is part of the key — two printers never collide."""
  1253. f = tmp_path / "A.3mf"
  1254. f.write_bytes(b"x")
  1255. cache_3mf_download(1, "A.3mf", f)
  1256. assert get_cached_3mf(2, "A.3mf") is None
  1257. def test_cache_evicts_when_file_deleted(self, tmp_path):
  1258. """Stale entry (file gone) returns None and is dropped from the dict."""
  1259. f = tmp_path / "A.3mf"
  1260. f.write_bytes(b"x")
  1261. cache_3mf_download(1, "A.3mf", f)
  1262. f.unlink()
  1263. assert get_cached_3mf(1, "A.3mf") is None
  1264. # Re-populating after eviction works — no ghost entries remain.
  1265. f.write_bytes(b"y")
  1266. cache_3mf_download(1, "A.3mf", f)
  1267. assert get_cached_3mf(1, "A.3mf") == f
  1268. def test_clear_by_printer_scoped(self, tmp_path, monkeypatch):
  1269. """Clearing one printer leaves the other untouched."""
  1270. from backend.app.core import config as _config
  1271. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path)
  1272. temp_dir = tmp_path / "temp"
  1273. temp_dir.mkdir()
  1274. f1 = temp_dir / "one.3mf"
  1275. f1.write_bytes(b"1")
  1276. f2 = temp_dir / "two.3mf"
  1277. f2.write_bytes(b"2")
  1278. cache_3mf_download(1, "one.3mf", f1)
  1279. cache_3mf_download(2, "two.3mf", f2)
  1280. clear_3mf_cache(1)
  1281. assert get_cached_3mf(1, "one.3mf") is None
  1282. assert get_cached_3mf(2, "two.3mf") == f2
  1283. # clear_3mf_cache defaulted to delete_files=True, so the temp file is gone
  1284. assert not f1.exists()
  1285. assert f2.exists()
  1286. def test_clear_without_deleting_files(self, tmp_path, monkeypatch):
  1287. """delete_files=False leaves files on disk — used by tests."""
  1288. from backend.app.core import config as _config
  1289. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path)
  1290. temp_dir = tmp_path / "temp"
  1291. temp_dir.mkdir()
  1292. f = temp_dir / "keep.3mf"
  1293. f.write_bytes(b"x")
  1294. cache_3mf_download(1, "keep.3mf", f)
  1295. clear_3mf_cache(1, delete_files=False)
  1296. assert get_cached_3mf(1, "keep.3mf") is None
  1297. assert f.exists()
  1298. def test_clear_does_not_delete_persistent_files(self, tmp_path, monkeypatch):
  1299. """Regression for #1212 / "file disappeared overnight" reports.
  1300. Dispatch sites added in #1166 cache the live archive copy and library
  1301. file bytes — paths outside ``archive_dir/temp`` — so /cover can skip
  1302. FTP. Those files are user data; the cache cleanup must never unlink
  1303. them. Pre-fix, ``clear_3mf_cache(printer_id, delete_files=True)`` ran
  1304. on every ``on_print_complete`` and silently destroyed them, leaving a
  1305. DB row whose ``file_path`` pointed at nothing — breaking Reprint and
  1306. View G-code with a 404.
  1307. """
  1308. from backend.app.core import config as _config
  1309. monkeypatch.setattr(_config.settings, "archive_dir", tmp_path / "archive")
  1310. (tmp_path / "archive" / "temp").mkdir(parents=True)
  1311. archive_file = tmp_path / "archive" / "1" / "20260504_wallhooks" / "wallhooks.gcode.3mf"
  1312. archive_file.parent.mkdir(parents=True)
  1313. archive_file.write_bytes(b"archive bytes")
  1314. library_file = tmp_path / "library_files" / "abcd.3mf"
  1315. library_file.parent.mkdir(parents=True)
  1316. library_file.write_bytes(b"library bytes")
  1317. temp_file = tmp_path / "archive" / "temp" / "cover_1_x.3mf"
  1318. temp_file.write_bytes(b"temp bytes")
  1319. cache_3mf_download(1, "wallhooks.gcode.3mf", archive_file)
  1320. cache_3mf_download(1, "library.3mf", library_file)
  1321. cache_3mf_download(1, "cover_1_x.3mf", temp_file)
  1322. clear_3mf_cache(1)
  1323. # All three cache entries are dropped from the dict.
  1324. assert get_cached_3mf(1, "wallhooks.gcode.3mf") is None
  1325. assert get_cached_3mf(1, "library.3mf") is None
  1326. assert get_cached_3mf(1, "cover_1_x.3mf") is None
  1327. # But only the temp file is unlinked — user data survives.
  1328. assert archive_file.exists(), "archive 3mf must not be deleted by cache cleanup"
  1329. assert library_file.exists(), "library 3mf must not be deleted by cache cleanup"
  1330. assert not temp_file.exists(), "temp file should still be cleaned up"
  1331. @pytest.fixture
  1332. def slow_upload_client(monkeypatch):
  1333. """Replace BambuFTPClient with a fake whose upload streams slowly.
  1334. Mirrors the real client's contract for the bits that matter here: it fires
  1335. the progress callback once per chunk and treats a callback exception as
  1336. "stop now" — break out of the send loop, drop the partial file, re-raise.
  1337. The returned dict lets a test see what the worker thread actually did,
  1338. which is the whole point: the #2529 ghost transfer was invisible from the
  1339. event loop's side.
  1340. """
  1341. state = {
  1342. "attempts": 0,
  1343. "concurrent": 0,
  1344. "max_concurrent": 0,
  1345. "completed": False,
  1346. "cancelled": False,
  1347. "deleted": [],
  1348. "chunks": 20,
  1349. "chunk_delay": 0.05,
  1350. }
  1351. lock = threading.Lock()
  1352. class FakeClient:
  1353. def __init__(self, *args, **kwargs):
  1354. pass
  1355. def connect(self):
  1356. return True
  1357. def upload_file(self, local_path, remote_path, progress_callback=None):
  1358. with lock:
  1359. state["attempts"] += 1
  1360. state["concurrent"] += 1
  1361. state["max_concurrent"] = max(state["max_concurrent"], state["concurrent"])
  1362. try:
  1363. total = state["chunks"]
  1364. for sent in range(1, total + 1):
  1365. time.sleep(state["chunk_delay"])
  1366. if progress_callback:
  1367. try:
  1368. progress_callback(sent, total)
  1369. except Exception:
  1370. state["cancelled"] = True
  1371. state["deleted"].append(remote_path)
  1372. raise
  1373. state["completed"] = True
  1374. return True
  1375. finally:
  1376. with lock:
  1377. state["concurrent"] -= 1
  1378. def disconnect(self):
  1379. pass
  1380. monkeypatch.setattr(bambu_ftp, "BambuFTPClient", FakeClient)
  1381. monkeypatch.setattr(FakeClient, "_mode_cache", {}, raising=False)
  1382. monkeypatch.setattr(FakeClient, "A1_MODELS", ("A1", "A1 Mini"), raising=False)
  1383. monkeypatch.setattr(FakeClient, "cache_mode", staticmethod(lambda ip, mode: None), raising=False)
  1384. return state
  1385. # ---------------------------------------------------------------------------
  1386. # TestUploadDeadline (#2529)
  1387. # ---------------------------------------------------------------------------
  1388. class TestUploadDeadline:
  1389. """The upload deadline must be size-aware, and must actually stop the transfer.
  1390. Regression for #2529: a 96 MB 3MF to an A1 over WiFi sustains ~75 KB/s and
  1391. needs ~20 minutes. The old flat 600 s wall-clock cap declared it dead at
  1392. ~70 MB, `asyncio.wait_for` cancelled the *future* but not the executor
  1393. thread — which kept streaming — and `with_ftp_retry` then started a second
  1394. STOR of the same file onto the same printer. The reporter's video shows two
  1395. transfers of the same job climbing in parallel (2% and 72%), and the print
  1396. never landed.
  1397. """
  1398. def test_deadline_scales_with_file_size(self, tmp_path):
  1399. """A big file gets proportionally longer, a small one gets the floor."""
  1400. small = tmp_path / "small.3mf"
  1401. small.write_bytes(b"x" * 1024)
  1402. assert bambu_ftp._upload_deadline(small) == bambu_ftp._UPLOAD_MIN_TIMEOUT
  1403. # The reporter's file. At the 25 KB/s floor rate, 96 MB is ~64 minutes —
  1404. # far above the 600 s that killed it at 72%.
  1405. big = tmp_path / "big.3mf"
  1406. big.write_bytes(b"x" * (96 * 1024 * 1024))
  1407. deadline = bambu_ftp._upload_deadline(big)
  1408. assert deadline > bambu_ftp._UPLOAD_MIN_TIMEOUT
  1409. assert deadline == pytest.approx((96 * 1024 * 1024) / bambu_ftp._UPLOAD_FLOOR_BYTES_PER_SEC)
  1410. def test_deadline_falls_back_to_floor_for_unstatable_file(self, tmp_path):
  1411. assert bambu_ftp._upload_deadline(tmp_path / "nope.3mf") == bambu_ftp._UPLOAD_MIN_TIMEOUT
  1412. @pytest.mark.asyncio
  1413. async def test_timeout_stops_the_worker_thread(self, tmp_path, monkeypatch, slow_upload_client):
  1414. """The transfer stops when the deadline expires, instead of streaming on.
  1415. Mutation check: drop the `cancel.set()` in upload_file_async and the
  1416. worker runs to completion, which is exactly the ghost transfer #2529
  1417. reported.
  1418. """
  1419. state = slow_upload_client
  1420. local = tmp_path / "slow.3mf"
  1421. local.write_bytes(b"x" * 4096)
  1422. with pytest.raises(bambu_ftp.UploadCancelled):
  1423. await upload_file_async("127.0.0.1", "12345678", local, "/cache/slow.3mf", timeout=0.2, printer_model="X1C")
  1424. # The worker noticed the cancel and unwound — it did not run to the end.
  1425. await asyncio.sleep(0.5)
  1426. assert state["cancelled"] is True
  1427. assert state["completed"] is False
  1428. # And it cleaned the partial file off the printer on its way out.
  1429. assert state["deleted"] == ["/cache/slow.3mf"]
  1430. @pytest.mark.asyncio
  1431. async def test_timeout_is_not_retried(self, tmp_path, monkeypatch, slow_upload_client):
  1432. """with_ftp_retry must not start a second transfer after a deadline expiry.
  1433. This is the bug the reporter filmed: attempt 2 began while attempt 1 was
  1434. still sending. One attempt, then a hard failure.
  1435. """
  1436. state = slow_upload_client
  1437. local = tmp_path / "slow.3mf"
  1438. local.write_bytes(b"x" * 4096)
  1439. with pytest.raises(bambu_ftp.UploadCancelled):
  1440. await with_ftp_retry(
  1441. upload_file_async,
  1442. "127.0.0.1",
  1443. "12345678",
  1444. local,
  1445. "/cache/slow.3mf",
  1446. timeout=0.2,
  1447. printer_model="X1C",
  1448. max_retries=3,
  1449. retry_delay=0,
  1450. )
  1451. assert state["attempts"] == 1, "a timed-out upload must not be retried"
  1452. @pytest.mark.asyncio
  1453. async def test_uploads_to_one_printer_are_serialized(self, tmp_path, monkeypatch, slow_upload_client):
  1454. """Two dispatches to the same printer queue up; they never overlap.
  1455. Concurrent STORs of the same remote path leave a corrupt file on the SD
  1456. card and make the printer look like it has a flaky network.
  1457. """
  1458. state = slow_upload_client
  1459. state["chunk_delay"] = 0.05
  1460. local = tmp_path / "slow.3mf"
  1461. local.write_bytes(b"x" * 4096)
  1462. async def _dispatch(name: str) -> bool:
  1463. return await upload_file_async(
  1464. "127.0.0.1", "12345678", local, f"/cache/{name}.3mf", timeout=30.0, printer_model="X1C"
  1465. )
  1466. results = await asyncio.gather(_dispatch("a"), _dispatch("b"))
  1467. assert results == [True, True]
  1468. assert state["attempts"] == 2
  1469. assert state["max_concurrent"] == 1, "two uploads ran against the same printer at once"
  1470. def test_progress_callback_raising_deletes_the_partial_file(self, ftp_client_factory, ftp_root, tmp_path):
  1471. """The cancel path in the real client removes what it already wrote.
  1472. This is the mechanism the deadline now hangs off, exercised end to end
  1473. against the mock FTPS server rather than a fake.
  1474. """
  1475. client = ftp_client_factory()
  1476. assert client.connect() is True
  1477. try:
  1478. local = tmp_path / "cancelme.3mf"
  1479. # Two chunks, so the callback fires while there is a partial file.
  1480. local.write_bytes(b"x" * (BambuFTPClient.CHUNK_SIZE * 2))
  1481. def _stop_after_first_chunk(uploaded: int, total: int) -> None:
  1482. raise bambu_ftp.UploadCancelled("stop")
  1483. with pytest.raises(bambu_ftp.UploadCancelled):
  1484. client.upload_file(local, "/cancelme.3mf", _stop_after_first_chunk)
  1485. finally:
  1486. client.disconnect()
  1487. time.sleep(_UPLOAD_FLUSH_DELAY)
  1488. assert not (Path(ftp_root) / "cancelme.3mf").exists(), "partial file left on the printer"
  1489. # ---------------------------------------------------------------------------
  1490. # TestHandshakeCoolOff
  1491. # ---------------------------------------------------------------------------
  1492. class _PlaintextServer:
  1493. """A socket that accepts on 990 and answers in plaintext, not TLS.
  1494. This is what #2780's printers do once their file service wedges: the TCP
  1495. connect succeeds, so a port probe reports the printer as healthy, and then
  1496. the implicit-FTPS handshake dies on ``WRONG_VERSION_NUMBER`` because the
  1497. first bytes back are an FTP banner rather than a TLS record.
  1498. """
  1499. def __init__(self):
  1500. self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1501. self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  1502. self._sock.bind(("127.0.0.1", 0))
  1503. self._sock.listen(8)
  1504. self.port = self._sock.getsockname()[1]
  1505. self.accepts = 0
  1506. self._stop = threading.Event()
  1507. self._thread = threading.Thread(target=self._serve, daemon=True)
  1508. self._thread.start()
  1509. def _serve(self):
  1510. while not self._stop.is_set():
  1511. try:
  1512. conn, _addr = self._sock.accept()
  1513. except OSError:
  1514. return
  1515. self.accepts += 1
  1516. try:
  1517. conn.sendall(b"220 Welcome to the printer.\r\n")
  1518. except OSError:
  1519. pass
  1520. finally:
  1521. conn.close()
  1522. def stop(self):
  1523. self._stop.set()
  1524. self._sock.close()
  1525. self._thread.join(timeout=2)
  1526. @pytest.fixture()
  1527. def plaintext_server():
  1528. server = _PlaintextServer()
  1529. yield server
  1530. server.stop()
  1531. class TestHandshakeCoolOff:
  1532. """A wedged file service must be contacted once, not hundreds of times.
  1533. #2780: an X2D served clean FTPS for five days, flipped, and then failed
  1534. every handshake for eight more. Because each candidate path opened its own
  1535. connection, one reporter's log carried 3511 identical handshake failures.
  1536. """
  1537. def _client(self, server, ip="127.0.0.1"):
  1538. client = BambuFTPClient(ip, "12345678", timeout=5.0, printer_model="P2S")
  1539. client.FTP_PORT = server.port
  1540. return client
  1541. def test_plaintext_answer_on_990_blocks_the_printer(self, plaintext_server, caplog):
  1542. client = self._client(plaintext_server)
  1543. with caplog.at_level(logging.WARNING, logger="backend.app.services.bambu_ftp"):
  1544. assert client.connect() is False
  1545. assert bambu_ftp.ftps_handshake_blocked("127.0.0.1") is True
  1546. messages = [r.getMessage() for r in caplog.records]
  1547. assert any("WRONG_VERSION_NUMBER" in m for m in messages)
  1548. # The warning has to say more than the error: the port is open, so
  1549. # "unblock port 990" is the wrong reading and the operator needs to
  1550. # know what it actually costs them.
  1551. assert any("covers and timelapses cannot be fetched" in m for m in messages)
  1552. # It must NOT prescribe a restart. It used to, and #2780's reporter
  1553. # power-cycled both affected printers with no effect while a single
  1554. # manual connection to the same printers completed a clean handshake.
  1555. # Naming a remedy that is known not to work is worse than naming none.
  1556. assert not any("restart" in m.lower() for m in messages)
  1557. def test_blocked_printer_is_not_contacted_again(self, plaintext_server):
  1558. self._client(plaintext_server).connect()
  1559. # Two, not one: the failed handshake, then one cleartext read asking
  1560. # what the printer actually answered with (#2780). That read is the
  1561. # whole diagnosis and it happens once per cool-off, so the promise
  1562. # this test exists for -- contacted a couple of times, not ~110 --
  1563. # still holds.
  1564. assert plaintext_server.accepts == 2
  1565. for _ in range(5):
  1566. assert self._client(plaintext_server).connect() is False
  1567. # Still two: the cool-off answered without opening a socket, and it
  1568. # gates the probe as well as the handshake.
  1569. assert plaintext_server.accepts == 2
  1570. def test_cooloff_expiry_lets_the_printer_be_retried(self, plaintext_server, monkeypatch):
  1571. monkeypatch.setattr(bambu_ftp, "_HANDSHAKE_COOLOFF_SECONDS", 0.0)
  1572. self._client(plaintext_server).connect()
  1573. assert bambu_ftp.ftps_handshake_blocked("127.0.0.1") is False
  1574. assert self._client(plaintext_server).connect() is False
  1575. # Two handshakes and a cleartext probe on each. A zero-length cool-off
  1576. # is what makes the probe repeat -- it is gated on the window being
  1577. # already open, and here there is never a window. At the real 300s it
  1578. # runs once, which `test_blocked_printer_is_not_contacted_again` pins.
  1579. assert plaintext_server.accepts == 4
  1580. def test_block_is_per_printer(self, plaintext_server):
  1581. self._client(plaintext_server).connect()
  1582. assert bambu_ftp.ftps_handshake_blocked("127.0.0.1") is True
  1583. # A second printer that never failed must stay reachable.
  1584. assert bambu_ftp.ftps_handshake_blocked("192.0.2.77") is False
  1585. def test_expired_block_lets_a_recovered_printer_straight_back_in(self, ftp_client_factory):
  1586. """A power-cycled printer is picked up on the next print, not held out.
  1587. The expired entry is also dropped, so the map holds one key per
  1588. currently-wedged printer rather than one per printer this process has
  1589. ever failed against.
  1590. """
  1591. BambuFTPClient._handshake_blocked_until["127.0.0.1"] = time.monotonic() - 1
  1592. client = ftp_client_factory()
  1593. assert client.connect() is True
  1594. client.disconnect()
  1595. assert BambuFTPClient._handshake_blocked_until == {}