1/*
2 * Copyright 2007 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.stub.command;
17
18import java.net.InetAddress;
19import java.net.UnknownHostException;
20
21
22import org.apache.log4j.Logger;
23import org.mockftpserver.core.command.Command;
24import org.mockftpserver.core.command.CommandNames;
25import org.mockftpserver.core.command.ReplyCodes;
26import org.mockftpserver.stub.command.PasvCommandHandler;
27
28/**
29 * Tests for the PasvCommandHandler class
30 *
31 * @version $Revision$ - $Date$
32 *
33 * @author Chris Mair
34 */
35public final class PasvCommandHandlerTest extends AbstractCommandHandlerTest {
36
37    private static final Logger LOG = Logger.getLogger(PasvCommandHandlerTest.class);
38    private static final int PORT = (23 << 8) + 77;
39
40    private PasvCommandHandler commandHandler;
41
42    /**
43     * Test the handleCommand() method
44     */
45    public void testHandleCommand() throws Exception {
46
47        final InetAddress SERVER = inetAddress("192.168.0.2");
48        session.switchToPassiveMode();
49        control(session).setReturnValue(PORT);
50        session.getServerHost();
51        control(session).setReturnValue(SERVER);
52        session.sendReply(ReplyCodes.PASV_OK, formattedReplyTextFor(227, "(192,168,0,2,23,77)"));
53        replay(session);
54
55        final Command COMMAND = new Command(CommandNames.PASV, EMPTY);
56
57        commandHandler.handleCommand(COMMAND, session);
58        verify(session);
59
60        verifyNumberOfInvocations(commandHandler, 1);
61        verifyNoDataElements(commandHandler.getInvocation(0));
62    }
63
64    /**
65     * Test convertHostAndPortToStringOfBytes() method
66     */
67    public void testConvertHostAndPortToStringOfBytes() throws UnknownHostException {
68        InetAddress host = InetAddress.getByName("196.168.44.55");
69        String result = PasvCommandHandler.convertHostAndPortToStringOfBytes(host, PORT);
70        LOG.info("result=" + result);
71        assertEquals("result", "196,168,44,55,23,77", result);
72    }
73
74    /**
75     * Perform initialization before each test
76     * @see org.mockftpserver.stub.command.AbstractCommandHandlerTest#setUp()
77     */
78    protected void setUp() throws Exception {
79        super.setUp();
80        commandHandler = new PasvCommandHandler();
81        commandHandler.setReplyTextBundle(replyTextBundle);
82    }
83
84}
85