unit-tests: Updated asn1-parser tests
authorAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 5 May 2017 18:57:28 +0000 (20:57 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Mon, 29 May 2017 09:05:04 +0000 (11:05 +0200)
src/libstrongswan/tests/suites/test_asn1_parser.c

index 973562b..ebbe7dd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Andreas Steffen
+ * Copyright (C) 2014-2017 Andreas Steffen
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -250,6 +250,7 @@ START_TEST(test_asn1_parser_option)
                                i3 = *object.ptr;
                                break;
                        default:
+
                                break;
                }
        }
@@ -264,6 +265,105 @@ START_TEST(test_asn1_parser_option)
 }
 END_TEST
 
+/*******************************************************************************
+ * choice
+ */
+
+typedef struct {
+       int i1, i2, i3, i4;
+       chunk_t blob;
+} choice_test_t;
+
+static const asn1Object_t choiceObjects[] = {
+       { 0, "choiceObject",      ASN1_EOC,          ASN1_CHOICE          }, /*  0 */
+       { 1,   "choiceA",         ASN1_CONTEXT_C_0,  ASN1_OPT|ASN1_CHOICE }, /*  1 */
+       { 2,     "choice1",       ASN1_OCTET_STRING, ASN1_OPT|ASN1_BODY   }, /*  2 */
+       { 2,     "end choice1",   ASN1_EOC,          ASN1_END|ASN1_CH     }, /*  3 */
+       { 2,     "choice2",       ASN1_INTEGER,      ASN1_OPT|ASN1_BODY   }, /*  4 */
+       { 2,     "end choice2",   ASN1_EOC,          ASN1_END|ASN1_CH     }, /*  5 */
+       { 1,   "end choiceA",     ASN1_EOC,          ASN1_END|ASN1_CHOICE|
+                                                    ASN1_CH              }, /*  6 */
+       { 1,   "choiceB",         ASN1_SEQUENCE,     ASN1_OPT|ASN1_LOOP   }, /*  7 */
+       { 2,     "choiceObject",  ASN1_EOC,          ASN1_CHOICE          }, /*  8 */
+       { 3,       "choice3",     ASN1_INTEGER,      ASN1_OPT|ASN1_BODY   }, /*  9 */
+       { 3,       "end choice3", ASN1_EOC,          ASN1_END|ASN1_CH     }, /* 10 */
+       { 3,       "choice4",     ASN1_OCTET_STRING, ASN1_OPT|ASN1_BODY   }, /* 11 */
+       { 3,       "end choice4", ASN1_EOC,          ASN1_END|ASN1_CH     }, /* 12 */
+       { 2,     "end choices",   ASN1_EOC,          ASN1_END|ASN1_CHOICE }, /* 13 */
+       { 1,   "end loop/choice", ASN1_EOC,          ASN1_END|ASN1_CH     }, /* 14 */
+       { 0, "end choices",       ASN1_EOC,          ASN1_END|ASN1_CHOICE }, /* 15 */
+       { 0, "exit",              ASN1_EOC,          ASN1_EXIT            }
+};
+
+choice_test_t choice_tests[] = {
+       { 0, 0, 0, 0, { NULL, 0 } },
+       { 0, 0, 0, 0, chunk_from_chars(0xA0, 0x00) },
+       { 1, 0, 0, 0, chunk_from_chars(0xA0, 0x03, 0x04, 0x01, 0x01) },
+       { 1, 0, 0, 0, chunk_from_chars(0xA0, 0x06, 0x04, 0x01, 0x01,
+                                                  0x02, 0x01, 0x02) },
+       { 0, 2, 0, 0, chunk_from_chars(0xA0, 0x03, 0x02, 0x01, 0x02) },
+       { 0, 2, 0, 0, chunk_from_chars(0xA0, 0x03, 0x02, 0x01, 0x02,
+                                      0x30, 0x03, 0x02, 0x01, 0x03) },
+       { 0, 0, 0, 0, chunk_from_chars(0xA0, 0x04, 0x03, 0x02, 0x00, 0x04) },
+       { 0, 0, 3, 0, chunk_from_chars(0x30, 0x03, 0x02, 0x01, 0x03) },
+       { 0, 0, 0, 4, chunk_from_chars(0x30, 0x03, 0x04, 0x01, 0x04) },
+       { 0, 0, 3, 4, chunk_from_chars(0x30, 0x06, 0x04, 0x01, 0x04,
+                                                  0x02, 0x01, 0x03) },
+       { 0, 0, 3, 4, chunk_from_chars(0x30, 0x06, 0x02, 0x01, 0x03,
+                                                  0x04, 0x01, 0x04) },
+       { 0, 0, 6, 0, chunk_from_chars(0x30, 0x06, 0x02, 0x01, 0x03,
+                                                  0x02, 0x01, 0x03) },
+       { 0, 0, 0, 8, chunk_from_chars(0x30, 0x06, 0x04, 0x01, 0x04,
+                                                  0x04, 0x01, 0x04) },
+       { 0, 0, 0, 0, chunk_from_chars(0x30, 0x04, 0x03, 0x02, 0x00, 0x04) },
+       { 0, 0, 0, 0, chunk_from_chars(0x03, 0x02, 0x00, 0x04) }
+};
+
+START_TEST(test_asn1_parser_choice)
+{
+       asn1_parser_t *parser;
+       chunk_t object;
+       int objectID, i1 = 0, i2 = 0, i3 = 0, i4 = 0;
+       bool success;
+
+       parser = asn1_parser_create(choiceObjects, choice_tests[_i].blob);
+       while (parser->iterate(parser, &objectID, &object))
+       {
+               switch (objectID)
+               {
+                       case 2:
+                               i1 += *object.ptr;
+                               break;
+                       case 4:
+                               i2 += *object.ptr;
+                               break;
+                       case 9:
+                               i3 += *object.ptr;
+                               break;
+                       case 11:
+                               i4 += *object.ptr;
+                               break;
+                       default:
+
+                               break;
+               }
+       }
+       success = parser->success(parser);
+       parser->destroy(parser);
+
+       ck_assert(success == (choice_tests[_i].i1 ||
+                                                 choice_tests[_i].i2 ||
+                                                 choice_tests[_i].i3 ||
+                                                 choice_tests[_i].i4 ));
+
+       ck_assert(i1 == choice_tests[_i].i1 &&
+                         i2 == choice_tests[_i].i2 &&
+                         i3 == choice_tests[_i].i3 &&
+                         i4 == choice_tests[_i].i4 );
+}
+END_TEST
+
+
 Suite *asn1_parser_suite_create()
 {
        Suite *s;
@@ -287,5 +387,9 @@ Suite *asn1_parser_suite_create()
        tcase_add_loop_test(tc, test_asn1_parser_option, 0, countof(option_tests));
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("choice");
+       tcase_add_loop_test(tc, test_asn1_parser_choice, 0, countof(choice_tests));
+       suite_add_tcase(s, tc);
+
        return s;
 }