|
| 1 | +using Definitions; |
| 2 | +using Definitions.Database; |
| 3 | +using Definitions.ObjectModels.Types; |
| 4 | +using Definitions.Web; |
| 5 | +using Index; |
| 6 | +using Microsoft.Extensions.Configuration; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | +using NUnit.Framework; |
| 9 | +using System.IO.Compression; |
| 10 | + |
| 11 | +namespace ObjectService.Tests.Integration; |
| 12 | + |
| 13 | +[TestFixture] |
| 14 | +public class DownloadRoutesTest |
| 15 | +{ |
| 16 | + HttpClient? httpClient; |
| 17 | + TestWebApplicationFactory<Program>? testWebAppFactory; |
| 18 | + |
| 19 | + [SetUp] |
| 20 | + public void SetUp() |
| 21 | + { |
| 22 | + testWebAppFactory = new TestWebApplicationFactory<Program>(); |
| 23 | + httpClient = testWebAppFactory.CreateClient(); |
| 24 | + } |
| 25 | + |
| 26 | + [TearDown] |
| 27 | + public void TearDown() |
| 28 | + { |
| 29 | + httpClient?.Dispose(); |
| 30 | + testWebAppFactory?.Dispose(); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public async Task GetScenarioFileAsync_ReturnsFileMatchingSortedListOrder() |
| 35 | + { |
| 36 | + using var scope = testWebAppFactory!.Services.CreateScope(); |
| 37 | + var sfm = scope.ServiceProvider.GetRequiredService<ServerFolderManager>(); |
| 38 | + |
| 39 | + var alphaRelativePath = Path.Combine(ServerFolderManager.CustomFolderName, "alpha.SC5"); |
| 40 | + var zuluRelativePath = Path.Combine(ServerFolderManager.CustomFolderName, "zulu.SC5"); |
| 41 | + var alphaPath = Path.Combine(sfm.ScenariosFolder, alphaRelativePath); |
| 42 | + var zuluPath = Path.Combine(sfm.ScenariosFolder, zuluRelativePath); |
| 43 | + |
| 44 | + await File.WriteAllBytesAsync(zuluPath, [9, 9, 9]); |
| 45 | + await File.WriteAllBytesAsync(alphaPath, [1, 2, 3]); |
| 46 | + |
| 47 | + var list = await Client.GetListAsync<Definitions.DTO.DtoScenarioEntry>(httpClient!, Client.ScenariosEndpointGroup); |
| 48 | + var firstScenario = list.First(); |
| 49 | + |
| 50 | + using var response = await httpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.Scenarios}/{firstScenario.Id}{RoutesV2.File}"); |
| 51 | + var bytes = await response.Content.ReadAsByteArrayAsync(); |
| 52 | + |
| 53 | + using (Assert.EnterMultipleScope()) |
| 54 | + { |
| 55 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 56 | + Assert.That(firstScenario.Name, Is.EqualTo(alphaRelativePath)); |
| 57 | + Assert.That(bytes, Is.EqualTo(new byte[] { 1, 2, 3 })); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + public async Task GetSC5FilePackFileAsync_ReturnsZipWithOnlySafeScenarioEntries() |
| 63 | + { |
| 64 | + using var scope = testWebAppFactory!.Services.CreateScope(); |
| 65 | + var sp = scope.ServiceProvider; |
| 66 | + var db = sp.GetRequiredService<LocoDbContext>(); |
| 67 | + var sfm = sp.GetRequiredService<ServerFolderManager>(); |
| 68 | + var config = sp.GetRequiredService<IConfiguration>(); |
| 69 | + var rootFolder = config["ObjectService:RootFolder"]; |
| 70 | + ArgumentNullException.ThrowIfNull(rootFolder); |
| 71 | + |
| 72 | + var safeRelativePath = Path.Combine(ServerFolderManager.CustomFolderName, "pack-safe.SC5"); |
| 73 | + var safePath = Path.Combine(sfm.ScenariosFolder, safeRelativePath); |
| 74 | + await File.WriteAllBytesAsync(safePath, [1, 2, 3, 4]); |
| 75 | + |
| 76 | + var outsidePath = Path.Combine(rootFolder, "outside.SC5"); |
| 77 | + await File.WriteAllBytesAsync(outsidePath, [7, 7, 7]); |
| 78 | + |
| 79 | + var pack = new TblSC5FilePack |
| 80 | + { |
| 81 | + Id = 1, |
| 82 | + Name = "Scenario Pack", |
| 83 | + SC5Files = |
| 84 | + [ |
| 85 | + new TblSC5File { Id = 1, Name = safeRelativePath }, |
| 86 | + new TblSC5File { Id = 2, Name = Path.Combine("..", "outside.SC5") }, |
| 87 | + ], |
| 88 | + }; |
| 89 | + |
| 90 | + _ = await db.SC5FilePacks.AddAsync(pack); |
| 91 | + _ = await db.SaveChangesAsync(); |
| 92 | + |
| 93 | + using var response = await httpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.SC5FilePacks}/{pack.Id}{RoutesV2.File}"); |
| 94 | + var bytes = await response.Content.ReadAsByteArrayAsync(); |
| 95 | + using var archive = new ZipArchive(new MemoryStream(bytes), ZipArchiveMode.Read); |
| 96 | + |
| 97 | + using (Assert.EnterMultipleScope()) |
| 98 | + { |
| 99 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 100 | + Assert.That(response.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/zip")); |
| 101 | + Assert.That(archive.Entries.Select(x => x.FullName), Is.EqualTo(new[] { safeRelativePath.Replace('\\', '/') })); |
| 102 | + await using var entryStream = archive.Entries.Single().Open(); |
| 103 | + using var entryMemoryStream = new MemoryStream(); |
| 104 | + await entryStream.CopyToAsync(entryMemoryStream); |
| 105 | + Assert.That(entryMemoryStream.ToArray(), Is.EqualTo(new byte[] { 1, 2, 3, 4 })); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public async Task GetObjectPackFileAsync_ReturnsZipWithOnlySafeIndexedObjectEntries() |
| 111 | + { |
| 112 | + using var scope = testWebAppFactory!.Services.CreateScope(); |
| 113 | + var sp = scope.ServiceProvider; |
| 114 | + var db = sp.GetRequiredService<LocoDbContext>(); |
| 115 | + var sfm = sp.GetRequiredService<ServerFolderManager>(); |
| 116 | + var config = sp.GetRequiredService<IConfiguration>(); |
| 117 | + var rootFolder = config["ObjectService:RootFolder"]; |
| 118 | + ArgumentNullException.ThrowIfNull(rootFolder); |
| 119 | + |
| 120 | + var safeRelativePath = Path.Combine(ServerFolderManager.CustomFolderName, "safe-object.dat"); |
| 121 | + var safePath = Path.Combine(sfm.ObjectsFolder, safeRelativePath); |
| 122 | + await File.WriteAllBytesAsync(safePath, [4, 3, 2, 1]); |
| 123 | + |
| 124 | + var outsidePath = Path.Combine(rootFolder, "outside.dat"); |
| 125 | + await File.WriteAllBytesAsync(outsidePath, [8, 8, 8]); |
| 126 | + |
| 127 | + sfm.ObjectIndex.Objects.Add(new ObjectIndexEntry("SAFEOBJ", safeRelativePath, null, 111, null, ObjectType.Vehicle, ObjectSource.Custom, null, null)); |
| 128 | + sfm.ObjectIndex.Objects.Add(new ObjectIndexEntry("UNSAFEOBJ", Path.Combine("..", "outside.dat"), null, 222, null, ObjectType.Vehicle, ObjectSource.Custom, null, null)); |
| 129 | + |
| 130 | + var obj = new TblObject |
| 131 | + { |
| 132 | + Id = 1, |
| 133 | + Name = "safe-obj", |
| 134 | + SubObjectId = 1, |
| 135 | + ObjectType = ObjectType.Vehicle, |
| 136 | + ObjectSource = ObjectSource.Custom, |
| 137 | + Availability = ObjectAvailability.Available, |
| 138 | + DatObjects = |
| 139 | + [ |
| 140 | + new TblDatObject { Id = 1, DatName = "SAFEOBJ", DatChecksum = 111, xxHash3 = 1, ObjectId = 1 }, |
| 141 | + new TblDatObject { Id = 2, DatName = "UNSAFEOBJ", DatChecksum = 222, xxHash3 = 2, ObjectId = 1 }, |
| 142 | + ], |
| 143 | + }; |
| 144 | + |
| 145 | + var pack = new TblObjectPack |
| 146 | + { |
| 147 | + Id = 1, |
| 148 | + Name = "Object Pack", |
| 149 | + Objects = [obj], |
| 150 | + }; |
| 151 | + |
| 152 | + _ = await db.ObjectPacks.AddAsync(pack); |
| 153 | + _ = await db.SaveChangesAsync(); |
| 154 | + |
| 155 | + using var response = await httpClient!.GetAsync($"{RoutesV2.Prefix}{RoutesV2.ObjectPacks}/{pack.Id}{RoutesV2.File}"); |
| 156 | + var bytes = await response.Content.ReadAsByteArrayAsync(); |
| 157 | + using var archive = new ZipArchive(new MemoryStream(bytes), ZipArchiveMode.Read); |
| 158 | + |
| 159 | + using (Assert.EnterMultipleScope()) |
| 160 | + { |
| 161 | + Assert.That(response.IsSuccessStatusCode, Is.True); |
| 162 | + Assert.That(response.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/zip")); |
| 163 | + Assert.That(archive.Entries.Select(x => x.FullName), Is.EqualTo(new[] { safeRelativePath.Replace('\\', '/') })); |
| 164 | + await using var entryStream = archive.Entries.Single().Open(); |
| 165 | + using var entryMemoryStream = new MemoryStream(); |
| 166 | + await entryStream.CopyToAsync(entryMemoryStream); |
| 167 | + Assert.That(entryMemoryStream.ToArray(), Is.EqualTo(new byte[] { 4, 3, 2, 1 })); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments