|
| 1 | +using System; |
| 2 | +using NFluent; |
| 3 | +using ReClassNET.MemoryScanner; |
| 4 | +using ReClassNET.MemoryScanner.Comparer; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace ReClass.NET_Tests.MemoryScanner.Comparer |
| 8 | +{ |
| 9 | + public class IntegerMemoryComparerTest |
| 10 | + { |
| 11 | + [Theory] |
| 12 | + [InlineData(ScanCompareType.Equal, 0, 0)] |
| 13 | + [InlineData(ScanCompareType.Equal, 1, 2)] |
| 14 | + [InlineData(ScanCompareType.Equal, 2, 1)] |
| 15 | + [InlineData(ScanCompareType.Between, 2, 4)] |
| 16 | + [InlineData(ScanCompareType.BetweenOrEqual, 4, 2)] |
| 17 | + [InlineData(ScanCompareType.NotEqual, 0, 0)] |
| 18 | + public void TestConstructor(ScanCompareType compareType, int value1, int value2) |
| 19 | + { |
| 20 | + var sut = new IntegerMemoryComparer(compareType, value1, value2); |
| 21 | + |
| 22 | + Check.That(sut.CompareType).IsEqualTo(compareType); |
| 23 | + Check.That(sut.ValueSize).IsEqualTo(sizeof(int)); |
| 24 | + Check.That(sut.Value1).IsOneOf(value1, value2); |
| 25 | + Check.That(sut.Value2).IsOneOf(value1, value2); |
| 26 | + if (compareType == ScanCompareType.Between || compareType == ScanCompareType.BetweenOrEqual) |
| 27 | + { |
| 28 | + Check.That(sut.Value1 <= sut.Value2).IsTrue(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static TheoryData<ScanCompareType, int, int, byte[], bool, ScanResult> GetTestCompareBasicData() => new TheoryData<ScanCompareType, int, int, byte[], bool, ScanResult> |
| 33 | + { |
| 34 | + { ScanCompareType.Equal, 0, 0, BitConverter.GetBytes(0), true, new IntegerScanResult(0) }, |
| 35 | + { ScanCompareType.Equal, 0, 0, BitConverter.GetBytes(1), false, null }, |
| 36 | + { ScanCompareType.Equal, 1, 0, BitConverter.GetBytes(1), true, new IntegerScanResult(1) }, |
| 37 | + { ScanCompareType.Equal, 1, 0, BitConverter.GetBytes(0), false, null }, |
| 38 | + { ScanCompareType.NotEqual, 1, 0, BitConverter.GetBytes(0), true, new IntegerScanResult(0) }, |
| 39 | + { ScanCompareType.NotEqual, 1, 0, BitConverter.GetBytes(1), false, null }, |
| 40 | + { ScanCompareType.GreaterThan, 1, 0, BitConverter.GetBytes(0), false, null }, |
| 41 | + { ScanCompareType.GreaterThan, 1, 0, BitConverter.GetBytes(2), true, new IntegerScanResult(2) }, |
| 42 | + { ScanCompareType.GreaterThanOrEqual, 1, 0, BitConverter.GetBytes(0), false, null }, |
| 43 | + { ScanCompareType.GreaterThanOrEqual, 1, 0, BitConverter.GetBytes(1), true, new IntegerScanResult(1) }, |
| 44 | + { ScanCompareType.GreaterThanOrEqual, 1, 0, BitConverter.GetBytes(2), true, new IntegerScanResult(2) }, |
| 45 | + { ScanCompareType.LessThan, 1, 0, BitConverter.GetBytes(1), false, null }, |
| 46 | + { ScanCompareType.LessThan, 1, 0, BitConverter.GetBytes(0), true, new IntegerScanResult(0) }, |
| 47 | + { ScanCompareType.LessThanOrEqual, 1, 0, BitConverter.GetBytes(2), false, null }, |
| 48 | + { ScanCompareType.LessThanOrEqual, 1, 0, BitConverter.GetBytes(1), true, new IntegerScanResult(1) }, |
| 49 | + { ScanCompareType.LessThanOrEqual, 1, 0, BitConverter.GetBytes(0), true, new IntegerScanResult(0) }, |
| 50 | + { ScanCompareType.Between, 1, 2, BitConverter.GetBytes(0), false, null }, |
| 51 | + { ScanCompareType.Between, 1, 2, BitConverter.GetBytes(1), false, null }, |
| 52 | + { ScanCompareType.Between, 1, 2, BitConverter.GetBytes(2), false, null }, |
| 53 | + { ScanCompareType.Between, 1, 2, BitConverter.GetBytes(3), false, null }, |
| 54 | + { ScanCompareType.BetweenOrEqual, 1, 2, BitConverter.GetBytes(0), false, null }, |
| 55 | + { ScanCompareType.BetweenOrEqual, 1, 2, BitConverter.GetBytes(1), true, new IntegerScanResult(1) }, |
| 56 | + { ScanCompareType.BetweenOrEqual, 1, 2, BitConverter.GetBytes(2), true, new IntegerScanResult(2) }, |
| 57 | + { ScanCompareType.BetweenOrEqual, 1, 2, BitConverter.GetBytes(3), false, null } |
| 58 | + }; |
| 59 | + |
| 60 | + public static TheoryData<ScanCompareType, int, int, byte[], bool, ScanResult> GetTestCompareScanCompareTypeUnknownData() => new TheoryData<ScanCompareType, int, int, byte[], bool, ScanResult> |
| 61 | + { |
| 62 | + { ScanCompareType.Unknown, 0, 0, BitConverter.GetBytes(0), true, new IntegerScanResult(0) }, |
| 63 | + { ScanCompareType.Unknown, 0, 0, BitConverter.GetBytes(1), true, new IntegerScanResult(1) } |
| 64 | + }; |
| 65 | + |
| 66 | + [Theory] |
| 67 | + [MemberData(nameof(GetTestCompareBasicData))] |
| 68 | + [MemberData(nameof(GetTestCompareScanCompareTypeUnknownData))] |
| 69 | + public void TestCompare(ScanCompareType compareType, int value1, int value2, byte[] data, bool expectedResult, ScanResult expectedScanResult) |
| 70 | + { |
| 71 | + var sut = new IntegerMemoryComparer(compareType, value1, value2); |
| 72 | + |
| 73 | + Check.That(sut.Compare(data, 0, out var scanResult)).IsEqualTo(expectedResult); |
| 74 | + Check.That(scanResult).IsEqualTo(expectedScanResult); |
| 75 | + if (scanResult != null) |
| 76 | + { |
| 77 | + Check.That(scanResult).IsInstanceOf<IntegerScanResult>(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [Theory] |
| 82 | + [InlineData(ScanCompareType.Changed)] |
| 83 | + [InlineData(ScanCompareType.NotChanged)] |
| 84 | + [InlineData(ScanCompareType.Decreased)] |
| 85 | + [InlineData(ScanCompareType.DecreasedOrEqual)] |
| 86 | + [InlineData(ScanCompareType.Increased)] |
| 87 | + [InlineData(ScanCompareType.IncreasedOrEqual)] |
| 88 | + public void TestCompareInvalidCompareTypeThrows(ScanCompareType compareType) |
| 89 | + { |
| 90 | + var sut = new IntegerMemoryComparer(compareType, 0, 0); |
| 91 | + |
| 92 | + Check.ThatCode(() => sut.Compare(BitConverter.GetBytes(0), 0, out _)).Throws<InvalidCompareTypeException>(); |
| 93 | + } |
| 94 | + |
| 95 | + public static TheoryData<byte[], int, Type> GetTestCompareThrowsData() => new TheoryData<byte[], int, Type> |
| 96 | + { |
| 97 | + { null, 0, typeof(ArgumentNullException) }, |
| 98 | + { new byte[0], 0, typeof(ArgumentOutOfRangeException) }, |
| 99 | + { new byte[4], 4, typeof(ArgumentOutOfRangeException) }, |
| 100 | + { new byte[3], 0, typeof(ArgumentException) }, |
| 101 | + { new byte[4], 1, typeof(ArgumentException) } |
| 102 | + }; |
| 103 | + |
| 104 | + [Theory] |
| 105 | + [MemberData(nameof(GetTestCompareThrowsData))] |
| 106 | + public void TestCompareInvalidDataThrows(byte[] data, int index, Type expectedExceptionType) |
| 107 | + { |
| 108 | + var sut = new IntegerMemoryComparer(ScanCompareType.Equal, 0, 0); |
| 109 | + |
| 110 | + Check.ThatCode(() => sut.Compare(data, index, out _)).ThrowsType(expectedExceptionType); |
| 111 | + } |
| 112 | + |
| 113 | + public static TheoryData<ScanCompareType, int, int, byte[], ScanResult, bool, ScanResult> GetTestCompareWithPreviousData() |
| 114 | + { |
| 115 | + var data = new TheoryData<ScanCompareType, int, int, byte[], ScanResult, bool, ScanResult> |
| 116 | + { |
| 117 | + { ScanCompareType.Changed, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), true, new IntegerScanResult(0) }, |
| 118 | + { ScanCompareType.Changed, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), false, null }, |
| 119 | + { ScanCompareType.NotChanged, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), true, new IntegerScanResult(1) }, |
| 120 | + { ScanCompareType.NotChanged, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), false, null }, |
| 121 | + { ScanCompareType.Increased, 0, 0, BitConverter.GetBytes(2), new IntegerScanResult(1), true, new IntegerScanResult(2) }, |
| 122 | + { ScanCompareType.Increased, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), false, null }, |
| 123 | + { ScanCompareType.Increased, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), false, null }, |
| 124 | + { ScanCompareType.IncreasedOrEqual, 0, 0, BitConverter.GetBytes(2), new IntegerScanResult(1), true, new IntegerScanResult(2) }, |
| 125 | + { ScanCompareType.IncreasedOrEqual, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), true, new IntegerScanResult(1) }, |
| 126 | + { ScanCompareType.IncreasedOrEqual, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), false, null }, |
| 127 | + { ScanCompareType.Decreased, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), true, new IntegerScanResult(0) }, |
| 128 | + { ScanCompareType.Decreased, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), false, null }, |
| 129 | + { ScanCompareType.Decreased, 0, 0, BitConverter.GetBytes(2), new IntegerScanResult(1), false, null }, |
| 130 | + { ScanCompareType.DecreasedOrEqual, 0, 0, BitConverter.GetBytes(0), new IntegerScanResult(1), true, new IntegerScanResult(0) }, |
| 131 | + { ScanCompareType.DecreasedOrEqual, 0, 0, BitConverter.GetBytes(1), new IntegerScanResult(1), true, new IntegerScanResult(1) }, |
| 132 | + { ScanCompareType.DecreasedOrEqual, 0, 0, BitConverter.GetBytes(2), new IntegerScanResult(1), false, null } |
| 133 | + }; |
| 134 | + |
| 135 | + var basicData = GetTestCompareBasicData(); |
| 136 | + foreach (var x in basicData) |
| 137 | + { |
| 138 | + data.Add((ScanCompareType)x[0], (int)x[1], (int)x[2], (byte[])x[3], new IntegerScanResult(1), (bool)x[4], (ScanResult)x[5]); |
| 139 | + } |
| 140 | + |
| 141 | + return data; |
| 142 | + } |
| 143 | + |
| 144 | + [Theory] |
| 145 | + [MemberData(nameof(GetTestCompareWithPreviousData))] |
| 146 | + public void TestCompareWithPrevious(ScanCompareType compareType, int value1, int value2, byte[] data, ScanResult previousScanResult, bool expectedResult, ScanResult expectedScanResult) |
| 147 | + { |
| 148 | + var sut = new IntegerMemoryComparer(compareType, value1, value2); |
| 149 | + |
| 150 | + Check.That(sut.Compare(data, 0, previousScanResult, out var scanResult)).IsEqualTo(expectedResult); |
| 151 | + Check.That(scanResult).IsEqualTo(expectedScanResult); |
| 152 | + if (scanResult != null) |
| 153 | + { |
| 154 | + Check.That(scanResult).IsInstanceOf<IntegerScanResult>(); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public void TestCompareWithPreviousThrows() |
| 160 | + { |
| 161 | + var sut = new IntegerMemoryComparer(ScanCompareType.Unknown, 0, 0); |
| 162 | + |
| 163 | + Check.ThatCode(() => sut.Compare(BitConverter.GetBytes(0), 0, new IntegerScanResult(0), out _)).Throws<InvalidCompareTypeException>(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments