diff options
author | Pawel Kowalski <p.kowalski2@partner.samsung.com> | 2019-08-28 14:31:23 +0200 |
---|---|---|
committer | Pawel Kowalski <p.kowalski2@partner.samsung.com> | 2019-08-28 16:56:31 +0200 |
commit | dee455252ad321c660b411b4ecd65aa5e08f8fc1 (patch) | |
tree | b55f9ec039e963e42605e9e9bdc186354cf9df42 /tests/setof.c | |
parent | 5c7e0aaf17a296535495e3431db6a86ad64064b3 (diff) | |
parent | 26bea900a531662c6028ecc06f4adea825658434 (diff) | |
download | libtasn1-dee455252ad321c660b411b4ecd65aa5e08f8fc1.tar.gz libtasn1-dee455252ad321c660b411b4ecd65aa5e08f8fc1.tar.bz2 libtasn1-dee455252ad321c660b411b4ecd65aa5e08f8fc1.zip |
Merge tag 'upstream/4.14' into tizentizen_6.5.m2_releasetizen_6.0.m2_releasetizen_5.5.m2_releasesubmit/tizen_6.5/20211028.163501submit/tizen_6.0_hotfix/20201103.115102submit/tizen_6.0_hotfix/20201102.192902submit/tizen_6.0/20201029.205502submit/tizen_5.5_wearable_hotfix/20201026.184307submit/tizen_5.5_mobile_hotfix/20201026.185107submit/tizen_5.5/20191031.000007submit/tizen/20190904.070216submit/tizen/20190829.061506accepted/tizen/unified/20190904.110630accepted/tizen/6.5/unified/20211029.014057accepted/tizen/6.0/unified/hotfix/20201103.050702accepted/tizen/6.0/unified/hotfix/20201102.234932accepted/tizen/6.0/unified/20201030.110146accepted/tizen/5.5/unified/wearable/hotfix/20201027.100357accepted/tizen/5.5/unified/mobile/hotfix/20201027.074057accepted/tizen/5.5/unified/20191031.011500tizen_6.5tizen_6.0_hotfixtizen_6.0tizen_5.5_wearable_hotfixtizen_5.5_tvtizen_5.5_mobile_hotfixtizen_5.5accepted/tizen_6.5_unifiedaccepted/tizen_6.0_unified_hotfixaccepted/tizen_6.0_unifiedaccepted/tizen_5.5_unified_wearable_hotfixaccepted/tizen_5.5_unified_mobile_hotfixaccepted/tizen_5.5_unified
Change-Id: I6d4b47b2072db8e7c45daa6e269e2f0cc6f54a7f
Diffstat (limited to 'tests/setof.c')
-rw-r--r-- | tests/setof.c | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/tests/setof.c b/tests/setof.c new file mode 100644 index 0000000..ea4de9b --- /dev/null +++ b/tests/setof.c @@ -0,0 +1,184 @@ +/* + * 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); +} |