<commit_before>/*
 * Copyright (C) 2012 BMW Car IT GmbH
 *
 * 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.
 */

#include <gtest/gtest.h>
#include "capu/os/TcpServerSocket.h"
#include "capu/os/Thread.h"
#include "capu/os/TcpSocket.h"
#include "capu/os/Mutex.h"
#include "capu/os/CondVar.h"
#include "capu/util/TcpSocketOutputStream.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock-generated-matchers.h"

capu::Mutex mutex;
capu::CondVar cv;
bool cond = false;
uint16_t chosenPort = 0;

class RandomPort
{
public:
    /**
     * Gets a Random Port between 1024 and 10024
     */
    static uint16_t get()
    {
        return (rand() % 10000) + 40000; // 0-1023 = Well Known, 1024-49151 = User, 49152 - 65535 = Dynamic
    }
};

class ThreadClientTest : public capu::Runnable
{
    uint16_t port;
public:
    //client thread to test data exchange between client and server
    ThreadClientTest(uint16_t port) : port(port) {}

    void run()
    {
        int32_t communication_variable;
        int32_t numBytes = 0;
        //ALLOCATION AND SYNCH OF cient and  server
        capu::TcpSocket* cli_socket = new capu::TcpSocket();

        //wait for server to start up
        mutex.lock();
        while (!cond)
        {
            cv.wait(mutex);
        }
        cond = false;
        if (0 == port)
        {
            port = chosenPort;
        }
        mutex.unlock();
        EXPECT_LT(0, port);

        //TRY TO CONNECT TO IPV6
        EXPECT_EQ(capu::CAPU_SOCKET_EADDR, cli_socket->connect("::1", port));

        //connects to he given id
        capu::status_t result = capu::CAPU_ERROR;
        int32_t attemps = 0;
        while (result != capu::CAPU_OK && attemps < 100)
        {
            result = cli_socket->connect("localhost", port);
            attemps++;
            capu::Thread::Sleep(50);
        }
        EXPECT_EQ(capu::CAPU_OK, result);

        int32_t i = 5;
        int32_t sendData;
        //send data
        EXPECT_EQ(capu::CAPU_OK, cli_socket->send((char*) &i, sizeof(int32_t), sendData));

        //receive
        capu::status_t res = cli_socket->receive((char*) &communication_variable, sizeof(int32_t), numBytes);
        EXPECT_EQ(capu::CAPU_OK, res);

        //CHECK VALUE
        EXPECT_EQ(6, communication_variable);

        mutex.lock();
        cond = true;
        cv.signal();
        mutex.unlock();

        //socket close
        EXPECT_EQ(capu::CAPU_OK, cli_socket->close());
        //deallocating
        delete cli_socket;
    }
};

class ThreadTimeoutOnReceiveClientTest : public capu::Runnable
{
    uint16_t port;
public:
    //timeout test
    ThreadTimeoutOnReceiveClientTest(uint16_t port) : port(port) {}

    void run()
    {
        int32_t communication_variable;
        int32_t numBytes = 0;
        capu::TcpSocket* cli_socket = new capu::TcpSocket();

        cli_socket->setTimeout(2);

        //connects to he given id
        capu::status_t result = capu::CAPU_ERROR;
        //wait for server to start up
        mutex.lock();
        while (!cond)
        {
            cv.wait(mutex);
        }
        if (0 == port)
        {
            port = chosenPort;
        }
        cond = false;
        mutex.unlock();
        result = cli_socket->connect("localhost", port);
        ASSERT_TRUE(result == capu::CAPU_OK);

        int32_t i = 5;

        int32_t sentBytes;
        //send data
        EXPECT_EQ(capu::CAPU_OK, cli_socket->send((char*) &i, sizeof(int32_t), sentBytes));

        //receive
        EXPECT_EQ(capu::CAPU_ETIMEOUT, cli_socket->receive((char*) &communication_variable, sizeof(int32_t), numBytes));

        //client has received timeout, server can close socket
        mutex.lock();
        cond = true;
        cv.signal();
        mutex.unlock();
        //socket close
        EXPECT_EQ(capu::CAPU_OK, cli_socket->close());
        //deallocating
        delete cli_socket;
    }
};

class ThreadTimeoutOnSendClientTest : public capu::Runnable<commit_msg>tests: put socket variable on stack<commit_after>/*
 * Copyright (C) 2012 BMW Car IT GmbH
 *
 * 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.
 */

