001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.token;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotEquals;
022
023import java.io.UnsupportedEncodingException;
024import javax.crypto.SecretKey;
025import org.apache.hadoop.hbase.testclassification.SecurityTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030import org.mockito.Mockito;
031
032@Tag(SecurityTests.TAG)
033@Tag(SmallTests.TAG)
034public class TestAuthenticationKey {
035
036  @Test
037  public void test() throws UnsupportedEncodingException {
038    SecretKey secret = Mockito.mock(SecretKey.class);
039    Mockito.when(secret.getEncoded()).thenReturn(Bytes.toBytes("secret"));
040
041    AuthenticationKey key = new AuthenticationKey(0, 1234, secret);
042    assertEquals(key.hashCode(), new AuthenticationKey(0, 1234, secret).hashCode());
043    assertEquals(key, new AuthenticationKey(0, 1234, secret));
044
045    AuthenticationKey otherID = new AuthenticationKey(1, 1234, secret);
046    assertNotEquals(key.hashCode(), otherID.hashCode());
047    assertNotEquals(key, otherID);
048
049    AuthenticationKey otherExpiry = new AuthenticationKey(0, 8765, secret);
050    assertNotEquals(key.hashCode(), otherExpiry.hashCode());
051    assertNotEquals(key, otherExpiry);
052
053    SecretKey other = Mockito.mock(SecretKey.class);
054    Mockito.when(secret.getEncoded()).thenReturn(Bytes.toBytes("other"));
055
056    AuthenticationKey otherSecret = new AuthenticationKey(0, 1234, other);
057    assertNotEquals(key.hashCode(), otherSecret.hashCode());
058    assertNotEquals(key, otherSecret);
059  }
060
061}