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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.spring.remoting;
20  
21  import org.aopalliance.intercept.MethodInvocation;
22  import org.apache.shiro.session.mgt.DefaultSessionKey;
23  import org.apache.shiro.session.mgt.SessionKey;
24  import org.apache.shiro.session.mgt.SessionManager;
25  import org.apache.shiro.util.ThreadContext;
26  import org.junit.After;
27  import org.junit.Before;
28  import org.junit.Test;
29  import org.springframework.remoting.support.RemoteInvocation;
30  
31  import java.lang.reflect.Method;
32  import java.util.UUID;
33  
34  import static org.easymock.EasyMock.*;
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNull;
37  
38  /**
39   * //TODO - Class JavaDoc!
40   *
41   */
42  public class SecureRemoteInvocationFactoryTest {
43  
44      @Before
45      public void setup() {
46          ThreadContext.remove();
47      }
48  
49      @After
50      public void tearDown() {
51          ThreadContext.remove();
52      }
53  
54      protected Method getMethod(String name, Class clazz) {
55          Method[] methods = clazz.getMethods();
56          for (Method method : methods) {
57              if (method.getName().equals(name)) {
58                  return method;
59              }
60          }
61          throw new IllegalStateException("'" + name + "' method should exist.");
62      }
63  
64      @Test
65      public void testSessionManagerProxyStartRemoteInvocation() throws Exception {
66  
67          SecureRemoteInvocationFactory factory = new SecureRemoteInvocationFactory();
68  
69          MethodInvocation mi = createMock(MethodInvocation.class);
70          Method startMethod = getMethod("start", SessionManager.class);
71          expect(mi.getMethod()).andReturn(startMethod).anyTimes();
72  
73          Object[] args = {"localhost"};
74          expect(mi.getArguments()).andReturn(args).anyTimes();
75  
76          replay(mi);
77  
78          RemoteInvocation ri = factory.createRemoteInvocation(mi);
79  
80          verify(mi);
81  
82          assertNull(ri.getAttribute(SecureRemoteInvocationFactory.SESSION_ID_KEY));
83      }
84  
85      @Test
86      public void testSessionManagerProxyNonStartRemoteInvocation() throws Exception {
87  
88          SecureRemoteInvocationFactory factory = new SecureRemoteInvocationFactory();
89  
90          MethodInvocation mi = createMock(MethodInvocation.class);
91          Method method = getMethod("getSession", SessionManager.class);
92          expect(mi.getMethod()).andReturn(method).anyTimes();
93  
94          String dummySessionId = UUID.randomUUID().toString();
95          SessionKey sessionKey = new DefaultSessionKey(dummySessionId);
96          Object[] args = {sessionKey};
97          expect(mi.getArguments()).andReturn(args).anyTimes();
98  
99          replay(mi);
100 
101         RemoteInvocation ri = factory.createRemoteInvocation(mi);
102 
103         verify(mi);
104 
105         assertEquals(dummySessionId, ri.getAttribute(SecureRemoteInvocationFactory.SESSION_ID_KEY));
106     }
107 
108     /*@Test
109     public void testNonSessionManagerCall() throws Exception {
110 
111         SecureRemoteInvocationFactory factory = new SecureRemoteInvocationFactory();
112 
113         MethodInvocation mi = createMock(MethodInvocation.class);
114         Method method = getMethod("login", SecurityManager.class);
115         expect(mi.getMethod()).andReturn(method).anyTimes();
116     }*/
117 
118 }