Skip to content

Commit

Permalink
The permissions of scripts getting changed #48 : rfct to be able to e…
Browse files Browse the repository at this point in the history
…xtract & test (un-)tar command
  • Loading branch information
xonixx committed Sep 12, 2024
1 parent 7c14e39 commit 6054a31
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cmlteam/serv/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Command {

private static final HelpMessageGenerator helpMessageGenerator = new HelpMessageGenerator();

static Command fromArgs(String[] args) {
static Command fromArgs(String... args) {
Options options = new Options();

Option host =
Expand Down
80 changes: 44 additions & 36 deletions src/test/java/com/cmlteam/serv/SharingTests.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.cmlteam.serv;

import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static com.cmlteam.serv.TestsUtil.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.BufferedInputStream;
import java.io.File;
Expand All @@ -12,10 +11,12 @@
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;

import static com.cmlteam.serv.TestsUtil.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class SharingTests {
private static final String testPort = "18888";
Expand Down Expand Up @@ -155,10 +156,12 @@ void testServeFileSet(boolean isGz, Path tempDir) throws IOException {

// WHEN
File resultFile = tempDir.resolve("input_folder_result.tar" + (isGz ? ".gz" : "")).toFile();
getUrlToFile("http://" + address.getHostName() + ":" + address.getPort() + "/dl" + (isGz ? "?z" : ""), resultFile);
getUrlToFile(
"http://" + address.getHostName() + ":" + address.getPort() + "/dl" + (isGz ? "?z" : ""),
resultFile);

// System.out.println("resultFile: " + resultFile);
// System.out.println(" size=" + resultFile.length());
// System.out.println("resultFile: " + resultFile);
// System.out.println(" size=" + resultFile.length());

// THEN
Path resultExtractedFolder = createTestFolder(tempDir, "result_folder");
Expand All @@ -174,23 +177,33 @@ void testServeFileSet(boolean isGz, Path tempDir) throws IOException {
}

@Test
void testPreserveScriptsPermissions_extractViaTar_issue48(@TempDir Path tempDir) throws IOException {
void testPreserveScriptsPermissions_extractViaTar_issue48(@TempDir Path tempDir)
throws IOException {
// GIVEN
Path inputFolder = createTestFolder(tempDir, "input_folder");

String fname1 = "script.sh";

Path file1 = createTestFile(inputFolder, fname1, "#!/bin/sh\necho 123\n");

assertEquals(0, TestsUtil.exec("chmod", "+x", file1.toFile().getAbsolutePath()));
assertEquals(0, TestsUtil.exec("ls", "-l", file1.toFile().getAbsolutePath())); // show perms
// assertEquals(0, TestsUtil.exec(file1.toFile().getAbsolutePath()));

serv =
new Serv(
"-p",
testPort,
inputFolder.toFile().getAbsolutePath());
assertEquals(0, exec("chmod", "+x", file1.toFile().getAbsolutePath()));
assertEquals(0, exec("ls", "-l", file1.toFile().getAbsolutePath())); // show perms
// assertEquals(0, TestsUtil.exec(file1.toFile().getAbsolutePath()));

Command command = Command.fromArgs("-p", testPort, inputFolder.toFile().getAbsolutePath());
String helpString = command.getHelpString();
String[] lines = helpString.split("[\r\n]+");
String cmdTar = "";
for (String line : lines) {
if (line.contains(" tar ")) {
cmdTar = line;
break;
}
}
// System.out.println(helpString);
// System.out.println("cmdTar: " + cmdTar);

serv = new Serv(command);

InetSocketAddress address = serv.getAddress();

Expand All @@ -204,15 +217,14 @@ void testPreserveScriptsPermissions_extractViaTar_issue48(@TempDir Path tempDir)
// THEN
Path resultExtractedFolder = createTestFolder(tempDir, "result_folder");

// FileExtractUtils.extractTarOrTgz(resultFile, resultExtractedFolder.toFile());

assertEquals(0, system("cd '" + resultExtractedFolder.toFile().getAbsolutePath() + "'; " + cmdTar));


Path file1Res = resultExtractedFolder.resolve(fname1);
assertFilesEqual(file1, file1Res);
assertEquals(0, TestsUtil.exec("tar", "-tvf", resultFile.getAbsolutePath())); // show perms in tar
assertEquals(0, TestsUtil.exec("ls", "-l", file1Res.toFile().getAbsolutePath())); // show perms
assertEquals(0, TestsUtil.exec(file1Res.toFile().getAbsolutePath())); // is still executable
assertEquals(0, exec("tar", "-tvf", resultFile.getAbsolutePath())); // show perms in tar
assertEquals(0, exec("ls", "-l", file1Res.toFile().getAbsolutePath())); // show perms
assertEquals(0, exec(file1Res.toFile().getAbsolutePath())); // is still executable
}

@Test
Expand All @@ -224,15 +236,11 @@ void testPreserveScriptsPermissions_issue48(@TempDir Path tempDir) throws IOExce

Path file1 = createTestFile(inputFolder, fname1, "#!/bin/sh\necho 123\n");

assertEquals(0, TestsUtil.exec("chmod", "+x", file1.toFile().getAbsolutePath()));
assertEquals(0, TestsUtil.exec("ls", "-l", file1.toFile().getAbsolutePath())); // show perms
// assertEquals(0, TestsUtil.exec(file1.toFile().getAbsolutePath()));
assertEquals(0, exec("chmod", "+x", file1.toFile().getAbsolutePath()));
assertEquals(0, exec("ls", "-l", file1.toFile().getAbsolutePath())); // show perms
// assertEquals(0, TestsUtil.exec(file1.toFile().getAbsolutePath()));

serv =
new Serv(
"-p",
testPort,
inputFolder.toFile().getAbsolutePath());
serv = new Serv("-p", testPort, inputFolder.toFile().getAbsolutePath());

InetSocketAddress address = serv.getAddress();

Expand All @@ -250,8 +258,8 @@ void testPreserveScriptsPermissions_issue48(@TempDir Path tempDir) throws IOExce

Path file1Res = resultExtractedFolder.resolve(fname1);
assertFilesEqual(file1, file1Res);
assertEquals(0, TestsUtil.exec("tar", "-tvf", resultFile.getAbsolutePath())); // show perms in tar
assertEquals(0, TestsUtil.exec("ls", "-l", file1Res.toFile().getAbsolutePath())); // show perms
assertEquals(0, TestsUtil.exec(file1Res.toFile().getAbsolutePath())); // is still executable
assertEquals(0, exec("tar", "-tvf", resultFile.getAbsolutePath())); // show perms in tar
assertEquals(0, exec("ls", "-l", file1Res.toFile().getAbsolutePath())); // show perms
assertEquals(0, exec(file1Res.toFile().getAbsolutePath())); // is still executable
}
}

0 comments on commit 6054a31

Please sign in to comment.