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 org.apache.hadoop.conf.Configuration; |
---|
21 | import org.apache.hadoop.fs.permission.*; |
---|
22 | import org.apache.hadoop.util.StringUtils; |
---|
23 | import org.apache.hadoop.util.Shell; |
---|
24 | |
---|
25 | import java.io.*; |
---|
26 | import java.util.*; |
---|
27 | |
---|
28 | import junit.framework.*; |
---|
29 | |
---|
30 | /** |
---|
31 | * This class tests the local file system via the FileSystem abstraction. |
---|
32 | */ |
---|
33 | public class TestLocalFileSystemPermission extends TestCase { |
---|
34 | static final String TEST_PATH_PREFIX = new Path(System.getProperty( |
---|
35 | "test.build.data", "/tmp")).toString().replace(' ', '_') |
---|
36 | + "/" + TestLocalFileSystemPermission.class.getSimpleName() + "_"; |
---|
37 | |
---|
38 | { |
---|
39 | try { |
---|
40 | ((org.apache.commons.logging.impl.Log4JLogger)FileSystem.LOG).getLogger() |
---|
41 | .setLevel(org.apache.log4j.Level.DEBUG); |
---|
42 | } |
---|
43 | catch(Exception e) { |
---|
44 | System.out.println("Cannot change log level\n" |
---|
45 | + StringUtils.stringifyException(e)); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | private Path writeFile(FileSystem fs, String name) throws IOException { |
---|
50 | Path f = new Path(TEST_PATH_PREFIX + name); |
---|
51 | FSDataOutputStream stm = fs.create(f); |
---|
52 | stm.writeBytes("42\n"); |
---|
53 | stm.close(); |
---|
54 | return f; |
---|
55 | } |
---|
56 | |
---|
57 | private void cleanupFile(FileSystem fs, Path name) throws IOException { |
---|
58 | assertTrue(fs.exists(name)); |
---|
59 | fs.delete(name, true); |
---|
60 | assertTrue(!fs.exists(name)); |
---|
61 | } |
---|
62 | |
---|
63 | /** Test LocalFileSystem.setPermission */ |
---|
64 | public void testLocalFSsetPermission() throws IOException { |
---|
65 | if (Path.WINDOWS) { |
---|
66 | System.out.println("Cannot run test for Windows"); |
---|
67 | return; |
---|
68 | } |
---|
69 | Configuration conf = new Configuration(); |
---|
70 | LocalFileSystem localfs = FileSystem.getLocal(conf); |
---|
71 | String filename = "foo"; |
---|
72 | Path f = writeFile(localfs, filename); |
---|
73 | try { |
---|
74 | System.out.println(filename + ": " + getPermission(localfs, f)); |
---|
75 | } |
---|
76 | catch(Exception e) { |
---|
77 | System.out.println(StringUtils.stringifyException(e)); |
---|
78 | System.out.println("Cannot run test"); |
---|
79 | return; |
---|
80 | } |
---|
81 | |
---|
82 | try { |
---|
83 | // create files and manipulate them. |
---|
84 | FsPermission all = new FsPermission((short)0777); |
---|
85 | FsPermission none = new FsPermission((short)0); |
---|
86 | |
---|
87 | localfs.setPermission(f, none); |
---|
88 | assertEquals(none, getPermission(localfs, f)); |
---|
89 | |
---|
90 | localfs.setPermission(f, all); |
---|
91 | assertEquals(all, getPermission(localfs, f)); |
---|
92 | } |
---|
93 | finally {cleanupFile(localfs, f);} |
---|
94 | } |
---|
95 | |
---|
96 | FsPermission getPermission(LocalFileSystem fs, Path p) throws IOException { |
---|
97 | return fs.getFileStatus(p).getPermission(); |
---|
98 | } |
---|
99 | |
---|
100 | /** Test LocalFileSystem.setOwner */ |
---|
101 | public void testLocalFSsetOwner() throws IOException { |
---|
102 | if (Path.WINDOWS) { |
---|
103 | System.out.println("Cannot run test for Windows"); |
---|
104 | return; |
---|
105 | } |
---|
106 | |
---|
107 | Configuration conf = new Configuration(); |
---|
108 | LocalFileSystem localfs = FileSystem.getLocal(conf); |
---|
109 | String filename = "bar"; |
---|
110 | Path f = writeFile(localfs, filename); |
---|
111 | List<String> groups = null; |
---|
112 | try { |
---|
113 | groups = getGroups(); |
---|
114 | System.out.println(filename + ": " + getPermission(localfs, f)); |
---|
115 | } |
---|
116 | catch(IOException e) { |
---|
117 | System.out.println(StringUtils.stringifyException(e)); |
---|
118 | System.out.println("Cannot run test"); |
---|
119 | return; |
---|
120 | } |
---|
121 | if (groups == null || groups.size() < 1) { |
---|
122 | System.out.println("Cannot run test: need at least one group. groups=" |
---|
123 | + groups); |
---|
124 | return; |
---|
125 | } |
---|
126 | |
---|
127 | // create files and manipulate them. |
---|
128 | try { |
---|
129 | String g0 = groups.get(0); |
---|
130 | localfs.setOwner(f, null, g0); |
---|
131 | assertEquals(g0, getGroup(localfs, f)); |
---|
132 | |
---|
133 | if (groups.size() > 1) { |
---|
134 | String g1 = groups.get(1); |
---|
135 | localfs.setOwner(f, null, g1); |
---|
136 | assertEquals(g1, getGroup(localfs, f)); |
---|
137 | } else { |
---|
138 | System.out.println("Not testing changing the group since user " + |
---|
139 | "belongs to only one group."); |
---|
140 | } |
---|
141 | } |
---|
142 | finally {cleanupFile(localfs, f);} |
---|
143 | } |
---|
144 | |
---|
145 | static List<String> getGroups() throws IOException { |
---|
146 | List<String> a = new ArrayList<String>(); |
---|
147 | String s = Shell.execCommand(Shell.getGROUPS_COMMAND()); |
---|
148 | for(StringTokenizer t = new StringTokenizer(s); t.hasMoreTokens(); ) { |
---|
149 | a.add(t.nextToken()); |
---|
150 | } |
---|
151 | return a; |
---|
152 | } |
---|
153 | |
---|
154 | String getGroup(LocalFileSystem fs, Path p) throws IOException { |
---|
155 | return fs.getFileStatus(p).getGroup(); |
---|
156 | } |
---|
157 | } |
---|