doc/
[gpgme.git] / tests / gpg / t-genkey.c
1 /* t-genkey.c  - regression test
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2003 g10 Code GmbH
4
5    This file is part of GPGME.
6  
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU 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    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16  
17    You should have received a copy of the GNU General Public License
18    along with GPGME; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <gpgme.h>
25
26 #define fail_if_err(err)                                        \
27   do                                                            \
28     {                                                           \
29       if (err)                                                  \
30         {                                                       \
31           fprintf (stderr, "%s:%d: GpgmeError %s\n",            \
32                    __FILE__, __LINE__, gpgme_strerror (err));   \
33           exit (1);                                             \
34         }                                                       \
35     }                                                           \
36   while (0)
37
38 /* True if progress function printed something on the screen.  */
39 int progress_called;
40
41 static void
42 progress (void *self, const char *what, int type, int current, int total)
43 {
44   if (!strcmp (what, "primegen") && !current && !total
45       && (type == '.' || type == '+' || type == '!'
46           || type == '^' || type == '<' || type == '>'))
47     {
48       printf ("%c", type);
49       fflush (stdout);
50       progress_called = 1;
51     }
52   else
53     {
54       fprintf (stderr, "unknown progress `%s' %d %d %d\n", what, type,
55                current, total);
56       exit (1);
57     }
58 }
59
60
61 int 
62 main (int argc, char **argv)
63 {
64   GpgmeCtx ctx;
65   GpgmeError err;
66   const char *parms = "<GnupgKeyParms format=\"internal\">\n"
67     "Key-Type: DSA\n"
68     "Key-Length: 1024\n"
69     "Subkey-Type: ELG-E\n"
70     "Subkey-Length: 1024\n"
71     "Name-Real: Joe Tester\n"
72     "Name-Comment: (pp=abc)\n"
73     "Name-Email: joe@foo.bar\n"
74     "Expire-Date: 0\n"
75     "Passphrase: abc\n"
76     "</GnupgKeyParms>\n";
77   GpgmeGenKeyResult result;
78
79   err = gpgme_new (&ctx);
80   fail_if_err (err);
81
82   gpgme_set_progress_cb (ctx, progress, NULL);
83   
84   err = gpgme_op_genkey (ctx, parms, NULL, NULL);
85   fail_if_err (err);
86
87   result = gpgme_op_genkey_result (ctx);
88   if (!result)
89     {
90       fprintf (stderr, "%s:%d: gpgme_op_genkey_result returns NULL\n",
91                __FILE__, __LINE__);
92       exit (1);
93     }
94   if (progress_called)
95     printf ("\n");
96
97   printf ("Generated key: %s (%s)\n", result->fpr ? result->fpr : "none",
98           result->primary ? (result->sub ? "primary, sub" : "primary")
99           : (result->sub ? "sub" : "none"));
100
101   if (strlen (result->fpr) != 40)
102     {
103       fprintf (stderr, "%s:%d: generated key has unexpected fingerprint\n",
104                __FILE__, __LINE__);
105       exit (1);
106     }
107   if (!result->primary)
108     {
109       fprintf (stderr, "%s:%d: primary key was not generated\n",
110                __FILE__, __LINE__);
111       exit (1);
112     }
113   if (!result->sub)
114     {
115       fprintf (stderr, "%s:%d: sub key was not generated\n",
116                __FILE__, __LINE__);
117       exit (1);
118     }
119   gpgme_release (ctx);
120   return 0;
121 }