PortParserTest.groovy revision 3e469b93fd10bc09ea2c088516168bf6a5cbaa43
1/* 2 * Copyright 2009 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.mockftpserver.core.util 17 18import org.apache.log4j.Logger 19import org.mockftpserver.core.CommandSyntaxException 20import org.mockftpserver.test.AbstractGroovyTest 21 22/** 23 * Tests for the PortParser class 24 * 25 * @author Chris Mair 26 * @version $Revision$ - $Date$ 27 */ 28class PortParserTest extends AbstractGroovyTest { 29 30 static final Logger LOG = Logger.getLogger(PortParserTest.class) 31 static final String[] PARAMETERS = ["192", "22", "250", "44", "1", "206"] 32 static final String[] PARAMETERS_INSUFFICIENT = ["7", "29", "99", "11", "77"] 33 static final int PORT = (1 << 8) + 206 34 static final InetAddress HOST = inetAddress("192.22.250.44") 35 36 static final PARAMETER_IPV4 = "|1|132.235.1.2|6275|" 37 static final HOST_IPV4 = InetAddress.getByName("132.235.1.2") 38 static final PARAMETER_IPV6 = "|2|1080::8:800:200C:417A|6275|" 39 static final HOST_IPV6 = InetAddress.getByName("1080::8:800:200C:417A") 40 static final E_PORT = 6275 41 42 void testParseExtendedAddressHostAndPort_IPv4() { 43 def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV4) 44 assert client.host == HOST_IPV4 45 assert client.port == E_PORT 46 } 47 48 void testParseExtendedAddressHostAndPort_IPv6() { 49 def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV6) 50 assert client.host == HOST_IPV6 51 assert client.port == E_PORT 52 } 53 54 void testParseExtendedAddressHostAndPort_IPv6_CustomDelimiter() { 55 def client = PortParser.parseExtendedAddressHostAndPort("@2@1080::8:800:200C:417A@6275@") 56 assert client.host == HOST_IPV6 57 assert client.port == E_PORT 58 } 59 60 void testParseExtendedAddressHostAndPort_IllegalParameterFormat() { 61 final PARM = 'abcdef' 62 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) } 63 } 64 65 void testParseExtendedAddressHostAndPort_PortMissing() { 66 final PARM = '|1|132.235.1.2|' 67 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) } 68 } 69 70 void testParseExtendedAddressHostAndPort_IllegalHostName() { 71 final PARM = '|1|132.xxx.rrr|6275|' 72 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) } 73 } 74 75 void testParseExtendedAddressHostAndPort_Null() { 76 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(null) } 77 } 78 79 void testParseExtendedAddressHostAndPort_Empty() { 80 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort('') } 81 } 82 83 void testParseHost() { 84 InetAddress host = PortParser.parseHost(PARAMETERS) 85 assertEquals("InetAddress", HOST, host) 86 } 87 88 void testParsePortNumber() { 89 int portNumber = PortParser.parsePortNumber(PARAMETERS) 90 assertEquals("portNumber", PORT, portNumber) 91 } 92 93 void testParseHost_Null() { 94 shouldFail(CommandSyntaxException) { PortParser.parseHost(null) } 95 } 96 97 void testParseHost_InsufficientParameters() throws UnknownHostException { 98 shouldFail(CommandSyntaxException) { PortParser.parseHost(PARAMETERS_INSUFFICIENT) } 99 } 100 101 void testParsePortNumber_Null() { 102 shouldFail(CommandSyntaxException) { PortParser.parsePortNumber(null) } 103 } 104 105 void testParsePortNumber_InsufficientParameters() { 106 shouldFail(CommandSyntaxException) { PortParser.parsePortNumber(PARAMETERS_INSUFFICIENT) } 107 } 108 109 void testConvertHostAndPortToStringOfBytes() { 110 int port = (23 << 8) + 77 111 InetAddress host = InetAddress.getByName("196.168.44.55") 112 String result = PortParser.convertHostAndPortToCommaDelimitedBytes(host, port) 113 LOG.info("result=" + result) 114 assertEquals("result", "196,168,44,55,23,77", result) 115 } 116 117}