summaryrefslogtreecommitdiffstats
path: root/src/global/hfrom_format.c
blob: f0f850a3e5e9fc718abedf5e00294e8cc879f571 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*++
/* NAME
/*	hfrom_format 3
/* SUMMARY
/*	Parse a header_from_format setting
/* SYNOPSIS
/*	#include <hfrom_format.h>
/*
/*	int	hfrom_format_parse(
/*	const char *name,
/*	const char *value)
/*
/*	const char *str_hfrom_format_code(int code)
/* DESCRIPTION
/*	hfrom_format_parse() takes a parameter name (used for
/*	diagnostics) and value, and maps it to the corresponding
/*	code: HFROM_FORMAT_NAME_STD maps to HFROM_FORMAT_CODE_STD,
/*	and HFROM_FORMAT_NAME_OBS maps to HFROM_FORMAT_CODE_OBS.
/*
/*	str_hfrom_format_code() does the reverse mapping.
/* DIAGNOSTICS
/*	All input errors are fatal.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

 /*
  * System library.
  */
#include <sys_defs.h>

 /*
  * Utility library.
  */
#include <name_code.h>
#include <msg.h>

 /*
  * Global library.
  */
#include <mail_params.h>

 /*
  * Application-specific.
  */
#include <hfrom_format.h>

 /*
  * Primitive dependency injection.
  */
#ifdef TEST
extern NORETURN PRINTFLIKE(1, 2) test_msg_fatal(const char *,...);

#define msg_fatal test_msg_fatal
#endif

 /*
  * The name-to-code mapping.
  */
static const NAME_CODE hfrom_format_table[] = {
    HFROM_FORMAT_NAME_STD, HFROM_FORMAT_CODE_STD,
    HFROM_FORMAT_NAME_OBS, HFROM_FORMAT_CODE_OBS,
    0, -1,
};

/* hfrom_format_parse - parse header_from_format setting */

int     hfrom_format_parse(const char *name, const char *value)
{
    int     code;

    if ((code = name_code(hfrom_format_table, NAME_CODE_FLAG_NONE, value)) < 0)
	msg_fatal("invalid setting: \"%s = %s\"", name, value);
    return (code);
}

/* str_hfrom_format_code - convert code to string */

const char *str_hfrom_format_code(int code)
{
    const char *name;

    if ((name = str_name_code(hfrom_format_table, code)) == 0)
	msg_fatal("invalid header format code: %d", code);
    return (name);
}

#ifdef TEST
#include <stdlib.h>
#include <setjmp.h>
#include <string.h>

#include <vstream.h>
#include <vstring.h>
#include <msg_vstream.h>

#define STR(x) vstring_str(x)

 /*
  * TODO(wietse) make this a proper VSTREAM interface. Instead of temporarily
  * swapping streams, we could temporarily swap the stream's write function.
  */

/* vstream_swap - kludge to capture output for testing */

static void vstream_swap(VSTREAM *one, VSTREAM *two)
{
    VSTREAM save;

    save = *one;
    *one = *two;
    *two = save;
}

jmp_buf test_fatal_jbuf;

#undef msg_fatal

/* test_msg_fatal - does not return, and does not terminate */

void    test_msg_fatal(const char *fmt,...)
{
    va_list ap;

    va_start(ap, fmt);
    vmsg_warn(fmt, ap);
    va_end(ap);
    longjmp(test_fatal_jbuf, 1);
}

struct name_test_case {
    const char *label;			/* identifies test case */
    const char *config;			/* configuration under test */
    const char *exp_warning;		/* expected warning or empty */
    const int exp_code;			/* expected code */
};

static struct name_test_case name_test_cases[] = {
    {"hfrom_format_parse good-standard",
	 /* config */ HFROM_FORMAT_NAME_STD,
	 /* warning */ "",
	 /* exp_code */ HFROM_FORMAT_CODE_STD
    },
    {"hfrom_format_parse good-obsolete",
	 /* config */ HFROM_FORMAT_NAME_OBS,
	 /* warning */ "",
	 /* exp_code */ HFROM_FORMAT_CODE_OBS
    },
    {"hfrom_format_parse bad",
	 /* config */ "does-not-exist",
	 /* warning */ "hfrom_format: warning: invalid setting: \"hfrom_format_parse bad = does-not-exist\"\n",
	 /* code */ 0,
    },
    {"hfrom_format_parse empty",
	 /* config */ "",
	 /* warning */ "hfrom_format: warning: invalid setting: \"hfrom_format_parse empty = \"\n",
	 /* code */ 0,
    },
    0,
};

