|
| 1 | +using FirmwareKit.Comm.Fastboot.DataModel; |
| 2 | +using FirmwareKit.Comm.Fastboot.Usb; |
| 3 | +using System.Text; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace FirmwareKit.Comm.Fastboot.Tests |
| 7 | +{ |
| 8 | + public class AospFastbootDriverTests |
| 9 | + { |
| 10 | + private class MockTransport : IFastbootTransport |
| 11 | + { |
| 12 | + private readonly Queue<byte[]> _responses = new Queue<byte[]>(); |
| 13 | + public List<string> WrittenCommands { get; } = new List<string>(); |
| 14 | + |
| 15 | + public void EnqueueResponse(string response) |
| 16 | + { |
| 17 | + // AOSP fastboot protocol responses are truncated at 256 bytes in some cases, |
| 18 | + // but the string itself is what matters for the test. |
| 19 | + _responses.Enqueue(Encoding.UTF8.GetBytes(response)); |
| 20 | + } |
| 21 | + |
| 22 | + public byte[] Read(int length) |
| 23 | + { |
| 24 | + if (_responses.Count == 0) return Array.Empty<byte>(); |
| 25 | + return _responses.Dequeue(); |
| 26 | + } |
| 27 | + |
| 28 | + public long Write(byte[] data, int length) |
| 29 | + { |
| 30 | + string cmd = Encoding.UTF8.GetString(data, 0, length); |
| 31 | + WrittenCommands.Add(cmd); |
| 32 | + return length; |
| 33 | + } |
| 34 | + |
| 35 | + public void Dispose() { } |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Test_GetVar_Aosp() |
| 40 | + { |
| 41 | + // Ported from: fastboot_driver_test.cpp -> TEST_F(DriverTest, GetVar) |
| 42 | + var transport = new MockTransport(); |
| 43 | + var util = new FastbootUtil(transport); |
| 44 | + |
| 45 | + transport.EnqueueResponse("OKAY0.4"); |
| 46 | + |
| 47 | + string output = util.GetVar("version"); |
| 48 | + |
| 49 | + Assert.Equal("0.4", output); |
| 50 | + Assert.Contains("getvar:version", transport.WrittenCommands); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void Test_InfoMessage_Aosp() |
| 55 | + { |
| 56 | + // Ported from: fastboot_driver_test.cpp -> TEST_F(DriverTest, InfoMessage) |
| 57 | + var transport = new MockTransport(); |
| 58 | + var util = new FastbootUtil(transport); |
| 59 | + |
| 60 | + transport.EnqueueResponse("INFOthis is an info line"); |
| 61 | + transport.EnqueueResponse("OKAY"); |
| 62 | + |
| 63 | + var response = util.RawCommand("oem dmesg"); |
| 64 | + |
| 65 | + Assert.Equal(FastbootState.Success, response.Result); |
| 66 | + Assert.Single(response.Info); |
| 67 | + Assert.Equal("this is an info line", response.Info[0]); |
| 68 | + Assert.Contains("oem dmesg", transport.WrittenCommands); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void Test_TextMessage_Aosp() |
| 73 | + { |
| 74 | + // Ported from: fastboot_driver_test.cpp -> TEST_F(DriverTest, TextMessage) |
| 75 | + var transport = new MockTransport(); |
| 76 | + var util = new FastbootUtil(transport); |
| 77 | + string capturedText = ""; |
| 78 | + |
| 79 | + util.ReceivedFromDevice += (s, e) => |
| 80 | + { |
| 81 | + if (e.Type == FastbootState.Text) |
| 82 | + { |
| 83 | + capturedText += e.NewText; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + transport.EnqueueResponse("TEXTthis is a text line"); |
| 88 | + transport.EnqueueResponse("TEXT, albeit very long and split over multiple TEXT messages."); |
| 89 | + transport.EnqueueResponse("TEXT Indeed we can do that now with a TEXT message whenever we feel like it."); |
| 90 | + transport.EnqueueResponse("TEXT Isn't that truly super cool?"); |
| 91 | + transport.EnqueueResponse("OKAY"); |
| 92 | + |
| 93 | + var response = util.RawCommand("oem trusty runtest trusty.hwaes.bench"); |
| 94 | + |
| 95 | + Assert.Equal(FastbootState.Success, response.Result); |
| 96 | + string expected = "this is a text line" + |
| 97 | + ", albeit very long and split over multiple TEXT messages." + |
| 98 | + " Indeed we can do that now with a TEXT message whenever we feel like it." + |
| 99 | + " Isn't that truly super cool?"; |
| 100 | + Assert.Equal(expected, capturedText); |
| 101 | + // Also check the accumulated text in response object |
| 102 | + Assert.Equal(expected, response.Text); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void Test_Download_Fail_Aosp() |
| 107 | + { |
| 108 | + // Logical check: If download command fails, should return FAIL immediately |
| 109 | + var transport = new MockTransport(); |
| 110 | + var util = new FastbootUtil(transport); |
| 111 | + |
| 112 | + transport.EnqueueResponse("FAILdata too large"); |
| 113 | + |
| 114 | + var response = util.DownloadData(new byte[1024]); |
| 115 | + |
| 116 | + Assert.Equal(FastbootState.Fail, response.Result); |
| 117 | + Assert.Equal("data too large", response.Response); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments