Skip to content

Commit

Permalink
增加TorrentService测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kaedei committed Jun 23, 2020
1 parent 7fbde04 commit 52f2d00
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 8 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ResourceMonitor/ResourceMonitor.Test/ResourceMonitor.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.14.3" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
<PackageReference Include="Refit" Version="5.1.67" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ResourceMonitor\ResourceMonitor.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="valid.torrent">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
101 changes: 101 additions & 0 deletions ResourceMonitor/ResourceMonitor.Test/TorrentServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Refit;
using ResourceMonitor.Services.Declaration;
using ResourceMonitor.Services.Implementation;

namespace ResourceMonitor.Test
{
[TestClass]
public class TorrentServiceTests
{
[TestMethod]
public void IsTorrentFileValid_Pass()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var torrentBytes = File.ReadAllBytes("valid.torrent");
Assert.IsTrue(service.IsTorrentFileValid(torrentBytes));
}

[TestMethod]
public void IsTorrentFileValid_NotPass()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var torrentBytes = File.ReadAllBytes("valid.torrent");
//修改一些文件内容
for (int i = 100; i < 200; i++)
{
torrentBytes[i] = 0;
}
Assert.IsFalse(service.IsTorrentFileValid(torrentBytes));
}

[TestMethod]
public void NormalizeMagnetUrl_Pass_40()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b";
var after = service.NormalizeMagnetUrl(magnet);
Assert.AreEqual(magnet, after);
}

[TestMethod]
public void NormalizeMagnetUrl_Pass_32()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:BDBMGDFILT3Y5UKHJCFEGHM25ZIIEST3";
var after = service.NormalizeMagnetUrl(magnet);
Assert.AreEqual("magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b", after);
}

[TestMethod]
public void NormalizeMagnetUrl_Pass_Tracker()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet =
"magnet:?xt=urn:btih:4cda49aa1c28db946e89ecb6e18482c8d347b41d&tr=udp://9.rarbg.to:2710/announce&tr=udp://9.rarbg.me:2710/announce&tr=http://tr.cili001.com:8070/announce&tr=http://tracker.trackerfix.com:80/announce&tr=udp://open.demonii.com:1337&tr=udp://tracker.opentrackr.org:1337/announce&tr=udp://p4p.arenabg.com:1337&tr=wss://tracker.openwebtorrent.com&tr=wss://tracker.btorrent.xyz&tr=wss://tracker.fastcast.nz";
var after = service.NormalizeMagnetUrl(magnet);
Assert.AreEqual("magnet:?xt=urn:btih:4cda49aa1c28db946e89ecb6e18482c8d347b41d", after);
}

[TestMethod]
public void GetHash_Pass_32()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:BDBMGDFILT3Y5UKHJCFEGHM25ZIIEST3";
var hash = service.GetHash(magnet);
Assert.AreEqual("08c2c30ca85cf78ed147488a431d9aee50824a7b", hash);
}

[TestMethod]
public void GetHash_Pass_40()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b";
var hash = service.GetHash(magnet);
Assert.AreEqual("08c2c30ca85cf78ed147488a431d9aee50824a7b", hash);
}

[TestMethod]
public void GetHash_NotPass_UpperCase()
{
var service = new TorrentService(null, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b";
var hash = service.GetHash(magnet);
Assert.AreNotEqual("08C2C30CA85CF78ED147488A431D9AEE50824A7B", hash);
}

//[TestMethod]
public async Task DownloadTorrent_Pass_Online()
{
var api = RestService.For<IMagnetApi>("https://m2t.chinacloudsites.cn");
var service = new TorrentService(api, new NullLogger<TorrentService>());
var magnet = "magnet:?xt=urn:btih:08c2c30ca85cf78ed147488a431d9aee50824a7b";
var torrentBytes = await service.DownloadTorrent(magnet);
Assert.IsTrue(service.IsTorrentFileValid(torrentBytes));
}
}
}
Binary file not shown.
2 changes: 2 additions & 0 deletions ResourceMonitor/ResourceMonitor/ResourceMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
<PackageReference Include="OctoTorrent" Version="0.1.7" />
<PackageReference Include="Refit" Version="5.1.67" />
<PackageReference Include="Refit.HttpClientFactory" Version="5.1.67" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
Expand Down
12 changes: 12 additions & 0 deletions ResourceMonitor/ResourceMonitor/Services/Declaration/IMagnetApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Net.Http;
using System.Threading.Tasks;
using Refit;

