Imported Upstream version 2.2.15
[platform/upstream/gpg2.git] / common / t-mbox-util.c
1 /* t-mbox-util.c - Module test for mbox-util.c
2  * Copyright (C) 2015 Werner Koch
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "util.h"
26 #include "mbox-util.h"
27
28 #define pass()  do { ; } while(0)
29 #define fail(a)  do { fprintf (stderr, "%s:%d: test %d failed\n",\
30                                __FILE__,__LINE__, (a));          \
31                        exit (1);                                 \
32                     } while(0)
33
34
35 static void
36 run_mbox_test (void)
37 {
38   static struct
39   {
40     const char *userid;
41     const char *mbox;
42   } testtbl[] =
43     {
44       { "Werner Koch <wk@gnupg.org>", "wk@gnupg.org" },
45       { "<wk@gnupg.org>", "wk@gnupg.org" },
46       { "wk@gnupg.org", "wk@gnupg.org" },
47       { "wk@gnupg.org ", NULL },
48       { " wk@gnupg.org", NULL },
49       { "Werner Koch (test) <wk@gnupg.org>", "wk@gnupg.org" },
50       { "Werner Koch <wk@gnupg.org> (test)", "wk@gnupg.org" },
51       { "Werner Koch <wk@gnupg.org (test)", NULL },
52       { "Werner Koch <wk@gnupg.org >", NULL },
53       { "Werner Koch <wk@gnupg.org", NULL },
54       { "", NULL },
55       { "@", NULL },
56       { "bar <>", NULL },
57       { "<foo@example.org>", "foo@example.org" },
58       { "<foo.@example.org>", "foo.@example.org" },
59       { "<.foo.@example.org>", ".foo.@example.org" },
60       { "<foo..@example.org>", "foo..@example.org" },
61       { "<foo..bar@example.org>", "foo..bar@example.org" },
62       { "<foo@example.org.>", NULL },
63       { "<foo@example..org>", NULL },
64       { "<foo@.>", NULL },
65       { "<@example.org>", NULL },
66       { "<foo@@example.org>", NULL },
67       { "<@foo@example.org>", NULL },
68       { "<foo@example.org> ()", "foo@example.org" },
69       { "<fo()o@example.org> ()", "fo()o@example.org" },
70       { "<fo()o@example.org> ()", "fo()o@example.org" },
71       { "fo()o@example.org", NULL},
72       { "Mr. Foo <foo@example.org><bar@example.net>", "foo@example.org"},
73       { NULL, NULL }
74     };
75   int idx;
76
77   for (idx=0; testtbl[idx].userid; idx++)
78     {
79       char *mbox = mailbox_from_userid (testtbl[idx].userid);
80
81       if (!testtbl[idx].mbox)
82         {
83           if (mbox)
84             fail (idx);
85         }
86       else if (!mbox)
87         fail (idx);
88       else if (strcmp (mbox, testtbl[idx].mbox))
89         fail (idx);
90
91       xfree (mbox);
92     }
93 }
94
95
96 static void
97 run_dns_test (void)
98 {
99   static struct
100   {
101     const char *name;
102     int valid;
103   } testtbl[] =
104     {
105       { "", 0 },
106       { ".", 0 },
107       { "-", 0 },
108       { "a", 1 },
109       { "ab", 1 },
110       { "a.b", 1 },
111       { "a.b.", 1 },
112       { ".a.b.", 0 },
113       { ".a.b", 0 },
114       { "-a.b", 0 },
115       { "a-.b", 0 },
116       { "a.-b", 0 },
117       { "a.b-", 0 },
118       { "a.b-.", 0 },
119       { "a..b", 0 },
120       { "ab.c", 1 },
121       { "a-b.c", 1 },
122       { "a-b-.c", 0 },
123       { "-a-b.c", 0 },
124       { "example.org", 1 },
125       { "x.example.org", 1 },
126       { "xy.example.org", 1 },
127       { "Xy.example.org", 1 },
128       { "-Xy.example.org", 0 },
129       { "Xy.example-.org", 0 },
130       { "foo.example.org..", 0 },
131       { "foo.example.org.", 1 },
132       { ".foo.example.org.", 0 },
133       { "..foo.example.org.", 0 },
134       { NULL, 0 }
135     };
136   int idx;
137
138   for (idx=0; testtbl[idx].name; idx++)
139     {
140       if (is_valid_domain_name (testtbl[idx].name) != testtbl[idx].valid)
141         fail (idx);
142     }
143 }
144
145
146 int
147 main (int argc, char **argv)
148 {
149   (void)argc;
150   (void)argv;
151
152   run_mbox_test ();
153   run_dns_test ();
154
155   return 0;
156 }