Add SkipperMatch tests, fix broken Skipper stuff

This commit is contained in:
Matt Nadareski
2020-12-18 17:07:58 -08:00
parent f7a19c53bf
commit dc6bcc1b17
6 changed files with 400 additions and 33 deletions

View File

@@ -129,9 +129,10 @@ namespace SabreTools.Skippers
SkipperRule rule = ParseRule(xtr);
if (rule != null)
Rules.Add(rule);
xtr.Skip();
xtr.Read();
break;
default:
xtr.Read();
break;
@@ -168,27 +169,27 @@ namespace SabreTools.Skippers
SourceFile = this.SourceFile,
};
if (xtr.GetAttribute("start_offset") != null)
string startOffset = xtr.GetAttribute("start_offset");
if (startOffset != null)
{
string offset = xtr.GetAttribute("start_offset");
if (offset.ToLowerInvariant() == "eof")
if (startOffset.ToLowerInvariant() == "eof")
rule.StartOffset = null;
else
rule.StartOffset = Convert.ToInt64(offset, 16);
rule.StartOffset = Convert.ToInt64(startOffset, 16);
}
if (xtr.GetAttribute("end_offset") != null)
string endOffset = xtr.GetAttribute("end_offset");
if (endOffset != null)
{
string offset = xtr.GetAttribute("end_offset");
if (offset.ToLowerInvariant() == "eof")
if (endOffset.ToLowerInvariant() == "eof")
rule.EndOffset = null;
else
rule.EndOffset = Convert.ToInt64(offset, 16);
rule.EndOffset = Convert.ToInt64(endOffset, 16);
}
if (xtr.GetAttribute("operation") != null)
string operation = xtr.GetAttribute("operation");
if (operation != null)
{
string operation = xtr.GetAttribute("operation");
switch (operation.ToLowerInvariant())
{
case "bitswap":
@@ -205,19 +206,32 @@ namespace SabreTools.Skippers
// Now read the individual tests into the Rule
XmlReader subreader = xtr.ReadSubtree();
if (subreader != null)
{
subreader.MoveToContent();
while (!subreader.EOF)
{
if (subreader.NodeType != XmlNodeType.Element)
subreader.Read();
SkipperTest test = ParseTest(xtr);
switch (xtr.Name.ToLowerInvariant())
{
case "data":
case "or":
case "xor":
case "and":
case "file":
SkipperTest test = ParseTest(subreader);
if (test != null)
rule.Tests.Add(test);
// Add the created test to the rule
rule.Tests.Add(test);
subreader.Read();
subreader.Read();
break;
default:
subreader.Read();
break;
}
}
}
@@ -275,8 +289,7 @@ namespace SabreTools.Skippers
break;
default:
xtr.Read();
break;
return null;
}
// Now populate all the parts that we can

View File

@@ -226,7 +226,7 @@ namespace SabreTools.Skippers
Name = "Atari 7800",
Author = "Roman Scherzer",
Version = "1.0",
SourceFile = "a7800.xml",
SourceFile = "a7800",
Rules = new List<SkipperRule>
{
rule1,
@@ -289,7 +289,7 @@ namespace SabreTools.Skippers
Name = "Atari Lynx",
Author = "Roman Scherzer",
Version = "1.0",
SourceFile = "lynx.xml",
SourceFile = "lynx",
Rules = new List<SkipperRule>
{
rule1,
@@ -409,7 +409,7 @@ namespace SabreTools.Skippers
Name = "psid",
Author = "Yori Yoshizuki",
Version = "1.2",
SourceFile = "psid.xml",
SourceFile = "psid",
Rules = new List<SkipperRule>
{
rule1,
@@ -456,7 +456,7 @@ namespace SabreTools.Skippers
Name = "NEC TurboGrafx-16/PC-Engine",
Author = "Matt Nadareski (darksabre76)",
Version = "1.0",
SourceFile = "pce.xml",
SourceFile = "pce",
Rules = new List<SkipperRule>
{
rule1,
@@ -537,7 +537,7 @@ namespace SabreTools.Skippers
Name = "Nintendo 64 - ABCD",
Author = "CUE",
Version = "1.1",
SourceFile = "n64.xml",
SourceFile = "n64",
Rules = new List<SkipperRule>
{
rule1, // V64
@@ -582,7 +582,7 @@ namespace SabreTools.Skippers
Name = "Nintendo Famicon/NES",
Author = "Roman Scherzer",
Version = "1.1",
SourceFile = "nes.xml",
SourceFile = "nes",
Rules = new List<SkipperRule>
{
rule1,
@@ -682,7 +682,7 @@ namespace SabreTools.Skippers
Name = "fds",
Author = "Yori Yoshizuki",
Version = "1.0",
SourceFile = "fds.xml",
SourceFile = "fds",
Rules = new List<SkipperRule>
{
rule1,
@@ -766,7 +766,7 @@ namespace SabreTools.Skippers
Name = "Nintendo Super Famicom/SNES",
Author = "Matt Nadareski (darksabre76)",
Version = "1.0",
SourceFile = "snes.xml",
SourceFile = "snes",
Rules = new List<SkipperRule>
{
rule1, // FIG
@@ -811,7 +811,7 @@ namespace SabreTools.Skippers
Name = "Nintendo Super Famicon SPC",
Author = "Yori Yoshizuki",
Version = "1.0",
SourceFile = "spc.xml",
SourceFile = "spc",
Rules = new List<SkipperRule>
{
rule1,

View File

@@ -178,13 +178,10 @@ namespace SabreTools.Skippers
case HeaderSkipOperation.Byteswap:
if (pos % 2 == 1)
{
buffer[pos - 1] = b;
}
if (pos % 2 == 0)
{
else
buffer[pos + 1] = b;
}
break;
case HeaderSkipOperation.Wordswap:

View File

@@ -7,6 +7,7 @@
<ItemGroup>
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
<ProjectReference Include="..\SabreTools.Skippers\SabreTools.Skippers.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,178 @@
using System.IO;
using SabreTools.Skippers;
using Xunit;
namespace SabreTools.Test.Skippers
{
[Collection("SkipperMatch")]
public class SkipperMatchHeaderTests
{
public SkipperMatchHeaderTests()
{
// Initialize the skippers
SkipperMatch.Init(false);
}
[Theory]
[InlineData(0x01, new byte[] { 0x41, 0x54, 0x41, 0x52, 0x49, 0x37, 0x38, 0x30, 0x30})]
[InlineData(0x64, new byte[] { 0x41, 0x43, 0x54, 0x55, 0x41, 0x4C, 0x20, 0x43, 0x41, 0x52, 0x54, 0x20, 0x44, 0x41, 0x54, 0x41, 0x20, 0x53, 0x54, 0x41, 0x52, 0x54, 0x53, 0x20, 0x48, 0x45, 0x52, 0x45 })]
public void A7800Test(int offset, byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < offset; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("a7800", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
public void FDSTest(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("fds", rule.SourceFile);
}
[Theory]
[InlineData(0x00, new byte[] { 0x4C, 0x59, 0x4E, 0x58 })]
[InlineData(0x06, new byte[] { 0x42, 0x53, 0x39 })]
public void LynxTest(int offset, byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < offset; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("lynx", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x80, 0x37, 0x12, 0x40 })]
[InlineData(new byte[] { 0x37, 0x80, 0x40, 0x12 })]
[InlineData(new byte[] { 0x40, 0x12, 0x37, 0x80 })]
public void N64Test(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("n64", rule.SourceFile);
}
[Fact]
public void NESTest()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x4E, 0x45, 0x53, 0x1A });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("nes", rule.SourceFile);
}
[Fact]
public void PCETest()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0x02 });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("pce", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x76 })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x03, 0x00, 0x7c })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x7c })]
[InlineData(new byte[] { 0x52, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c })]
public void PSIDTest(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("psid", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0xAA, 0xBB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x53, 0x55, 0x50, 0x45, 0x52, 0x55, 0x46, 0x4F })]
public void SNESTest(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < 0x16; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("snes", rule.SourceFile);
}
[Fact]
public void SPCTest()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x53, 0x4E, 0x45, 0x53, 0x2D, 0x53, 0x50, 0x43 });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("spc", rule.SourceFile);
}
}
}

