[FIX Test to check we have the expected permissions
@@ -6,20 +6,36 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.jvnet.hudson.test.Issue;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFilePermission;
+import java.util.EnumSet;
+import java.util.Set;
import static org.hamcrest.core.StringContains.*;
+import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
+import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
+import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE;
+import static java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE;
+import static java.nio.file.attribute.PosixFilePermission.OTHERS_READ;
+import static java.nio.file.attribute.PosixFilePermission.OTHERS_WRITE;
+import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE;
+import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;
+import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;
+import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeThat;
public class AtomicFileWriterTest {
private static final String PREVIOUS = "previous value \n blah";
@@ -124,4 +140,55 @@ public class AtomicFileWriterTest {
containsString("exists and is neither a directory nor a symlink to a directory"));
}
}
+
+ @Issue("JENKINS-48407")
+ @Test
+ public void checkPermissions() throws IOException, InterruptedException {
+
+ final File newFile = tmp.newFile();
+
+ // Check Posix calls are supported (to avoid running this test on Windows for instance)
+ boolean posixSupported = true;
+ try {
+ Files.getPosixFilePermissions(newFile.toPath());
+ } catch (UnsupportedOperationException e) {
+ posixSupported = false;
+ }
+ assumeThat(posixSupported, is(true));
+
+ // given
+ final Set<PosixFilePermission> givenPermissions = EnumSet.of(OWNER_READ,
+ OWNER_WRITE,
+ GROUP_READ,
+ GROUP_WRITE,
+ OTHERS_READ
+ );
+
+ final Set<PosixFilePermission> notGivenPermissions = EnumSet.of(OWNER_EXECUTE,
+ GROUP_EXECUTE,
+ OTHERS_WRITE,
+ OTHERS_EXECUTE);
+
+ Files.setPosixFilePermissions(newFile.toPath(), givenPermissions);
+ Path filePath = newFile.toPath();
+
+ // when
+ AtomicFileWriter w = new AtomicFileWriter(filePath, StandardCharsets.UTF_8);
+ w.write("whatever");
+ w.commit();
+
+ // then
+ assertFalse(w.getTemporaryPath().toFile().exists());
+ assertTrue(filePath.toFile().exists());
+
+ final Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(filePath);
+
+ for (PosixFilePermission perm : givenPermissions) {
+ assertTrue("missing: " + perm, posixFilePermissions.contains(perm));
+ }
+
+ for (PosixFilePermission perm : notGivenPermissions) {
+ assertFalse("should not be allowed: " + perm, posixFilePermissions.contains(perm));
+ }
+ }
}
