Skip to content

Commit 9b646c3

Browse files
committed
Fix AdbSocket.ReadString #131
1 parent 2c11060 commit 9b646c3

File tree

5 files changed

+71
-15
lines changed

5 files changed

+71
-15
lines changed

AdvancedSharpAdbClient.Tests/AdbSocketTests.Async.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ public async Task ReadStringAsyncTest()
8484
Assert.Equal("Hello", await socket.ReadStringAsync());
8585
}
8686

87+
/// <summary>
88+
/// Tests the <see cref="AdbSocket.ReadStringAsync(CancellationToken)"/> method.
89+
/// </summary>
90+
[Fact]
91+
public async Task ReadFailStringAsyncTest()
92+
{
93+
using DummyTcpSocket tcpSocket = new();
94+
using AdbSocket socket = new(tcpSocket);
95+
96+
await using (BinaryWriter writer = new(tcpSocket.InputStream, Encoding.UTF8, true))
97+
{
98+
writer.Write(Encoding.UTF8.GetBytes(nameof(SyncCommand.FAIL)));
99+
writer.Write(Encoding.UTF8.GetBytes(5.ToString("X4")));
100+
writer.Write("Hello"u8);
101+
writer.Flush();
102+
}
103+
104+
tcpSocket.InputStream.Position = 0;
105+
106+
Assert.Equal("Hello", await socket.ReadStringAsync());
107+
}
108+
87109
/// <summary>
88110
/// Tests the <see cref="AdbSocket.ReadSyncStringAsync(CancellationToken)"/> method.
89111
/// </summary>

AdvancedSharpAdbClient.Tests/AdbSocketTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ public void ReadStringTest()
129129
Assert.Equal("Hello", socket.ReadString());
130130
}
131131

132+
/// <summary>
133+
/// Tests the <see cref="AdbSocket.ReadString"/> method.
134+
/// </summary>
135+
[Fact]
136+
public void ReadFailStringTest()
137+
{
138+
using DummyTcpSocket tcpSocket = new();
139+
using AdbSocket socket = new(tcpSocket);
140+
141+
using (BinaryWriter writer = new(tcpSocket.InputStream, Encoding.UTF8, true))
142+
{
143+
writer.Write(Encoding.UTF8.GetBytes(nameof(SyncCommand.FAIL)));
144+
writer.Write(Encoding.UTF8.GetBytes(5.ToString("X4")));
145+
writer.Write("Hello"u8);
146+
writer.Flush();
147+
}
148+
149+
tcpSocket.InputStream.Position = 0;
150+
151+
Assert.Equal("Hello", socket.ReadString());
152+
}
153+
132154
/// <summary>
133155
/// Tests the <see cref="AdbSocket.ReadSyncString"/> method.
134156
/// </summary>

AdvancedSharpAdbClient/AdbSocket.Async.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public virtual async Task<int> ReadAsync(byte[] data, int offset, int length, Ca
179179
/// <inheritdoc/>
180180
public virtual async Task<string> ReadStringAsync(CancellationToken cancellationToken = default)
181181
{
182+
start:
182183
// The first 4 bytes contain the length of the string
183184
byte[] reply = new byte[4];
184185
int read = await ReadAsync(reply, cancellationToken).ConfigureAwait(false);
@@ -191,14 +192,19 @@ public virtual async Task<string> ReadStringAsync(CancellationToken cancellation
191192

192193
// Convert the bytes to a hex string
193194
string lenHex = AdbClient.Encoding.GetString(reply);
194-
int len = int.Parse(lenHex, NumberStyles.HexNumber);
195-
196-
// And get the string
197-
reply = new byte[len];
198-
_ = await ReadAsync(reply, cancellationToken).ConfigureAwait(false);
195+
if (int.TryParse(lenHex, NumberStyles.HexNumber, null, out int len))
196+
{
197+
// And get the string
198+
reply = new byte[len];
199+
_ = await ReadAsync(reply, cancellationToken).ConfigureAwait(false);
199200

200-
string value = AdbClient.Encoding.GetString(reply);
201-
return value;
201+
string value = AdbClient.Encoding.GetString(reply);
202+
return value;
203+
}
204+
else
205+
{
206+
goto start;
207+
}
202208
}
203209

204210
/// <inheritdoc/>

AdvancedSharpAdbClient/AdbSocket.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public virtual int Read(byte[] data, int offset, int length)
263263
/// <inheritdoc/>
264264
public virtual string ReadString()
265265
{
266+
start:
266267
// The first 4 bytes contain the length of the string
267268
byte[] reply = new byte[4];
268269
int read = Read(reply);
@@ -275,14 +276,19 @@ public virtual string ReadString()
275276

276277
// Convert the bytes to a hex string
277278
string lenHex = AdbClient.Encoding.GetString(reply);
278-
int len = int.Parse(lenHex, NumberStyles.HexNumber);
279-
280-
// And get the string
281-
reply = new byte[len];
282-
_ = Read(reply);
279+
if (int.TryParse(lenHex, NumberStyles.HexNumber, null, out int len))
280+
{
281+
// And get the string
282+
reply = new byte[len];
283+
_ = Read(reply);
283284

284-
string value = AdbClient.Encoding.GetString(reply);
285-
return value;
285+
string value = AdbClient.Encoding.GetString(reply);
286+
return value;
287+
}
288+
else
289+
{
290+
goto start;
291+
}
286292
}
287293

288294
/// <inheritdoc/>

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<RepositoryUrl>https://github.com/SharpAdb/AdvancedSharpAdbClient</RepositoryUrl>
2424
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2525
<Title>.NET client for adb, Android Debug Bridge (AdvancedSharpAdbClient)</Title>
26-
<VersionPrefix>3.5.15</VersionPrefix>
26+
<VersionPrefix>3.5.16</VersionPrefix>
2727
</PropertyGroup>
2828

2929
<PropertyGroup>

0 commit comments

Comments
 (0)