-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathAdbSocketTests.cs
More file actions
335 lines (278 loc) · 10.7 KB
/
AdbSocketTests.cs
File metadata and controls
335 lines (278 loc) · 10.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Xunit;
namespace AdvancedSharpAdbClient.Tests
{
/// <summary>
/// Tests the <see cref="AdbSocket"/> class.
/// </summary>
public partial class AdbSocketTests
{
/// <summary>
/// Tests the <see cref="AdbSocket.Close"/> method.
/// </summary>
[Fact]
public void CloseTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
Assert.True(socket.Connected);
socket.Close();
Assert.False(socket.Connected);
}
/// <summary>
/// Tests the <see cref="AdbSocket.Dispose()"/> method.
/// </summary>
[Fact]
public void DisposeTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
Assert.True(socket.Connected);
socket.Dispose();
Assert.False(socket.Connected);
}
/// <summary>
/// Tests the <see cref="AdbSocket.IsOkay(ReadOnlySpan{byte})"/> method.
/// </summary>
[Fact]
public void IsOkayTest()
{
byte[] okay = "OKAY"u8.ToArray();
byte[] fail = "FAIL"u8.ToArray();
Assert.True(AdbSocket.IsOkay(okay));
Assert.False(AdbSocket.IsOkay(fail));
}
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequest(SyncCommand, int)"/> method.
/// </summary>
[Fact]
public void SendSyncDATARequestTest() =>
RunTest(
socket => socket.SendSyncRequest(SyncCommand.DATA, 2),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0]);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequest(SyncCommand, string, UnixFileStatus)"/> method.
/// </summary>
[Fact]
public void SendSyncSENDRequestTest() =>
RunTest(
socket => socket.SendSyncRequest(SyncCommand.SEND, "/test", UnixFileStatus.GroupMask | UnixFileStatus.StickyBit | UnixFileStatus.UserExecute | UnixFileStatus.OtherExecute),
[(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']);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequest(SyncCommand, string)"/> method.
/// </summary>
[Fact]
public void SendSyncDENTRequestTest() =>
RunTest(
socket => socket.SendSyncRequest(SyncCommand.DENT, "/data"),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 5, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a']);
/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequest(SyncCommand, string)"/> method.
/// </summary>
[Fact]
public void SendSyncNullRequestTest() =>
_ = Assert.Throws<ArgumentNullException>(() =>
RunTest(socket => socket.SendSyncRequest(SyncCommand.DATA, null), []));
/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncResponse"/> method.
/// </summary>
[Fact]
public void ReadSyncResponseTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
writer.Write("DENT");
}
tcpSocket.InputStream.Position = 0;
Assert.Equal(SyncCommand.DENT, socket.ReadSyncResponse());
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadString"/> method.
/// </summary>
[Fact]
public void ReadStringTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
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", socket.ReadString());
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncString"/> method.
/// </summary>
[Fact]
public void ReadSyncStringTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
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", socket.ReadSyncString());
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAdbResponse"/> method.
/// </summary>
[Fact]
public void ReadAdbOkayResponseTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
writer.Write("OKAY");
}
tcpSocket.InputStream.Position = 0;
AdbResponse response = socket.ReadAdbResponse();
Assert.True(response.IOSuccess);
Assert.Equal(string.Empty, response.Message);
Assert.True(response.Okay);
Assert.False(response.Timeout);
}
/// <summary>
/// Tests the <see cref="AdbSocket.ReadAdbResponse"/> method.
/// </summary>
[Fact]
public void ReadAdbFailResponseTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
using (StreamWriter writer = new(tcpSocket.InputStream, Encoding.ASCII, 4, true))
{
writer.Write("FAIL");
writer.Write(17.ToString("X4"));
writer.Write("This did not work");
}
tcpSocket.InputStream.Position = 0;
_ = Assert.Throws<AdbException>(() => _ = socket.ReadAdbResponse());
}
/// <summary>
/// Tests the <see cref="AdbSocket.Read(byte[], int)"/> method.
/// </summary>
[Fact]
public void ReadTest()
{
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;
}
tcpSocket.InputStream.Write(data);
tcpSocket.InputStream.Position = 0;
// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];
socket.Read(received, 100);
for (int i = 0; i < 100; i++)
{
Assert.Equal(received[i], (byte)i);
}
Assert.Equal(0, received[100]);
}
/// <summary>
/// Tests the <see cref="AdbSocket.Read(Span{byte})"/> method.
/// </summary>
[Fact]
public void ReadSpanTest()
{
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;
}
tcpSocket.InputStream.Write(data);
tcpSocket.InputStream.Position = 0;
// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];
_ = socket.Read(received.AsSpan(0, 100));
for (int i = 0; i < 100; i++)
{
Assert.Equal(received[i], (byte)i);
}
Assert.Equal(0, received[100]);
}
/// <summary>
/// Tests the <see cref="AdbSocket.SendAdbRequest(string)"/> method.
/// </summary>
[Fact]
public void SendAdbRequestTest() =>
RunTest(
socket => socket.SendAdbRequest("Test"),
"0004Test"u8.ToArray());
/// <summary>
/// Tests the <see cref="AdbSocket.SendAdbRequest(DefaultInterpolatedStringHandler)"/> method.
/// </summary>
[Fact]
public void SendAdbRequestSpanTest()
{
RunTest(
socket => socket.SendAdbRequest((DefaultInterpolatedStringHandler)$"Test"),
"0004Test"u8.ToArray());
}
/// <summary>
/// Tests the <see cref="AdbSocket.GetShellStream"/> method.
/// </summary>
[Fact]
public void GetShellStreamTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
using Stream stream = socket.GetShellStream();
Assert.IsType<ShellStream>(stream);
using ShellStream shellStream = (ShellStream)stream;
Assert.Equal(tcpSocket.OutputStream, shellStream.Inner);
}
/// <summary>
/// Tests the <see cref="AdbSocket.Clone()"/> method.
/// </summary>
[Fact]
public void CloneTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket adbSocket = new(tcpSocket);
Assert.True(adbSocket is ICloneable<IAdbSocket>);
using AdbSocket socket = adbSocket.Clone();
Assert.NotEqual(adbSocket.Socket, socket.Socket);
Assert.Equal(adbSocket.Connected, socket.Connected);
}
/// <summary>
/// Tests the <see cref="AdbSocket.ToString()"/> method.
/// </summary>
[Fact]
public void ToStringTest()
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket adbSocket = new(tcpSocket);
Assert.Equal($"{typeof(AdbSocket)} {{ {nameof(AdbSocket.Socket)} = {tcpSocket} }}", adbSocket.ToString());
}
private static void RunTest(Action<IAdbSocket> test, byte[] expectedDataSent)
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);
// Run the test.
test(socket);
// Validate the data that was sent over the wire.
Assert.Equal(expectedDataSent, tcpSocket.GetBytesSent());
}
}
}