asn1_read_value() and friends understand the ?CURRENT keyword.
[platform/upstream/libtasn1.git] / tests / Test_strings.c
1 /*
2  * Copyright (C) 2012-2014 Free Software Foundation, Inc.
3  *
4  * This file is part of LIBTASN1.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Written by Simon Josefsson
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "libtasn1.h"
28
29 struct tv
30 {
31   unsigned int etype;
32   unsigned int str_len;
33   const void *str;
34   unsigned int der_len;
35   const void *der;
36 };
37
38 static const struct tv tv[] = {
39   {ASN1_ETYPE_IA5_STRING, 20,
40    "\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72",
41    22,
42    "\x16\x14\x63\x73\x63\x61\x40\x70\x61\x73\x73\x70\x6f\x72\x74\x2e\x67\x6f\x76\x2e\x67\x72"},
43   {ASN1_ETYPE_PRINTABLE_STRING, 5, "\x4e\x69\x6b\x6f\x73",
44    7, "\x13\x05\x4e\x69\x6b\x6f\x73"},
45   {ASN1_ETYPE_UTF8_STRING, 12, "Αττική",
46    14, "\x0c\x0c\xce\x91\xcf\x84\xcf\x84\xce\xb9\xce\xba\xce\xae"},
47   {ASN1_ETYPE_TELETEX_STRING, 15,
48    "\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e",
49    17,
50    "\x14\x0f\x53\x69\x6d\x6f\x6e\x20\x4a\x6f\x73\x65\x66\x73\x73\x6f\x6e"},
51   {ASN1_ETYPE_OCTET_STRING, 36,
52    "\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",
53    38,
54    "\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"}
55 };
56
57 int
58 main (int argc, char *argv[])
59 {
60   int ret;
61   unsigned char tl[ASN1_MAX_TL_SIZE];
62   unsigned int tl_len, der_len, str_len;
63   const unsigned char *str;
64   unsigned int i;
65
66   /* Dummy test */
67
68   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
69     {
70       /* Encode */
71       tl_len = sizeof (tl);
72       ret = asn1_encode_simple_der (tv[i].etype, tv[i].str, tv[i].str_len,
73                                     tl, &tl_len);
74       if (ret != ASN1_SUCCESS)
75         {
76           fprintf (stderr, "Encoding error in %u: %s\n", i,
77                    asn1_strerror (ret));
78           return 1;
79         }
80       der_len = tl_len + tv[i].str_len;
81
82       if (der_len != tv[i].der_len || memcmp (tl, tv[i].der, tl_len) != 0)
83         {
84           fprintf (stderr,
85                    "DER encoding differs in %u! (size: %u, expected: %u)\n",
86                    i, der_len, tv[i].der_len);
87           return 1;
88         }
89
90       /* decoding */
91       ret =
92         asn1_decode_simple_der (tv[i].etype, tv[i].der, tv[i].der_len, &str,
93                                 &str_len);
94       if (ret != ASN1_SUCCESS)
95         {
96           fprintf (stderr, "Decoding error in %u: %s\n", i,
97                    asn1_strerror (ret));
98           return 1;
99         }
100
101       if (str_len != tv[i].str_len || memcmp (str, tv[i].str, str_len) != 0)
102         {
103           fprintf (stderr,
104                    "DER decoded data differ in %u! (size: %u, expected: %u)\n",
105                    i, der_len, tv[i].str_len);
106           return 1;
107         }
108     }
109
110
111   return 0;
112 }