mirror of
https://github.com/volodymyrsmirnov/MalwareMultiScan.git
synced 2025-08-23 21:12:22 +00:00
121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using MalwareMultiScan.Api.Data.Configuration;
|
|
using MalwareMultiScan.Api.Data.Models;
|
|
using MalwareMultiScan.Api.Services.Implementations;
|
|
using MalwareMultiScan.Api.Services.Interfaces;
|
|
using Mongo2Go;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Driver.GridFS;
|
|
using Moq;
|
|
using NUnit.Framework;
|
|
|
|
namespace MalwareMultiScan.Tests.Api
|
|
{
|
|
public class ScanResultServiceTest
|
|
{
|
|
private MongoDbRunner _mongoDbRunner;
|
|
private IScanResultService _resultService;
|
|
private Mock<IScanBackendService> _scanBackendService;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_mongoDbRunner = MongoDbRunner.Start(additionalMongodArguments: "--quiet");
|
|
|
|
var connection = new MongoClient(_mongoDbRunner.ConnectionString);
|
|
var database = connection.GetDatabase("Test");
|
|
var gridFsBucket = new GridFSBucket(database);
|
|
|
|
_scanBackendService = new Mock<IScanBackendService>();
|
|
|
|
_scanBackendService
|
|
.SetupGet(x => x.List)
|
|
.Returns(() => new[]
|
|
{
|
|
new ScanBackend {Id = "dummy", Enabled = true},
|
|
new ScanBackend {Id = "clamav", Enabled = true},
|
|
new ScanBackend {Id = "disabled", Enabled = false}
|
|
});
|
|
|
|
_resultService = new ScanResultService(
|
|
database, gridFsBucket, _scanBackendService.Object);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_mongoDbRunner.Dispose();
|
|
}
|
|
|
|
[Test]
|
|
public async Task TestFileStorageStoreObtain()
|
|
{
|
|
var originalData = new byte[] {0xDE, 0xAD, 0xBE, 0xEF};
|
|
|
|
string fileId;
|
|
|
|
await using (var dataStream = new MemoryStream(originalData))
|
|
{
|
|
fileId = await _resultService.StoreFile("test.txt", dataStream);
|
|
}
|
|
|
|
Assert.NotNull(fileId);
|
|
|
|
Assert.IsNull(await _resultService.ObtainFile("wrong-id"));
|
|
Assert.IsNull(await _resultService.ObtainFile(ObjectId.GenerateNewId().ToString()));
|
|
|
|
await using var fileSteam = await _resultService.ObtainFile(fileId);
|
|
|
|
var readData = new[]
|
|
{
|
|
(byte) fileSteam.ReadByte(),
|
|
(byte) fileSteam.ReadByte(),
|
|
(byte) fileSteam.ReadByte(),
|
|
(byte) fileSteam.ReadByte()
|
|
};
|
|
|
|
Assert.That(readData, Is.EquivalentTo(originalData));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TestScanResultCreateGet()
|
|
{
|
|
var result = await _resultService.CreateScanResult();
|
|
|
|
Assert.NotNull(result.Id);
|
|
Assert.That(result.Results, Contains.Key("dummy"));
|
|
|
|
await _resultService.UpdateScanResultForBackend(
|
|
result.Id, "dummy", 100, true, true, new[] {"Test"});
|
|
|
|
result = await _resultService.GetScanResult(result.Id);
|
|
|
|
Assert.NotNull(result.Id);
|
|
Assert.That(result.Results, Contains.Key("dummy"));
|
|
|
|
var dummyResult = result.Results["dummy"];
|
|
|
|
Assert.IsTrue(dummyResult.Completed);
|
|
Assert.IsTrue(dummyResult.Succeeded);
|
|
Assert.AreEqual(dummyResult.Duration, 100);
|
|
Assert.IsNotEmpty(dummyResult.Threats);
|
|
Assert.That(dummyResult.Threats, Is.EquivalentTo(new[] {"Test"}));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TestQueueUrlScan()
|
|
{
|
|
var result = await _resultService.CreateScanResult();
|
|
|
|
await _resultService.QueueUrlScan(
|
|
result, "http://url.com");
|
|
|
|
_scanBackendService.Verify(x => x.QueueUrlScan(
|
|
It.IsAny<ScanResult>(),
|
|
It.IsAny<ScanBackend>(),
|
|
"http://url.com"), Times.Exactly(2));
|
|
}
|
|
}
|
|
} |