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.permission; |
---|
19 | |
---|
20 | import junit.framework.TestCase; |
---|
21 | |
---|
22 | import static org.apache.hadoop.fs.permission.FsAction.*; |
---|
23 | |
---|
24 | public class TestFsPermission extends TestCase { |
---|
25 | public void testFsAction() { |
---|
26 | //implies |
---|
27 | for(FsAction a : FsAction.values()) { |
---|
28 | assertTrue(ALL.implies(a)); |
---|
29 | } |
---|
30 | for(FsAction a : FsAction.values()) { |
---|
31 | assertTrue(a == NONE? NONE.implies(a): !NONE.implies(a)); |
---|
32 | } |
---|
33 | for(FsAction a : FsAction.values()) { |
---|
34 | assertTrue(a == READ_EXECUTE || a == READ || a == EXECUTE || a == NONE? |
---|
35 | READ_EXECUTE.implies(a): !READ_EXECUTE.implies(a)); |
---|
36 | } |
---|
37 | |
---|
38 | //masks |
---|
39 | assertEquals(EXECUTE, EXECUTE.and(READ_EXECUTE)); |
---|
40 | assertEquals(READ, READ.and(READ_EXECUTE)); |
---|
41 | assertEquals(NONE, WRITE.and(READ_EXECUTE)); |
---|
42 | |
---|
43 | assertEquals(READ, READ_EXECUTE.and(READ_WRITE)); |
---|
44 | assertEquals(NONE, READ_EXECUTE.and(WRITE)); |
---|
45 | assertEquals(WRITE_EXECUTE, ALL.and(WRITE_EXECUTE)); |
---|
46 | } |
---|
47 | |
---|
48 | public void testFsPermission() { |
---|
49 | for(short s = 0; s < (1<<9); s++) { |
---|
50 | assertEquals(s, new FsPermission(s).toShort()); |
---|
51 | } |
---|
52 | |
---|
53 | String symbolic = "-rwxrwxrwx"; |
---|
54 | StringBuilder b = new StringBuilder("-123456789"); |
---|
55 | for(int i = 0; i < (1<<9); i++) { |
---|
56 | for(int j = 1; j < 10; j++) { |
---|
57 | b.setCharAt(j, '-'); |
---|
58 | } |
---|
59 | String binary = Integer.toBinaryString(i); |
---|
60 | int len = binary.length(); |
---|
61 | for(int j = 0; j < len; j++) { |
---|
62 | if (binary.charAt(j) == '1') { |
---|
63 | int k = 9 - (len - 1 - j); |
---|
64 | b.setCharAt(k, symbolic.charAt(k)); |
---|
65 | } |
---|
66 | } |
---|
67 | assertEquals(i, FsPermission.valueOf(b.toString()).toShort()); |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|