-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from blackducksoftware/sb_doNotFollowSymLinks
Sb do not follow sym links
- Loading branch information
Showing
13 changed files
with
136 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ble/src/test/java/com/synopsys/integration/detectable/file/impl/SimpleFileFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.synopsys.integration.detectable.file.impl; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.commons.io.filefilter.CanReadFileFilter; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.synopsys.integration.detectable.detectable.file.impl.SimpleFileFinder; | ||
|
||
|
||
public class SimpleFileFinderTest { | ||
|
||
private static Path initialDirectoryPath; | ||
|
||
@BeforeAll | ||
public static void setup() throws IOException { | ||
initialDirectoryPath = Files.createTempDirectory("DetectorFinderTest"); | ||
} | ||
|
||
@AfterAll | ||
public static void cleanup() { | ||
initialDirectoryPath.toFile().delete(); | ||
} | ||
|
||
@Test | ||
public void testSymlinksNotFollowed() throws IOException { | ||
// Create a subDir with a symlink that loops back to its parent | ||
final File initialDirectory = initialDirectoryPath.toFile(); | ||
final File subDir = new File(initialDirectory, "sub"); | ||
subDir.mkdirs(); | ||
final File link = new File(subDir, "linkToInitial"); | ||
final Path linkPath = link.toPath(); | ||
Files.createSymbolicLink(linkPath, initialDirectoryPath); | ||
|
||
final File regularDir = new File(subDir, "regularDir"); | ||
regularDir.mkdir(); | ||
final File regularFile = new File(subDir, "regularFile"); | ||
regularFile.createNewFile(); | ||
|
||
final SimpleFileFinder finder = new SimpleFileFinder(); | ||
final List<String> filenamePatterns = Arrays.asList("sub", "linkToInitial", "regularDir", "regularFile"); | ||
final List<File> foundFiles = finder.findFiles(initialDirectoryPath.toFile(), filenamePatterns, 10); | ||
|
||
// make sure symlink not followed during dir traversal | ||
assertEquals(4, foundFiles.size()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
detector/src/test/java/com/synopsys/integration/detector/finder/DetectorFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.synopsys.integration.detector.finder; | ||
|
||
import static com.sun.javafx.PlatformUtil.isWindows; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.synopsys.integration.detector.base.DetectorEvaluationTree; | ||
import com.synopsys.integration.detector.rule.DetectorRule; | ||
import com.synopsys.integration.detector.rule.DetectorRuleSet; | ||
|
||
public class DetectorFinderTest { | ||
private static Path initialDirectoryPath; | ||
|
||
@BeforeAll | ||
public static void setup() throws IOException { | ||
initialDirectoryPath = Files.createTempDirectory("DetectorFinderTest"); | ||
} | ||
|
||
@AfterAll | ||
public static void cleanup() { | ||
initialDirectoryPath.toFile().delete(); | ||
} | ||
|
||
@Test | ||
public void testSymLinksNotFollowed() throws IOException, DetectorFinderDirectoryListException { | ||
org.junit.Assume.assumeFalse(isWindows()); | ||
|
||
// Create a subDir with a symlink that loops back to its parent | ||
final File initialDirectory = initialDirectoryPath.toFile(); | ||
final File subDir = new File(initialDirectory, "sub"); | ||
subDir.mkdirs(); | ||
final File link = new File(subDir, "linkToInitial"); | ||
final Path linkPath = link.toPath(); | ||
Files.createSymbolicLink(linkPath, initialDirectoryPath); | ||
|
||
final File regularDir = new File(subDir, "regularDir"); | ||
regularDir.mkdir(); | ||
|
||
final DetectorRuleSet detectorRuleSet = new DetectorRuleSet(new ArrayList<DetectorRule>(0), new HashMap<DetectorRule, Set<DetectorRule>>(0)); | ||
final Predicate<File> fileFilter = f -> { return true; }; | ||
final int maximumDepth = 10; | ||
final DetectorFinderOptions options = new DetectorFinderOptions(fileFilter, maximumDepth); | ||
|
||
final DetectorFinder finder = new DetectorFinder(); | ||
final Optional<DetectorEvaluationTree> tree = finder.findDetectors(initialDirectory, detectorRuleSet, options); | ||
|
||
// make sure the symlink was omitted from results | ||
final Set<DetectorEvaluationTree> subDirResults = tree.get().getChildren().iterator().next().getChildren(); | ||
assertEquals(1, subDirResults.size()); | ||
String subDirContentsName = subDirResults.iterator().next().getDirectory().getName(); | ||
assertEquals("regularDir", subDirContentsName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.