using System.Reflection; using System.Text.Json; using Microsoft.AspNetCore.Mvc; using PlotLine.Controllers; using PlotLine.Models; using PlotLine.Services; using SkiaSharp; internal static class MapAppearanceTests { private static void Check(bool value,string message){if(!value)throw new InvalidOperationException(message);} public static byte[] Image(int width=128,int height=96,SKColor? colour=null) { using var bitmap=new SKBitmap(width,height);bitmap.Erase(colour??SKColors.LightGoldenrodYellow);using var image=SKImage.FromBitmap(bitmap);using var bytes=image.Encode(SKEncodedImageFormat.Png,100);return bytes.ToArray(); } public static void ValidationAndImages() { var doc=new MapDocument();Check(MapValidation.Validate(doc) is null,"Old maps default to a Light canvas without theme coupling."); doc.Map.AppearanceJson="{\"preset\":\"custom\",\"backgroundColor\":\"#152b39\",\"landColor\":\"#e9e4c5\"}";Check(MapValidation.Validate(doc) is null,"Custom canvas colours are valid author data."); doc.Map.AppearanceJson="{\"preset\":\"custom\",\"backgroundColor\":\"url(https://example.invalid)\"}";Check(MapValidation.Validate(doc) is not null,"Canvas settings cannot inject URLs/CSS."); doc.Map.AppearanceJson="{}";doc.Map.ReferenceJson="{\"imageID\":\"not-a-guid\"}";Check(MapValidation.Validate(doc) is not null,"References use validated opaque IDs, not paths."); doc.Map.ReferenceJson="{\"opacity\":1.5}";Check(MapValidation.Validate(doc) is not null,"Opacity must be bounded."); var normalized=MapReferenceImageService.Normalize(Image(),"../sketch.png");normalized.ProjectID=7; Check(normalized.OriginalFileName=="sketch.png"&&normalized.ContentType=="image/webp"&&normalized.ImageData!.Length"),"photo.png"),(new byte[10*1024*1024+1],"large.png"),(Image()[..32],"truncated.png")}) {var blocked=false;try{MapReferenceImageService.Normalize(pair.Item1,pair.Item2);}catch(InvalidDataException){blocked=true;}Check(blocked,"Unsafe, oversized and incomplete image uploads must be rejected.");} var resized=MapReferenceImageService.Normalize(Image(5000,1000),"wide.png");Check(resized.Width==4096&&resized.Height==819,"Large references are downsampled once, not repeatedly during navigation."); var tooManyPixels=false;try{MapReferenceImageService.Normalize(Image(4200,4200),"pixels.png");}catch(InvalidDataException){tooManyPixels=true;}Check(tooManyPixels,"Compressed pixel bombs are bounded before decoding."); var backup=new ProjectBackupExport{Project=new(){ProjectID=7},Data=new(){StoryMaps=[new(){MapID=1,ProjectID=7,AppearanceJson="{\"preset\":\"paper\"}"}],MapReferenceImages=[normalized]}}; var copy=JsonSerializer.Deserialize(JsonSerializer.Serialize(backup))!;Check(copy.Data.MapReferenceImages.Single().ImageData!.SequenceEqual(normalized.ImageData!),"Project backup carries exact image bytes, not filesystem paths."); var validate=typeof(ProjectRestoreService).GetMethod("ValidateBackup",BindingFlags.NonPublic|BindingFlags.Static)!; Check(validate.Invoke(null,[7,copy]) is null,"Current format 1.1 restore points support map appearance and references."); normalized.ProjectID=8;Check(validate.Invoke(null,[7,backup]) is string,"Restore cannot import another project's image asset."); } public static void Access() { var controller=new MapsController(null!,null!,null!,null!,new Denied(),null!); Check(controller.ReferenceImage(7,Guid.NewGuid()).Result is NotFoundResult,"Image reads enforce project access before touching the repository."); Check(controller.UploadReference(7,null,null!).Result is NotFoundResult,"Image uploads enforce project access."); Check(controller.DeleteUnusedReference(7,Guid.NewGuid()).Result is NotFoundResult,"Unused image deletion enforces project access."); } private sealed class Denied:IProjectAccessService{public Task CanAccessProjectAsync(int id)=>Task.FromResult(false);public Task CanOwnProjectAsync(int id)=>Task.FromResult(false);public Task CanAccessEntityAsync(string type,int id)=>Task.FromResult(false);} }