summaryrefslogtreecommitdiff
path: root/ccan/rfc822/test/run-hdr-of-name.c
blob: 9c2e2a373c85fe6a91648397704903b3b71207a0 (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
#include <ccan/foreach/foreach.h>
#include <ccan/failtest/failtest_override.h>
#include <ccan/failtest/failtest.h>
#include <stdlib.h>
#include <string.h>

#define CCAN_RFC822_DEBUG

#include <ccan/rfc822/rfc822.h>

#include <ccan/rfc822/rfc822.c>

#include "testdata.h"
#include "helper.h"

static void test_hdrbyname(const struct aexample *e, const char *buf, size_t len,
			   const char *exname, int crlf)
{
	struct rfc822_msg *msg;
	struct rfc822_header *h, *hx;
	int i, j;

	msg = rfc822_start(NULL, buf, len);
	allocation_failure_check();
	ok(msg, "opened %s", exname);

	for (i = 0; i < e->nhdrs; i++) {
		struct testhdr *eh = &e->hdrs[i];

		h = rfc822_first_header_of_name(msg, eh->name);
		hx = rfc822_next_header_of_name(msg, NULL, eh->name);
		ok1(h == hx);

		for (j = 0; h && j < eh->index; j++)
			h = rfc822_next_header_of_name(msg, h, eh->name);
		ok(h, "header \"%s\" (#%d) exists", eh->name, eh->index);
		if (!h)
			break;
		check_header(msg, h, eh->name, eh->val, eh->errors, crlf);

		h = rfc822_next_header_of_name(msg, h, eh->name);
		ok1((eh->index != eh->last) ^ !h);
	}

	h = rfc822_first_header_of_name(msg, NULL);
	ok(!h, "Never match NULL name");

	rfc822_free(msg);
	allocation_failure_check();
}

int main(int argc, char *argv[])
{
	struct aexample *e;

	/* This is how many tests you plan to run */
	plan_tests(6*num_aexamples() + 14*num_aexample_hdrs());

	failtest_setup(argc, argv);

	for_each_aexample(e) {
		int crlf;

		foreach_int(crlf, 0, 1) {
			const char *buf;
			size_t len;
			char exname[256];

			sprintf(exname, "%s[%s]", e->name, NLT(crlf));

			buf = assemble_msg(e, &len, crlf);
			ok((buf), "assembled %s", exname);
			if (!buf)
				continue;

			test_hdrbyname(e, buf, len, exname, crlf);

			tal_free(buf);
		}
	}

	/* This exits depending on whether all tests passed */
	failtest_exit(exit_status());
}