forked from quamotion/madb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdbServerTests.cs
213 lines (161 loc) · 7.27 KB
/
AdbServerTests.cs
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
using Moq;
using SharpAdbClient.Exceptions;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Xunit;
namespace SharpAdbClient.Tests
{
public class AdbServerTests
{
private Func<string, IAdbCommandLineClient> adbCommandLineClientFactory;
private DummyAdbSocket socket;
private DummyAdbCommandLineClient commandLineClient;
private Func<EndPoint, IAdbSocket> adbSocketFactory;
private AdbClient adbClient;
private AdbServer adbServer;
public AdbServerTests()
{
this.socket = new DummyAdbSocket();
this.adbSocketFactory = (endPoint) => this.socket;
this.commandLineClient = new DummyAdbCommandLineClient();
this.adbCommandLineClientFactory = (version) => this.commandLineClient;
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);
}
[Fact]
public void GetStatusNotRunningTest()
{
var adbClientMock = new Mock<IAdbClient>();
adbClientMock.Setup(c => c.GetAdbVersion())
.Throws(new SocketException(AdbServer.ConnectionRefused));
var adbServer = new AdbServer(adbClientMock.Object, this.adbCommandLineClientFactory);
var status = adbServer.GetStatus();
Assert.False(status.IsRunning);
Assert.Null(status.Version);
}
[Fact]
public void GetStatusRunningTest()
{
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0020");
var status = this.adbServer.GetStatus();
Assert.Empty(this.socket.Responses);
Assert.Empty(this.socket.ResponseMessages);
Assert.Single(this.socket.Requests);
Assert.Equal("host:version", this.socket.Requests[0]);
Assert.True(status.IsRunning);
Assert.Equal(new Version(1, 0, 32), status.Version);
}
[Fact]
public void GetStatusOtherSocketExceptionTest()
{
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException();
};
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);
Assert.Throws<SocketException>(() => this.adbServer.GetStatus());
}
[Fact]
public void GetStatusOtherExceptionTest()
{
this.adbSocketFactory = (endPoint) =>
{
throw new Exception();
};
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);
Assert.Throws<Exception>(() => this.adbServer.GetStatus());
}
[Fact]
public void StartServerAlreadyRunningTest()
{
this.commandLineClient.Version = new Version(1, 0, 20);
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0020");
var result = this.adbServer.StartServer(null, false);
Assert.Equal(StartServerResult.AlreadyRunning, result);
Assert.Single(this.socket.Requests);
Assert.Equal("host:version", this.socket.Requests[0]);
}
[Fact]
public void StartServerOutdatedRunningNoExecutableTest()
{
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0010");
Assert.Throws<AdbException>(() => this.adbServer.StartServer(null, false));
}
[Fact]
public void StartServerNotRunningNoExecutableTest()
{
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException(AdbServer.ConnectionRefused);
};
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);
Assert.Throws<AdbException>(() => this.adbServer.StartServer(null, false));
}
[Fact]
public void StartServerOutdatedRunningTest()
{
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("0010");
this.commandLineClient.Version = new Version(1, 0, 32);
Assert.False(this.commandLineClient.ServerStarted);
var result = this.adbServer.StartServer(this.ServerName, false);
Assert.True(this.commandLineClient.ServerStarted);
Assert.Equal(2, this.socket.Requests.Count);
Assert.Equal("host:version", this.socket.Requests[0]);
Assert.Equal("host:kill", this.socket.Requests[1]);
}
[Fact]
public void StartServerNotRunningTest()
{
this.adbSocketFactory = (endPoint) =>
{
throw new SocketException(AdbServer.ConnectionRefused);
};
this.adbClient = new AdbClient(AdbClient.DefaultEndPoint, this.adbSocketFactory);
this.adbServer = new AdbServer(this.adbClient, this.adbCommandLineClientFactory);
this.commandLineClient.Version = new Version(1, 0, 32);
Assert.False(this.commandLineClient.ServerStarted);
var result = this.adbServer.StartServer(this.ServerName, false);
Assert.True(this.commandLineClient.ServerStarted);
}
[Fact]
public void StartServerIntermediateRestartRequestedRunningTest()
{
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("001f");
this.commandLineClient.Version = new Version(1, 0, 32);
Assert.False(this.commandLineClient.ServerStarted);
var result = this.adbServer.StartServer(this.ServerName, true);
Assert.True(this.commandLineClient.ServerStarted);
Assert.Equal(2, this.socket.Requests.Count);
Assert.Equal("host:version", this.socket.Requests[0]);
Assert.Equal("host:kill", this.socket.Requests[1]);
}
[Fact]
public void StartServerIntermediateRestartNotRequestedRunningTest()
{
this.socket.Responses.Enqueue(AdbResponse.OK);
this.socket.ResponseMessages.Enqueue("001f");
this.commandLineClient.Version = new Version(1, 0, 32);
Assert.False(this.commandLineClient.ServerStarted);
var result = this.adbServer.StartServer(this.ServerName, false);
Assert.False(this.commandLineClient.ServerStarted);
Assert.Single(this.socket.Requests);
Assert.Equal("host:version", this.socket.Requests[0]);
}
[Fact]
public void ConstructorAdbClientNullTest()
{
Assert.Throws<ArgumentNullException>(() => new AdbServer(null, this.adbCommandLineClientFactory));
}
private string ServerName => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "adb.exe" : "adb";
}
}