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.hdfs.server.datanode.web.webhdfs; 19 20 import org.apache.hadoop.conf.Configuration; 21 import org.apache.hadoop.hdfs.DFSTestUtil; 22 import org.apache.hadoop.hdfs.HAUtil; 23 import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier; 24 import org.apache.hadoop.hdfs.web.resources.DelegationParam; 25 import org.apache.hadoop.hdfs.web.resources.NamenodeAddressParam; 26 import org.apache.hadoop.hdfs.web.resources.OffsetParam; 27 import org.apache.hadoop.security.token.Token; 28 import org.junit.Assert; 29 import org.junit.Test; 30 31 import io.netty.handler.codec.http.QueryStringDecoder; 32 33 import javax.servlet.ServletContext; 34 35 import java.io.IOException; 36 37 import static org.mockito.Mockito.doReturn; 38 import static org.mockito.Mockito.mock; 39 40 public class TestParameterParser { 41 private static final String LOGICAL_NAME = "minidfs"; 42 43 @Test testDeserializeHAToken()44 public void testDeserializeHAToken() throws IOException { 45 Configuration conf = DFSTestUtil.newHAConfiguration(LOGICAL_NAME); 46 final Token<DelegationTokenIdentifier> token = new 47 Token<DelegationTokenIdentifier>(); 48 QueryStringDecoder decoder = new QueryStringDecoder( 49 WebHdfsHandler.WEBHDFS_PREFIX + "/?" 50 + NamenodeAddressParam.NAME + "=" + LOGICAL_NAME + "&" 51 + DelegationParam.NAME + "=" + token.encodeToUrlString()); 52 ParameterParser testParser = new ParameterParser(decoder, conf); 53 final Token<DelegationTokenIdentifier> tok2 = testParser.delegationToken(); 54 Assert.assertTrue(HAUtil.isTokenForLogicalUri(tok2)); 55 } 56 57 @Test testDecodePath()58 public void testDecodePath() { 59 final String ESCAPED_PATH = "/test%25+1%26%3Dtest?op=OPEN&foo=bar"; 60 final String EXPECTED_PATH = "/test%+1&=test"; 61 62 Configuration conf = new Configuration(); 63 QueryStringDecoder decoder = new QueryStringDecoder( 64 WebHdfsHandler.WEBHDFS_PREFIX + ESCAPED_PATH); 65 ParameterParser testParser = new ParameterParser(decoder, conf); 66 Assert.assertEquals(EXPECTED_PATH, testParser.path()); 67 } 68 69 @Test testOffset()70 public void testOffset() throws IOException { 71 final long X = 42; 72 73 long offset = new OffsetParam(Long.toString(X)).getOffset(); 74 Assert.assertEquals("OffsetParam: ", X, offset); 75 76 offset = new OffsetParam((String) null).getOffset(); 77 Assert.assertEquals("OffsetParam with null should have defaulted to 0", 0, offset); 78 79 try { 80 offset = new OffsetParam("abc").getValue(); 81 Assert.fail("OffsetParam with nondigit value should have thrown IllegalArgumentException"); 82 } catch (IllegalArgumentException iae) { 83 // Ignore 84 } 85 } 86 } 87