<commit_before>#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE read buffer test
#include <boost/test/unit_test.hpp>
#include <errno.h>
#include <stdint.h>

#include "../config.h"
#include "../peer.h"

static const int BADFD = -1;
static const int TOO_MUCH_DATA = 1;
static const int CLIENT_CLOSE = 2;
static const int AGAIN = 3;
static const int SLOW_READ = 4;
static const int FAST_READ = 5;
static const int HANDLE_FAST_PEER = 6;
static const int HANDLE_SLOW_PEER = 7;

extern "C" {

static uint32_t parsed_length = 0;
static char *parsed_msg = NULL;

int parse_message(char *msg, uint32_t length)
{
	parsed_msg = msg;
	parsed_length = length;
	return 0;
}

static unsigned char slow_read_counter = 0;
static const char fast_read_msg[] = "HelloWorld";
static const char handle_fast_peer_msg[] = "Hello World!";
static const char handle_slow_peer_msg[] = "<NAME>!";
static int handle_fast_peer_first = 0;
static int handle_slow_peer_count = 0;

ssize_t fake_read(int fd, void *buf, size_t count)
{
	if (fd == BADFD) {
		errno = EBADF;
		return -1;
	}
	if (fd == CLIENT_CLOSE) {
		return 0;
	}
	if (fd == AGAIN) {
		errno = EAGAIN;
		return -1;
	}

	if (fd == SLOW_READ) {
		char val = ++slow_read_counter;
		if (val % 2 == 0) {
			errno = EAGAIN;
			return -1;
		} else {
			*((char *)buf) = val;
			return 1;
		}
	}

	if (fd == FAST_READ) {
		memcpy(buf, fast_read_msg, strlen(fast_read_msg));
		return strlen(fast_read_msg);
	}

	if (fd == HANDLE_FAST_PEER) {

		if (handle_fast_peer_first == 0) {
			char *write_ptr = (char *)buf;
			uint32_t length = strlen(handle_fast_peer_msg);
			length = htobe32(length);
			memcpy(write_ptr, &length, sizeof(length));
			write_ptr += sizeof(length);
			memcpy(write_ptr, handle_fast_peer_msg, sizeof(handle_fast_peer_msg));
			handle_fast_peer_first = 1;
			return sizeof(length) + sizeof(handle_fast_peer_msg);
		} else {
			errno = EAGAIN;
			return -1;
		}
	}

	if (fd == HANDLE_SLOW_PEER) {
		switch (handle_slow_peer_count++) {
			char *write_ptr;
			uint32_t length;
			char *read_ptr;

		case 0:
			write_ptr = (char *)buf;
			length = strlen(handle_slow_peer_msg);
			length = htobe32(length);
			memcpy(write_ptr, &length, sizeof(length) - 1);
			return sizeof(length) - 1;

		case 2:
			write_ptr = (char *)buf;
			length = strlen(handle_slow_peer_msg);
			length = htobe32(length);
			read_ptr = (char *)&length;
			read_ptr = read_ptr + 3;
			memcpy(write_ptr, read_ptr, 1);
			write_ptr++;
			memcpy(write_ptr, handle_slow_peer_msg, 3);
			return 4;

		case 4:
			write_ptr = (char *)buf;
			memcpy(write_ptr, &handle_slow_peer_msg[3], strlen(handle_slow_peer_msg) - 3);
			return strlen(handle_slow_peer_msg) - 3;

		default:
			errno = EAGAIN;
			return -1;
		}
	}
}

}

BOOST_AUTO_TEST_CASE(wrong_fd)
{
	struct peer *p = alloc_peer(BADFD);
	BOOST_REQUIRE(p != NULL);

	char *read_ptr = get_read_ptr(p, 100);
	BOOST_CHECK(read_ptr == NULL);

	free_peer(p);
}

BOOST_AUTO_TEST_CASE(too_much_data_requested)
{
	struct peer *p = alloc_peer(TOO_MUCH_DATA);
	BOOST_REQUIRE(p != NULL);

	char *read_ptr = get_read_ptr(p, MAX_MESSAGE_SIZE + 1);
	BOOST_CHECK(read_ptr == NULL);

	free_peer(p);
}

