~ubuntu-branches/ubuntu/trusty/libtasn1-3/trusty

« back to all changes in this revision

Viewing changes to tests/Test_encoding.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2006-06-07 20:14:52 UTC
  • Revision ID: james.westby@ubuntu.com-20060607201452-7a9dojyu7i2kfbyp
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Copyright (C) 2006 Free Software Foundation
 
3
 *      Copyright (C) 2002 Fabio Fiorina
 
4
 *
 
5
 * This file is part of LIBTASN1.
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU General Public License as
 
9
 * published by the Free Software Foundation; either version 2 of the
 
10
 * License, or (at your option) any later version.
 
11
 *
 
12
 * This program 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 this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
20
 * 02110-1301, USA
 
21
 */
 
22
 
 
23
 
 
24
/******************************************************/
 
25
/* File: Test_encoding.c                              */
 
26
/* Description: Test writing values and DER encoding. */
 
27
/******************************************************/
 
28
 
 
29
#include <stdio.h>
 
30
#include <string.h>
 
31
#include <stdlib.h>
 
32
#include "libtasn1.h"
 
33
 
 
34
 
 
35
unsigned char data[256];
 
36
int data_size = sizeof (data);
 
37
 
 
38
 
 
39
int
 
40
main (int argc, char *argv[])
 
41
{
 
42
  asn1_retCode result;
 
43
  ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
 
44
  ASN1_TYPE asn1_element = ASN1_TYPE_EMPTY;
 
45
  char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
 
46
  const char *treefile = getenv ("ASN1ENCODING");
 
47
 
 
48
  if (!treefile)
 
49
    treefile = "Test_encoding.asn";
 
50
 
 
51
  printf ("\n\n/****************************************/\n");
 
52
  printf ("/*     Test sequence : coding-decoding  */\n");
 
53
  printf ("/****************************************/\n\n");
 
54
 
 
55
  /* Check version */
 
56
  if (asn1_check_version ("0.3.3") == NULL)
 
57
    printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
 
58
            asn1_check_version (NULL));
 
59
 
 
60
  result = asn1_parser2tree (treefile, &definitions, errorDescription);
 
61
 
 
62
  if (result != ASN1_SUCCESS)
 
63
    {
 
64
      libtasn1_perror (result);
 
65
      printf ("ErrorDescription = %s\n\n", errorDescription);
 
66
      exit (1);
 
67
    }
 
68
 
 
69
  result = asn1_create_element (definitions, "TEST_TREE.Koko", &asn1_element);
 
70
  if (result != ASN1_SUCCESS)
 
71
    {
 
72
      fprintf (stderr, "asn1_create_element(): ");
 
73
      libtasn1_perror (result);
 
74
      exit (1);
 
75
    }
 
76
 
 
77
  result = asn1_write_value (asn1_element, "seqint", "NEW", 1);
 
78
  if (result != ASN1_SUCCESS)
 
79
    {
 
80
      fprintf (stderr, "asn1_write_value(): seqint ");
 
81
      libtasn1_perror (result);
 
82
      exit (1);
 
83
    }
 
84
 
 
85
  result = asn1_write_value (asn1_element, "seqint.?LAST", "1234", 0);
 
86
  if (result != ASN1_SUCCESS)
 
87
    {
 
88
      fprintf (stderr, "asn1_write_value(): seqint.?LAST ");
 
89
      libtasn1_perror (result);
 
90
      exit (1);
 
91
    }
 
92
 
 
93
  result = asn1_write_value (asn1_element, "int", "\x0f\xff\x01", 3);
 
94
  if (result != ASN1_SUCCESS)
 
95
    {
 
96
      fprintf (stderr, "asn1_write_value(): int ");
 
97
      libtasn1_perror (result);
 
98
      exit (1);
 
99
    }
 
100
 
 
101
  result = asn1_write_value (asn1_element, "str", "string", 6);
 
102
  if (result != ASN1_SUCCESS)
 
103
    {
 
104
      fprintf (stderr, "asn1_write_value(): str ");
 
105
      libtasn1_perror (result);
 
106
      exit (1);
 
107
    }
 
108
 
 
109
  /* Clear the definition structures */
 
110
  asn1_delete_structure (&definitions);
 
111
 
 
112
  result = asn1_der_coding (asn1_element, "", data, &data_size, NULL);
 
113
  if (result != ASN1_SUCCESS)
 
114
    {
 
115
      fprintf (stderr, "Encoding error.\n");
 
116
      libtasn1_perror (result);
 
117
      exit (1);
 
118
    }
 
119
 
 
120
  result = asn1_der_decoding (&asn1_element, data, data_size, NULL);
 
121
  if (result != ASN1_SUCCESS)
 
122
    {
 
123
      fprintf (stderr, "Decoding error.\n");
 
124
      libtasn1_perror (result);
 
125
      exit (1);
 
126
    }
 
127
 
 
128
 
 
129
  printf ("Success\n");
 
130
  exit (0);
 
131
}