tests: added unit test of setof encoding

Signed-off-by: Nikos Mavrogiannopoulos's avatarNikos Mavrogiannopoulos <nmav@redhat.com>
parent 750f03a3
......@@ -25,7 +25,7 @@ LDADD = ../lib/libtasn1.la ../gl/libgnu.la
EXTRA_DIST = Test_parser.asn Test_tree.asn Test_tree_asn1_tab.c mscat.asn \
Test_encoding.asn pkix.asn TestIndef.p12 choice.asn coding-decoding2.asn \
TestIndef2.p12 TestIndef3.der TestCertOctetOverflow.der \
cert-invalid-time.der \
cert-invalid-time.der setof.asn \
libtasn1.supp ocsp-basic-response.der spc_pe_image_data.der \
invalid-x509/id-000000.der invalid-x509/id-000001.der \
invalid-x509/id-000002.der invalid-x509/id-000003.der \
......@@ -60,14 +60,14 @@ check_PROGRAMS = Test_parser Test_tree Test_encoding Test_indefinite \
Test_errors Test_simple Test_overflow Test_strings Test_choice \
Test_encdec copynode coding-decoding2 strict-der Test_choice_ocsp \
ocsp-basic-response octet-string coding-long-oid object-id-decoding \
spc_pe_image_data
spc_pe_image_data setof
TESTS = Test_parser Test_tree Test_encoding Test_indefinite \
Test_errors Test_simple Test_overflow crlf threadsafety \
Test_strings Test_choice Test_encdec copynode coding-decoding2 \
strict-der Test_choice_ocsp decoding decoding-invalid-x509 \
ocsp-basic-response octet-string coding-long-oid object-id-decoding \
spc_pe_image_data decoding-invalid-pkcs7 coding
spc_pe_image_data decoding-invalid-pkcs7 coding setof
TESTS_ENVIRONMENT = \
ASN1PARSER=$(srcdir)/Test_parser.asn \
......@@ -75,6 +75,7 @@ TESTS_ENVIRONMENT = \
ASN1CHOICE=$(srcdir)/choice.asn \
ASN1CODINGDECODING2=$(srcdir)/coding-decoding2.asn \
ASN1PKIX=$(srcdir)/pkix.asn \
ASN1SETOF=$(srcdir)/setof.asn \
ASN1CRLDER=$(srcdir)/crl.der \
ASN1INDEF=$(srcdir)/TestIndef.p12 \
ASN1INDEF2=$(srcdir)/TestIndef2.p12 \
......
TEST {}
DEFINITIONS IMPLICIT TAGS ::=
BEGIN
Seq ::= SEQUENCE {
val OCTET STRING
}
Set ::= SET OF Seq
END
/*
* Copyright (C) 2002-2018 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/>.
*
*/
/****************************************************************/
/* File: Test_setof.c */
/* Description: Test writing of SET OF values and DER encoding. */
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "libtasn1.h"
static unsigned char expected_der[] = {
0x31, 0x40, 0x30, 0x03, 0x04, 0x01, 0x01, 0x30,
0x03, 0x04, 0x01, 0x05, 0x30, 0x04, 0x04, 0x02,
0x00, 0x02, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x01, 0x30, 0x06, 0x04, 0x04, 0x00, 0x00,
0x00, 0x02, 0x30, 0x06, 0x04, 0x04, 0x00, 0x01,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x01, 0x00,
0x00, 0x00, 0x30, 0x06, 0x04, 0x04, 0x01, 0x01,
0x00, 0x00
};
static unsigned char data[1024];
int data_size = sizeof (data);
int
main (int argc, char *argv[])
{
int result, verbose = 0;
asn1_node definitions = NULL;
asn1_node asn1_element = NULL;
char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
const char *treefile = getenv ("ASN1SETOF");
unsigned i;
if (argc > 1)
verbose = 1;
if (!treefile)
treefile = "setof.asn";
if (verbose != 0)
{
printf ("\n\n/****************************************/\n");
printf ("/* Test sequence : coding-decoding */\n");
printf ("/****************************************/\n\n");
}
/* Check version */
if (asn1_check_version ("0.3.3") == NULL)
printf ("\nLibrary version check ERROR:\n actual version: %s\n\n",
asn1_check_version (NULL));
result = asn1_parser2tree (treefile, &definitions, errorDescription);
if (result != ASN1_SUCCESS)
{
asn1_perror (result);
printf ("ErrorDescription = %s\n\n", errorDescription);
exit (1);
}
result = asn1_create_element (definitions, "TEST.Set", &asn1_element);
if (result != ASN1_SUCCESS)
{
fprintf (stderr, "asn1_create_element(): ");
asn1_perror (result);
exit (1);
}
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x02", 2);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x01\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x02", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x00\x00\x00\x01", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x00\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01\x01\x00\x00", 4);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x05", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "", "NEW", 1);
assert(result == ASN1_SUCCESS);
result = asn1_write_value (asn1_element, "?LAST.val", "\x01", 1);
assert(result == ASN1_SUCCESS);
/* Clear the definition structures */
result = asn1_der_coding (asn1_element, "", data, &data_size, NULL);
if (result != ASN1_SUCCESS)
{
fprintf (stderr, "Encoding error.\n");
asn1_perror (result);
exit (1);
}
asn1_delete_structure (&asn1_element);
asn1_delete_structure (&definitions);
if (data_size != sizeof(expected_der) || memcmp(data, expected_der, data_size) != 0)
{
fprintf(stderr, "encoded data differ to expected [%d - %d]!\n", data_size, (int)sizeof(expected_der));
printf("static unsigned char got[] = {\n");
for (i=0;i<(unsigned)data_size;i++) {
printf("0x%.2x, ", (unsigned)data[i]);
if ((i+1) % 8 == 0)
printf("\n");
}
printf("};\n");
printf("static unsigned char expected[] = {\n");
for (i=0;i<(unsigned)sizeof(expected_der);i++) {
printf("0x%.2x, ", (unsigned)expected_der[i]);
if ((i+1) % 8 == 0)
printf("\n");
}
printf("};\n");
exit(1);
}
if (verbose)
printf ("Success\n");
exit (0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment