-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathAdbSocketTests.Async.cs
More file actions
263 lines (217 loc) · 9.94 KB
/
Copy pathAdbSocketTests.Async.cs
File metadata and controls
263 lines (217 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AdvancedSharpAdbClient.Tests
{
public partial class AdbSocketTests
{
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, int, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncDATARequestAsyncTest() =>
await RunTestAsync(
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DATA, 2, ctx),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0],
TestContext.Current.CancellationToken);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, UnixFileStatus, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncSENDRequestAsyncTest() =>
await RunTestAsync(
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.SEND, "/test", UnixFileStatus.GroupMask | UnixFileStatus.StickyBit | UnixFileStatus.UserExecute | UnixFileStatus.OtherExecute, ctx),
[(byte)'S', (byte)'E', (byte)'N', (byte)'D', 9, 0, 0, 0, (byte)'/', (byte)'t', (byte)'e', (byte)'s', (byte)'t', (byte)',', (byte)'6', (byte)'3', (byte)'3'],
TestContext.Current.CancellationToken);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncDENTRequestAsyncTest() =>
await RunTestAsync(
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DENT, "/data", ctx),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 5, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a'],
TestContext.Current.CancellationToken);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncNullRequestAsyncTest() =>
_ = await Assert.ThrowsAsync<ArgumentNullException>(() =>
RunTestAsync((socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DATA, null, ctx), [], TestContext.Current.CancellationToken));
/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncResponseAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadSyncResponseAsync()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
await writer.WriteAsync("DENT");
}
tcpSocket.InputStream.Position = 0;
Assert.Equal(SyncCommand.DENT, await socket.ReadSyncResponseAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadStringAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadStringAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (BinaryWriter writer = new(tcpSocket.InputStream, Encoding.UTF8, true))
{
writer.Write(Encoding.UTF8.GetBytes(5.ToString("X4")));
writer.Write("Hello"u8);
writer.Flush();
}
tcpSocket.InputStream.Position = 0;
Assert.Equal("Hello", await socket.ReadStringAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadStringAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadFailStringAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (BinaryWriter writer = new(tcpSocket.InputStream, Encoding.UTF8, true))
{
writer.Write(Encoding.UTF8.GetBytes(nameof(SyncCommand.FAIL)));
writer.Write(Encoding.UTF8.GetBytes(5.ToString("X4")));
writer.Write("Hello"u8);
writer.Flush();
}
tcpSocket.InputStream.Position = 0;
Assert.Equal("Hello", await socket.ReadStringAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncStringAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadSyncStringAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (BinaryWriter writer = new(tcpSocket.InputStream, Encoding.UTF8, true))
{
writer.Write(5);
writer.Write("Hello"u8);
writer.Flush();
}
tcpSocket.InputStream.Position = 0;
Assert.Equal("Hello", await socket.ReadSyncStringAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAdbResponseAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadAdbOkayResponseAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
await writer.WriteAsync("OKAY");
}
tcpSocket.InputStream.Position = 0;
AdbResponse response = await socket.ReadAdbResponseAsync(TestContext.Current.CancellationToken);
Assert.True(response.IOSuccess);
Assert.Equal(string.Empty, response.Message);
Assert.True(response.Okay);
Assert.False(response.Timeout);
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAdbResponseAsync(CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadAdbFailResponseAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
await using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
await writer.WriteAsync("FAIL");
await writer.WriteAsync(17.ToString("X4"));
await writer.WriteAsync("This did not work");
}
tcpSocket.InputStream.Position = 0;
_ = await Assert.ThrowsAsync<AdbException>(() => socket.ReadAdbResponseAsync(TestContext.Current.CancellationToken));
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAsync(byte[], int, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadAsyncTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
// Read 100 bytes from a stream which has 101 bytes available
byte[] data = new byte[101];
for (int i = 0; i < 101; i++)
{
data[i] = (byte)i;
}
await tcpSocket.InputStream.WriteAsync(data, TestContext.Current.CancellationToken);
tcpSocket.InputStream.Position = 0;
// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];
await socket.ReadAsync(received, 100, TestContext.Current.CancellationToken);
for (int i = 0; i < 100; i++)
{
Assert.Equal(received[i], (byte)i);
}
Assert.Equal(0, received[100]);
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAsync(Memory{byte}, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task ReadAsyncMemoryTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
// Read 100 bytes from a stream which has 101 bytes available
byte[] data = new byte[101];
for (int i = 0; i < 101; i++)
{
data[i] = (byte)i;
}
await tcpSocket.InputStream.WriteAsync(data, TestContext.Current.CancellationToken);
tcpSocket.InputStream.Position = 0;
// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];
await socket.ReadAsync(received.AsMemory(0, 100), TestContext.Current.CancellationToken);
for (int i = 0; i < 100; i++)
{
Assert.Equal(received[i], (byte)i);
}
Assert.Equal(0, received[100]);
}
/// <summary>
/// Tests the <see cref="AdbSocket.SendAdbRequestAsync(string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendAdbRequestAsyncTest() =>
await RunTestAsync(
(socket, ctx) => socket.SendAdbRequestAsync("Test", ctx),
"0004Test"u8.ToArray(),
TestContext.Current.CancellationToken);
private static async Task RunTestAsync(Func<IAdbSocket, CancellationToken, Task> test, byte[] expectedDataSent, CancellationToken cancellationToken = default)
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
// Run the test.
await test(socket, cancellationToken);
// Validate the data that was sent over the wire.
Assert.Equal(expectedDataSent, tcpSocket.GetBytesSent());
}
}
}