The Great Formatting of 2022

This commit is contained in:
jvyden 2022-01-18 23:09:02 -05:00
parent 59cc7f02fb
commit 35f50f5f8c
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
162 changed files with 6609 additions and 6809 deletions

View file

@ -2,28 +2,22 @@ using LBPUnion.ProjectLighthouse.Types.Settings;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests
{
public sealed class DatabaseFactAttribute : FactAttribute
{
private static readonly object migrateLock = new();
namespace LBPUnion.ProjectLighthouse.Tests;
public DatabaseFactAttribute()
{
ServerSettings.Instance = new ServerSettings();
ServerSettings.Instance.DbConnectionString = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse";
if (!ServerStatics.DbConnected)
public sealed class DatabaseFactAttribute : FactAttribute
{
private static readonly object migrateLock = new();
public DatabaseFactAttribute()
{
ServerSettings.Instance = new ServerSettings();
ServerSettings.Instance.DbConnectionString = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse";
if (!ServerStatics.DbConnected) this.Skip = "Database not available";
else
lock(migrateLock)
{
this.Skip = "Database not available";
using Database database = new();
database.Database.Migrate();
}
else
{
lock(migrateLock)
{
using Database database = new();
database.Database.Migrate();
}
}
}
}
}

View file

@ -4,58 +4,57 @@ using System.Text;
using LBPUnion.ProjectLighthouse.Types.Files;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests
namespace LBPUnion.ProjectLighthouse.Tests;
public class FileTypeTests
{
public class FileTypeTests
[Fact]
public void ShouldRecognizeLevel()
{
[Fact]
public void ShouldRecognizeLevel()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestLevel.lvl"));
Assert.True(file.FileType == LbpFileType.Level);
}
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestLevel.lvl"));
Assert.True(file.FileType == LbpFileType.Level);
}
[Fact]
public void ShouldRecognizeScript()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestScript.ff"));
Assert.True(file.FileType == LbpFileType.Script);
}
[Fact]
public void ShouldRecognizeScript()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestScript.ff"));
Assert.True(file.FileType == LbpFileType.Script);
}
[Fact]
public void ShouldRecognizeTexture()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestTexture.tex"));
Assert.True(file.FileType == LbpFileType.Texture);
}
[Fact]
public void ShouldRecognizeTexture()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestTexture.tex"));
Assert.True(file.FileType == LbpFileType.Texture);
}
[Fact]
public void ShouldRecognizeFileArchive()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
Assert.True(file.FileType == LbpFileType.FileArchive);
}
[Fact]
public void ShouldRecognizeFileArchive()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
Assert.True(file.FileType == LbpFileType.FileArchive);
}
[Fact]
public void ShouldNotRecognizeFileArchiveAsScript()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
Assert.False(file.FileType == LbpFileType.Script);
Assert.True(file.FileType == LbpFileType.FileArchive);
}
[Fact]
public void ShouldNotRecognizeFileArchiveAsScript()
{
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
Assert.False(file.FileType == LbpFileType.Script);
Assert.True(file.FileType == LbpFileType.FileArchive);
}
[Fact]
public void ShouldRecognizeNothingAsUnknown()
{
LbpFile file = new(Array.Empty<byte>());
Assert.True(file.FileType == LbpFileType.Unknown);
}
[Fact]
public void ShouldRecognizeNothingAsUnknown()
{
LbpFile file = new(Array.Empty<byte>());
Assert.True(file.FileType == LbpFileType.Unknown);
}
[Fact]
public void ShouldRecognizeGarbageAsUnknown()
{
LbpFile file = new(Encoding.ASCII.GetBytes("free pc only $900"));
Assert.True(file.FileType == LbpFileType.Unknown);
}
[Fact]
public void ShouldRecognizeGarbageAsUnknown()
{
LbpFile file = new(Encoding.ASCII.GetBytes("free pc only $900"));
Assert.True(file.FileType == LbpFileType.Unknown);
}
}

View file

