Skip to content

Commit

Permalink
Merge pull request #1003 from jenkinsci/skip-archives
Browse files Browse the repository at this point in the history
[JENKINS-68415] Skip archives, jar-files and executables from fingerprinting
  • Loading branch information
uhafner authored Jan 17, 2024
2 parents fa22228 + 217d5c3 commit 68a8d40
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
18 changes: 16 additions & 2 deletions src/main/java/edu/hm/hafner/analysis/FingerprintGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.nio.charset.MalformedInputException;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import edu.hm.hafner.util.FilteredLog;
Expand All @@ -19,6 +22,9 @@
* @author Ullrich Hafner
*/
public class FingerprintGenerator {
private static final Set<String> NON_SOURCE_CODE_EXTENSIONS = Set.of(
"o", "exe", "dll", "so", "a", "lib", "jar", "war", "zip", "7z", "gz", "bz2");

/**
* Creates fingerprints for the specified set of issues.
*
Expand All @@ -34,14 +40,22 @@ public void run(final FullTextFingerprint algorithm, final Report report, final
int sum = 0;
for (Issue issue : report) {
if (!issue.hasFingerprint()) {
sum += computeFingerprint(issue, algorithm, charset, log);
if (hasAllowedExtension(issue.getFileName())) {
sum += computeFingerprint(issue, algorithm, charset, log);
}
else {
issue.setFingerprint(createDefaultFingerprint(issue));
}
}
}
log.logSummary();
report.mergeLogMessages(log);
report.logInfo("-> created fingerprints for %d issues (skipped %d issues)", sum, report.size() - sum);
}

private boolean hasAllowedExtension(final String fileName) {
return !NON_SOURCE_CODE_EXTENSIONS.contains(StringUtils.lowerCase(FilenameUtils.getExtension(fileName)));
}

private int computeFingerprint(final Issue issue, final FullTextFingerprint algorithm, final Charset charset,
final FilteredLog log) {
String absolutePath = issue.getAbsolutePath();
Expand Down
58 changes: 40 additions & 18 deletions src/test/java/edu/hm/hafner/analysis/FingerprintGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void shouldSkipFingerprintingIfEncodingIsWrong() throws IOException {

FileSystem fileSystem = mock(FileSystem.class);
when(fileSystem.readLinesFromFile(anyString(), any()))
.thenThrow(new UncheckedIOException(new MalformedInputException(1)));
.thenThrow(new UncheckedIOException(new MalformedInputException(1)));

generator.run(new FullTextFingerprint(fileSystem), report, CHARSET_AFFECTED_FILE);

Expand All @@ -59,7 +59,7 @@ void shouldNotChangeIssuesWithFingerPrint() {

String alreadySet = "already-set";
report.add(issueBuilder.setFingerprint(alreadySet).setMessage(AFFECTED_FILE_NAME).build());
generator.run(createFullTextFingerprint("fingerprint-one.txt", "fingerprint-two.txt"),
generator.run(createFullTextFingerprint("fingerprint-two.txt"),
report, CHARSET_AFFECTED_FILE);

assertThat(report.get(0).hasFingerprint()).isTrue();
Expand All @@ -69,22 +69,24 @@ void shouldNotChangeIssuesWithFingerPrint() {

@Test
void shouldSetDefaultFingerprintIfNoFileIsGiven() {
FingerprintGenerator generator = new FingerprintGenerator();
try (IssueBuilder issueBuilder = new IssueBuilder()) {
FingerprintGenerator generator = new FingerprintGenerator();

Report report = new Report();
report.add(new IssueBuilder().build());
Report report = new Report();
report.add(issueBuilder.build());

generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE);
generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE);

assertThatIssueHasDefaultFingerprint(report);
assertThat(report.getErrorMessages()).isEmpty();
assertThatIssueHasDefaultFingerprint(report);
assertThat(report.getErrorMessages()).isEmpty();
}
}

@Test
void shouldAssignIdenticalFingerprint() {
Report report = createTwoIssues();
FingerprintGenerator generator = new FingerprintGenerator();
FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt", "fingerprint-one.txt");
FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt");

generator.run(fingerprint, report, CHARSET_AFFECTED_FILE);

Expand Down Expand Up @@ -117,7 +119,7 @@ private FileSystem stubFileSystem(final String firstFile, final String secondFil
void shouldAssignDifferentFingerprint() {
Report report = createTwoIssues();
FingerprintGenerator generator = new FingerprintGenerator();
FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt", "fingerprint-two.txt");
FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-two.txt");

generator.run(fingerprint, report, CHARSET_AFFECTED_FILE);

Expand All @@ -128,27 +130,47 @@ void shouldAssignDifferentFingerprint() {
assertThat(referenceIssue.getFingerprint()).isNotEqualTo(currentIssue.getFingerprint());
}

@ParameterizedTest(name = "[{index}] Illegal filename")
@ParameterizedTest(name = "[{index}] Illegal filename {0}")
@ValueSource(strings = {"/does/not/exist", "!<>$&/&(", "\0 Null-Byte"})
void shouldUseFallbackFingerprintOnError(final String fileName) {
Report report;
try (IssueBuilder issueBuilder = new IssueBuilder()) {
report = new Report();
report.add(issueBuilder.setFileName(fileName).build());
}
var report = createReportWithOneIssueFor(fileName);

FingerprintGenerator generator = new FingerprintGenerator();
generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE);

assertThatIssueHasDefaultFingerprint(report);
}

@ParameterizedTest(name = "[{index}] Skip non source code file {0}")
@ValueSource(strings = {"library.o", "program.exe", "library.dll", "program.so", "library.a", "program.lib",
"library.jar", "library.war", "program.zip", "library.7z", "program.tar.gz", "library.tar.bz2",
"UPPER_CASE.EXE"})
void shouldUseFallbackFingerprintOnNonSourceFiles(final String fileName) {
var report = createReportWithOneIssueFor(fileName);

FingerprintGenerator generator = new FingerprintGenerator();
generator.run(createFullTextFingerprint("fingerprint-two.txt"),
report, CHARSET_AFFECTED_FILE);

assertThatIssueHasDefaultFingerprint(report);
}

private Report createReportWithOneIssueFor(final String fileName) {
Report report;
try (IssueBuilder issueBuilder = new IssueBuilder()) {
report = new Report();
report.add(issueBuilder.setFileName(fileName).build());
}
return report;
}

private void assertThatIssueHasDefaultFingerprint(final Report report) {
assertThat(report.get(0)).hasFingerprint(FingerprintGenerator.createDefaultFingerprint(report.get(0)));
}

private FullTextFingerprint createFullTextFingerprint(final String firstFile, final String secondFile) {
FileSystem fileSystem = stubFileSystem(firstFile, secondFile);
private FullTextFingerprint createFullTextFingerprint(final String secondFile) {
FileSystem fileSystem = stubFileSystem("fingerprint-one.txt", secondFile);

return new FullTextFingerprint(fileSystem);
}

Expand Down

0 comments on commit 68a8d40

Please sign in to comment.