Skip to content

Commit

Permalink
Add 100% test coverage for PublishEndpoints (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden authored Oct 19, 2023
2 parents 3949f2e + 27e75ec commit c0d0854
Show file tree
Hide file tree
Showing 3 changed files with 388 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Refresh.GameServer/Endpoints/Game/Levels/PublishEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Refresh.GameServer.Services;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.UserData;
using Refresh.GameServer.Verification;

namespace Refresh.GameServer.Endpoints.Game.Levels;

Expand All @@ -37,7 +38,7 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, Logger log
return false;
}

if (body.MaxPlayers > 4 || body.MinPlayers > 4)
if (body.MaxPlayers is > 4 or < 0 || body.MinPlayers is > 4 or < 0)
{
return false;
}
Expand All @@ -55,11 +56,12 @@ private static bool VerifyLevel(GameLevelRequest body, GameUser user, Logger log
List<string> hashes = new();
hashes.AddRange(body.XmlResources);
hashes.Add(body.RootResource);
hashes.Add(body.IconHash);

//Remove all invalid or GUID assets
hashes.RemoveAll(r => r == "0" || r.StartsWith('g') || string.IsNullOrWhiteSpace(r));

if (hashes.Any(hash => hash.Length != 40)) return null;
//Verify all hashes are valid SHA1 hashes
if (hashes.Any(hash => !CommonPatterns.Sha1Regex().IsMatch(hash))) return null;

//Mark the user as publishing
command.StartPublishing(user.UserId);
Expand All @@ -84,7 +86,9 @@ public Response PublishLevel(RequestContext context, GameUser user, Token token,

string rootResourcePath = context.IsPSP() ? $"psp/{level.RootResource}" : level.RootResource;

if (level.RootResource.Length != 40) return BadRequest;
//Check if the root resource is a SHA1 hash
if (!CommonPatterns.Sha1Regex().IsMatch(level.RootResource)) return BadRequest;
//Make sure the root resource exists in the data store
if (!dataStore.ExistsInStore(rootResourcePath)) return NotFound;

if (level.LevelId != default) // Republish requests contain the id of the old level
Expand Down
2 changes: 2 additions & 0 deletions RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ protected override void SetupServices()
this._server.AddService<TimeProviderService>(this.DateTimeProvider);
this._server.AddService<CategoryService>();
this._server.AddService<MatchService>();
this._server.AddService<CommandService>();
this._server.AddService<ImportService>();
}
}
Loading

0 comments on commit c0d0854

Please sign in to comment.