Package net.ceidwarfare.coyote.classes

Source Code of net.ceidwarfare.coyote.classes.RconConnectionTest

/*******************************************************************************
* =============================================================================
* Filename:      RconConnectionTest.java
* Project:       Coyote
* Creation Date: 2013-03-13
* Author:        Dimitris Zarras (feugatos) <zarras.dim@gmail.com>
* =============================================================================
* =============================================================================
* Copyright 2013 Dimitris Zarras
*
* 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.
* =============================================================================
* =============================================================================
* Contributors:
*      * Dimitris Zarras (feugatos) <zarras.dim@gmail.com>
* =============================================================================
******************************************************************************/
package net.ceidwarfare.coyote.classes;

import net.ceidwarfare.coyote.exceptions.ConnectionException;
import net.ceidwarfare.coyote.exceptions.PacketReadException;
import net.ceidwarfare.coyote.exceptions.PacketWriteException;
import org.ini4j.Wini;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.naming.LimitExceededException;
import java.io.File;
import java.net.URL;
import java.net.UnknownHostException;

import static org.junit.Assert.assertTrue;

@RunWith(JUnit4.class)
public class RconConnectionTest {

    private RconConnection connection;
    private static String serverIp;
    private static int serverPort;
    private static String serverRconPassword;
    private static boolean readIni;

    @BeforeClass
    public static void readServerParameters() {
        Wini serverSettings;

        URL url = RconPacketTest.class.getResource("/server-settings.ini");

        try {
            serverSettings = new Wini(new File(url.getFile()));
        } catch (Exception e) {
            //if we reach this point then the file server-settings.ini doesn't exist or
            //an error occurred.
            serverIp = null;
            serverPort = 0;
            serverRconPassword = null;
            readIni = false;
            return;
        }

        serverIp = serverSettings.get("server-settings", "serverIp");
        serverPort = serverSettings.get("server-settings", "serverPort", int.class);
        serverRconPassword = serverSettings.get("server-settings", "serverRconPassword");
        readIni = true;

    }

    /**
     * This is just a test to see if the socket connection can actually be established.
     * We do this test against google.com on the port 80 (httpd). This test will fail if
     * there is a problem with the local connection or if google.com is down, which is next
     * to impossible :P
     */
    @Test
    public void socketConnectionTest() throws ConnectionException {
        connection = new RconConnection("google.com", 80);
        connection.connect();
        connection.disconnect();

    }

    @Test(expected = UnknownHostException.class)
    public void invalidHostnameConstructorTest() throws Throwable {
        try {
            connection = new RconConnection("thisis.wrong", 80);
        } catch (ConnectionException e) {
            throw e.getCause();
        }
    }

    @Test(expected = IllegalArgumentException.class)
    public void invalidPortNumberTest() throws Throwable {
        try {
            connection = new RconConnection("google.com", -10);
        } catch (ConnectionException e) {
            throw e.getCause();
        }
    }

    @Test
    public void connectionTest() throws ConnectionException {
        connection = new RconConnection("google.com", 80);
        connection.connect();
        assertTrue(connection.isConnected());
        connection.disconnect();
    }

    @Test
    public void readWriteTest() throws ConnectionException, LimitExceededException, PacketWriteException, PacketReadException {

        connection = new RconConnection(serverIp, serverPort);
        connection.connect();
        connection.authenticate(serverRconPassword);

        connection.sendCommand("status");
        System.out.println(connection.getResponse());

        connection.sendCommand("hostname");
        System.out.println("\"" + connection.getResponse() + "\"");

        connection.disconnect();

    }
}
TOP

Related Classes of net.ceidwarfare.coyote.classes.RconConnectionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.