2019-12-12 22:52:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-02-24 23:18:29 +00:00
|
|
|
|
using System.IO;
|
2019-12-12 22:52:24 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-03-26 04:25:48 +00:00
|
|
|
|
using Xunit;
|
2019-12-12 22:52:24 +00:00
|
|
|
|
|
2020-03-26 04:25:48 +00:00
|
|
|
|
namespace Wabbajack.Common.Test
|
2019-12-12 22:52:24 +00:00
|
|
|
|
{
|
|
|
|
|
public class UtilsTests
|
|
|
|
|
{
|
2020-02-24 23:18:29 +00:00
|
|
|
|
|
2020-03-26 04:25:48 +00:00
|
|
|
|
[Fact]
|
2019-12-12 22:52:24 +00:00
|
|
|
|
public void IsInPathTests()
|
|
|
|
|
{
|
2020-03-26 04:25:48 +00:00
|
|
|
|
Assert.True("c:\\foo\\bar.exe".IsInPath("c:\\foo"));
|
|
|
|
|
Assert.False("c:\\foo\\bar.exe".IsInPath("c:\\fo"));
|
|
|
|
|
Assert.True("c:\\Foo\\bar.exe".IsInPath("c:\\foo"));
|
|
|
|
|
Assert.True("c:\\foo\\bar.exe".IsInPath("c:\\Foo"));
|
|
|
|
|
Assert.True("c:\\foo\\bar.exe".IsInPath("c:\\fOo"));
|
|
|
|
|
Assert.True("c:\\foo\\bar.exe".IsInPath("c:\\foo\\"));
|
|
|
|
|
Assert.True("c:\\foo\\bar\\".IsInPath("c:\\foo\\"));
|
2019-12-12 22:52:24 +00:00
|
|
|
|
}
|
2020-02-24 23:18:29 +00:00
|
|
|
|
|
|
|
|
|
|
2020-03-26 04:25:48 +00:00
|
|
|
|
[Theory]
|
|
|
|
|
[ClassData(typeof(PatchData))]
|
2020-02-24 23:18:29 +00:00
|
|
|
|
public async Task DiffCreateAndApply(byte[] src, byte[] dest, DiffMethod method)
|
|
|
|
|
{
|
|
|
|
|
await using var ms = new MemoryStream();
|
|
|
|
|
switch (method)
|
|
|
|
|
{
|
|
|
|
|
case DiffMethod.Default:
|
|
|
|
|
await Utils.CreatePatch(src, dest, ms);
|
|
|
|
|
break;
|
|
|
|
|
case DiffMethod.BSDiff:
|
|
|
|
|
BSDiff.Create(src, dest, ms);
|
|
|
|
|
break;
|
|
|
|
|
case DiffMethod.OctoDiff:
|
|
|
|
|
OctoDiff.Create(src, dest, ms);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(method), method, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
|
|
|
|
var patch = ms.ToArray();
|
|
|
|
|
await using var resultStream = new MemoryStream();
|
|
|
|
|
Utils.ApplyPatch(new MemoryStream(src), () => new MemoryStream(patch), resultStream);
|
2020-03-26 04:25:48 +00:00
|
|
|
|
Assert.Equal(dest, resultStream.ToArray());
|
2020-02-24 23:18:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public enum DiffMethod
|
|
|
|
|
{
|
|
|
|
|
Default,
|
|
|
|
|
BSDiff,
|
|
|
|
|
OctoDiff
|
|
|
|
|
}
|
2020-03-26 04:25:48 +00:00
|
|
|
|
public class PatchData : TheoryData<byte[], byte[], DiffMethod>
|
2020-02-24 23:18:29 +00:00
|
|
|
|
{
|
2020-03-26 04:25:48 +00:00
|
|
|
|
public PatchData()
|
|
|
|
|
{
|
|
|
|
|
var maxSize = 64;
|
|
|
|
|
Enumerable.Range(0, 10).Do(x =>
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
Add(TestUtils.RandomData(maxSize: maxSize), TestUtils.RandomData(maxSize: maxSize),
|
|
|
|
|
(DiffMethod)TestUtils.RandomOne(DiffMethod.Default, DiffMethod.OctoDiff, DiffMethod.BSDiff));
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-02-24 23:18:29 +00:00
|
|
|
|
}
|
2019-12-12 22:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|