first commit

This commit is contained in:
2025-07-06 00:23:46 +02:00
commit 38f50c8819
1788 changed files with 112878 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace NitroxModel.Helper;
[TestClass]
public class KeyValueStoreTest
{
[TestMethod]
public void SetAndReadValue()
{
const string TEST_KEY = "test";
KeyValueStore.Instance.SetValue(TEST_KEY, -50);
Assert.AreEqual(-50, KeyValueStore.Instance.GetValue<int>(TEST_KEY));
KeyValueStore.Instance.SetValue(TEST_KEY, 1337);
Assert.AreEqual(1337, KeyValueStore.Instance.GetValue<int>(TEST_KEY));
// Cleanup
KeyValueStore.Instance.DeleteKey(TEST_KEY);
Assert.IsNull(KeyValueStore.Instance.GetValue<int?>(TEST_KEY));
Assert.IsFalse(KeyValueStore.Instance.KeyExists(TEST_KEY));
}
}

View File

@@ -0,0 +1,68 @@
using System.Net;
using System.Net.Sockets;
namespace NitroxModel.Helper;
[TestClass]
public class NetHelperTest
{
[TestMethod]
public void ShouldMatchPrivateIps()
{
// Tested subnet ranges that are reserved for private networks:
// 10.0.0.0/8
// 127.0.0.0/8
// 172.16.0.0/12
// 192.0.0.0/24
// 192.168.0.0/16
// 198.18.0.0/15
IPAddress.Parse("10.0.0.0").IsPrivate().Should().BeTrue();
IPAddress.Parse("10.0.0.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("172.31.255.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.0.0.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.1").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.254").IsPrivate().Should().BeTrue();
IPAddress.Parse("192.168.2.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("198.18.0.1").IsPrivate().Should().BeTrue();
IPAddress.Parse("198.19.255.255").IsPrivate().Should().BeTrue();
IPAddress.Parse("9.255.255.255").IsPrivate().Should().BeFalse();
IPAddress.Parse("91.63.176.12").IsPrivate().Should().BeFalse();
IPAddress.Parse("172.32.0.1").IsPrivate().Should().BeFalse();
IPAddress.Parse("192.0.1.0").IsPrivate().Should().BeFalse();
IPAddress.Parse("198.17.255.255").IsPrivate().Should().BeFalse();
IPAddress.Parse("198.20.0.0").IsPrivate().Should().BeFalse();
}
[TestMethod]
public void ShouldMatchLocalhostIps()
{
IPAddress GetSlightlyDifferentIp(IPAddress address)
{
if (address.AddressFamily != AddressFamily.InterNetwork)
{
throw new Exception("Only supports IPv4");
}
byte[] bytes = address.GetAddressBytes();
unchecked
{
while (bytes[3] is < 1 or > 253)
{
bytes[3]++;
}
bytes[3]++;
}
return new IPAddress(bytes);
}
IPAddress.Parse("127.0.0.1").IsLocalhost().Should().BeTrue();
IPAddress.Parse("127.0.0.2").IsLocalhost().Should().BeTrue();
IPAddress.Parse("192.168.0.255").IsLocalhost().Should().BeFalse();
NetHelper.GetLanIp().IsLocalhost().Should().BeTrue();
IPAddress differentIp = GetSlightlyDifferentIp(NetHelper.GetLanIp());
differentIp.Should().NotBeEquivalentTo(NetHelper.GetLanIp());
differentIp.IsLocalhost().Should().BeFalse();
}
}

View File

@@ -0,0 +1,97 @@
using System;
using System.Diagnostics;
using System.Reflection;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NitroxClient.MonoBehaviours.Gui.Input;
namespace NitroxModel.Helper
{
[TestClass]
public class ReflectTest
{
[TestMethod]
public void Method()
{
// Get static method.
MethodInfo staticMethod = Reflect.Method(() => AbusedClass.StaticMethodReturnsInt());
staticMethod.Should().NotBeNull();
staticMethod.ReturnType.Should().Be<int>();
staticMethod.Name.Should().BeEquivalentTo(nameof(AbusedClass.StaticMethodReturnsInt));
staticMethod.Invoke(null, Array.Empty<object>());
// Extra check for method with parameters, just to be safe.
staticMethod = Reflect.Method(() => AbusedClass.StaticMethodHasParams("", null));
staticMethod.Should().NotBeNull();
staticMethod.ReturnType.Should().Be<string>();
staticMethod.Name.Should().BeEquivalentTo(nameof(AbusedClass.StaticMethodHasParams));
staticMethod.GetParameters().Should().OnlyHaveUniqueItems();
staticMethod.GetParameters()[0].Name.Should().BeEquivalentTo("myValue");
staticMethod.GetParameters()[0].ParameterType.Should().Be<string>();
staticMethod.GetParameters()[1].ParameterType.Should().Be<Process>();
staticMethod.Invoke(null, new[] { "hello, reflection", (object)null }).Should().BeEquivalentTo("hello, reflection");
// Get instance method.
MethodInfo instanceMethod = Reflect.Method((AbusedClass t) => t.Method());
instanceMethod.Should().NotBeNull();
instanceMethod.ReturnType.Should().Be<int>();
instanceMethod.Name.Should().BeEquivalentTo(nameof(AbusedClass.Method));
}
[TestMethod]
public void Field()
{
// Get static field.
FieldInfo staticField = Reflect.Field(() => AbusedClass.StaticField);
staticField.Name.Should().BeEquivalentTo(nameof(AbusedClass.StaticField));
staticField.FieldType.Should().Be<int>();
// Get instance field.
FieldInfo instanceField = Reflect.Field((AbusedClass t) => t.InstanceField);
instanceField.Name.Should().BeEquivalentTo(nameof(AbusedClass.InstanceField));
instanceField.FieldType.Should().Be<int>();
}
[TestMethod]
public void Property()
{
// Get static property.
PropertyInfo staticProperty = Reflect.Property(() => AbusedClass.StaticProperty);
staticProperty.Name.Should().BeEquivalentTo(nameof(AbusedClass.StaticProperty));
staticProperty.PropertyType.Should().Be<int>();
// Get instance property.
PropertyInfo instanceProperty = Reflect.Property((AbusedClass t) => t.InstanceProperty);
instanceProperty.Name.Should().BeEquivalentTo(nameof(AbusedClass.InstanceProperty));
instanceProperty.PropertyType.Should().Be<int>();
}
[TestMethod]
public void Constructor()
{
ConstructorInfo method = Reflect.Constructor(() => new KeyBindingManager());
method.DeclaringType.Should().Be<KeyBindingManager>();
}
private class AbusedClass
{
public static readonly int StaticReadOnlyField = 1;
public static int StaticField = 2;
public int InstanceField = 3;
public static int StaticProperty { get; set; } = 4;
public int InstanceProperty { get; set; } = 5;
public static int StaticMethodReturnsInt()
{
return 2;
}
public static string StaticMethodHasParams(string myValue, Process process)
{
return myValue;
}
public int Method()
{
return 1;
}
}
}
}