forked from shadowsocks/shadowsocks-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlTest.cs
252 lines (226 loc) · 9.84 KB
/
UrlTest.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
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
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shadowsocks.Controller;
using System.Threading;
using System.Collections.Generic;
using Shadowsocks.Model;
using System.Diagnostics;
namespace Shadowsocks.Test
{
[TestClass]
public class UrlTest
{
Server server1, server1WithRemark, server1WithPlugin, server1WithPluginAndRemark;
string server1CanonUrl, server1WithRemarkCanonUrl, server1WithPluginCanonUrl, server1WithPluginAndRemarkCanonUrl;
Server server2, server2WithRemark, server2WithPlugin, server2WithPluginAndRemark;
string server2CanonUrl, server2WithRemarkCanonUrl, server2WithPluginCanonUrl, server2WithPluginAndRemarkCanonUrl;
[TestInitialize]
public void PrepareTestData()
{
server1 = new Server
{
server = "192.168.100.1",
server_port = 8888,
password = "test",
method = "bf-cfb"
};
server1CanonUrl = "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4";
// server2 has base64 padding
server2 = new Server
{
server = "192.168.1.1",
server_port = 8388,
password = "test",
method = "bf-cfb"
};
server2CanonUrl = "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xLjE6ODM4OA==";
server1WithRemark = new Server
{
server = server1.server,
server_port = server1.server_port,
password = server1.password,
method = server1.method,
remarks = "example-server"
};
server1WithRemarkCanonUrl = "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4#example-server";
server2WithRemark = new Server
{
server = server2.server,
server_port = server2.server_port,
password = server2.password,
method = server2.method,
remarks = "example-server"
};
server2WithRemarkCanonUrl = "ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xLjE6ODM4OA==#example-server";
server1WithPlugin = new Server
{
server = server1.server,
server_port = server1.server_port,
password = server1.password,
method = server1.method,
plugin = "obfs-local",
plugin_opts = "obfs=http;obfs-host=google.com"
};
server1WithPluginCanonUrl =
"ss://[email protected]:8888/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com";
server2WithPlugin = new Server
{
server = server2.server,
server_port = server2.server_port,
password = server2.password,
method = server2.method,
plugin = "obfs-local",
plugin_opts = "obfs=http;obfs-host=google.com"
};
server2WithPluginCanonUrl =
"ss://[email protected]:8388/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com";
server1WithPluginAndRemark = new Server
{
server = server1.server,
server_port = server1.server_port,
password = server1.password,
method = server1.method,
plugin = server1WithPlugin.plugin,
plugin_opts = server1WithPlugin.plugin_opts,
remarks = server1WithRemark.remarks
};
server1WithPluginAndRemarkCanonUrl =
"ss://[email protected]:8888/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com#example-server";
server2WithPluginAndRemark = new Server
{
server = server2.server,
server_port = server2.server_port,
password = server2.password,
method = server2.method,
plugin = server2WithPlugin.plugin,
plugin_opts = server2WithPlugin.plugin_opts,
remarks = server2WithRemark.remarks
};
server2WithPluginAndRemarkCanonUrl =
"ss://[email protected]:8388/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com#example-server";
}
[TestMethod]
public void TestParseUrl_Server1()
{
RunParseShadowsocksUrlTest(
string.Join(
"\r\n",
server1CanonUrl,
"\r\n",
"ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4/",
server1WithRemarkCanonUrl,
"ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4/#example-server"),
new[]
{
server1,
server1,
server1WithRemark,
server1WithRemark
});
RunParseShadowsocksUrlTest(
string.Join(
"\r\n",
"ss://[email protected]:8888",
"\r\n",
"ss://[email protected]:8888/",
"ss://[email protected]:8888#example-server",
"ss://[email protected]:8888/#example-server",
server1WithPluginCanonUrl,
server1WithPluginAndRemarkCanonUrl,
"ss://[email protected]:8888/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com&unsupported=1#example-server"),
new[]
{
server1,
server1,
server1WithRemark,
server1WithRemark,
server1WithPlugin,
server1WithPluginAndRemark,
server1WithPluginAndRemark
});
}
[TestMethod]
public void TestParseUrl_Server2()
{
RunParseShadowsocksUrlTest(
string.Join(
"\r\n",
server2CanonUrl,
"\r\n",
"ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xLjE6ODM4OA==/",
server2WithRemarkCanonUrl,
"ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xLjE6ODM4OA==/#example-server"),
new[]
{
server2,
server2,
server2WithRemark,
server2WithRemark
});
RunParseShadowsocksUrlTest(
string.Join(
"\r\n",
"ss://[email protected]:8388",
"\r\n",
"ss://[email protected]:8388/",
"ss://[email protected]:8388#example-server",
"ss://[email protected]:8388/#example-server",
server2WithPluginCanonUrl,
server2WithPluginAndRemarkCanonUrl,
"ss://[email protected]:8388/?plugin=obfs-local%3bobfs%3dhttp%3bobfs-host%3dgoogle.com&unsupported=1#example-server"),
new[]
{
server2,
server2,
server2WithRemark,
server2WithRemark,
server2WithPlugin,
server2WithPluginAndRemark,
server2WithPluginAndRemark
});
}
[TestMethod]
public void TestUrlGenerate()
{
var generateUrlCases = new Dictionary<string, Server>
{
[server1CanonUrl] = server1,
[server1WithRemarkCanonUrl] = server1WithRemark,
[server1WithPluginCanonUrl] = server1WithPlugin,
[server1WithPluginAndRemarkCanonUrl] = server1WithPluginAndRemark
};
RunGenerateShadowsocksUrlTest(generateUrlCases);
}
private static void RunParseShadowsocksUrlTest(string testCase, IReadOnlyList<Server> expected)
{
var actual = Server.GetServers(testCase);
if (actual.Count != expected.Count)
{
Assert.Fail("Wrong number of configs. Expected: {0}. Actual: {1}", expected.Count, actual.Count);
}
for (int i = 0; i < expected.Count; i++)
{
var expectedServer = expected[i];
var actualServer = actual[i];
Assert.AreEqual(expectedServer.server, actualServer.server);
Assert.AreEqual(expectedServer.server_port, actualServer.server_port);
Assert.AreEqual(expectedServer.password, actualServer.password);
Assert.AreEqual(expectedServer.method, actualServer.method);
Assert.AreEqual(expectedServer.plugin, actualServer.plugin);
Assert.AreEqual(expectedServer.plugin_opts, actualServer.plugin_opts);
Assert.AreEqual(expectedServer.remarks, actualServer.remarks);
Assert.AreEqual(expectedServer.timeout, actualServer.timeout);
}
}
private static void RunGenerateShadowsocksUrlTest(IReadOnlyDictionary<string, Server> testCases)
{
foreach (var testCase in testCases)
{
string expected = testCase.Key;
Server config = testCase.Value;
var actual = ShadowsocksController.GetServerURL(config);
Assert.AreEqual(expected, actual);
}
}
}
}