Home | History | Annotate | Line # | Download | only in testutil
      1 /*
      2  * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
      3  *
      4  * Licensed under the OpenSSL license (the "License").  You may not use
      5  * this file except in compliance with the License.  You can obtain a copy
      6  * in the file LICENSE in the source distribution or at
      7  * https://www.openssl.org/source/license.html
      8  */
      9 
     10 #include "../testutil.h"
     11 #include "internal/nelem.h"
     12 #include "output.h"
     13 #include "tu_local.h"
     14 
     15 #include <string.h>
     16 
     17 static size_t arg_count;
     18 static char **args;
     19 static unsigned char arg_used[1000];
     20 
     21 static void check_arg_usage(void)
     22 {
     23     size_t i, n = arg_count < OSSL_NELEM(arg_used) ? arg_count
     24                                                    : OSSL_NELEM(arg_used);
     25 
     26     for (i = 0; i < n; i++)
     27         if (!arg_used[i+1])
     28             test_printf_stderr("Warning ignored command-line argument %zu: %s\n",
     29                                i, args[i+1]);
     30     if (i < arg_count)
     31         test_printf_stderr("Warning arguments %zu and later unchecked\n", i);
     32 }
     33 
     34 int main(int argc, char *argv[])
     35 {
     36     int ret = EXIT_FAILURE;
     37 
     38     test_open_streams();
     39 
     40     if (!global_init()) {
     41         test_printf_stderr("Global init failed - aborting\n");
     42         return ret;
     43     }
     44 
     45     arg_count = argc - 1;
     46     args = argv;
     47 
     48     setup_test_framework();
     49 
     50     if (setup_tests())
     51         ret = run_tests(argv[0]);
     52     cleanup_tests();
     53     check_arg_usage();
     54 
     55     ret = pulldown_test_framework(ret);
     56     test_close_streams();
     57     return ret;
     58 }
     59 
     60 const char *test_get_program_name(void)
     61 {
     62     return args[0];
     63 }
     64 
     65 char *test_get_argument(size_t n)
     66 {
     67     if (n > arg_count)
     68         return NULL;
     69     if (n + 1 < OSSL_NELEM(arg_used))
     70         arg_used[n + 1] = 1;
     71     return args[n + 1];
     72 }
     73 
     74 size_t test_get_argument_count(void)
     75 {
     76     return arg_count;
     77 }
     78 
     79 int test_has_option(const char *option)
     80 {
     81     size_t i;
     82 
     83     for (i = 1; i <= arg_count; i++)
     84         if (strcmp(args[i], option) == 0) {
     85             arg_used[i] = 1;
     86             return 1;
     87         }
     88     return 0;
     89 }
     90 
     91 const char *test_get_option_argument(const char *option)
     92 {
     93     size_t i, n = strlen(option);
     94 
     95     for (i = 1; i <= arg_count; i++)
     96         if (strncmp(args[i], option, n) == 0) {
     97             arg_used[i] = 1;
     98             if (args[i][n] == '\0' && i + 1 < arg_count) {
     99                 arg_used[++i] = 1;
    100                 return args[i];
    101             }
    102             return args[i] + n;
    103         }
    104     return NULL;
    105 }
    106 
    107