View File

@@ -0,0 +1,178 @@
using System.IO;
using SabreTools.Skippers;
using Xunit;
namespace SabreTools.Test.Skippers
{
[Collection("SkipperMatch")]
public class SkipperMatchInternalTests
{
public SkipperMatchInternalTests()
{
// Initialize the skippers
SkipperMatch.Init(true);
}
[Theory]
[InlineData(0x01, new byte[] { 0x41, 0x54, 0x41, 0x52, 0x49, 0x37, 0x38, 0x30, 0x30})]
[InlineData(0x64, new byte[] { 0x41, 0x43, 0x54, 0x55, 0x41, 0x4C, 0x20, 0x43, 0x41, 0x52, 0x54, 0x20, 0x44, 0x41, 0x54, 0x41, 0x20, 0x53, 0x54, 0x41, 0x52, 0x54, 0x53, 0x20, 0x48, 0x45, 0x52, 0x45 })]
public void A7800TestInternal(int offset, byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < offset; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("a7800", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x46, 0x44, 0x53, 0x1A, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
public void FDSTestInternal(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("fds", rule.SourceFile);
}
[Theory]
[InlineData(0x00, new byte[] { 0x4C, 0x59, 0x4E, 0x58 })]
[InlineData(0x06, new byte[] { 0x42, 0x53, 0x39 })]
public void LynxTestInternal(int offset, byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < offset; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("lynx", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x80, 0x37, 0x12, 0x40 })]
[InlineData(new byte[] { 0x37, 0x80, 0x40, 0x12 })]
[InlineData(new byte[] { 0x40, 0x12, 0x37, 0x80 })]
public void N64TestInternal(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("n64", rule.SourceFile);
}
[Fact]
public void NESTestInternal()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x4E, 0x45, 0x53, 0x1A });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("nes", rule.SourceFile);
}
[Fact]
public void PCETestInternal()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xBB, 0x02 });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("pce", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x76 })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x03, 0x00, 0x7c })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c })]
[InlineData(new byte[] { 0x50, 0x53, 0x49, 0x44, 0x00, 0x01, 0x00, 0x7c })]
[InlineData(new byte[] { 0x52, 0x53, 0x49, 0x44, 0x00, 0x02, 0x00, 0x7c })]
public void PSIDTestInternal(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("psid", rule.SourceFile);
}
[Theory]
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0xAA, 0xBB, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 })]
[InlineData(new byte[] { 0x53, 0x55, 0x50, 0x45, 0x52, 0x55, 0x46, 0x4F })]
public void SNESTestInternal(byte[] content)
{
// Create the stream with the required input
var ms = new MemoryStream();
for (int i = 0; i < 0x16; i++)
{
ms.WriteByte(0xFF);
}
ms.Write(content);
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("snes", rule.SourceFile);
}
[Fact]
public void SPCTestInternal()
{
// Create the stream with the required input
var ms = new MemoryStream();
ms.Write(new byte[] { 0x53, 0x4E, 0x45, 0x53, 0x2D, 0x53, 0x50, 0x43 });
ms.Seek(0, SeekOrigin.Begin);
// Check that we get a match
var rule = SkipperMatch.GetMatchingRule(ms, string.Empty);
Assert.NotNull(rule);
Assert.Equal("spc", rule.SourceFile);
}
}
}