summaryrefslogtreecommitdiff
path: root/tags/2.3/src/test/groovy/org/mockftpserver/core/util/PortParserTest.groovy
blob: e98aaa239ff47daa4c2c3d21fdeddf440b840787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright 2009 the original author or authors.
 * 
 * Licensed 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.mockftpserver.core.util

import org.apache.log4j.Logger
import org.mockftpserver.core.CommandSyntaxException
import org.mockftpserver.test.AbstractGroovyTestCase

/**
 * Tests for the PortParser class
 *
 * @author Chris Mair
 * @version $Revision$ - $Date$
 */
class PortParserTest extends AbstractGroovyTestCase {

    static final Logger LOG = Logger.getLogger(PortParserTest.class)
    static final String[] PARAMETERS = ["192", "22", "250", "44", "1", "206"]
    static final String[] PARAMETERS_INSUFFICIENT = ["7", "29", "99", "11", "77"]
    static final int PORT = (1 << 8) + 206
    static final InetAddress HOST = inetAddress("192.22.250.44")

    static final PARAMETER_IPV4 = "|1|132.235.1.2|6275|"
    static final HOST_IPV4 = InetAddress.getByName("132.235.1.2")
    static final PARAMETER_IPV6 = "|2|1080::8:800:200C:417A|6275|"
    static final HOST_IPV6 = InetAddress.getByName("1080::8:800:200C:417A")
    static final E_PORT = 6275

    void testParseExtendedAddressHostAndPort_IPv4() {
        def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV4)
        assert client.host == HOST_IPV4
        assert client.port == E_PORT
    }

    void testParseExtendedAddressHostAndPort_IPv6() {
        def client = PortParser.parseExtendedAddressHostAndPort(PARAMETER_IPV6)
        assert client.host == HOST_IPV6
        assert client.port == E_PORT
    }

    void testParseExtendedAddressHostAndPort_IPv6_CustomDelimiter() {
        def client = PortParser.parseExtendedAddressHostAndPort("@2@1080::8:800:200C:417A@6275@")
        assert client.host == HOST_IPV6
        assert client.port == E_PORT
    }

    void testParseExtendedAddressHostAndPort_IllegalParameterFormat() {
        final PARM = 'abcdef'
        shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
    }

    void testParseExtendedAddressHostAndPort_PortMissing() {
        final PARM = '|1|132.235.1.2|'
        shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
    }

    void testParseExtendedAddressHostAndPort_IllegalHostName() {
        final PARM = '|1|132.xxx.rrr|6275|'
        shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(PARM) }
    }

    void testParseExtendedAddressHostAndPort_Null() {
        shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort(null) }
    }

    void testParseExtendedAddressHostAndPort_Empty() {
        shouldFail(CommandSyntaxException) { PortParser.parseExtendedAddressHostAndPort('') }
    }

    void testParseHostAndPort() {
        def client = PortParser.parseHostAndPort(PARAMETERS)
        assert client.host == HOST
        assert client.port == PORT
    }

    void testParseHostAndPort_Null() {
        shouldFail(CommandSyntaxException) { PortParser.parseHostAndPort(null) }
    }

    void testParseHostAndPort_InsufficientParameters() throws UnknownHostException {
        shouldFail(CommandSyntaxException) { PortParser.parseHostAndPort(PARAMETERS_INSUFFICIENT) }
    }

    void testConvertHostAndPortToStringOfBytes() {
        int port = (23 << 8) + 77
        InetAddress host = InetAddress.getByName("196.168.44.55")
        String result = PortParser.convertHostAndPortToCommaDelimitedBytes(host, port)
        LOG.info("result=" + result)
        assertEquals("result", "196,168,44,55,23,77", result)
    }

}