namespace ResourceMonitor.Services.Declaration
{
public interface IMagnetApi
{
[Get("/Magnet/Parse")]
Task<HttpContent> ParseMagnet(string magnet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public CheckNewResourcesBackgroundService(IRulesContainer rulesContainer,

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation($"{nameof(CheckNewResourcesBackgroundService)} 开始在后台运行");
_logger.LogInformation($"[检查新资源] 开始在后台运行");

while (!stoppingToken.IsCancellationRequested)
{
Expand All @@ -66,6 +66,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
_logger.LogInformation("全部规则解析完毕,等待 15 分钟后再次执行");
await Task.Delay(TimeSpan.FromMinutes(15), stoppingToken);
}

_logger.LogInformation("[检查新资源] 结束运行");
}

private async Task DownloadRule(AutoDownloadRule rule)
Expand Down Expand Up @@ -98,6 +100,12 @@ private async Task DownloadRule(AutoDownloadRule rule)
//解析磁力链至种子文件
var torrentBytes = await _torrentService.DownloadTorrent(magnet);

if (!_torrentService.IsTorrentFileValid(torrentBytes))
{
_logger.LogWarning($"解析磁力链得到的种子文件无效 {magnet}");
continue;
}

if (torrentBytes == null || torrentBytes.Length <= 0)
{
_logger.LogWarning($"解析磁力链失败 {magnet}");
Expand All @@ -112,7 +120,7 @@ private async Task DownloadRule(AutoDownloadRule rule)
/// <summary>
/// 根据规则过滤资源列表
/// </summary>
public ResourceInfo[] FilterResources(IEnumerable<ResourceInfo> resources, bool chooseNewerIfDuplicate,
private ResourceInfo[] FilterResources(IEnumerable<ResourceInfo> resources, bool chooseNewerIfDuplicate,
int limitFileSize, bool logDetails = false)
{
if (resources == null) return new ResourceInfo[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ public SyncRulesBackgroundService(IDandanplayApi dandanplayApi,

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("规则同步 开始运行.");
_logger.LogInformation("[规则同步] 开始运行.");

while (!stoppingToken.IsCancellationRequested)
{
_rulesContainer.IsUpdating = true;
bool success = await DoWork();
_rulesContainer.IsUpdating = false;
//每十分钟运行一次
_logger.LogInformation($"规则同步 在后台运行完成,等待 10 分钟后重新执行。此次运行结果为:{success}");
_logger.LogInformation($"[规则同步] 在后台运行完成,等待 10 分钟后重新执行。此次运行结果为:{success}");
await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken);
}

_logger.LogInformation("因触发取消,规则同步 终止运行.");
_logger.LogInformation("因触发取消,[规则同步] 终止运行.");
}

private async Task<bool> DoWork()
{
//增加计数器
var count = Interlocked.Increment(ref _executionCount);
_logger.LogInformation($"规则同步 在后台第 {count} 次运行");
_logger.LogInformation($"[规则同步] 在后台第 {count} 次运行");

//之前登录过,先尝试直接刷新 jwt token
if (_lastLoginResponse != null && _lastLoginResponse.tokenExpireTime >= DateTime.UtcNow)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OctoTorrent;
using OctoTorrent.Common;
using ResourceMonitor.Services.Declaration;

namespace ResourceMonitor.Services.Implementation
{
public class TorrentService : ITorrentService
{
private readonly IMagnetApi _magnetApi;
private readonly ILogger<TorrentService> _logger;

public TorrentService(IMagnetApi magnetApi, ILogger<TorrentService> logger)
{
_magnetApi = magnetApi;
_logger = logger;
}

public bool IsTorrentFileValid(byte[] torrentBytes)
{
return Torrent.TryLoad(torrentBytes, out var _);
}

public string NormalizeMagnetUrl(string magnet)
{
return "magnet:?xt=urn:btih:" + GetHash(magnet);
}

public string GetHash(string magnet)
{
return InfoHash.FromMagnetLink(magnet).ToHex().ToLowerInvariant();
}

public async Task<byte[]> DownloadTorrent(string magnet)
{
try
{
var content = await _magnetApi.ParseMagnet(magnet);
var torrent = await content.ReadAsByteArrayAsync();
return torrent;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "下载种子文件时出错");
return null;
}
}
}
}
12 changes: 10 additions & 2 deletions ResourceMonitor/ResourceMonitor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ public void ConfigureServices(IServiceCollection services)
c.BaseAddress = new Uri(Configuration["Api:ResBaseUrl"]);
});

services.AddRefitClient<IMagnetApi>()
.ConfigureHttpClient(c =>
{
c.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
c.BaseAddress = new Uri(Configuration["Api:MagnetBaseUrl"]);
});

services.AddSingleton<IRulesContainer, RulesContainer>();

services.AddControllers();
services.AddTransient<ITorrentService, TorrentService>();

services.AddControllers().AddNewtonsoftJson();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down

0 comments on commit 52f2d00

Please sign in to comment.