package tests; import static io.kojan.runit.api.RUnit.*; import static org.hamcrest.Matchers.*; import java.nio.file.Path; import java.util.Arrays; import org.junit.jupiter.api.DisplayName; import io.kojan.javadeptools.rpm.*; import io.kojan.runit.api.*; @DisplayName("/file") public class FileCheck { @FileTest @IncludeRegularFile @ExcludeBinary(".*-debuginfo") public void elvesLiveInUserLib(RpmInfo rpm, Path path, byte[] data, RpmFile entry) { assumeThat("File is not too small to be ELF", entry.getSize(), is(greaterThanOrEqualTo(4L))); assumeThat("ELF magic is present", Arrays.copyOfRange(data, 0, 4), is(new byte[] { 0x7f, 'E', 'L', 'F' })); assertThat("ELF files must be under /usr/lib", path.toString(), startsWith("/usr/lib/")); assertThat("Noarch packages must not contain ELF files", rpm.getArch(), is(not("noarch"))); } @FileTest @IncludeBinary @ExcludeBinary(".*-debuginfo") public void modeTest(RpmInfo rpm, Path path, byte[] data, RpmFile entry) throws Exception { String mode = Integer.toOctalString((int) entry.getMode() & 07777); if (entry.isRegularFile()) { if (entry.getSize() >= 4 && data[0] == 0x7f && data[1] == 'E' && data[2] == 'L' && data[3] == 'F') { assertThat("ELF files must have mode 755", mode, is("755")); } else if (entry.getSize() >= 2 && data[0] == '#' && data[1] == '!') { assertThat("Files with shebang must have mode 755", mode, is("755")); } else if (entry.getSize() == 0) { assertThat("Empty files must have mode 644 or 0", mode, either(is("644")).or(is("0"))); } else { assertThat("Other regular files must have mode 644", mode, is("644")); } } else if (entry.isDirectory()) { assertThat("Directories must have mode 755", mode, is("755")); } else if (!entry.isSymbolicLink()) { assertThat("Not a regular file, not a symlink and not a directory", false, is(true)); } } }