1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.hadoop.fs;
19 
20 import static org.junit.Assert.fail;
21 
22 import java.io.IOException;
23 
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hdfs.DFSTestUtil;
26 import org.apache.hadoop.hdfs.DistributedFileSystem;
27 import org.apache.hadoop.hdfs.HdfsConfiguration;
28 import org.apache.hadoop.hdfs.MiniDFSCluster;
29 import org.apache.hadoop.test.GenericTestUtils;
30 import org.junit.Test;
31 
32 public class TestSymlinkHdfsDisable {
33 
34   @Test(timeout=60000)
testSymlinkHdfsDisable()35   public void testSymlinkHdfsDisable() throws Exception {
36     Configuration conf = new HdfsConfiguration();
37     // disable symlink resolution
38     conf.setBoolean(
39         CommonConfigurationKeys.FS_CLIENT_RESOLVE_REMOTE_SYMLINKS_KEY, false);
40     // spin up minicluster, get dfs and filecontext
41     MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build();
42     DistributedFileSystem dfs = cluster.getFileSystem();
43     FileContext fc = FileContext.getFileContext(cluster.getURI(0), conf);
44     // Create test files/links
45     FileContextTestHelper helper = new FileContextTestHelper(
46         "/tmp/TestSymlinkHdfsDisable");
47     Path root = helper.getTestRootPath(fc);
48     Path target = new Path(root, "target");
49     Path link = new Path(root, "link");
50     DFSTestUtil.createFile(dfs, target, 4096, (short)1, 0xDEADDEAD);
51     fc.createSymlink(target, link, false);
52 
53     // Try to resolve links with FileSystem and FileContext
54     try {
55       fc.open(link);
56       fail("Expected error when attempting to resolve link");
57     } catch (IOException e) {
58       GenericTestUtils.assertExceptionContains("resolution is disabled", e);
59     }
60     try {
61       dfs.open(link);
62       fail("Expected error when attempting to resolve link");
63     } catch (IOException e) {
64       GenericTestUtils.assertExceptionContains("resolution is disabled", e);
65     }
66   }
67 }
68