Skip to content

Commit

Permalink
Filter filenames in maps
Browse files Browse the repository at this point in the history
Fixes #207
  • Loading branch information
1-alex98 committed Mar 21, 2018
1 parent 06def6d commit e75f15e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/faforever/api/map/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void uploadMap(byte[] mapData, String mapFilename, Player author, boolean isRank
Assert.notNull(author, "'author' must not be null");
Assert.isTrue(mapData.length > 0, "'mapData' must not be empty");

mapFilename = mapFilename.replaceAll("[^\\w.\\-]", ""); //replacing all characters that are illegal in filenames

MapUploadData progressData = new MapUploadData()
.setBaseDir(contentService.createTempDir())
.setUploadFileName(mapFilename)
Expand Down Expand Up @@ -236,7 +238,7 @@ private void updateMapEntities(MapUploadData progressData) {
if (map == null) {
map = new Map();
}
map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString())
map.setDisplayName(scenarioInfo.get(ScenarioMapInfo.NAME).toString().replaceAll("[^\\w.\\-\\ ]", ""))
.setMapType(scenarioInfo.get(ScenarioMapInfo.TYPE).tojstring())
.setBattleType(scenarioInfo.get(ScenarioMapInfo.CONFIGURATIONS).get(ScenarioMapInfo.CONFIGURATION_STANDARD).get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS).get(1)
.get(ScenarioMapInfo.CONFIGURATION_STANDARD_TEAMS_NAME).tojstring())
Expand Down
71 changes: 38 additions & 33 deletions src/test/java/com/faforever/api/map/MapServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import com.faforever.api.error.ApiException;
import com.faforever.api.error.ApiExceptionWithMultipleCodes;
import com.faforever.api.error.ErrorCode;
import com.faforever.commons.io.Unzipper;
import com.google.common.io.ByteStreams;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
import junitx.framework.FileAssert;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -24,16 +22,12 @@
import org.mockito.Mockito;
import org.springframework.util.FileSystemUtils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Stream;
import java.util.zip.ZipInputStream;

import static com.faforever.api.error.ApiExceptionWithCode.apiExceptionWithCode;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -221,6 +215,42 @@ public void mapWithMoreRootFoldersInZip() throws IOException {
}
}

@Test
public void positiveUploadTestWithProblematicCharacters() throws IOException {
String zipFilename = "scmp_037_no_ascii.zip";
when(mapRepository.findOneByDisplayName(any())).thenReturn(Optional.empty());
try (InputStream inputStream = loadMapResourceAsStream(zipFilename)) {
byte[] mapData = ByteStreams.toByteArray(inputStream);

Path tmpDir = temporaryDirectory.getRoot().toPath();
instance.uploadMap(mapData, zipFilename, author, true);

ArgumentCaptor<com.faforever.api.data.domain.Map> mapCaptor = ArgumentCaptor.forClass(com.faforever.api.data.domain.Map.class);
verify(mapRepository, Mockito.times(1)).save(mapCaptor.capture());
assertEquals("No_Ascii", mapCaptor.getValue().getDisplayName());
assertEquals("skirmish", mapCaptor.getValue().getMapType());
assertEquals("FFA", mapCaptor.getValue().getBattleType());
assertEquals(1, mapCaptor.getValue().getVersions().size());

MapVersion mapVersion = mapCaptor.getValue().getVersions().get(0);
assertEquals("The thick, brackish water clings to everything, staining anything it touches. If it weren't for this planet's proximity to the Quarantine Zone, no one would ever bother coming here.", mapVersion.getDescription());
assertEquals(1, mapVersion.getVersion());
assertEquals(256, mapVersion.getHeight());
assertEquals(256, mapVersion.getWidth());
assertEquals(3, mapVersion.getMaxPlayers());
assertEquals("maps/no_ascii.v0001.zip", mapVersion.getFilename());

assertFalse(Files.exists(tmpDir));

Path generatedFile = finalDirectory.getRoot().toPath().resolve("no_ascii.v0001.zip");
assertTrue(Files.exists(generatedFile));

assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathLarge().resolve("no_ascii.v0001.png")));
assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathSmall().resolve("no_ascii.v0001.png")));

}
}

@Test
public void positiveUploadTest() throws IOException {
String zipFilename = "scmp_037.zip";
Expand Down Expand Up @@ -251,34 +281,9 @@ public void positiveUploadTest() throws IOException {
Path generatedFile = finalDirectory.getRoot().toPath().resolve("sludge_test.v0001.zip");
assertTrue(Files.exists(generatedFile));

Path generatedFiles = finalDirectory.getRoot().toPath().resolve("generated_files");
try (ZipInputStream inputStreamOfExpectedFile = new ZipInputStream(
new BufferedInputStream(new FileInputStream(generatedFile.toFile())))) {
Unzipper.from(inputStreamOfExpectedFile).to(generatedFiles).unzip();
}

Path expectedFiles = finalDirectory.getRoot().toPath().resolve("expected_files");
try (ZipInputStream inputStreamOfExpectedFile = new ZipInputStream(new BufferedInputStream(
loadMapResourceAsStream("sludge_test.v0001.zip")))) {
Unzipper.from(inputStreamOfExpectedFile).to(expectedFiles).unzip();
}
assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathLarge().resolve("sludge_test.v0001.png")));
assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathSmall().resolve("sludge_test.v0001.png")));

expectedFiles = expectedFiles.resolve("sludge_test.v0001");
try (Stream<Path> fileStream = Files.list(expectedFiles)) {
assertEquals(fileStream.count(), (long) 4);
}

try (Stream<Path> fileStream = Files.list(expectedFiles)) {
Path finalGeneratedFile = generatedFiles.resolve("sludge_test.v0001");
fileStream.forEach(expectedFile ->
FileAssert.assertEquals("Difference in " + expectedFile.getFileName().toString(),
expectedFile.toFile(),
finalGeneratedFile.resolve(expectedFile.getFileName().toString()).toFile())
);

assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathLarge().resolve("sludge_test.v0001.png")));
assertTrue(Files.exists(mapProperties.getDirectoryPreviewPathSmall().resolve("sludge_test.v0001.png")));
}
}
}

Expand Down
Binary file added src/test/resources/maps/scmp_037_no_ascii.zip
Binary file not shown.

0 comments on commit e75f15e

Please sign in to comment.