struct code_test_case {
    const char *label;			/* identifies test case */
    int     code;			/* code under test */
    const char *exp_warning;		/* expected warning or empty */
    const char *exp_name;		/* expected namme */
};

static struct code_test_case code_test_cases[] = {
    {"str_hfrom_format_code good-standard",
	 /* code */ HFROM_FORMAT_CODE_STD,
	 /* warning */ "",
	 /* exp_name */ HFROM_FORMAT_NAME_STD
    },
    {"str_hfrom_format_code good-obsolete",
	 /* code */ HFROM_FORMAT_CODE_OBS,
	 /* warning */ "",
	 /* exp_name */ HFROM_FORMAT_NAME_OBS
    },
    {"str_hfrom_format_code bad",
	 /* config */ 12345,
	 /* warning */ "hfrom_format: warning: invalid header format code: 12345\n",
	 /* exp_name */ 0
    },
    0,
};

int     main(int argc, char **argv)
{
    struct name_test_case *np;
    int     code;
    struct code_test_case *cp;
    const char *name;
    int     pass = 0;
    int     fail = 0;
    int     test_failed;
    VSTRING *msg_buf;
    VSTREAM *memory_stream;

    msg_vstream_init("hfrom_format", VSTREAM_ERR);
    msg_buf = vstring_alloc(100);

    for (np = name_test_cases; np->label != 0; np++) {
	VSTRING_RESET(msg_buf);
	VSTRING_TERMINATE(msg_buf);
	test_failed = 0;
	if ((memory_stream = vstream_memopen(msg_buf, O_WRONLY)) == 0)
	    msg_fatal("open memory stream: %m");
	vstream_swap(VSTREAM_ERR, memory_stream);
	if (setjmp(test_fatal_jbuf) == 0)
	    code = hfrom_format_parse(np->label, np->config);
	vstream_swap(memory_stream, VSTREAM_ERR);
	if (vstream_fclose(memory_stream))
	    msg_fatal("close memory stream: %m");
	if (strcmp(STR(msg_buf), np->exp_warning) != 0) {
	    msg_warn("test case %s: got error: \"%s\", want: \"%s\"",
		     np->label, STR(msg_buf), np->exp_warning);
	    test_failed = 1;
	}
	if (*np->exp_warning == 0) {
	    if (code != np->exp_code) {
		msg_warn("test case %s: got code: \"%d\", want: \"%d\"(%s)",
			 np->label, code, np->exp_code,
			 str_hfrom_format_code(np->exp_code));
		test_failed = 1;
	    }
	}
	if (test_failed) {
	    msg_info("%s: FAIL", np->label);
	    fail++;
	} else {
	    msg_info("%s: PASS", np->label);
	    pass++;
	}
    }

    for (cp = code_test_cases; cp->label != 0; cp++) {
	VSTRING_RESET(msg_buf);
	VSTRING_TERMINATE(msg_buf);
	test_failed = 0;
	if ((memory_stream = vstream_memopen(msg_buf, O_WRONLY)) == 0)
	    msg_fatal("open memory stream: %m");
	vstream_swap(VSTREAM_ERR, memory_stream);
	if (setjmp(test_fatal_jbuf) == 0)
	    name = str_hfrom_format_code(cp->code);
	vstream_swap(memory_stream, VSTREAM_ERR);
	if (vstream_fclose(memory_stream))
	    msg_fatal("close memory stream: %m");
	if (strcmp(STR(msg_buf), cp->exp_warning) != 0) {
	    msg_warn("test case %s: got error: \"%s\", want: \"%s\"",
		     cp->label, STR(msg_buf), cp->exp_warning);
	    test_failed = 1;
	} else if (*cp->exp_warning == 0) {
	    if (strcmp(name, cp->exp_name)) {
		msg_warn("test case %s: got name: \"%s\", want: \"%s\"",
			 cp->label, name, cp->exp_name);
		test_failed = 1;
	    }
	}
	if (test_failed) {
	    msg_info("%s: FAIL", cp->label);
	    fail++;
	} else {
	    msg_info("%s: PASS", cp->label);
	    pass++;
	}
    }

    msg_info("PASS=%d FAIL=%d", pass, fail);
    vstring_free(msg_buf);
    exit(fail != 0);
}

#endif