~ubuntu-branches/ubuntu/lucid/gpgme1.0/lucid

« back to all changes in this revision

Viewing changes to tests/gpg/t-sign.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Carlos Garcia Sogo
  • Date: 2004-11-16 21:36:40 UTC
  • Revision ID: james.westby@ubuntu.com-20041116213640-6ffmegu7bqe05u7l
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* t-sign.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
/* We need to include config.h so that we know whether we are building
 
22
   with large file system (LFS) support. */
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <stdlib.h>
 
28
#include <stdio.h>
 
29
#include <string.h>
 
30
#include <unistd.h>
 
31
 
 
32
#include <gpgme.h>
 
33
 
 
34
#include "t-support.h"
 
35
 
 
36
 
 
37
static void
 
38
check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
 
39
{
 
40
  if (result->invalid_signers)
 
41
    {
 
42
      fprintf (stderr, "Invalid signer found: %s\n",
 
43
               result->invalid_signers->fpr);
 
44
      exit (1);
 
45
    }
 
46
  if (!result->signatures || result->signatures->next)
 
47
    {
 
48
      fprintf (stderr, "Unexpected number of signatures created\n");
 
49
      exit (1);
 
50
    }
 
51
  if (result->signatures->type != type)
 
52
    {
 
53
      fprintf (stderr, "Wrong type of signature created\n");
 
54
      exit (1);
 
55
    }
 
56
  if (result->signatures->pubkey_algo != GPGME_PK_DSA)
 
57
    {
 
58
      fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
 
59
               result->signatures->pubkey_algo);
 
60
      exit (1);
 
61
    }
 
62
  if (result->signatures->hash_algo != GPGME_MD_SHA1)
 
63
    {
 
64
      fprintf (stderr, "Wrong hash algorithm reported: %i\n",
 
65
               result->signatures->hash_algo);
 
66
      exit (1);
 
67
    }
 
68
  if (result->signatures->sig_class != 1)
 
69
    {
 
70
      fprintf (stderr, "Wrong signature class reported: %u\n",
 
71
               result->signatures->sig_class);
 
72
      exit (1);
 
73
    }
 
74
  if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
 
75
              result->signatures->fpr))
 
76
    {
 
77
      fprintf (stderr, "Wrong fingerprint reported: %s\n",
 
78
               result->signatures->fpr);
 
79
      exit (1);
 
80
    }
 
81
}
 
82
 
 
83
 
 
84
int 
 
85
main (int argc, char **argv)
 
86
{
 
87
  gpgme_ctx_t ctx;
 
88
  gpgme_error_t err;
 
89
  gpgme_data_t in, out;
 
90
  gpgme_sign_result_t result;
 
91
  char *agent_info;
 
92
 
 
93
  init_gpgme (GPGME_PROTOCOL_OpenPGP);
 
94
 
 
95
  err = gpgme_new (&ctx);
 
96
  fail_if_err (err);
 
97
 
 
98
  agent_info = getenv ("GPG_AGENT_INFO");
 
99
  if (!(agent_info && strchr (agent_info, ':')))
 
100
    gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
 
101
 
 
102
  gpgme_set_textmode (ctx, 1);
 
103
  gpgme_set_armor (ctx, 1);
 
104
 
 
105
  err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
 
106
  fail_if_err (err);
 
107
 
 
108
  /* First a normal signature.  */
 
109
  err = gpgme_data_new (&out);
 
110
  fail_if_err (err);
 
111
  err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
 
112
  fail_if_err (err);
 
113
  result = gpgme_op_sign_result (ctx);
 
114
  check_result (result, GPGME_SIG_MODE_NORMAL);
 
115
  print_data (out);
 
116
  gpgme_data_release (out);
 
117
    
 
118
  /* Now a detached signature.  */ 
 
119
  gpgme_data_seek (in, 0, SEEK_SET);
 
120
  err = gpgme_data_new (&out);
 
121
  fail_if_err (err);
 
122
  err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
 
123
  fail_if_err (err);
 
124
  result = gpgme_op_sign_result (ctx);
 
125
  check_result (result, GPGME_SIG_MODE_DETACH);
 
126
  print_data (out);
 
127
  gpgme_data_release (out);
 
128
 
 
129
  /* And finally a cleartext signature.  */
 
130
  gpgme_data_seek (in, 0, SEEK_SET);
 
131
  err = gpgme_data_new (&out);
 
132
  fail_if_err (err);
 
133
  err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR);
 
134
  fail_if_err (err);
 
135
  result = gpgme_op_sign_result (ctx);
 
136
  check_result (result, GPGME_SIG_MODE_CLEAR);
 
137
  print_data (out);
 
138
  gpgme_data_release (out);
 
139
 
 
140
  gpgme_data_release (in);
 
141
  gpgme_release (ctx);
 
142
  return 0;
 
143
}