From 24b712cd157a0865c1b7e59682a400501da72995 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 26 Oct 2021 17:27:59 -0400 Subject: [PATCH] Fix tests --- ProjectLighthouse.Tests/LighthouseTest.cs | 6 +++-- .../Tests/AuthenticationTests.cs | 2 +- ProjectLighthouse.Tests/Tests/MatchTests.cs | 26 +++++++++++++++++-- .../Controllers/MatchController.cs | 4 +-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs index d9027f7f..176989dc 100644 --- a/ProjectLighthouse.Tests/LighthouseTest.cs +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -23,10 +23,12 @@ namespace LBPUnion.ProjectLighthouse.Tests { public async Task AuthenticateResponse(int number = 0) { const char nullChar = (char)0x00; - const char sepChar = (char)0x20; const string username = "unitTestUser"; - string stringContent = $"{nullChar}{sepChar}{username}{number}{nullChar}"; + string nullString = ""; + for(int i = 0; i < 80; i++) nullString += nullChar; + + string stringContent = $"{nullString}{username}{number}{nullChar}"; HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new StringContent(stringContent)); return response; diff --git a/ProjectLighthouse.Tests/Tests/AuthenticationTests.cs b/ProjectLighthouse.Tests/Tests/AuthenticationTests.cs index 5ac770ff..ea51884b 100644 --- a/ProjectLighthouse.Tests/Tests/AuthenticationTests.cs +++ b/ProjectLighthouse.Tests/Tests/AuthenticationTests.cs @@ -47,7 +47,7 @@ namespace LBPUnion.ProjectLighthouse.Tests { string responseContent = await response.Content.ReadAsStringAsync(); Assert.True(response.IsSuccessStatusCode); - Assert.Contains("You are logged in", responseContent); + Assert.Contains("You are now logged in", responseContent); } [DatabaseFact] diff --git a/ProjectLighthouse.Tests/Tests/MatchTests.cs b/ProjectLighthouse.Tests/Tests/MatchTests.cs index e6ba1207..138501cf 100644 --- a/ProjectLighthouse.Tests/Tests/MatchTests.cs +++ b/ProjectLighthouse.Tests/Tests/MatchTests.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Types; @@ -10,11 +11,27 @@ namespace LBPUnion.ProjectLighthouse.Tests { private static readonly SemaphoreSlim semaphore = new(1, 1); [DatabaseFact] - public async Task ShouldReturnOk() { + public async Task ShouldRejectEmptyData() { LoginResult loginResult = await this.Authenticate(); await semaphore.WaitAsync(); HttpResponseMessage result = await this.AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty(), loginResult.AuthTicket); + Assert.False(result.IsSuccessStatusCode); + + semaphore.Release(); + } + + [DatabaseFact] + public async Task ShouldReturnOk() { + LoginResult loginResult = await this.Authenticate(); + await semaphore.WaitAsync(); + + HttpResponseMessage result = await this.AuthenticatedUploadDataRequest( + "LITTLEBIGPLANETPS3_XML/match", + Encoding.ASCII.GetBytes("[UpdateMyPlayerData,[\"Player\":\"1984\"]]"), + loginResult.AuthTicket + ); + Assert.True(result.IsSuccessStatusCode); semaphore.Release(); @@ -29,7 +46,12 @@ namespace LBPUnion.ProjectLighthouse.Tests { int oldPlayerCount = await this.GetPlayerCount(); - HttpResponseMessage result = await this.AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty(), loginResult.AuthTicket); + HttpResponseMessage result = await this.AuthenticatedUploadDataRequest( + "LITTLEBIGPLANETPS3_XML/match", + Encoding.ASCII.GetBytes("[UpdateMyPlayerData,[\"Player\":\"1984\"]]"), + loginResult.AuthTicket + ); + Assert.True(result.IsSuccessStatusCode); int playerCount = await this.GetPlayerCount(); diff --git a/ProjectLighthouse/Controllers/MatchController.cs b/ProjectLighthouse/Controllers/MatchController.cs index 6d34b60e..61d63013 100644 --- a/ProjectLighthouse/Controllers/MatchController.cs +++ b/ProjectLighthouse/Controllers/MatchController.cs @@ -32,12 +32,12 @@ namespace LBPUnion.ProjectLighthouse.Controllers { #region Parse match data // Example POST /match: [UpdateMyPlayerData,["Player":"FireGamer9872"]] - string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); + string? bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync(); if(bodyString.Contains("FindBestRoom")) { return this.Ok("[{\"StatusCode\":200},{\"Players\":[{\"PlayerId\":\"literally1984\",\"matching_res\":0},{\"PlayerId\":\"jvyden\",\"matching_res\":1}],\"Slots\":[[5,0]],\"RoomState\":\"E_ROOM_IN_POD\",\"HostMood\":\"E_MOOD_EVERYONE\",\"LevelCompletionEstimate\":0,\"PassedNoJoinPoint\":0,\"MoveConnected\":false,\"Location\":[\"127.0.0.1\"],\"BuildVersion\":289,\"Language\":1,\"FirstSeenTimestamp\":1427331263756,\"LastSeenTimestamp\":1635112546000,\"GameId\":1,\"NatType\":2,\"Friends\":[],\"Blocked\":[],\"RecentlyLeft\":[],\"FailedJoin\":[]}]"); } - if(bodyString[0] != '[') return this.BadRequest(); + if(string.IsNullOrEmpty(bodyString) || bodyString[0] != '[') return this.BadRequest(); IMatchData? matchData; try {