summaryrefslogtreecommitdiff
path: root/ccan/rfc822/test/helper.c
blob: cc62a6621e4caf4bd94bd09f4374f6935bdae584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdlib.h>
#include <stdio.h>

#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>

#include <ccan/rfc822/rfc822.h>

#include "helper.h"

/* failtest limitations mean we need these wrappers to test talloc
 * failure paths. */
#ifndef TAL_USE_TALLOC
static void *malloc_wrapper(size_t size)
{
	return malloc(size);
}

static void free_wrapper(void *ptr)
{
	free(ptr);
}

static void *realloc_wrapper(void *ptr, size_t size)
{
	return realloc(ptr, size);
}
#endif

#if 0
static void allocation_failure_exit(const char *s)
{
	fprintf(stderr, "Allocation failure: %s", s);
	exit(0);
}
#endif

static bool allocation_failed = false;

static void allocation_failure_continue(const char *s)
{
	fprintf(stderr, "Allocation failure: %s", s);
	allocation_failed = true;
}

void allocation_failure_check(void)
{
	if (allocation_failed) {
		fprintf(stderr, "Exiting due to earlier failed allocation\n");
		exit(0);
	}
}

#ifdef TAL_USE_TALLOC
#include <ccan/tal/talloc/talloc.h>
#else
#include <ccan/tal/tal.h>
#endif

/* Don't abort on allocation failures! */
static void noabort_wrapper(const char *why)
{
	return;
}

void failtest_setup(int argc, char *argv[])
{
	failtest_init(argc, argv);
	rfc822_set_allocation_failure_handler(allocation_failure_continue);
#ifdef TAL_USE_TALLOC
	/* FIXME: we can't inject allocation failures in talloc! */
	tal_set_backend(NULL, NULL, NULL, noabort_wrapper);
#else
	tal_set_backend(malloc_wrapper, realloc_wrapper, free_wrapper,
			noabort_wrapper);
#endif
}

void check_header(struct rfc822_msg *msg,
		  struct rfc822_header *h,
		  const char *name, const char *val,
		  enum rfc822_header_errors experr, int crlf)
{
	enum rfc822_header_errors errs;
	struct bytestring hname, hvalue, hfull;
	size_t namelen = strlen(name);
	size_t valuelen = strlen(val);
	size_t nln = crlf ? 2 : 1;
	size_t fulllen = namelen + valuelen + 1 + nln;

	errs = rfc822_header_errors(msg, h);
	ok(errs == experr, "Header errors 0x%x != 0x%x", errs, experr);
	allocation_failure_check();

	hname = rfc822_header_raw_name(msg, h);
	allocation_failure_check();

	ok(hname.ptr && bytestring_eq(hname, bytestring_from_string(name)),
	   "Header name \"%.*s\"", (int)hname.len, hname.ptr);

	hvalue = rfc822_header_raw_value(msg, h);
	allocation_failure_check();

	ok(hvalue.ptr && ((valuelen + nln) == hvalue.len)
	   && (memcmp(val, hvalue.ptr, valuelen) == 0)
	   && (!crlf || (hvalue.ptr[hvalue.len - 2] == '\r'))
	   && (hvalue.ptr[hvalue.len - 1] == '\n'),
	   "Header value");

	hfull = rfc822_header_raw_content(msg, h);
	allocation_failure_check();

	ok(hfull.ptr && (fulllen == hfull.len)
	   && (memcmp(name, hfull.ptr, namelen) == 0)
	   && (hfull.ptr[namelen] == ':')
	   && (memcmp(val, hfull.ptr + namelen + 1, valuelen) == 0)
	   && (!crlf || (hfull.ptr[fulllen-2] == '\r'))
	   && (hfull.ptr[fulllen-1] == '\n'),
	   "Full header");
}