Skip to content

Commit d90ca4a

Browse files
committed
feat: Enhance transports, response parsing, and tests
Multiple improvements across fastboot transports, protocol handling, USB behavior, and tests: - Tests: add many unit tests and test helpers (MockTransport, ProtocolDownloadCaptureTransport, DelegatingTransport) covering packed frames, DATA parsing, download/upload edge cases, UDP behaviors, and GetVar caching. - UDP: refactor UdpTransport constructor to accept timeout and max attempts, enforce timeouts, validate packet sizes, implement proper continuation handling, sequence tracking, out-of-turn data/error detection, and clearer error reporting. - TCP: add connect/read/write timeouts and use ConnectAsync with a timeout to avoid hangs during handshake. - USB: align host behavior with AOSP by avoiding forced host-side ZLPs (Linux, macOS, WinUSB), and add IO timeouts for legacy Windows device reads/writes using tasks to prevent blocking. - FastbootUtil: reduce OnceSendDataSize to 512KB for better compatibility, avoid caching transient getvar failures, clamp sparse download logic, ensure SparseFile is disposed, and add validation for download sizes and stream EOF handling. - HandleResponse: make response parsing more robust for packed INFO/TEXT frames, fragmented prefixes, DATA size validation (hex/length checks), and better timeout handling. These changes improve reliability, timeout behavior, and correctness when communicating with various fastboot implementations and increase test coverage for edge cases.
1 parent d6a0264 commit d90ca4a

File tree

14 files changed

+894
-173
lines changed

14 files changed

+894
-173
lines changed

FirmwareKit.Comm.Fastboot.Tests/FastbootInfoTests.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,83 @@ public void FlashFromInfo_RebootCommands_MappedCorrectly()
196196
Assert.Contains("reboot-bootloader", transport.Commands);
197197
Assert.Contains("reboot", transport.Commands);
198198
}
199+
200+
[Fact]
201+
public void GetMaxDownloadSize_ClampsToSparseLimit()
202+
{
203+
int originalLimit = FastbootUtil.SparseMaxDownloadSize;
204+
try
205+
{
206+
FastbootUtil.SparseMaxDownloadSize = 1024 * 1024 * 1024;
207+
var transport = new ProtocolScriptTransport(new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
208+
{
209+
["getvar:max-download-size"] = "OKAY0x80000000"
210+
});
211+
var util = new FastbootUtil(transport);
212+
213+
long maxDownloadSize = util.GetMaxDownloadSize();
214+
215+
Assert.Equal(FastbootUtil.SparseMaxDownloadSize, maxDownloadSize);
216+
}
217+
finally
218+
{
219+
FastbootUtil.SparseMaxDownloadSize = originalLimit;
220+
}
221+
}
222+
223+
[Fact]
224+
public void GetVar_FailureIsNotCached_AllowsRetrySuccess()
225+
{
226+
// First read fails, second read succeeds for same key.
227+
int attempt = 0;
228+
string key = "max-download-size";
229+
230+
var util = new FastbootUtil(new DelegatingTransport(
231+
onWrite: (cmd) =>
232+
{
233+
if (cmd.Equals("getvar:" + key, StringComparison.OrdinalIgnoreCase))
234+
{
235+
attempt++;
236+
return attempt == 1 ? "FAILtemporary" : "OKAY0x100000";
237+
}
238+
return "OKAY";
239+
}));
240+
241+
string first = util.GetVar(key);
242+
string second = util.GetVar(key);
243+
244+
Assert.Equal(string.Empty, first);
245+
Assert.Equal("0x100000", second);
246+
}
247+
248+
private sealed class DelegatingTransport : IFastbootTransport
249+
{
250+
private readonly Func<string, string> _onWrite;
251+
private readonly Queue<byte[]> _readQueue = new();
252+
253+
public DelegatingTransport(Func<string, string> onWrite)
254+
{
255+
_onWrite = onWrite;
256+
}
257+
258+
public byte[] Read(int length)
259+
{
260+
if (_readQueue.Count == 0)
261+
{
262+
return Encoding.UTF8.GetBytes("OKAY");
263+
}
264+
return _readQueue.Dequeue();
265+
}
266+
267+
public long Write(byte[] data, int length)
268+
{
269+
string cmd = Encoding.UTF8.GetString(data, 0, length);
270+
string response = _onWrite(cmd);
271+
_readQueue.Enqueue(Encoding.UTF8.GetBytes(response));
272+
return length;
273+
}
274+
275+
public void Dispose() { }
276+
}
199277
}
200278
}

0 commit comments

Comments
 (0)