forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostsFileTest.cs
More file actions
406 lines (364 loc) · 17.2 KB
/
HostsFileTest.cs
File metadata and controls
406 lines (364 loc) · 17.2 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using Moq;
using NUnit.Framework;
using System.Application.Services;
using System.Application.Services.Implementation;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using R = System.Application.Properties.Resources;
namespace System.Application
{
[TestFixture]
public class HostsFileTest
{
static readonly List<string> tempFileNames = new();
public static void DeleteAllTempFileName()
{
foreach (var item in tempFileNames)
{
IOPath.FileIfExistsItDelete(item, notCreateDir: true);
}
}
static void Test(string[] hosts, Action<IHostsFileService> action)
=> Test(hosts, (_, s, _) => action(s));
static void Test(string[] hosts, Action<string[], IHostsFileService, IDesktopPlatformService> action)
{
var tempFileName = Path.GetTempFileName();
tempFileNames.Add(tempFileName);
IOPath.FileIfExistsItDelete(tempFileName);
File.WriteAllLines(tempFileName, hosts);
var mock_dps = new Mock<IDesktopPlatformService>();
mock_dps.Setup(x => x.HostsFilePath).Returns(tempFileName);
mock_dps.Setup(x => x.Default).Returns(DefaultEncoding);
IDesktopPlatformService dps = mock_dps.Object;
IHostsFileService s = new HostsFileServiceImpl(dps);
action(hosts, s, dps);
File.Delete(tempFileName);
tempFileNames.Remove(tempFileName);
}
#region DefaultEncoding
static volatile Encoding? defaultEncoding;
static Encoding DefaultEncoding
{
get
{
if (OperatingSystem2.IsWindows)
{
if (defaultEncoding == null)
{
defaultEncoding = CreateDefaultEncoding();
}
return defaultEncoding;
}
else
{
return Encoding.Default;
}
}
}
static Encoding CreateDefaultEncoding()
{
int codePage = GetACP();
var encoding = CodePagesEncodingProvider.Instance.GetEncoding(codePage);
return encoding ?? Encoding.Default;
}
[DllImport("kernel32.dll")]
[ResourceExposure(ResourceScope.None)]
static extern int GetACP();
#endregion
static bool TestEquals(IEnumerable<(string ip, string domain)> left, IEnumerable<(string ip, string domain)> right)
{
if (left.Count() != right.Count()) return false;
foreach (var (ip, domain) in left)
{
var item = right.FirstOrDefault(x => x.domain == domain);
if (item.ip != ip)
{
return false;
}
}
return true;
}
static readonly string[] hosts_;
static readonly List<string[]> hosts_list;
static HostsFileTest()
{
hosts_ = new[]
{
"127.0.0.1 steampp.net",
"127.0.0.1 bbb.steampp.net",
"127.0.0.1 ccc.steampp.net",
"127.0.0.1 test.steampp.net",
"",
"127.0.0.1 steamcommunity.com #Steam++",
"127.0.0.1 www.steamcommunity.com #Steam++",
"127.0.0.1 steamcdn-a.akamaihd.net #Steam++",
"127.0.0.1 raw.github.com #Steam++",
"127.0.0.1 ddd.steampp.net",
"# Steam++ End",
"# xxx adas dsgdsa scxvcxzvcxzv",
"# Copyright (c) 1993-2009 Microsoft Corp.",
"# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.",
"#",
"# ",
"# ::1 localhost",
};
hosts_list = new()
{
hosts_,
R.hosts_8F473F98.Split(Environment.NewLine),
};
}
static IEnumerable<(string ip, string domain)> GetHosts(string[] hosts)
{
var query = from m in hosts
where !string.IsNullOrWhiteSpace(m)
let line_split_array = m.Split(' ', StringSplitOptions.RemoveEmptyEntries)
where !HostsFileServiceImpl.IsV1Format(line_split_array) &&
line_split_array.Length >= 2 &&
!line_split_array[0].StartsWith('#')
select (ip: line_split_array[0], domain: line_split_array[1], line: line_split_array[0] + ' ' + line_split_array[1]);
var values = query.ToArray();
var lines = query.Select(x => x.line).Distinct().ToArray();
return lines.Select(x => values.Reverse().FirstOrDefault(y => y.line == x)).Select(x => (x.ip, x.domain)).ToArray();
}
[Test]
public void ReadHostsAllLines()
{
foreach (var hosts in hosts_list)
{
Test(hosts, x =>
{
var values = x.ReadHostsAllLines();
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var query = GetHosts(hosts);
var values2 = query.ToList();
var result = TestEquals(values.AppendData, values2);
Assert.IsTrue(result);
});
}
}
[Test]
public void SingleUpdateHosts()
{
const string ip = "127.0.0.2";
const string domain = "abcdefg.steampp.net";
foreach (var hosts in hosts_list)
{
void Action(string[] hosts, IHostsFileService x, string ip, string domain)
{
var values_ = x.UpdateHosts(ip, domain);
Assert.IsTrue(values_.ResultType == OperationResultType.Success, values_.Message);
var values = x.ReadHostsAllLines();
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var query = GetHosts(hosts);
var values2 = query.ToList();
values2.RemoveAll(x => x.domain == domain);
values2.Add((ip, domain));
var result = TestEquals(values.AppendData, values2);
Assert.IsTrue(result);
}
Test(hosts, (hosts, s, dps) =>
{
Action(hosts, s, ip, domain);
var filePath = dps.HostsFilePath;
var lines = File.ReadAllLines(filePath).ToList();
var markSIndex = lines.FindIndex(x => x == HostsFileServiceImpl.MarkStart);
Assert.IsTrue(markSIndex >= 0, "markSIndex");
var markEIndex = lines.FindIndex(x => x == HostsFileServiceImpl.MarkEnd);
Assert.IsTrue(markEIndex >= 0, "markEIndex");
var index = lines.FindIndex(x => x == $"{ip} {domain}");
Assert.IsTrue(index > markSIndex, "index > markSIndex");
Assert.IsTrue(index < markEIndex, "index < markEIndex");
});
var test_line_value = $"{ip}3 {domain}";
var hosts2 = new List<string>(hosts_)
{
test_line_value,
};
Test(hosts2.ToArray(), (hosts, s, dps) =>
{
var test_line_value_array = test_line_value.Split(' ');
Action(hosts, s, test_line_value_array[0], test_line_value_array[1]);
var filePath = dps.HostsFilePath;
var lines = File.ReadAllLines(filePath).ToList();
var backMarkSIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkStart);
Assert.IsTrue(backMarkSIndex >= 0, "backMarkSIndex");
var backMarkEIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkEnd);
Assert.IsTrue(backMarkEIndex >= 0, "backMarkEIndex");
var backIndex = lines.FindIndex(x => x.EndsWith(test_line_value) && x.StartsWith('#'));
Assert.IsTrue(backIndex > backMarkSIndex, "backIndex > backMarkSIndex");
Assert.IsTrue(backIndex < backMarkEIndex, "backIndex < backMarkEIndex");
});
}
}
[Test]
public void MultipleUpdateHosts()
{
(string ip, string domain)[] datas = new[]
{
("127.0.0.2", "qwert.steampp.net"),
("127.0.0.3", "zxcvb.steampp.net"),
};
foreach (var hosts in hosts_list)
{
void Action(string[] hosts, IHostsFileService x, IEnumerable<(string ip, string domain)> datas)
{
var values_ = x.UpdateHosts(datas);
Assert.IsTrue(values_.ResultType == OperationResultType.Success, values_.Message);
var values = x.ReadHostsAllLines();
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var query = GetHosts(hosts);
var values2 = query.ToList();
values2.RemoveAll(x => datas.Select(x => x.domain).Contains(x.domain));
values2.AddRange(datas);
var result = TestEquals(values.AppendData, values2);
Assert.IsTrue(result);
}
Test(hosts, (hosts, s, dps) =>
{
Action(hosts, s, datas);
var filePath = dps.HostsFilePath;
var lines = File.ReadAllLines(filePath).ToList();
var markSIndex = lines.FindIndex(x => x == HostsFileServiceImpl.MarkStart);
Assert.IsTrue(markSIndex >= 0, "markSIndex");
var markEIndex = lines.FindIndex(x => x == HostsFileServiceImpl.MarkEnd);
Assert.IsTrue(markEIndex >= 0, "markEIndex");
foreach (var (ip, domain) in datas)
{
var index = lines.FindIndex(x => x == $"{ip} {domain}");
Assert.IsTrue(index > markSIndex, "index > markSIndex");
Assert.IsTrue(index < markEIndex, "index < markEIndex");
}
});
var test_line_values = datas.Select(x => $"{x.ip}4 {x.domain}");
var hosts2 = new List<string>(hosts_);
hosts2.AddRange(test_line_values);
Test(hosts2.ToArray(), (hosts, s, dps) =>
{
Action(hosts, s,
from m in test_line_values
let test_line_values_array = m.Split(' ')
select (test_line_values_array[0], test_line_values_array[1]));
var filePath = dps.HostsFilePath;
var lines = File.ReadAllLines(filePath).ToList();
var backMarkSIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkStart);
Assert.IsTrue(backMarkSIndex >= 0, "backMarkSIndex");
var backMarkEIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkEnd);
Assert.IsTrue(backMarkEIndex >= 0, "backMarkEIndex");
foreach (var test_line_value in test_line_values)
{
var backIndex = lines.FindIndex(x => x.EndsWith(test_line_value) && x.StartsWith('#'));
Assert.IsTrue(backIndex > backMarkSIndex, "backIndex > backMarkSIndex");
Assert.IsTrue(backIndex < backMarkEIndex, "backIndex < backMarkEIndex");
}
});
}
}
[Test]
public void RemoveHosts()
{
(string ip, string domain)[] datas = new[]
{
("127.1.0.2", "1qwert.steampp.net"),
("127.1.0.3", "1zxcvb.steampp.net"),
};
foreach (var hosts_ in hosts_list)
{
var hosts = hosts_;
var datas2 = datas.Select(x => $"{x.ip} {x.domain}");
hosts = hosts.Concat(datas2).ToArray();
Test(hosts, (hosts, s, dps) =>
{
var str_value_0 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_0_ = File.ReadAllText(dps.HostsFilePath);
var (ip, domain) = datas.First();
var values = s.RemoveHosts(domain);
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var filePath = dps.HostsFilePath;
var lines = File.ReadAllLines(filePath).ToList();
var backMarkSIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkStart);
Assert.IsTrue(backMarkSIndex >= 0, "backMarkSIndex");
var backMarkEIndex = lines.FindIndex(x => x == HostsFileServiceImpl.BackupMarkEnd);
Assert.IsTrue(backMarkEIndex >= 0, "backMarkEIndex");
var backIndex = lines.FindIndex(x => x.EndsWith($"{ip} {domain}") && x.StartsWith('#'));
Assert.IsTrue(backIndex > backMarkSIndex, "backIndex > backMarkSIndex");
Assert.IsTrue(backIndex < backMarkEIndex, "backIndex < backMarkEIndex");
var str_value_1 = File.ReadAllText(dps.HostsFilePath);
TestContext.WriteLine(str_value_1);
var values2 = s.RemoveHosts(domain);
Assert.IsTrue(values2.ResultType == OperationResultType.Success, values2.Message);
var str_value_2 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_2_ = File.ReadAllText(dps.HostsFilePath);
var isTrue = TestEquals(str_value_0, str_value_2);
Assert.IsTrue(isTrue, "str_value_0 == str_value_2");
});
}
}
[Test]
public void RemoveHostsByTag()
{
(string ip, string domain)[] datas = new[]
{
("127.1.0.2", "1qwert.steampp.net"),
("127.1.0.3", "1zxcvb.steampp.net"),
};
var hosts_list = new List<string[]>()
{
hosts_,
R.hosts_8F473F98.Split(Environment.NewLine),
};
foreach (var hosts in hosts_list)
{
Test(hosts, (hosts, s, dps) =>
{
var str_value_0 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_0_ = File.ReadAllText(dps.HostsFilePath);
var values = s.UpdateHosts(datas);
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var str_value_1 = File.ReadAllText(dps.HostsFilePath);
TestContext.WriteLine(str_value_1);
var values2 = s.RemoveHostsByTag();
Assert.IsTrue(values2.ResultType == OperationResultType.Success, values2.Message);
var str_value_2 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_2_ = File.ReadAllText(dps.HostsFilePath);
var isTrue = TestEquals(str_value_0, str_value_2);
Assert.IsTrue(isTrue, "str_value_0 == str_value_2");
});
}
}
[Test]
public void RemoveHostsByTag2()
{
(string ip, string domain)[] datas = new[]
{
("127.1.0.2", "1qwert.steampp.net"),
("127.1.0.3", "1zxcvb.steampp.net"),
};
foreach (var hosts_ in hosts_list)
{
var hosts = hosts_;
var datas2 = datas.Select(x => $"{x.ip} {x.domain}");
hosts = hosts.Concat(datas2).ToArray();
Test(hosts, (hosts, s, dps) =>
{
var str_value_0 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_0_ = File.ReadAllText(dps.HostsFilePath);
var values = s.UpdateHosts(datas.Select(x => (x.ip + "3", x.domain)));
Assert.IsTrue(values.ResultType == OperationResultType.Success, values.Message);
var str_value_1 = File.ReadAllText(dps.HostsFilePath);
TestContext.WriteLine(str_value_1);
var values2 = s.RemoveHostsByTag();
Assert.IsTrue(values2.ResultType == OperationResultType.Success, values2.Message);
var str_value_2 = GetHosts(File.ReadAllLines(dps.HostsFilePath));
//var str_value_2_ = File.ReadAllText(dps.HostsFilePath);
var isTrue = TestEquals(str_value_0, str_value_2);
Assert.IsTrue(isTrue, "str_value_0 == str_value_2");
});
}
}
}
}