23
23
*/
24
24
package org .opengrok .indexer .index ;
25
25
26
+ import static org .junit .Assert .assertEquals ;
26
27
import static org .junit .Assert .assertFalse ;
27
28
import static org .junit .Assert .assertTrue ;
28
29
@@ -113,9 +114,19 @@ public static void tearDownClass() {
113
114
}
114
115
115
116
@ 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
+
117
128
runIndexer ();
118
- lsdir (env .getDataRootPath ());
129
+ lsDir (env .getDataRootPath ());
119
130
120
131
Path xref = Paths .get (env .getDataRootPath (), "xref" );
121
132
assertFalse (xref + " should not exist" , xref .toFile ().exists ());
@@ -125,10 +136,15 @@ public void testSymlinksDisallowed() throws IOException, IndexerException {
125
136
public void testSymlinksWithFullCanonicalRoot () throws IOException , IndexerException {
126
137
File externalRoot = new File (repository .getExternalRoot ());
127
138
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
+ */
128
144
env .setCanonicalRoots (new HashSet <>(Collections .singletonList (
129
145
externalRoot .getCanonicalPath ())));
130
146
runIndexer ();
131
- lsdir (env .getDataRootPath ());
147
+ lsDir (env .getDataRootPath ());
132
148
133
149
Path xref = Paths .get (env .getDataRootPath (), "xref" );
134
150
assertTrue (xref + " should exist" , xref .toFile ().exists ());
@@ -138,26 +154,40 @@ public void testSymlinksWithFullCanonicalRoot() throws IOException, IndexerExcep
138
154
139
155
Path gitDir = links .resolve ("gt" );
140
156
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 );
141
160
142
161
Path mercurialDir = links .resolve ("mrcrl" );
143
162
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 );
144
166
145
167
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 );
148
174
}
149
175
150
176
@ Test
151
- public void testSymlinksWithOneAllowedSymlink () throws IOException , IndexerException {
177
+ public void testSymlinksWithOneAddedSymlink () throws IOException , IndexerException {
152
178
File canonicalSourceRoot = new File (repository .getSourceRoot ()).getCanonicalFile ();
153
179
Path linksSourceDir = Paths .get (canonicalSourceRoot .getPath (), "links" );
154
180
Path gitSourceDir = linksSourceDir .resolve ("gt" );
155
181
assertTrue (gitSourceDir + " should exist" , gitSourceDir .toFile ().exists ());
156
182
183
+ /*
184
+ * By "one added symlink", we don't count default-accepted links
185
+ * immediately under sourceRoot, which we also include here.
186
+ */
157
187
env .setAllowedSymlinks (new HashSet <>(Arrays .asList (
158
188
linksSourceDir .toString (), gitSourceDir .toString ())));
159
189
runIndexer ();
160
- lsdir (env .getDataRootPath ());
190
+ lsDir (env .getDataRootPath ());
161
191
162
192
Path xref = Paths .get (env .getDataRootPath (), "xref" );
163
193
assertTrue (xref + " should exist" , xref .toFile ().exists ());
@@ -167,6 +197,9 @@ public void testSymlinksWithOneAllowedSymlink() throws IOException, IndexerExcep
167
197
168
198
Path gitDir = links .resolve ("gt" );
169
199
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 );
170
203
171
204
Path mercurialDir = links .resolve ("mrcrl" );
172
205
assertFalse (mercurialDir + " should not exist" , mercurialDir .toFile ().exists ());
@@ -177,8 +210,16 @@ public void testSymlinksWithOneAllowedSymlink() throws IOException, IndexerExcep
177
210
* already-accepted symlink, gt, and is reachable upon traversal by
178
211
* indexDown() (to affirm that any intermediate symlinks are allowed).
179
212
*/
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 );
182
223
}
183
224
184
225
private static void runIndexer () throws IndexerException , IOException {
@@ -188,13 +229,22 @@ private static void runIndexer() throws IndexerException, IOException {
188
229
indexer .doIndexerExecution (true , null , null );
189
230
}
190
231
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 {
192
242
File file = Paths .get (name ).toFile ();
193
243
if (!file .exists ()) {
194
244
return ;
195
245
}
196
246
197
- lsobj (file );
247
+ lsObj (file );
198
248
if (Files .isSymbolicLink (file .toPath ())) {
199
249
return ;
200
250
}
@@ -206,17 +256,21 @@ private static void lsdir(String name) {
206
256
207
257
for (String filename : fileList ) {
208
258
Path child = Paths .get (name , filename );
209
- lsdir (child .toString ());
259
+ lsDir (child .toString ());
210
260
}
211
261
}
212
262
213
- private static void lsobj (File file ) {
263
+ private static void lsObj (File file ) throws IOException {
214
264
if (!file .exists ()) {
215
265
return ;
216
266
}
267
+
268
+ Path file1 = file .toPath ();
269
+
217
270
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 ));
220
274
} else if (file .isDirectory ()) {
221
275
System .out .print ("/" );
222
276
}
0 commit comments