summaryrefslogtreecommitdiff
path: root/tests/Test_strings.c
blob: a69774a73ac21c01bc44496bc6f6673193b44245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2012-2013 Free Software Foundation, Inc.
 *
 * This file is part of LIBTASN1.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Written by Simon Josefsson
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "libtasn1.h"

struct tv
{
  unsigned int etype;
  unsigned int str_len;
  const void *str;
  unsigned int der_len;
  const void *der;
};

static const struct tv tv[] = {
  {ASN1_ETYPE_IA5_STRING, 20,
   "\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72",
   22,
   "\x16\x14\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72"},
  {ASN1_ETYPE_PRINTABLE_STRING, 5, "\x4e\x69\x6b\x6f\x73",
   7, "\x13\x05\x4e\x69\x6b\x6f\x73"},
  {ASN1_ETYPE_UTF8_STRING, 12, "Αττική",
   14, "\x0c\x0c\xce\x91\xcf\x84\xcf\x84\xce\xb9\xce\xba\xce\xae"},
  {ASN1_ETYPE_TELETEX_STRING, 15,
   "\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e",
   17,
   "\x14\x0f\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e"},
  {ASN1_ETYPE_OCTET_STRING, 36,
   "\x30\x22\x80\x0F\x32\x30\x31\x31\x30\x38\x32\x31\x30\x38\x30\x30\x30\x36\x5A\x81\x0F\x32\x30\x31\x31\x30\x38\x32\x33\x32\x30\x35\x39\x35\x39\x5A",
   38,
   "\x04\x24\x30\x22\x80\x0F\x32\x30\x31\x31\x30\x38\x32\x31\x30\x38\x30\x30\x30\x36\x5A\x81\x0F\x32\x30\x31\x31\x30\x38\x32\x33\x32\x30\x35\x39\x35\x39\x5A"}
};

int
main (int argc, char *argv[])
{
  int ret;
  unsigned char tl[ASN1_MAX_TL_SIZE];
  unsigned int tl_len, der_len, str_len;
  const unsigned char *str;
  unsigned int i;

  /* Dummy test */

  for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
    {
      /* Encode */
      tl_len = sizeof (tl);
      ret = asn1_encode_simple_der (tv[i].etype, tv[i].str, tv[i].str_len,
				    tl, &tl_len);
      if (ret != ASN1_SUCCESS)
	{
	  fprintf (stderr, "Encoding error in %u: %s\n", i,
		   asn1_strerror (ret));
	  return 1;
	}
      der_len = tl_len + tv[i].str_len;

      if (der_len != tv[i].der_len || memcmp (tl, tv[i].der, tl_len) != 0)
	{
	  fprintf (stderr,
		   "DER encoding differs in %u! (size: %u, expected: %u)\n",
		   i, der_len, tv[i].der_len);
	  return 1;
	}

      /* decoding */
      ret =
	asn1_decode_simple_der (tv[i].etype, tv[i].der, tv[i].der_len, &str,
				&str_len);
      if (ret != ASN1_SUCCESS)
	{
	  fprintf (stderr, "Decoding error in %u: %s\n", i,
		   asn1_strerror (ret));
	  return 1;
	}

      if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0)
	{
	  fprintf (stderr,
		   "DER decoded data differ in %u! (size: %u, expected: %u)\n",
		   i, der_len, tv[i].str_len);
	  return 1;
	}
    }


  return 0;
}