Skip to content

Commit

Permalink
preventing dump file conversion by it on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
eskimo committed Apr 14, 2023
1 parent 680085c commit 1632fea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sh text eol=lf
2 changes: 1 addition & 1 deletion src/main/distrib/eskimo.bat
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ REM overriding serviceDefinitionFile in eskimo.properties
SET SERVICESDEFINITIONFILE=conf\services.json

REM encoding UTF-8 is required to parse SSH command results properly.
%JAVA_HOME%\bin\java ^
java ^
-Xms1024m ^
-Xmx1024m ^
-Dfile.encoding=UTF-8 ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ String getFileMimeType(Node node, String newPath) throws SSHCommandException {
return sshCommandService.runSSHCommand(node, "file --brief --mime-type " + StringEscapeUtils.escapeXSI(newPath));
}

boolean isTextMimeType(String fileMimeType) {
static boolean isTextMimeType(String fileMimeType) {
if (fileMimeType.startsWith("text") || fileMimeType.contains("+xml")) {
return true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ public void applySetup(SetupCommand setupCommand) throws SetupException {
boolean success = false;
try {

logger.info ("Starting Setup Operation.");
operationsMonitoringService.startCommand(setupCommand);

JsonWrapper setupConfig = setupCommand.getRawSetup();
Expand Down Expand Up @@ -619,6 +620,7 @@ public void applySetup(SetupCommand setupCommand) throws SetupException {

} finally {
operationsMonitoringService.endCommand(success);
logger.info ("Setup Operation Completed.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

package ch.niceideas.eskimo.services;

import ch.niceideas.common.json.JsonWrapper;
import ch.niceideas.common.utils.FileUtils;
import ch.niceideas.common.utils.ResourceUtils;
import ch.niceideas.common.utils.StreamUtils;
import ch.niceideas.common.utils.StringUtils;
import ch.niceideas.eskimo.AbstractBaseSSHTest;
import ch.niceideas.eskimo.EskimoApplication;
Expand All @@ -55,7 +58,11 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -102,7 +109,7 @@ public void setUp() throws Exception {
connectionManagerServiceTest.setPrivateSShKeyContent(privateKeyRaw);
connectionManagerServiceTest.setSShPort(getSShPort());

configurationServiceTest.saveSetupConfig("{ \"" + SetupService.SSH_USERNAME_FIELD + "\" : \"test\" }");
configurationServiceTest.saveSetupConfig("{ \"" + SetupService.SSH_USERNAME_FIELD + "\" : \"" + System.getProperty("user.name") + "\" }");
}

@AfterEach
Expand Down Expand Up @@ -174,19 +181,41 @@ public void testRunSSHScriptPath() throws Exception {
}

@Test
public void testScpFile() throws Exception {
public void testCopyScpFile() throws Exception {
File source = File.createTempFile("test_ssh", ".txt");
FileUtils.writeFile(source, "test content");
try {
sshCommandService.copySCPFile(Node.fromName("localhost"), source.getAbsolutePath());
File dest = new File ("/home/" + source.getName());
assertTrue (dest.exists());
assertEquals ("test content", FileUtils.readFile(dest));
} catch (SSHCommandException e) {
// this can happen if we don't have the rights to write in /home
if (!e.getMessage().equals("Error during SCP transfer.")) {
fail (e.getMessage() + " is not expected");
}
sshCommandService.copySCPFile(Node.fromName("localhost"), source.getAbsolutePath());
File dest = new File ("/home/" + source.getName());
assertTrue (dest.exists());
assertEquals ("test content", FileUtils.readFile(dest));
}

@Test
public void testCopyScpFile_edgeCase() throws Exception {

}

@Test
public void testCopyScpFile_dos2Unix() throws Exception {
File source = File.createTempFile("test_ssh", ".txt");
FileUtils.writeFile(source, "test\r\ncontent\r\n");
sshCommandService.copySCPFile(Node.fromName("localhost"), source.getAbsolutePath());
File dest = new File ("/home/" + source.getName());
assertTrue (dest.exists());
assertEquals ("test\ncontent\n", FileUtils.readFile(dest));
}

@Test
public void testCopyScpFile_binaryFile() throws Exception {
File source = File.createTempFile("favicon", ".png");
byte[] favicon = StreamUtils.getBytes(ResourceUtils.getResourceAsStream("SSHCommandServiceTest/favicon.png"));
assertNotNull(favicon);
try (FileOutputStream fos = new FileOutputStream (source)) {
StreamUtils.copy(new ByteArrayInputStream(favicon), fos);
}
sshCommandService.copySCPFile(Node.fromName("localhost"), source.getAbsolutePath());
File dest = new File ("/home/" + source.getName());
assertTrue (dest.exists());
assertEquals (favicon, StreamUtils.getBytes(new FileInputStream(dest)));
}
}

0 comments on commit 1632fea

Please sign in to comment.