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, 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.hbase.security.token;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotEquals;
22  
23  import java.io.UnsupportedEncodingException;
24  
25  import javax.crypto.SecretKey;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  import org.mockito.Mockito;
31  
32  @Category(SmallTests.class)
33  public class TestAuthenticationKey {
34  
35    @Test
36    public void test() throws UnsupportedEncodingException {
37      SecretKey secret = Mockito.mock(SecretKey.class);
38      Mockito.when(secret.getEncoded()).thenReturn("secret".getBytes("UTF-8"));
39  
40      AuthenticationKey key = new AuthenticationKey(0, 1234, secret);
41      assertEquals(key.hashCode(), new AuthenticationKey(0, 1234, secret).hashCode());
42      assertEquals(key, new AuthenticationKey(0, 1234, secret));
43      
44      AuthenticationKey otherID = new AuthenticationKey(1, 1234, secret);
45      assertNotEquals(key.hashCode(), otherID.hashCode());
46      assertNotEquals(key, otherID);
47      
48      AuthenticationKey otherExpiry = new AuthenticationKey(0, 8765, secret);
49      assertNotEquals(key.hashCode(), otherExpiry.hashCode());
50      assertNotEquals(key, otherExpiry);
51      
52      SecretKey other = Mockito.mock(SecretKey.class);
53      Mockito.when(secret.getEncoded()).thenReturn("other".getBytes("UTF-8"));
54  
55      AuthenticationKey otherSecret = new AuthenticationKey(0, 1234, other);
56      assertNotEquals(key.hashCode(), otherSecret.hashCode());
57      assertNotEquals(key, otherSecret);
58    }
59  
60  }