Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / tests / misc / rfc2047.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@novell.com>
4  *
5  *  Copyright 2005 Novell, Inc. (www.novell.com)
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <camel/camel-object.h>
33 #include <camel/camel-mime-utils.h>
34
35 #include "camel-test.h"
36
37 struct {
38         const char *encoded;
39         const char *decoded;
40         int dummy;
41 } test1[] = {
42         /* the first half are rfc compliant cases (which are the most important) */
43         { "=?iso-8859-1?q?this=20is=20some=20text?=", "this is some text", 0 },
44         { "this =?iso-8859-1?q?is_some?= text", "this is some text", 0 },
45         { "=?iso-8859-1?q?th?= =?iso-8859-1?q?is?= is some text", "this is some text", 0 },
46         { "=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=  =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=",
47           "If you can read this you understand the example.", 0 },
48 #if 0   
49         /* And oddly enough, camel fails on these, removed for now */
50
51         /* second half: brokenly encoded rfc2047 words */
52         { "foo=?UTF-8?Q?bar?=baz", "foobarbaz", 0 },
53         { "foo =?UTF-8?Q?bar?=baz", "foo barbaz", 0 },
54         { "foo=?UTF-8?Q?bar?= baz", "foobar baz", 0 },
55         { "=?UTF-8?Q?foo bar?=baz", "foo barbaz", 0 },
56         { "foo=?UTF-8?Q?bar baz?=", "foobar baz", 0 },
57         { "=?UTF-8?Q?foo?==?UTF-8?Q?bar baz?=", "foobar baz", 0 },
58         { "=?UTF-8?Q?foo?= =?UTF-8?Q?bar baz?=", "foobar baz", 0 },
59         { "=?UTF-8?Q?foo?= bar =?UTF-8?Q?baz?=", "foo bar baz", 0 },
60         { "=?foo=?UTF-8?Q?bar baz?=", "=?foobar baz", 0 },
61         { "=?foo?=?UTF-8?Q?bar baz?=", "=?foo?bar baz", 0 },
62         { "=?foo?Q=?UTF-8?Q?bar baz?=", "=?foo?Qbar baz", 0 },
63         { "=?foo?Q?=?UTF-8?Q?bar baz?=", "=?foo?Q?bar baz", 0 },
64         { "=?UTF-8?Q?bar ? baz?=", "=?UTF-8?Q?bar ? baz?=", 0 },
65 #endif
66 };
67
68 struct {
69         const char *encoded;
70         const char *decoded;
71         int dummy;
72 } test2[] = {
73         /* ctext tests */
74         { "Test of ctext (=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)", "Test of ctext (ab)", 1 },
75         { "ctext with \\\"quoted\\\" pairs", "ctext with \"quoted\" pairs", 1 }
76 };
77
78 int main (int argc, char ** argv)
79 {
80         char *decoded;
81         int i;
82         
83         camel_test_init (argc, argv);
84         
85         camel_test_start ("rfc2047 decoding");
86         
87         for (i = 0; i < G_N_ELEMENTS (test1); i++) {
88                 camel_test_push ("rfc2047 decoding[%d] '%s'", i, test1[i].encoded);
89                 decoded = camel_header_decode_string (test1[i].encoded, "iso-8859-1");
90                 check (decoded && strcmp (decoded, test1[i].decoded) == 0);
91                 g_free (decoded);
92                 camel_test_pull ();
93         }
94         
95         camel_test_end ();
96         
97         camel_test_start ("rfc2047 ctext decoding");
98         
99         for (i = 0; i < G_N_ELEMENTS (test2); i++) {
100                 camel_test_push ("rfc2047 ctext decoding[%d] '%s'", i, test2[i].encoded);
101                 decoded = camel_header_format_ctext (test2[i].encoded, "iso-8859-1");
102                 check (decoded && strcmp (decoded, test2[i].decoded) == 0);
103                 g_free (decoded);
104                 camel_test_pull ();
105         }
106         
107         camel_test_end ();
108         
109         return 0;
110 }