#include <gtest/gtest.h>
#include "capu/os/TcpServerSocket.h"
#include "capu/os/Thread.h"
#include "capu/os/TcpSocket.h"
#include "capu/os/Mutex.h"
#include "capu/os/CondVar.h"
#include "capu/util/TcpSocketOutputStream.h"
#include "gmock/gmock-matchers.h"
#include "gmock/gmock-generated-matchers.h"

capu::Mutex mutex;
capu::CondVar cv;
bool cond = false;
uint16_t chosenPort = 0;

class RandomPort
{
public:
    /**
     * Gets a Random Port between 1024 and 10024
     */
    static uint16_t get()
    {
        return (rand() % 10000) + 40000; // 0-1023 = Well Known, 1024-49151 = User, 49152 - 65535 = Dynamic
    }
};

class ThreadClientTest : public capu::Runnable
{
    uint16_t port;
public:
    //client thread to test data exchange between client and server
    ThreadClientTest(uint16_t port) : port(port) {}

    void run()
    {
        int32_t communication_variable;
        int32_t numBytes = 0;
        //ALLOCATION AND SYNCH OF cient and  server
        capu::TcpSocket* cli_socket = new capu::TcpSocket();

        //wait for server to start up
        mutex.lock();
        while (!cond)
        {
            cv.wait(mutex);
        }
        cond = false;
        if (0 == port)
        {
            port = chosenPort;
        }
        mutex.unlock();
        EXPECT_LT(0, port);

        //TRY TO CONNECT TO IPV6
        EXPECT_EQ(capu::CAPU_SOCKET_EADDR, cli_socket->connect("::1", port));

        //connects to he given id
        capu::status_t result = capu::CAPU_ERROR;
        int32_t attemps = 0;
        while (result != capu::CAPU_OK && attemps < 100)
        {
            result = cli_socket->connect("localhost", port);
            attemps++;
            capu::Thread::Sleep(50);
        }
        EXPECT_EQ(capu::CAPU_OK, result);

        int32_t i = 5;
        int32_t sendData;
        //send data
        EXPECT_EQ(capu::CAPU_OK, cli_socket->send((char*) &i, sizeof(int32_t), sendData));

        //receive
        capu::status_t res = cli_socket->receive((char*) &communication_variable, sizeof(int32_t), numBytes);
        EXPECT_EQ(capu::CAPU_OK, res);

        //CHECK VALUE
        EXPECT_EQ(6, communication_variable);

        mutex.lock();
        cond = true;
        cv.signal();
        mutex.unlock();

        //socket close
        EXPECT_EQ(capu::CAPU_OK, cli_socket->close());
        //deallocating
        delete cli_socket;
    }
};

class ThreadTimeoutOnReceiveClientTest : public capu::Runnable
{
    uint16_t port;
public:
    //timeout test
    ThreadTimeoutOnReceiveClientTest(uint16_t port) : port(port) {}

    void run()
    {
        int32_t communication_variable;
        int32_t numBytes = 0;
        capu::TcpSocket cli_socket;

        cli_socket.setTimeout(2);

        //connects to he given id
        capu::status_t result = capu::CAPU_ERROR;
        //wait for server to start up
        mutex.lock();
        while (!cond)
        {
            cv.wait(mutex);
        }
        if (0 == port)
        {
            port = chosenPort;
        }
        cond = false;
        mutex.unlock();
        result = cli_socket.connect("localhost", port);
        ASSERT_TRUE(result == capu::CAPU_OK);

        int32_t i = 5;

        int32_t sentBytes;
        //send data
        EXPECT_EQ(capu::CAPU_OK, cli_socket.send((char*) &i, sizeof(int32_t), sentBytes));

        //receive
        EXPECT_EQ(capu::CAPU_ETIMEOUT, cli_socket.receive((char*) &communication_variable, sizeof(int32_t), numBytes));

        //client has received timeout, server can close socket
        mutex.lock();
        cond = true;
        cv.signal();
        mutex.unlock();
        //socket close
        EXPECT_EQ(capu::CAPU_OK, cli_socket.close());
    }
};

class ThreadTimeoutOnSendClientTest : public capu::Runnable
{'fraction_non_alphanumeric': 0.11583314644539135, 'fraction_numerical': 0.02399641175151379, 'mean_word_length': 2.3329596412556053, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '107535', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2807, 'n_words': 725}