This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tests.cs
162 lines (149 loc) · 6.22 KB
/
Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using RCONServerLib.Utils;
using Xunit;
namespace RCONServerLib.Tests
{
public class Tests
{
[Theory]
[InlineData(false)]
[InlineData(true)]
public void RconPacketLengthTest(bool useUTF8)
{
Assert.Throws<LengthMismatchException>(() =>
new RemoteConPacket(new byte[] { 0xFF, 0xFF, 0x00, 0x00 }, useUTF8));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void RconPacketNullTerminatorEndTest(bool useUTF8)
{
Assert.Throws<NullTerminatorMissingException>(() => new RemoteConPacket(new byte[]
{
0x0A, 0x00, 0x00, 0x00, // Size
0x00, 0x00, 0x00, 0x00, // Id
0x00, 0x00, 0x00, 0x00, // Type
0x00, // Payload
0x01 // Null-terminator end
}, useUTF8));
}
[Fact]
public void RconPacketTooLongTest()
{
var bytes = new byte[4096];
Array.Copy(new byte[]
{
0xFC, 0x0F, 0x00, 0x00, // Size - 4092
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
}, 0, bytes, 0, 12);
bytes[4094] = 0x00;
bytes[4095] = 0x00;
for (var i = 13; i < 4094; i++) bytes[i] = 0xFF;
Assert.Throws<NullTerminatorMissingException>(() => new RemoteConPacket(bytes));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void RconPacketTypeTest(bool useUTF8)
{
Assert.Throws<InvalidPacketTypeException>(() => new RemoteConPacket(new byte[]
{
0x08, 0x00, 0x00, 0x00, // Size
0x00, 0x00, 0x00, 0x00, // Id
0xFF, 0xFF, 0x00, 0x00 // Type
}, useUTF8));
}
[Fact]
public void IpPatternMatchTest()
{
Assert.True(IpExtension.Match("127.0.0.1", "127.0.0.1"));
Assert.True(IpExtension.Match("192.*.*.*", "192.168.178.1"));
Assert.True(IpExtension.Match("192.*.*.*", "192.255.255.255"));
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.2"));
Assert.False(IpExtension.Match("127.0.0.1", "127.0.0.9"));
Assert.False(IpExtension.Match("192.*.*.*", "178.75.68.49"));
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void RconPacketSuccessTest(bool useUTF8)
{
var packet = new RemoteConPacket(new byte[]
{
0x0A, 0x00, 0x00, 0x00, // Size
0x02, 0x00, 0x00, 0x00, // Id
0x03, 0x00, 0x00, 0x00, // Type
0x00,
0x00
}, useUTF8);
Assert.Equal(2, packet.Id);
Assert.Equal(10, packet.Size);
Assert.Equal(RemoteConPacket.PacketType.Auth, packet.Type);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void RconPacketGetBytesTest(bool useUTF8)
{
var testBytes = new byte[]
{
0x0A, 0x00, 0x00, 0x00, // Size
0x02, 0x00, 0x00, 0x00, // Id
0x03, 0x00, 0x00, 0x00, // Type
0x00,
0x00
};
var packet = new RemoteConPacket(2, RemoteConPacket.PacketType.Auth, "", useUTF8);
var packetBytes = packet.GetBytes();
for (var index = 0; index < testBytes.Length; index++)
Assert.Equal(testBytes[index], packetBytes[index]);
}
[Fact]
public void ExtendedBinaryReaderTest()
{
var testBytes = new byte[]
{
0x15, 0x00, 0x00, 0x00, // Size
0x02, 0x00, 0x00, 0x00, // Id
0x03, 0x00, 0x00, 0x00, // Type
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x00,
0x00
};
using (var ms = new MemoryStream(testBytes))
{
using (var reader = new BinaryReaderExt(ms))
{
var sizeTestValue = reader.ReadInt32LittleEndian();
Assert.Equal(21, sizeTestValue);
var idTestValue = reader.ReadInt32LittleEndian();
Assert.Equal(2, idTestValue);
var typeTestValue = reader.ReadInt32LittleEndian();
Assert.Equal(3, typeTestValue);
var payloadTestValue = reader.ReadAscii();
Assert.Equal("Hello World", payloadTestValue);
}
}
}
[Fact]
public void DateTimeTest()
{
var nowTime = DateTime.Now;
var unix = nowTime.ToUnixTimestamp();
Assert.True(nowTime.Day == DateTimeExtensions.FromUnixTimestamp(unix).Day, "Day doesn't match");
Assert.True(nowTime.Month == DateTimeExtensions.FromUnixTimestamp(unix).Month, "Month doesn't match");
Assert.True(nowTime.Year == DateTimeExtensions.FromUnixTimestamp(unix).Year, "Year doesn't match");
Assert.True(nowTime.Hour == DateTimeExtensions.FromUnixTimestamp(unix).Hour, "Hour doesn't match");
Assert.True(nowTime.Minute == DateTimeExtensions.FromUnixTimestamp(unix).Minute, "Minute doesn't match");
Assert.True(nowTime.Second == DateTimeExtensions.FromUnixTimestamp(unix).Second, "Second doesn't match");
var unix2 = DateTime.Now.UnixTimestamp();
Assert.True(nowTime.Day == DateTimeExtensions.FromUnixTimestamp(unix2).Day, "Day doesn't match");
Assert.True(nowTime.Month == DateTimeExtensions.FromUnixTimestamp(unix2).Month, "Month doesn't match");
Assert.True(nowTime.Year == DateTimeExtensions.FromUnixTimestamp(unix2).Year, "Year doesn't match");
Assert.True(nowTime.Hour == DateTimeExtensions.FromUnixTimestamp(unix2).Hour, "Hour doesn't match");
Assert.True(nowTime.Minute == DateTimeExtensions.FromUnixTimestamp(unix2).Minute, "Minute doesn't match");
Assert.True(nowTime.Second == DateTimeExtensions.FromUnixTimestamp(unix2).Second, "Second doesn't match");
}
}
}