BOOST_AUTO_TEST_CASE(client_closed_connection)
{
	struct peer *p = alloc_peer(CLIENT_CLOSE);
	BOOST_REQUIRE(p != NULL);
<commit_msg>Add return value for every case.<commit_after>#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE read buffer test
#include <boost/test/unit_test.hpp>
#include <errno.h>
#include <stdint.h>

#include "../config.h"
#include "../peer.h"

static const int BADFD = -1;
static const int TOO_MUCH_DATA = 1;
static const int CLIENT_CLOSE = 2;
static const int AGAIN = 3;
static const int SLOW_READ = 4;
static const int FAST_READ = 5;
static const int HANDLE_FAST_PEER = 6;
static const int HANDLE_SLOW_PEER = 7;

extern "C" {

static uint32_t parsed_length = 0;
static char *parsed_msg = NULL;

int parse_message(char *msg, uint32_t length)
{
	parsed_msg = msg;
	parsed_length = length;
	return 0;
}

static unsigned char slow_read_counter = 0;
static const char fast_read_msg[] = "HelloWorld";
static const char handle_fast_peer_msg[] = "Hello World!";
static const char handle_slow_peer_msg[] = "<NAME>!";
static int handle_fast_peer_first = 0;
static int handle_slow_peer_count = 0;

ssize_t fake_read(int fd, void *buf, size_t count)
{
	if (fd == BADFD) {
		errno = EBADF;
		return -1;
	}
	if (fd == CLIENT_CLOSE) {
		return 0;
	}
	if (fd == AGAIN) {
		errno = EAGAIN;
		return -1;
	}

	if (fd == SLOW_READ) {
		char val = ++slow_read_counter;
		if (val % 2 == 0) {
			errno = EAGAIN;
			return -1;
		} else {
			*((char *)buf) = val;
			return 1;
		}
	}

	if (fd == FAST_READ) {
		memcpy(buf, fast_read_msg, strlen(fast_read_msg));
		return strlen(fast_read_msg);
	}

	if (fd == HANDLE_FAST_PEER) {

		if (handle_fast_peer_first == 0) {
			char *write_ptr = (char *)buf;
			uint32_t length = strlen(handle_fast_peer_msg);
			length = htobe32(length);
			memcpy(write_ptr, &length, sizeof(length));
			write_ptr += sizeof(length);
			memcpy(write_ptr, handle_fast_peer_msg, sizeof(handle_fast_peer_msg));
			handle_fast_peer_first = 1;
			return sizeof(length) + sizeof(handle_fast_peer_msg);
		} else {
			errno = EAGAIN;
			return -1;
		}
	}

	if (fd == HANDLE_SLOW_PEER) {
		switch (handle_slow_peer_count++) {
			char *write_ptr;
			uint32_t length;
			char *read_ptr;

		case 0:
			write_ptr = (char *)buf;
			length = strlen(handle_slow_peer_msg);
			length = htobe32(length);
			memcpy(write_ptr, &length, sizeof(length) - 1);
			return sizeof(length) - 1;

		case 2:
			write_ptr = (char *)buf;
			length = strlen(handle_slow_peer_msg);
			length = htobe32(length);
			read_ptr = (char *)&length;
			read_ptr = read_ptr + 3;
			memcpy(write_ptr, read_ptr, 1);
			write_ptr++;
			memcpy(write_ptr, handle_slow_peer_msg, 3);
			return 4;

		case 4:
			write_ptr = (char *)buf;
			memcpy(write_ptr, &handle_slow_peer_msg[3], strlen(handle_slow_peer_msg) - 3);
			return strlen(handle_slow_peer_msg) - 3;

		default:
			errno = EAGAIN;
			return -1;
		}
	}

	return 0;
}

}

BOOST_AUTO_TEST_CASE(wrong_fd)
{
	struct peer *p = alloc_peer(BADFD);
	BOOST_REQUIRE(p != NULL);

	char *read_ptr = get_read_ptr(p, 100);
	BOOST_CHECK(read_ptr == NULL);

	free_peer(p);
}

BOOST_AUTO_TEST_CASE(too_much_data_requested)
{
	struct peer *p = alloc_peer(TOO_MUCH_DATA);
	BOOST_REQUIRE(p != NULL);

	char *read_ptr = get_read_ptr(p, MAX_MESSAGE_SIZE + 1);
	BOOST_CHECK(read_ptr == NULL);

	free_peer(p);
}

BOOST_AUTO_TEST_CASE(client_closed_connection)
{
	struct peer *p = alloc_peer(CLIENT_CLOSE);
	BOOST_REQUIRE(p != NULL);

{'fraction_non_alphanumeric': 0.1259960908134115, 'fraction_numerical': 0.016388513005563075, 'mean_word_length': 4.275178429817605, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7453760', 'n_tokens_mistral': 2957, 'n_tokens_neox': 2616, 'n_words': 607}