blob: 1bf5ed88f91a425c8526210a6719d33b61bbd36b [file] [log] [blame]
chrismair00dc7bd2014-05-11 21:21:28 +00001/*
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.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20import org.mockftpserver.core.CommandSyntaxException
21import org.mockftpserver.test.AbstractGroovyTestCase
22
23/**
24 * Tests for the PortParser class
25 *
26 * @author Chris Mair
27 * @version $Revision$ - $Date$
28 */
29class PortParserTest extends AbstractGroovyTestCase {
30
31 static final Logger LOG = LoggerFactory.getLogger(PortParserTest.class)
32 static final String[] PARAMETERS = ["192", "22", "250", "44", "1", "206"]
33 static final String[] PARAMETERS_INSUFFICIENT = ["7", "29", "99", "11", "77"]
34 static final int PORT = (1 << 8) + 206
35 static final InetAddress HOST = inetAddress("192.22.250.44")
36
37 static final PARAMETER_IPV4 = "|1|132.235.1.2|6275|"
38 static final HOST_IPV4 = InetAddress.getByName("132.235.1.2")
39 static final PARAMETER_IPV6 = "|2|1080::8:800:200C:417A|6275|"
40 static final HOST_IPV6 = InetAddress.getByName("1080::8:800:200C:417A")
41 static final E_PORT = 6275
42
43 void testParseExtendedAddressHostAndPort_IPv4() {
44 def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV4)
45 assert client.host == HOST_IPV4
46 assert client.port == E_PORT
47 }
48
49 void testParseExtendedAddressHostAndPort_IPv6() {
50 def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV6)
51 assert client.host == HOST_IPV6
52 assert client.port == E_PORT
53 }
54
55 void testParseExtendedAddressHostAndPort_IPv6_CustomDelimiter() {
56 def client = PortParser.parseExtendedAddressHostAndPort("@2@1080::8:800:200C:417A@6275@")
57 assert client.host == HOST_IPV6
58 assert client.port == E_PORT
59 }
60
61 void testParseExtendedAddressHostAndPort_IllegalParameterFormat() {
62 final PARM = 'abcdef'
63 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
64 }
65
66 void testParseExtendedAddressHostAndPort_PortMissing() {
67 final PARM = '|1|132.235.1.2|'
68 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
69 }
70
71 void testParseExtendedAddressHostAndPort_IllegalHostName() {
72 final PARM = '|1|132.@|6275|'
73 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
74 }
75
76 void testParseExtendedAddressHostAndPort_Null() {
77 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(null) }
78 }
79
80 void testParseExtendedAddressHostAndPort_Empty() {
81 shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort('') }
82 }
83
84 void testParseHostAndPort() {
85 def client = PortParser.parseHostAndPort(PARAMETERS)
86 assert client.host == HOST
87 assert client.port == PORT
88 }
89
90 void testParseHostAndPort_Null() {
91 shouldFail(CommandSyntaxException) { PortParser.parseHostAndPort(null) }
92 }
93
94 void testParseHostAndPort_InsufficientParameters() throws UnknownHostException {
95 shouldFail(CommandSyntaxException) { PortParser.parseHostAndPort(PARAMETERS_INSUFFICIENT) }
96 }
97
98 void testConvertHostAndPortToStringOfBytes() {
99 int port = (23 << 8) + 77
100 InetAddress host = InetAddress.getByName("196.168.44.55")
101 String result = PortParser.convertHostAndPortToCommaDelimitedBytes(host, port)
102 LOG.info("result=" + result)
103 assertEquals("result", "196,168,44,55,23,77", result)
104 }
105
106}