<commit_before>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "networkgateway.h"
#include "debug.h"
#include "log_console.h"

using namespace pelagicore;

class MockController :
    public ControllerAbstractInterface
{
public:

    virtual bool startApp()
    {
            return true;
    }

    virtual bool shutdown()
    {
        return true;
    }

    virtual bool setEnvironmentVariable(const std::string &variable,
        const std::string &value)
    {
        return true;
    }

    MOCK_METHOD1(systemCall,
        bool(const std::string &cmd));
};


class SystemcallInterfaceStub :
    public SystemcallAbstractInterface
{
public:

    virtual bool makeCall(const std::string &cmd)
    {
	return true;
    }

    virtual bool makeCall(const std::string &cmd, int &exitCode)
    {
	exitCode = 0;
	return true;
    }

};

using ::testing::InSequence;
using ::testing::_;
using ::testing::Return;
using ::testing::NiceMock;


/*! Test NetworkGateway calls ControllerInterface::systemCall when
 * NetworkGateway::setConfig() has been called
 *
 * The NetworkGateway::setConfig() should try to issue a ifconfig
 * system call inside the container in order to set the containers IP
 * address.
 */
TEST(NetworkGatewayTest, TestSetConfig) {

    std::string config = "{\"internet-access\": \"false\", \"gateway\":\"\"}";
    /* Nice mock, i.e. don't warn about uninteresting calls on this mock */
    NiceMock<MockController> controllerInterface;
    SystemcallInterfaceStub systemCallInterface;
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    bool success = gw.setConfig(config);
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful.
 */
TEST(NetworkGatewayTest, TestActivate) {
    std::string config = "{\"internet-access\": \"true\", \"gateway\":\"10.0.3.1\"}";
    NiceMock<MockController> controllerInterface;
    SystemcallInterfaceStub systemCallInterface;
    NetworkGateway gw(&controllerInterface, &systemCallInterface);
    ASSERT_TRUE(gw.setConfig(config));

    std::string ip = gw.ip();

    std::string cmd_1 = "ifconfig eth0 " + ip + " netmask 255.255.255.0 up";
    std::string cmd_2 = "route add default gw 10.0.3.1";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful.
 */
TEST(NetworkGatewayTest, TestActivateTwice) {
    std::string config = "{\"internet-access\": \"true\", \"gateway\":\"10.0.3.1\"}";
    NiceMock<MockController> controllerInterface;
    SystemcallInterfaceStub systemCallInterface;
    NetworkGateway gw(&controllerInterface, &systemCallInterface);
    ASSERT_TRUE(gw.setConfig(config));

    std::string ip = gw.ip();

    std::string cmd_1 = "ifconfig eth0 " + ip + " netmask 255.255.255.0 up";
    std::string cmd_2 = "route add default gw 10.0.3.1";
    std::string cmd_3 = "ifconfig eth0 up";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_3)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    success = gw.activate();
    ASSERT_TRUE(success);

}

/*! Test NetworkGateway::activate is successful.
 */
TEST(NetworkGatewayTest, TestActivateNoConfig) {
    NiceMock<MockController> controllerInterface;
    SystemcallInterfaceStub systemCallInterface;
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    std::string cmd_1 = "ifconfig eth0 down";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful.
 */
TEST(NetworkGatewayTest, TestActivateBadConfig) {
    std::string config = "{\"internet-access\": \"true\"}";
    NiceMock<MockController> controllerInterface;
    SystemcallInterfaceStub systemCallInterface;
    NetworkGateway gw(&controllerInterface, &systemCallInterface);
    ASSERT_TRUE(gw.setConfig(config));

    std::string cmd_1 = "ifconfig eth0 down";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}<commit_msg>networkgateway_unittest.cpp: Added tests and cleaned up a bit<commit_after>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "networkgateway.h"
#include "debug.h"
#include "log_console.h"

using namespace pelagicore;

class MockController :
    public ControllerAbstractInterface
{
public:

    virtual bool startApp()
    {
            return true;
    }

    virtual bool shutdown()
    {
        return true;
    }

    virtual bool setEnvironmentVariable(const std::string &variable,
        const std::string &value)
    {
        return true;
    }

    MOCK_METHOD1(systemCall,
        bool(const std::string &cmd));
};


class MockSystemcallInterface :
    public SystemcallAbstractInterface
{
public:
    virtual bool makeCall(const std::string &cmd, int &exitCode)
    {
	exitCode = 0;
	return true;
    }

    MOCK_METHOD1(makeCall, bool(const std::string &cmd));

};

using ::testing::DefaultValue;
using ::testing::InSequence;
using ::testing::_;
using ::testing::Return;
using ::testing::NiceMock;

class NetworkGatewayTest :
    public ::testing::Test
{
protected:
    virtual void SetUp()
    {
	DefaultValue<bool>::Set(true);
    }

    virtual void TearDown()
    {
	using ::testing::DefaultValue;
	DefaultValue<bool>::Clear();
    }

    NiceMock<MockController> controllerInterface;
    NiceMock<MockSystemcallInterface> systemCallInterface;
};

/*! Test NetworkGateway calls ControllerInterface::systemCall when
 * NetworkGateway::setConfig() has been called
 *
 * The NetworkGateway::setConfig() should try to issue a ifconfig
 * system call inside the container in order to set the containers IP
 * address.
 */
TEST_F(NetworkGatewayTest, TestSetConfig) {

    std::string config = "{\"internet-access\": \"false\", \"gateway\":\"\"}";
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    bool success = gw.setConfig(config);
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful.
 */
TEST_F(NetworkGatewayTest, TestActivate) {
    std::string config = "{\"internet-access\": \"true\", \"gateway\":\"10.0.3.1\"}";
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    ASSERT_TRUE(gw.setConfig(config));

    std::string ip = gw.ip();

    std::string cmd_0 = "ifconfig | grep -C 2 \"container-br0\" | grep -q \"10.0.3.1\"";
    std::string cmd_1 = "ifconfig eth0 " + ip + " netmask 255.255.255.0 up";
    std::string cmd_2 = "route add default gw 10.0.3.1";

    {
	InSequence sequence;
	EXPECT_CALL(systemCallInterface, makeCall(cmd_0)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful and that correct command is
 *  issued the second time it is called.
 */
TEST_F(NetworkGatewayTest, TestActivateTwice) {
    std::string config = "{\"internet-access\": \"true\", \"gateway\":\"10.0.3.1\"}";
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    ASSERT_TRUE(gw.setConfig(config));

    std::string ip = gw.ip();

    std::string cmd_0 = "ifconfig | grep -C 2 \"container-br0\" | grep -q \"10.0.3.1\"";
    std::string cmd_1 = "ifconfig eth0 " + ip + " netmask 255.255.255.0 up";
    std::string cmd_2 = "route add default gw 10.0.3.1";
    std::string cmd_3 = "ifconfig eth0 up";

    {
	InSequence sequence;
	EXPECT_CALL(systemCallInterface, makeCall(cmd_0)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);

    {
	InSequence sequence;
	EXPECT_CALL(systemCallInterface, makeCall(cmd_0)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_3)).Times(1);
	EXPECT_CALL(controllerInterface, systemCall(cmd_2)).Times(1);
    }

    success = gw.activate();
    ASSERT_TRUE(success);

}

/*! Test NetworkGateway::activate is successful but that no network interface
 *  is brought up when there is no config for networking.
 */
TEST_F(NetworkGatewayTest, TestActivateNoConfig) {
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    std::string cmd_1 = "ifconfig eth0 down";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate is successful but that no network interface
 *  is brought up when the networking config is malformed.
 */
TEST_F(NetworkGatewayTest, TestActivateBadConfig) {
    std::string config = "{\"internet-access\": \"true\"}";
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    ASSERT_TRUE(gw.setConfig(config));

    std::string cmd_1 = "ifconfig eth0 down";

    {
	InSequence sequence;
	EXPECT_CALL(controllerInterface, systemCall(cmd_1)).Times(1);
    }

    bool success = gw.activate();
    ASSERT_TRUE(success);
}

/*! Test NetworkGateway::activate fails when there is no network bridge setup
 *  on the host.
 */
TEST_F(NetworkGatewayTest, TestActivateNoBridge) {
    std::string config = "{\"internet-access\": \"true\", \"gateway\":\"10.0.3.1\"}";
    NetworkGateway gw(&controllerInterface, &systemCallInterface);

    ASSERT_TRUE(gw.setConfig(config));

    DefaultValue<bool>::Set(false);

    bool success = gw.activate();
    EXPECT_CALL(systemCallInterface, makeCall(_)).Times(0);
    ASSERT_TRUE(!success);

    DefaultValue<bool>::Clear();
}
{'fraction_non_alphanumeric': 0.1334051091318391, 'fraction_numerical': 0.0173240677302535, 'mean_word_length': 4.16582406471183, 'pattern_counts': {'":': 16, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 15, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5445592', 'n_tokens_mistral': 3226, 'n_tokens_neox': 3158, 'n_words': 705}