View Javadoc

1   package org.ogf.saga.permissions;
2   
3   import org.apache.log4j.Logger;
4   import org.junit.Test;
5   import org.ogf.saga.context.Context;
6   import org.ogf.saga.namespace.abstracts.AbstractData;
7   
8   /* ***************************************************
9   * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
10  * ***             http://cc.in2p3.fr/             ***
11  * ***************************************************
12  * File:   PermissionsTest
13  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
14  * Author: lionel.schwarz@in2p3.fr
15  * Date:   5 NOV 2013
16  * ***************************************************
17  * Description:                                      */
18  /**
19   *
20   */
21  public abstract class PermissionsTest extends AbstractData {
22      private static Logger s_logger = Logger.getLogger(PermissionsTest.class);
23  
24      public PermissionsTest(String protocol) throws Exception {
25          super(protocol);
26      }
27  
28      @Test
29      public void test_ownerEXECPermissions() throws Exception {
30      	test_permissions(null, Permission.EXEC);
31      }
32      
33      @Test
34      public void test_ownerREADPermissions() throws Exception {
35      	test_permissions(null, Permission.READ);
36      }
37      
38      @Test
39      public void test_ownerWRITEPermissions() throws Exception {
40      	test_permissions(null, Permission.WRITE);
41      }
42      
43      @Test
44      public void test_groupEXECPermissions() throws Exception {
45      	test_permissions("group-" + m_file.getGroup(), Permission.EXEC);
46      }
47      
48      @Test
49      public void test_groupREADPermissions() throws Exception {
50      	test_permissions("group-" + m_file.getGroup(), Permission.READ);
51      }
52      
53      @Test
54      public void test_groupWRITEPermissions() throws Exception {
55      	test_permissions("group-" + m_file.getGroup(), Permission.WRITE);
56      }
57      
58      @Test
59      public void test_otherEXECPermissions() throws Exception {
60      	test_permissions("*", Permission.EXEC);
61      }
62      
63      @Test
64      public void test_otherREADPermissions() throws Exception {
65      	test_permissions("*", Permission.READ);
66      }
67      
68      @Test
69      public void test_otherWRITEPermissions() throws Exception {
70      	test_permissions("*", Permission.WRITE);
71      }
72      
73      private void test_permissions(String id, Permission permission)throws Exception {
74      	boolean toreset = false;
75      	if(m_file.permissionsCheck(id, permission.getValue())){
76      		m_file.permissionsDeny(id, permission.getValue());
77      		assertFalse(m_file.permissionsCheck(id, permission.getValue()));
78      		toreset = true;
79      	}
80          m_file.permissionsAllow(id, permission.getValue());
81          assertTrue(m_file.permissionsCheck(id, permission.getValue()));
82          if(toreset){
83          	m_file.permissionsDeny(id, permission.getValue());
84      		assertFalse(m_file.permissionsCheck(id, permission.getValue()));
85          }
86      }
87  
88      @Test
89      public void test_getOwner() throws Exception {
90          String owner = m_file.getOwner();
91          if (owner==null || "*".equals(owner)) {
92              fail("Adaptor returned unexpected value: "+owner);
93          }
94          for (Context ctx : m_session.listContexts()) {
95              String userId = ctx.getAttribute(Context.USERID);
96              if (owner.equals(userId)) {
97                  return; //==========> SUCCESS
98              }
99          }
100         fail("Owner not found in security contexts: "+owner);
101     }
102 
103     @Test
104     public void test_getGroup() throws Exception {
105         String group = m_file.getGroup();
106         if (group==null || "*".equals(group)) {
107             fail("Adaptor returned unexpected value: "+group);
108         } else if ("".equals(group)) {
109             s_logger.warn("Method getGroup not supported by adaptor: "+m_fileUrl.getScheme());
110         }
111     }
112 }