@ -11,89 +11,84 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Tests
namespace LBPUnion.ProjectLighthouse.Tests;
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class LighthouseServerTest
{
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class LighthouseServerTest
public readonly HttpClient Client;
public readonly TestServer Server;
public LighthouseServerTest()
{
public readonly HttpClient Client;
public readonly TestServer Server;
this.Server = new TestServer(new WebHostBuilder().UseStartup<TestStartup>());
this.Client = this.Server.CreateClient();
}
public async Task<HttpResponseMessage> AuthenticateResponse(int number = -1, bool createUser = true)
{
if (number == -1) number = new Random().Next();
public LighthouseServerTest()
const string username = "unitTestUser";
if (createUser)
{
this.Server = new TestServer(new WebHostBuilder().UseStartup<TestStartup>());
this.Client = this.Server.CreateClient();
}
public async Task<HttpResponseMessage> AuthenticateResponse(int number = -1, bool createUser = true)
{
if (number == -1)
{
number = new Random().Next();
}
const string username = "unitTestUser";
if (createUser)
{
await using Database database = new();
if (await database.Users.FirstOrDefaultAsync(u => u.Username == $"{username}{number}") == null)
await database.CreateUser($"{username}{number}", HashHelper.BCryptHash($"unitTestPassword{number}"));
}
string stringContent = $"{LoginData.UsernamePrefix}{username}{number}{(char)0x00}";
HttpResponseMessage response = await this.Client.PostAsync
($"/LITTLEBIGPLANETPS3_XML/login?titleID={GameVersionHelper.LittleBigPlanet2TitleIds[0]}", new StringContent(stringContent));
return response;
await using Database database = new();
if (await database.Users.FirstOrDefaultAsync(u => u.Username == $"{username}{number}") == null)
await database.CreateUser($"{username}{number}", HashHelper.BCryptHash($"unitTestPassword{number}"));
}
public async Task<LoginResult> Authenticate(int number = 0)
{
HttpResponseMessage response = await this.AuthenticateResponse(number);
string stringContent = $"{LoginData.UsernamePrefix}{username}{number}{(char)0x00}";
string responseContent = LbpSerializer.StringElement("loginResult", await response.Content.ReadAsStringAsync());
HttpResponseMessage response = await this.Client.PostAsync
($"/LITTLEBIGPLANETPS3_XML/login?titleID={GameVersionHelper.LittleBigPlanet2TitleIds[0]}", new StringContent(stringContent));
return response;
}
XmlSerializer serializer = new(typeof(LoginResult));
return (LoginResult)serializer.Deserialize(new StringReader(responseContent))!;
}
public async Task<LoginResult> Authenticate(int number = 0)
{
HttpResponseMessage response = await this.AuthenticateResponse(number);
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth) => this.AuthenticatedRequest(endpoint, mmAuth, HttpMethod.Get);
string responseContent = LbpSerializer.StringElement("loginResult", await response.Content.ReadAsStringAsync());
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method)
{
using HttpRequestMessage requestMessage = new(method, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
XmlSerializer serializer = new(typeof(LoginResult));
return (LoginResult)serializer.Deserialize(new StringReader(responseContent))!;
}
return this.Client.SendAsync(requestMessage);
}
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth) => this.AuthenticatedRequest(endpoint, mmAuth, HttpMethod.Get);
public async Task<HttpResponseMessage> UploadFileEndpointRequest(string filePath)
{
byte[] bytes = await File.ReadAllBytesAsync(filePath);
string hash = HashHelper.Sha1Hash(bytes).ToLower();
public Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method)
{
using HttpRequestMessage requestMessage = new(method, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}
return this.Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath)
=> await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));
public async Task<HttpResponseMessage> UploadFileEndpointRequest(string filePath)
{
byte[] bytes = await File.ReadAllBytesAsync(filePath);
string hash = HashHelper.Sha1Hash(bytes).ToLower();
public async Task<HttpResponseMessage> UploadDataRequest(string endpoint, byte[] data)
=> await this.Client.PostAsync(endpoint, new ByteArrayContent(data));
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}
public async Task<HttpResponseMessage> AuthenticatedUploadFileRequest(string endpoint, string filePath, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath));
return await this.Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath)
=> await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));
public async Task<HttpResponseMessage> AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new ByteArrayContent(data);
return await this.Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> UploadDataRequest(string endpoint, byte[] data) => await this.Client.PostAsync(endpoint, new ByteArrayContent(data));
public async Task<HttpResponseMessage> AuthenticatedUploadFileRequest(string endpoint, string filePath, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new StringContent(await File.ReadAllTextAsync(filePath));
return await this.Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> AuthenticatedUploadDataRequest(string endpoint, byte[] data, string mmAuth)
{
using HttpRequestMessage requestMessage = new(HttpMethod.Post, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
requestMessage.Content = new ByteArrayContent(data);
return await this.Client.SendAsync(requestMessage);
}
}

View file

@ -2,42 +2,41 @@ using System.Collections.Generic;
using LBPUnion.ProjectLighthouse.Serialization;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests
namespace LBPUnion.ProjectLighthouse.Tests;
public class SerializerTests
{
public class SerializerTests
[Fact]
public void BlankElementWorks()
{
[Fact]
public void BlankElementWorks()
{
Assert.Equal("<test></test>", LbpSerializer.BlankElement("test"));
}
Assert.Equal("<test></test>", LbpSerializer.BlankElement("test"));
}
[Fact]
public void StringElementWorks()
{
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement("test", "asd"));
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement(new KeyValuePair<string, object>("test", "asd")));
}
[Fact]
public void StringElementWorks()
{
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement("test", "asd"));
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement(new KeyValuePair<string, object>("test", "asd")));
}
[Fact]
public void TaggedStringElementWorks()
{
Assert.Equal("<test foo=\"bar\">asd</test>", LbpSerializer.TaggedStringElement("test", "asd", "foo", "bar"));
Assert.Equal
(
"<test foo=\"bar\">asd</test>",
LbpSerializer.TaggedStringElement(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
);
}
[Fact]
public void TaggedStringElementWorks()
{
Assert.Equal("<test foo=\"bar\">asd</test>", LbpSerializer.TaggedStringElement("test", "asd", "foo", "bar"));
Assert.Equal
(
"<test foo=\"bar\">asd</test>",
LbpSerializer.TaggedStringElement(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
);
}
[Fact]
public void ElementsWorks()
{
Assert.Equal
(
"<test>asd</test><foo>bar</foo>",
LbpSerializer.Elements(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
);
}
[Fact]
public void ElementsWorks()
{
Assert.Equal
(
"<test>asd</test><foo>bar</foo>",
LbpSerializer.Elements(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
);
}
}