View Javadoc
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.mgt;
20  
21  import org.apache.shiro.subject.PrincipalCollection;
22  import org.apache.shiro.subject.Subject;
23  import org.apache.shiro.subject.SubjectContext;
24  import org.apache.shiro.subject.support.DefaultSubjectContext;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertNull;
28  
29  /**
30   * Test cases for the {@link AbstractRememberMeManager} implementation.
31   */
32  public class AbstractRememberMeManagerTest {
33  
34      /**
35       * Tests the {@link AbstractRememberMeManager#getRememberedPrincipals(SubjectContext)} method
36       * implementation when the internal
37       * {@link AbstractRememberMeManager#getRememberedSerializedIdentity(SubjectContext)} method
38       * returns null or empty bytes.
39       */
40      @Test
41      public void testGetRememberedPrincipalsWithEmptySerializedBytes() {
42          AbstractRememberMeManager rmm = new DummyRememberMeManager();
43          //Since the dummy's getRememberedSerializedIdentity implementation returns an empty byte
44          //array, we should be ok:
45          PrincipalCollection principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
46          assertNull(principals);
47  
48          //try with a null return value too:
49          rmm = new DummyRememberMeManager() {
50              @Override
51              protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) {
52                  return null;
53              }
54          };
55          principals = rmm.getRememberedPrincipals(new DefaultSubjectContext());
56          assertNull(principals);
57      }
58  
59      private static class DummyRememberMeManager extends AbstractRememberMeManager {
60          public void forgetIdentity(SubjectContext subjectContext) {
61              //do nothing
62          }
63  
64          @Override
65          protected void forgetIdentity(Subject subject) {
66          }
67  
68          @Override
69          protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {
70          }
71  
72          @Override
73          protected byte[] getRememberedSerializedIdentity(SubjectContext subjectContext) {
74              return new byte[0];
75          }
76      }
77  }