Skip to content

Commit 1c3bcdb

Browse files
committed
Add (failing) tests of additional symlink scenarios
1 parent 6d7e50d commit 1c3bcdb

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexDatabaseSymlinksTest.java

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package org.opengrok.indexer.index;
2525

26+
import static org.junit.Assert.assertEquals;
2627
import static org.junit.Assert.assertFalse;
2728
import static org.junit.Assert.assertTrue;
2829

@@ -113,9 +114,19 @@ public static void tearDownClass() {
113114
}
114115

115116
@Test
116-
public void testSymlinksDisallowed() throws IOException, IndexerException {
117+
public void testNoAddedSymlinks() throws IOException, IndexerException {
118+
File canonicalSourceRoot = new File(repository.getSourceRoot()).getCanonicalFile();
119+
Path linksSourceDir = Paths.get(canonicalSourceRoot.getPath(), "links");
120+
121+
/*
122+
* By "no added symlinks", we don't count default-accepted links
123+
* immediately under sourceRoot, which we do include here.
124+
*/
125+
env.setAllowedSymlinks(new HashSet<>(Collections.singletonList(
126+
linksSourceDir.toString())));
127+
117128
runIndexer();
118-
lsdir(env.getDataRootPath());
129+
lsDir(env.getDataRootPath());
119130

120131
Path xref = Paths.get(env.getDataRootPath(), "xref");
121132
assertFalse(xref + " should not exist", xref.toFile().exists());
@@ -125,10 +136,15 @@ public void testSymlinksDisallowed() throws IOException, IndexerException {
125136
public void testSymlinksWithFullCanonicalRoot() throws IOException, IndexerException {
126137
File externalRoot = new File(repository.getExternalRoot());
127138

139+
/*
140+
* For this test, don't even bother to include default-accepted links
141+
* immediately under sourceRoot, as -C,--canonicalRoot as specified
142+
* here encompasses all of external/.
143+
*/
128144
env.setCanonicalRoots(new HashSet<>(Collections.singletonList(
129145
externalRoot.getCanonicalPath())));
130146
runIndexer();
131-
lsdir(env.getDataRootPath());
147+
lsDir(env.getDataRootPath());
132148

133149
Path xref = Paths.get(env.getDataRootPath(), "xref");
134150
assertTrue(xref + " should exist", xref.toFile().exists());
@@ -138,26 +154,40 @@ public void testSymlinksWithFullCanonicalRoot() throws IOException, IndexerExcep
138154

139155
Path gitDir = links.resolve("gt");
140156
assertTrue(gitDir + " should exist", gitDir.toFile().exists());
157+
Path subLink = gitDir.resolve("b");
158+
File expectedCanonical = gitDir.resolve("a").toFile().getCanonicalFile();
159+
assertSymlinkAsExpected("gt/b should == gt/a", expectedCanonical, subLink);
141160

142161
Path mercurialDir = links.resolve("mrcrl");
143162
assertTrue(mercurialDir + " should exist", mercurialDir.toFile().exists());
163+
subLink = mercurialDir.resolve("b");
164+
expectedCanonical = mercurialDir.resolve("a").toFile().getCanonicalFile();
165+
assertSymlinkAsExpected("mrcrl/b should == mrcrl/a", expectedCanonical, subLink);
144166

145167
Path dupeLinkDir = links.resolve("zzz");
146-
assertTrue(dupeLinkDir + " should exist", dupeLinkDir.toFile().exists());
147-
assertTrue(dupeLinkDir + " should be symlink", Files.isSymbolicLink(dupeLinkDir));
168+
expectedCanonical = gitDir.toFile().getCanonicalFile();
169+
assertSymlinkAsExpected("zzz should == gt", expectedCanonical, dupeLinkDir);
170+
171+
Path dupe2LinkDir = links.resolve("zzz_a");
172+
expectedCanonical = gitDir.resolve("a").toFile().getCanonicalFile();
173+
assertSymlinkAsExpected("zzz_a should == gt/a", expectedCanonical, dupe2LinkDir);
148174
}
149175

150176
@Test
151-
public void testSymlinksWithOneAllowedSymlink() throws IOException, IndexerException {
177+
public void testSymlinksWithOneAddedSymlink() throws IOException, IndexerException {
152178
File canonicalSourceRoot = new File(repository.getSourceRoot()).getCanonicalFile();
153179
Path linksSourceDir = Paths.get(canonicalSourceRoot.getPath(), "links");
154180
Path gitSourceDir = linksSourceDir.resolve("gt");
155181
assertTrue(gitSourceDir + " should exist", gitSourceDir.toFile().exists());
156182

183+
/*
184+
* By "one added symlink", we don't count default-accepted links
185+
* immediately under sourceRoot, which we also include here.
186+
*/
157187
env.setAllowedSymlinks(new HashSet<>(Arrays.asList(
158188
linksSourceDir.toString(), gitSourceDir.toString())));
159189
runIndexer();
160-
lsdir(env.getDataRootPath());
190+
lsDir(env.getDataRootPath());
161191

162192
Path xref = Paths.get(env.getDataRootPath(), "xref");
163193
assertTrue(xref + " should exist", xref.toFile().exists());
@@ -167,6 +197,9 @@ public void testSymlinksWithOneAllowedSymlink() throws IOException, IndexerExcep
167197

168198
Path gitDir = links.resolve("gt");
169199
assertTrue(gitDir + " should exist", gitDir.toFile().exists());
200+
Path subLink = gitDir.resolve("b");
201+
File expectedCanonical = gitDir.resolve("a").toFile().getCanonicalFile();
202+
assertSymlinkAsExpected("gt/b should == gt/a", expectedCanonical, subLink);
170203

171204
Path mercurialDir = links.resolve("mrcrl");
172205
assertFalse(mercurialDir + " should not exist", mercurialDir.toFile().exists());
@@ -177,8 +210,16 @@ public void testSymlinksWithOneAllowedSymlink() throws IOException, IndexerExcep
177210
* already-accepted symlink, gt, and is reachable upon traversal by
178211
* indexDown() (to affirm that any intermediate symlinks are allowed).
179212
*/
180-
assertTrue(dupeLinkDir + " should exist", dupeLinkDir.toFile().exists());
181-
assertTrue(dupeLinkDir + " should be symlink", Files.isSymbolicLink(dupeLinkDir));
213+
expectedCanonical = gitDir.toFile().getCanonicalFile();
214+
assertSymlinkAsExpected("zzz should == gt", expectedCanonical, dupeLinkDir);
215+
216+
/*
217+
* zzz_a is an implicitly-allowed symlink because its target matches as
218+
* a canonical child of an already-accepted symlink, gt.
219+
*/
220+
Path dupe2LinkDir = links.resolve("zzz_a");
221+
expectedCanonical = gitDir.resolve("a").toFile().getCanonicalFile();
222+
assertSymlinkAsExpected("zzz_a should == gt/a", expectedCanonical, dupe2LinkDir);
182223
}
183224

184225
private static void runIndexer() throws IndexerException, IOException {
@@ -188,13 +229,22 @@ private static void runIndexer() throws IndexerException, IOException {
188229
indexer.doIndexerExecution(true, null, null);
189230
}
190231

191-
private static void lsdir(String name) {
232+
private void assertSymlinkAsExpected(String message, File expectedCanonical, Path symlink)
233+
throws IOException {
234+
assertTrue(symlink + " should exist", symlink.toFile().exists());
235+
assertTrue(symlink + " should be symlink", Files.isSymbolicLink(symlink));
236+
File actualCanonical = symlink.toFile().getCanonicalFile();
237+
assertTrue(actualCanonical + " should exist", actualCanonical.exists());
238+
assertEquals(message, expectedCanonical, actualCanonical);
239+
}
240+
241+
private static void lsDir(String name) throws IOException {
192242
File file = Paths.get(name).toFile();
193243
if (!file.exists()) {
194244
return;
195245
}
196246

197-
lsobj(file);
247+
lsObj(file);
198248
if (Files.isSymbolicLink(file.toPath())) {
199249
return;
200250
}
@@ -206,17 +256,21 @@ private static void lsdir(String name) {
206256

207257
for (String filename : fileList) {
208258
Path child = Paths.get(name, filename);
209-
lsdir(child.toString());
259+
lsDir(child.toString());
210260
}
211261
}
212262

213-
private static void lsobj(File file) {
263+
private static void lsObj(File file) throws IOException {
214264
if (!file.exists()) {
215265
return;
216266
}
267+
268+
Path file1 = file.toPath();
269+
217270
System.out.print(file.getPath());
218-
if (Files.isSymbolicLink(file.toPath())) {
219-
System.out.print(" ->");
271+
if (Files.isSymbolicLink(file1)) {
272+
System.out.print(" -> ");
273+
System.out.print(Files.readSymbolicLink(file1));
220274
} else if (file.isDirectory()) {
221275
System.out.print("/");
222276
}
Binary file not shown.

0 commit comments

Comments
 (0)