<commit_before>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */

#include <thread>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "generators.h"
#include "libpelagicontain.h"

#include "gateway/dbusgateway.h"
#include "gateway/waylandgateway.h"
#include "gateway/networkgateway.h"
#include "gateway/pulsegateway.h"
#include "gateway/filegateway.h"


LOG_DECLARE_DEFAULT_CONTEXT(defaultContext, "ff", "dd");

class PelagicontainApp :
    public::testing::Test
{

public:
    PelagicontainApp()
    {
    }

    void SetUp() override
    {
        ::testing::Test::SetUp();
        lib = std::unique_ptr<PelagicontainLib> (new PelagicontainLib());
        lib->setContainerIDPrefix("Test-");
        lib->setMainLoopContext(m_context);
        ASSERT_TRUE(isSuccess(lib->init()));
    }

    void TearDown() override
    {
        ::testing::Test::TearDown();  // Remember to tear down the base fixture after cleaning up FooTest!
    }

    void run()
    {
        m_ml = Glib::MainLoop::create(m_context);
        m_ml->run();
    }

    void exit()
    {
        m_ml->quit();
    }

    void setGatewayConfigs(const GatewayConfiguration &config)
    {
        getLib().getPelagicontain().setGatewayConfigs(config);
    }

    Glib::RefPtr<Glib::MainContext> getMainContext()
    {
        return m_context;
    }

    void openTerminal()
    {
        lib->openTerminal("konsole -e");
    }

    PelagicontainLib &getLib()
    {
        return *lib;
    }

    Glib::RefPtr<Glib::MainContext> m_context = Glib::MainContext::get_default();
    Glib::RefPtr<Glib::MainLoop> m_ml;
    std::unique_ptr<PelagicontainLib> lib;
};




TEST_F(PelagicontainApp, TestWayland) {

    GatewayConfiguration config;
    config[WaylandGateway::ID] = "[ { \"enabled\" : true } ]";

    getLib().getPelagicontain().setGatewayConfigs(config);

    //		openTerminal();
    //		sleep(10000);

    FunctionJob jobTrue(getLib(), [] (){

        bool ERROR = 1;
        bool SUCCESS = 0;

        const char *waylandDir = getenv("XDG_RUNTIME_DIR");
        log_debug() << "Wayland dir : " << waylandDir;
        if (waylandDir == nullptr) {
            return ERROR;
        }

        std::string socketPath = StringBuilder() << waylandDir << "/" << WaylandGateway::SOCKET_FILE_NAME;
        log_debug() << "isSocket : " << socketPath << " " << isSocket(socketPath);

        if ( !isSocket(socketPath) ) {
            return ERROR;
        }

        return SUCCESS;

    });
    jobTrue.start();
    ASSERT_TRUE(jobTrue.wait() == 0);

    CommandJob westonJob(getLib(),"/usr/bin/weston-terminal");
    westonJob.start();
    ASSERT_TRUE(westonJob.wait() == 0);

}


/*
 * Tests the convenience functions in common.cpp
 */
TEST_F(PelagicontainApp, CommonFunctions) {

    ASSERT_TRUE(isDirectory("/tmp"));

    // Create a temp file and check so it is a file
    char tempFilename[] = "/tmp/blablaXXXXXX";
    int fileDescriptor = mkstemp(tempFilename);
    close(fileDescriptor);
    ASSERT_TRUE(isFile(tempFilename));
    ASSERT_FALSE(isDirectory(tempFilename));
    ASSERT_FALSE(isSocket(tempFilename));

    // Let's use the same file and test the read/write functions
    std::string testData = "testData";
    std::string readBack;
    writeToFile(tempFilename, testData);<commit_msg>unit tests: change weston-terminal to weston-info to get some good output<commit_after>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */

#include <thread>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "generators.h"
#include "libpelagicontain.h"

#include "gateway/dbusgateway.h"
#include "gateway/waylandgateway.h"
#include "gateway/networkgateway.h"
#include "gateway/pulsegateway.h"
#include "gateway/filegateway.h"


LOG_DECLARE_DEFAULT_CONTEXT(defaultContext, "ff", "dd");

class PelagicontainApp :
    public::testing::Test
{

public:
    PelagicontainApp()
    {
    }

    void SetUp() override
    {
        ::testing::Test::SetUp();
        lib = std::unique_ptr<PelagicontainLib> (new PelagicontainLib());
        lib->setContainerIDPrefix("Test-");
        lib->setMainLoopContext(m_context);
        ASSERT_TRUE(isSuccess(lib->init()));
    }

    void TearDown() override
    {
        ::testing::Test::TearDown();  // Remember to tear down the base fixture after cleaning up FooTest!
    }

    void run()
    {
        m_ml = Glib::MainLoop::create(m_context);
        m_ml->run();
    }

    void exit()
    {
        m_ml->quit();
    }

    void setGatewayConfigs(const GatewayConfiguration &config)
    {
        getLib().getPelagicontain().setGatewayConfigs(config);
    }

    Glib::RefPtr<Glib::MainContext> getMainContext()
    {
        return m_context;
    }

    void openTerminal()
    {
        lib->openTerminal("konsole -e");
    }

    PelagicontainLib &getLib()
    {
        return *lib;
    }

    Glib::RefPtr<Glib::MainContext> m_context = Glib::MainContext::get_default();
    Glib::RefPtr<Glib::MainLoop> m_ml;
    std::unique_ptr<PelagicontainLib> lib;
};




TEST_F(PelagicontainApp, TestWayland) {

    GatewayConfiguration config;
    config[WaylandGateway::ID] = "[ { \"enabled\" : true } ]";

    getLib().getPelagicontain().setGatewayConfigs(config);

    //		openTerminal();
    //		sleep(10000);

    FunctionJob jobTrue(getLib(), [] (){

        bool ERROR = 1;
        bool SUCCESS = 0;

        const char *waylandDir = getenv("XDG_RUNTIME_DIR");
        log_debug() << "Wayland dir : " << waylandDir;
        if (waylandDir == nullptr) {
            return ERROR;
        }

        std::string socketPath = StringBuilder() << waylandDir << "/" << WaylandGateway::SOCKET_FILE_NAME;
        log_debug() << "isSocket : " << socketPath << " " << isSocket(socketPath);

        if ( !isSocket(socketPath) ) {
            return ERROR;
        }

        return SUCCESS;

    });
    jobTrue.start();
    ASSERT_TRUE(jobTrue.wait() == 0);

    CommandJob westonJob(getLib(),"/usr/bin/weston-info");
    westonJob.start();
    ASSERT_TRUE(westonJob.wait() == 0);

}


/*
 * Tests the convenience functions in common.cpp
 */
TEST_F(PelagicontainApp, CommonFunctions) {

    ASSERT_TRUE(isDirectory("/tmp"));

    // Create a temp file and check so it is a file
    char tempFilename[] = "/tmp/blablaXXXXXX";
    int fileDescriptor = mkstemp(tempFilename);
    close(fileDescriptor);
    ASSERT_TRUE(isFile(tempFilename));
    ASSERT_FALSE(isDirectory(tempFilename));
    ASSERT_FALSE(isSocket(tempFilename));

    // Let's use the same file and test the read/write functions
    std::string testData = "testData";
    std::string readBack;
    writeToFile(tempFilename, testData);
{'fraction_non_alphanumeric': 0.14216832261835183, 'fraction_numerical': 0.003798947983635301, 'mean_word_length': 3.0719809637120763, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5523804', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2245, 'n_words': 417}