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

« back to all changes in this revision

Viewing changes to tests/gpgsm/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
 
 
31
#include <gpgme.h>
 
32
#include "t-support.h"
 
33
 
 
34
 
 
35
static void
 
36
check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
 
37
{
 
38
  if (result->invalid_signers)
 
39
    {
 
40
      fprintf (stderr, "Invalid signer found: %s\n",
 
41
               result->invalid_signers->fpr);
 
42
      exit (1);
 
43
    }
 
44
  if (!result->signatures || result->signatures->next)
 
45
    {
 
46
      fprintf (stderr, "Unexpected number of signatures created\n");
 
47
      exit (1);
 
48
    }
 
49
  if (result->signatures->type != type)
 
50
    {
 
51
      fprintf (stderr, "Wrong type of signature created\n");
 
52
      exit (1);
 
53
    }
 
54
  if (result->signatures->pubkey_algo != GPGME_PK_RSA)
 
55
    {
 
56
      fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
 
57
               result->signatures->pubkey_algo);
 
58
      exit (1);
 
59
    }
 
60
  if (result->signatures->hash_algo != GPGME_MD_SHA1)
 
61
    {
 
62
      fprintf (stderr, "Wrong hash algorithm reported: %i\n",
 
63
               result->signatures->hash_algo);
 
64
      exit (1);
 
65
    }
 
66
  if (result->signatures->sig_class != 0)
 
67
    {
 
68
      fprintf (stderr, "Wrong signature class reported: %u\n",
 
69
               result->signatures->sig_class);
 
70
      exit (1);
 
71
    }
 
72
  if (strcmp ("3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
 
73
              result->signatures->fpr))
 
74
    {
 
75
      fprintf (stderr, "Wrong fingerprint reported: %s\n",
 
76
               result->signatures->fpr);
 
77
      exit (1);
 
78
    }
 
79
}
 
80
 
 
81
 
 
82
int 
 
83
main (int argc, char *argv[])
 
84
{
 
85
  gpgme_ctx_t ctx;
 
86
  gpgme_error_t err;
 
87
  gpgme_data_t in, out;
 
88
  gpgme_sign_result_t result;
 
89
 
 
90
  init_gpgme (GPGME_PROTOCOL_CMS);
 
91
 
 
92
  err = gpgme_new (&ctx);
 
93
  fail_if_err (err);
 
94
 
 
95
  gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
 
96
  gpgme_set_textmode (ctx, 1);
 
97
  gpgme_set_armor (ctx, 1);
 
98
 
 
99
  err = gpgme_data_new_from_mem (&in, "Hallo Leute!\n", 13, 0);
 
100
  fail_if_err (err);
 
101
 
 
102
  /* First a normal signature.  */
 
103
  err = gpgme_data_new (&out);
 
104
  fail_if_err (err);
 
105
  err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
 
106
  fail_if_err (err);
 
107
  result = gpgme_op_sign_result (ctx);
 
108
  check_result (result, GPGME_SIG_MODE_NORMAL);
 
109
  print_data (out);
 
110
  gpgme_data_release (out);
 
111
    
 
112
  /* Now a detached signature.  */ 
 
113
  gpgme_data_seek (in, 0, SEEK_SET);
 
114
  err = gpgme_data_new (&out);
 
115
  fail_if_err (err);
 
116
  err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
 
117
  fail_if_err (err);
 
118
  result = gpgme_op_sign_result (ctx);
 
119
  check_result (result, GPGME_SIG_MODE_DETACH);
 
120
  print_data (out);
 
121
  gpgme_data_release (out);
 
122
 
 
123
  gpgme_data_release (in);
 
124
  gpgme_release (ctx);
 
125
  return 0;
 
126
}