<commit_before>/* test/heartbeat_test.c */
/*
 * Unit test for TLS heartbeats.
 *
 * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
 *
 * Author:  <NAME> (<EMAIL>, http://mike-bland.com/)
 * Date:    2014-04-12
 * License: Creative Commons Attribution 4.0 International (CC By 4.0)
 *          http://creativecommons.org/licenses/by/4.0/deed.en_US
 *
 * OUTPUT
 * ------
 * The program returns zero on success. It will print a message with a count
 * of the number of failed tests and return nonzero if any tests fail.
 *
 * It will print the contents of the request and response buffers for each
 * failing test. In a "fixed" version, all the tests should pass and there
 * should be no output.
 *
 * In a "bleeding" version, you'll see:
 *
 *   test_dtls1_heartbleed failed:
 *     expected payload len: 0
 *     received: 1024
 *   sent 26 characters
 *     "HEARTBLEED                "
 *   received 1024 characters
 *     "HEARTBLEED                \xde\xad\xbe\xef..."
 *   ** test_dtls1_heartbleed failed **
 *
 * The contents of the returned buffer in the failing test will depend on the
 * contents of memory on your machine.
 *
 * MORE INFORMATION
 * ----------------
 * http://mike-bland.com/2014/04/12/heartbleed.html
 * http://mike-bland.com/tags/heartbleed.html
 */

#include "../ssl/ssl_locl.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* As per https://tools.ietf.org/html/rfc6520#section-4 */
#define MIN_PADDING_SIZE	16

/* Maximum number of payload characters to print as test output */
#define MAX_PRINTABLE_CHARACTERS	1024

typedef struct heartbeat_test_fixture
	{
	SSL_CTX *ctx;
	SSL *s;
	const char* test_case_name;
	int (*process_heartbeat)(SSL* s);
	unsigned char* payload;
	int sent_payload_len;
	int expected_return_value;
	int return_payload_offset;
	int expected_payload_len;
	const char* expected_return_payload;
	} HEARTBEAT_TEST_FIXTURE;

static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name,
	const SSL_METHOD* meth)
	{
	HEARTBEAT_TEST_FIXTURE fixture;
	memset(&fixture, 0, sizeof(fixture));
	fixture.test_case_name = test_case_name;

	fixture.ctx = SSL_CTX_new(meth);
	if (!fixture.ctx)
		{
		fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
						test_case_name);
		goto fail;
		}

	fixture.s = SSL_new(fixture.ctx);
	if (!fixture.s)
		{
		fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
		goto fail;
		}

	ssl_init_wbio_buffer(fixture.s, 1);
	ssl3_setup_buffers(fixture.s);

	fail:
	if (!fixture.s)
		{
		ERR_print_errors_fp(stderr);
		exit(EXIT_FAILURE);
		}
	return fixture;
	}

static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)<commit_msg>More through error checks in set_up<commit_after>/* test/heartbeat_test.c */
/*
 * Unit test for TLS heartbeats.
 *
 * Acts as a regression test against the Heartbleed bug (CVE-2014-0160).
 *
 * Author:  <NAME> (<EMAIL>, http://mike-bland.com/)
 * Date:    2014-04-12
 * License: Creative Commons Attribution 4.0 International (CC By 4.0)
 *          http://creativecommons.org/licenses/by/4.0/deed.en_US
 *
 * OUTPUT
 * ------
 * The program returns zero on success. It will print a message with a count
 * of the number of failed tests and return nonzero if any tests fail.
 *
 * It will print the contents of the request and response buffers for each
 * failing test. In a "fixed" version, all the tests should pass and there
 * should be no output.
 *
 * In a "bleeding" version, you'll see:
 *
 *   test_dtls1_heartbleed failed:
 *     expected payload len: 0
 *     received: 1024
 *   sent 26 characters
 *     "HEARTBLEED                "
 *   received 1024 characters
 *     "HEARTBLEED                \xde\xad\xbe\xef..."
 *   ** test_dtls1_heartbleed failed **
 *
 * The contents of the returned buffer in the failing test will depend on the
 * contents of memory on your machine.
 *
 * MORE INFORMATION
 * ----------------
 * http://mike-bland.com/2014/04/12/heartbleed.html
 * http://mike-bland.com/tags/heartbleed.html
 */

#include "../ssl/ssl_locl.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* As per https://tools.ietf.org/html/rfc6520#section-4 */
#define MIN_PADDING_SIZE	16

/* Maximum number of payload characters to print as test output */
#define MAX_PRINTABLE_CHARACTERS	1024

typedef struct heartbeat_test_fixture
	{
	SSL_CTX *ctx;
	SSL *s;
	const char* test_case_name;
	int (*process_heartbeat)(SSL* s);
	unsigned char* payload;
	int sent_payload_len;
	int expected_return_value;
	int return_payload_offset;
	int expected_payload_len;
	const char* expected_return_payload;
	} HEARTBEAT_TEST_FIXTURE;

static HEARTBEAT_TEST_FIXTURE set_up(const char* const test_case_name,
	const SSL_METHOD* meth)
	{
	HEARTBEAT_TEST_FIXTURE fixture;
	int setup_ok = 1;
	memset(&fixture, 0, sizeof(fixture));
	fixture.test_case_name = test_case_name;

	fixture.ctx = SSL_CTX_new(meth);
	if (!fixture.ctx)
		{
		fprintf(stderr, "Failed to allocate SSL_CTX for test: %s\n",
			test_case_name);
		setup_ok = 0;
		goto fail;
		}

	fixture.s = SSL_new(fixture.ctx);
	if (!fixture.s)
		{
		fprintf(stderr, "Failed to allocate SSL for test: %s\n", test_case_name);
		setup_ok = 0;
		goto fail;
		}

	if (!ssl_init_wbio_buffer(fixture.s, 1))
		{
		fprintf(stderr, "Failed to set up wbio buffer for test: %s\n",
			test_case_name);
		setup_ok = 0;
		goto fail;
		}

	if (!ssl3_setup_buffers(fixture.s))
		{
		fprintf(stderr, "Failed to setup buffers for test: %s\n",
			test_case_name);
		setup_ok = 0;
		goto fail;
		}

	fail:
	if (!setup_ok)
		{
		ERR_print_errors_fp(stderr);
		exit(EXIT_FAILURE);
		}
	return fixture;
	}

static HEARTBEAT_TEST_FIXTURE set_up_dtls(const char* const test_case_name)
{'fraction_non_alphanumeric': 0.11616600104184754, 'fraction_numerical': 0.02066330960236152, 'mean_word_length': 3.5, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '3908520', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2041, 'n_words': 559}