/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.hadoop.hdfs.security; import java.util.EnumSet; import org.apache.hadoop.io.TestWritable; import junit.framework.TestCase; /** Unit tests for access tokens */ public class TestAccessToken extends TestCase { long accessKeyUpdateInterval = 10 * 60 * 1000; // 10 mins long accessTokenLifetime = 2 * 60 * 1000; // 2 mins long blockID1 = 0L; long blockID2 = 10L; long blockID3 = -108L; /** test Writable */ public void testWritable() throws Exception { TestWritable.testWritable(ExportedAccessKeys.DUMMY_KEYS); AccessTokenHandler handler = new AccessTokenHandler(true, accessKeyUpdateInterval, accessTokenLifetime); ExportedAccessKeys keys = handler.exportKeys(); TestWritable.testWritable(keys); TestWritable.testWritable(BlockAccessToken.DUMMY_TOKEN); BlockAccessToken token = handler.generateToken(blockID3, EnumSet .allOf(AccessTokenHandler.AccessMode.class)); TestWritable.testWritable(token); } private void tokenGenerationAndVerification(AccessTokenHandler master, AccessTokenHandler slave) throws Exception { // single-mode tokens for (AccessTokenHandler.AccessMode mode : AccessTokenHandler.AccessMode .values()) { // generated by master BlockAccessToken token1 = master.generateToken(blockID1, EnumSet.of(mode)); assertTrue(master.checkAccess(token1, null, blockID1, mode)); assertTrue(slave.checkAccess(token1, null, blockID1, mode)); // generated by slave BlockAccessToken token2 = slave.generateToken(blockID2, EnumSet.of(mode)); assertTrue(master.checkAccess(token2, null, blockID2, mode)); assertTrue(slave.checkAccess(token2, null, blockID2, mode)); } // multi-mode tokens BlockAccessToken mtoken = master.generateToken(blockID3, EnumSet .allOf(AccessTokenHandler.AccessMode.class)); for (AccessTokenHandler.AccessMode mode : AccessTokenHandler.AccessMode .values()) { assertTrue(master.checkAccess(mtoken, null, blockID3, mode)); assertTrue(slave.checkAccess(mtoken, null, blockID3, mode)); } } /** test access key and token handling */ public void testAccessTokenHandler() throws Exception { AccessTokenHandler masterHandler = new AccessTokenHandler(true, accessKeyUpdateInterval, accessTokenLifetime); AccessTokenHandler slaveHandler = new AccessTokenHandler(false, accessKeyUpdateInterval, accessTokenLifetime); ExportedAccessKeys keys = masterHandler.exportKeys(); slaveHandler.setKeys(keys); tokenGenerationAndVerification(masterHandler, slaveHandler); // key updating masterHandler.updateKeys(); tokenGenerationAndVerification(masterHandler, slaveHandler); keys = masterHandler.exportKeys(); slaveHandler.setKeys(keys); tokenGenerationAndVerification(masterHandler, slaveHandler); } }