xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/testutil/driver.c (revision 8fbed61efdd901c0e09614c9f45356aeeab23fe3)
113d40330Schristos /*
2*8fbed61eSchristos  * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
313d40330Schristos  *
4*8fbed61eSchristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
513d40330Schristos  * this file except in compliance with the License.  You can obtain a copy
613d40330Schristos  * in the file LICENSE in the source distribution or at
713d40330Schristos  * https://www.openssl.org/source/license.html
813d40330Schristos  */
913d40330Schristos 
1013d40330Schristos #include "../testutil.h"
1113d40330Schristos #include "output.h"
1213d40330Schristos #include "tu_local.h"
1313d40330Schristos 
1413d40330Schristos #include <string.h>
1513d40330Schristos #include <assert.h>
1613d40330Schristos 
1713d40330Schristos #include "internal/nelem.h"
1813d40330Schristos #include <openssl/bio.h>
1913d40330Schristos 
20*8fbed61eSchristos #include "platform.h"            /* From libapps */
21*8fbed61eSchristos 
22*8fbed61eSchristos #if defined(_WIN32) && !defined(__BORLANDC__)
2313d40330Schristos # define strdup _strdup
2413d40330Schristos #endif
2513d40330Schristos 
26*8fbed61eSchristos 
2713d40330Schristos /*
2813d40330Schristos  * Declares the structures needed to register each test case function.
2913d40330Schristos  */
3013d40330Schristos typedef struct test_info {
3113d40330Schristos     const char *test_case_name;
3213d40330Schristos     int (*test_fn) (void);
3313d40330Schristos     int (*param_test_fn)(int idx);
3413d40330Schristos     int num;
3513d40330Schristos 
3613d40330Schristos     /* flags */
3713d40330Schristos     int subtest:1;
3813d40330Schristos } TEST_INFO;
3913d40330Schristos 
4013d40330Schristos static TEST_INFO all_tests[1024];
4113d40330Schristos static int num_tests = 0;
42*8fbed61eSchristos static int show_list = 0;
43*8fbed61eSchristos static int single_test = -1;
44*8fbed61eSchristos static int single_iter = -1;
45*8fbed61eSchristos static int level = 0;
4613d40330Schristos static int seed = 0;
47*8fbed61eSchristos static int rand_order = 0;
48*8fbed61eSchristos 
4913d40330Schristos /*
50*8fbed61eSchristos  * A parameterised test runs a loop of test cases.
51*8fbed61eSchristos  * |num_test_cases| counts the total number of non-subtest test cases
5213d40330Schristos  * across all tests.
5313d40330Schristos  */
5413d40330Schristos static int num_test_cases = 0;
5513d40330Schristos 
56*8fbed61eSchristos static int process_shared_options(void);
57*8fbed61eSchristos 
58*8fbed61eSchristos 
add_test(const char * test_case_name,int (* test_fn)(void))5913d40330Schristos void add_test(const char *test_case_name, int (*test_fn) (void))
6013d40330Schristos {
6113d40330Schristos     assert(num_tests != OSSL_NELEM(all_tests));
6213d40330Schristos     all_tests[num_tests].test_case_name = test_case_name;
6313d40330Schristos     all_tests[num_tests].test_fn = test_fn;
6413d40330Schristos     all_tests[num_tests].num = -1;
6513d40330Schristos     ++num_tests;
6613d40330Schristos     ++num_test_cases;
6713d40330Schristos }
6813d40330Schristos 
add_all_tests(const char * test_case_name,int (* test_fn)(int idx),int num,int subtest)6913d40330Schristos void add_all_tests(const char *test_case_name, int(*test_fn)(int idx),
7013d40330Schristos                    int num, int subtest)
7113d40330Schristos {
7213d40330Schristos     assert(num_tests != OSSL_NELEM(all_tests));
7313d40330Schristos     all_tests[num_tests].test_case_name = test_case_name;
7413d40330Schristos     all_tests[num_tests].param_test_fn = test_fn;
7513d40330Schristos     all_tests[num_tests].num = num;
7613d40330Schristos     all_tests[num_tests].subtest = subtest;
7713d40330Schristos     ++num_tests;
78*8fbed61eSchristos     if (subtest)
79*8fbed61eSchristos         ++num_test_cases;
80*8fbed61eSchristos     else
8113d40330Schristos         num_test_cases += num;
8213d40330Schristos }
8313d40330Schristos 
gcd(int a,int b)8413d40330Schristos static int gcd(int a, int b)
8513d40330Schristos {
8613d40330Schristos     while (b != 0) {
8713d40330Schristos         int t = b;
8813d40330Schristos         b = a % b;
8913d40330Schristos         a = t;
9013d40330Schristos     }
9113d40330Schristos     return a;
9213d40330Schristos }
9313d40330Schristos 
set_seed(int s)94*8fbed61eSchristos static void set_seed(int s)
9513d40330Schristos {
96*8fbed61eSchristos     seed = s;
9713d40330Schristos     if (seed <= 0)
9813d40330Schristos         seed = (int)time(NULL);
99403eeac4Schristos     test_random_seed(seed);
10013d40330Schristos }
10113d40330Schristos 
102*8fbed61eSchristos 
setup_test_framework(int argc,char * argv[])103*8fbed61eSchristos int setup_test_framework(int argc, char *argv[])
104*8fbed61eSchristos {
105*8fbed61eSchristos     char *test_seed = getenv("OPENSSL_TEST_RAND_ORDER");
106*8fbed61eSchristos     char *TAP_levels = getenv("HARNESS_OSSL_LEVEL");
107*8fbed61eSchristos 
108*8fbed61eSchristos     if (TAP_levels != NULL)
109*8fbed61eSchristos         level = 4 * atoi(TAP_levels);
110*8fbed61eSchristos     test_adjust_streams_tap_level(level);
111*8fbed61eSchristos     if (test_seed != NULL) {
112*8fbed61eSchristos         rand_order = 1;
113*8fbed61eSchristos         set_seed(atoi(test_seed));
114*8fbed61eSchristos     } else {
115*8fbed61eSchristos         set_seed(0);
11613d40330Schristos     }
117*8fbed61eSchristos 
118*8fbed61eSchristos #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
119*8fbed61eSchristos     argv = copy_argv(&argc, argv);
120*8fbed61eSchristos #elif defined(_WIN32)
121*8fbed61eSchristos     /*
122*8fbed61eSchristos      * Replace argv[] with UTF-8 encoded strings.
123*8fbed61eSchristos      */
124*8fbed61eSchristos     win32_utf8argv(&argc, &argv);
12513d40330Schristos #endif
126*8fbed61eSchristos 
127*8fbed61eSchristos     if (!opt_init(argc, argv, test_get_options()))
128*8fbed61eSchristos         return 0;
129*8fbed61eSchristos     return 1;
13013d40330Schristos }
13113d40330Schristos 
132*8fbed61eSchristos 
133*8fbed61eSchristos /*
134*8fbed61eSchristos  * This can only be called after setup() has run, since num_tests and
135*8fbed61eSchristos  * all_tests[] are setup at this point
136*8fbed61eSchristos  */
check_single_test_params(char * name,char * testname,char * itname)137*8fbed61eSchristos static int check_single_test_params(char *name, char *testname, char *itname)
138*8fbed61eSchristos {
139*8fbed61eSchristos     if (name != NULL) {
140*8fbed61eSchristos         int i;
141*8fbed61eSchristos         for (i = 0; i < num_tests; ++i) {
142*8fbed61eSchristos             if (strcmp(name, all_tests[i].test_case_name) == 0) {
143*8fbed61eSchristos                 single_test = 1 + i;
144*8fbed61eSchristos                 break;
145*8fbed61eSchristos             }
146*8fbed61eSchristos         }
147*8fbed61eSchristos         if (i >= num_tests)
148*8fbed61eSchristos             single_test = atoi(name);
149*8fbed61eSchristos     }
150*8fbed61eSchristos 
151*8fbed61eSchristos 
152*8fbed61eSchristos     /* if only iteration is specified, assume we want the first test */
153*8fbed61eSchristos     if (single_test == -1 && single_iter != -1)
154*8fbed61eSchristos         single_test = 1;
155*8fbed61eSchristos 
156*8fbed61eSchristos     if (single_test != -1) {
157*8fbed61eSchristos         if (single_test < 1 || single_test > num_tests) {
158*8fbed61eSchristos             test_printf_stderr("Invalid -%s value "
159*8fbed61eSchristos                                "(Value must be a valid test name OR a value between %d..%d)\n",
160*8fbed61eSchristos                                testname, 1, num_tests);
161*8fbed61eSchristos             return 0;
162*8fbed61eSchristos         }
163*8fbed61eSchristos     }
164*8fbed61eSchristos     if (single_iter != -1) {
165*8fbed61eSchristos         if (all_tests[single_test - 1].num == -1) {
166*8fbed61eSchristos             test_printf_stderr("-%s option is not valid for test %d:%s\n",
167*8fbed61eSchristos                                itname,
168*8fbed61eSchristos                                single_test,
169*8fbed61eSchristos                                all_tests[single_test - 1].test_case_name);
170*8fbed61eSchristos             return 0;
171*8fbed61eSchristos         } else if (single_iter < 1
172*8fbed61eSchristos                    || single_iter > all_tests[single_test - 1].num) {
173*8fbed61eSchristos             test_printf_stderr("Invalid -%s value for test %d:%s\t"
174*8fbed61eSchristos                                "(Value must be in the range %d..%d)\n",
175*8fbed61eSchristos                                itname, single_test,
176*8fbed61eSchristos                                all_tests[single_test - 1].test_case_name,
177*8fbed61eSchristos                                1, all_tests[single_test - 1].num);
178*8fbed61eSchristos             return 0;
179*8fbed61eSchristos         }
180*8fbed61eSchristos     }
181*8fbed61eSchristos     return 1;
182*8fbed61eSchristos }
183*8fbed61eSchristos 
process_shared_options(void)184*8fbed61eSchristos static int process_shared_options(void)
185*8fbed61eSchristos {
186*8fbed61eSchristos     OPTION_CHOICE_DEFAULT o;
187*8fbed61eSchristos     int value;
188*8fbed61eSchristos     int ret = -1;
189*8fbed61eSchristos     char empty[] = "";
190*8fbed61eSchristos     char *flag_test = empty;
191*8fbed61eSchristos     char *flag_iter = empty;
192*8fbed61eSchristos     char *testname = NULL;
193*8fbed61eSchristos 
194*8fbed61eSchristos     opt_begin();
195*8fbed61eSchristos     while ((o = opt_next()) != OPT_EOF) {
196*8fbed61eSchristos         switch (o) {
197*8fbed61eSchristos         /* Ignore any test options at this level */
198*8fbed61eSchristos         default:
199*8fbed61eSchristos             break;
200*8fbed61eSchristos         case OPT_ERR:
201*8fbed61eSchristos             return ret;
202*8fbed61eSchristos         case OPT_TEST_HELP:
203*8fbed61eSchristos             opt_help(test_get_options());
204*8fbed61eSchristos             return 0;
205*8fbed61eSchristos         case OPT_TEST_LIST:
206*8fbed61eSchristos             show_list = 1;
207*8fbed61eSchristos             break;
208*8fbed61eSchristos         case OPT_TEST_SINGLE:
209*8fbed61eSchristos             flag_test = opt_flag();
210*8fbed61eSchristos             testname = opt_arg();
211*8fbed61eSchristos             break;
212*8fbed61eSchristos         case OPT_TEST_ITERATION:
213*8fbed61eSchristos             flag_iter = opt_flag();
214*8fbed61eSchristos             if (!opt_int(opt_arg(), &single_iter))
215*8fbed61eSchristos                 goto end;
216*8fbed61eSchristos             break;
217*8fbed61eSchristos         case OPT_TEST_INDENT:
218*8fbed61eSchristos             if (!opt_int(opt_arg(), &value))
219*8fbed61eSchristos                 goto end;
220*8fbed61eSchristos             level = 4 * value;
221*8fbed61eSchristos             test_adjust_streams_tap_level(level);
222*8fbed61eSchristos             break;
223*8fbed61eSchristos         case OPT_TEST_SEED:
224*8fbed61eSchristos             if (!opt_int(opt_arg(), &value))
225*8fbed61eSchristos                 goto end;
226*8fbed61eSchristos             set_seed(value);
227*8fbed61eSchristos             break;
228*8fbed61eSchristos         }
229*8fbed61eSchristos     }
230*8fbed61eSchristos     if (!check_single_test_params(testname, flag_test, flag_iter))
231*8fbed61eSchristos         goto end;
232*8fbed61eSchristos     ret = 1;
233*8fbed61eSchristos end:
234*8fbed61eSchristos     return ret;
235*8fbed61eSchristos }
236*8fbed61eSchristos 
237*8fbed61eSchristos 
pulldown_test_framework(int ret)23813d40330Schristos int pulldown_test_framework(int ret)
23913d40330Schristos {
24013d40330Schristos     set_test_title(NULL);
24113d40330Schristos     return ret;
24213d40330Schristos }
24313d40330Schristos 
finalize(int success)24413d40330Schristos static void finalize(int success)
24513d40330Schristos {
24613d40330Schristos     if (success)
24713d40330Schristos         ERR_clear_error();
24813d40330Schristos     else
24913d40330Schristos         ERR_print_errors_cb(openssl_error_cb, NULL);
25013d40330Schristos }
25113d40330Schristos 
25213d40330Schristos static char *test_title = NULL;
25313d40330Schristos 
set_test_title(const char * title)25413d40330Schristos void set_test_title(const char *title)
25513d40330Schristos {
25613d40330Schristos     free(test_title);
25713d40330Schristos     test_title = title == NULL ? NULL : strdup(title);
25813d40330Schristos }
25913d40330Schristos 
test_verdict(int verdict,const char * description,...)260*8fbed61eSchristos PRINTF_FORMAT(2, 3) static void test_verdict(int verdict,
261*8fbed61eSchristos                                              const char *description, ...)
26213d40330Schristos {
26313d40330Schristos     va_list ap;
26413d40330Schristos 
26513d40330Schristos     test_flush_stdout();
26613d40330Schristos     test_flush_stderr();
26713d40330Schristos 
268*8fbed61eSchristos     if (verdict == 0 && seed != 0)
269*8fbed61eSchristos         test_printf_tapout("# OPENSSL_TEST_RAND_ORDER=%d\n", seed);
270*8fbed61eSchristos     test_printf_tapout("%s ", verdict != 0 ? "ok" : "not ok");
271*8fbed61eSchristos     va_start(ap, description);
272*8fbed61eSchristos     test_vprintf_tapout(description, ap);
27313d40330Schristos     va_end(ap);
274*8fbed61eSchristos     if (verdict == TEST_SKIP_CODE)
275*8fbed61eSchristos         test_printf_tapout(" # skipped");
276*8fbed61eSchristos     test_printf_tapout("\n");
277*8fbed61eSchristos     test_flush_tapout();
27813d40330Schristos }
27913d40330Schristos 
run_tests(const char * test_prog_name)28013d40330Schristos int run_tests(const char *test_prog_name)
28113d40330Schristos {
28213d40330Schristos     int num_failed = 0;
28313d40330Schristos     int verdict = 1;
28413d40330Schristos     int ii, i, jj, j, jstep;
285*8fbed61eSchristos     int test_case_count = 0;
286*8fbed61eSchristos     int subtest_case_count = 0;
28713d40330Schristos     int permute[OSSL_NELEM(all_tests)];
28813d40330Schristos 
289*8fbed61eSchristos     i = process_shared_options();
290*8fbed61eSchristos     if (i == 0)
291*8fbed61eSchristos         return EXIT_SUCCESS;
292*8fbed61eSchristos     if (i == -1)
293*8fbed61eSchristos         return EXIT_FAILURE;
294*8fbed61eSchristos 
29513d40330Schristos     if (num_tests < 1) {
296*8fbed61eSchristos         test_printf_tapout("1..0 # Skipped: %s\n", test_prog_name);
297*8fbed61eSchristos     } else if (show_list == 0 && single_test == -1) {
298*8fbed61eSchristos         if (level > 0) {
299*8fbed61eSchristos             test_printf_stdout("Subtest: %s\n", test_prog_name);
30013d40330Schristos             test_flush_stdout();
301*8fbed61eSchristos         }
302*8fbed61eSchristos         test_printf_tapout("1..%d\n", num_test_cases);
303*8fbed61eSchristos     }
304*8fbed61eSchristos 
305*8fbed61eSchristos     test_flush_tapout();
30613d40330Schristos 
30713d40330Schristos     for (i = 0; i < num_tests; i++)
30813d40330Schristos         permute[i] = i;
309*8fbed61eSchristos     if (rand_order != 0)
31013d40330Schristos         for (i = num_tests - 1; i >= 1; i--) {
311403eeac4Schristos             j = test_random() % (1 + i);
31213d40330Schristos             ii = permute[j];
31313d40330Schristos             permute[j] = permute[i];
31413d40330Schristos             permute[i] = ii;
31513d40330Schristos         }
31613d40330Schristos 
31713d40330Schristos     for (ii = 0; ii != num_tests; ++ii) {
31813d40330Schristos         i = permute[ii];
31913d40330Schristos 
320*8fbed61eSchristos         if (single_test != -1 && ((i+1) != single_test)) {
321*8fbed61eSchristos             continue;
32213d40330Schristos         }
323*8fbed61eSchristos         else if (show_list) {
324*8fbed61eSchristos             if (all_tests[i].num != -1) {
325*8fbed61eSchristos                 test_printf_tapout("%d - %s (%d..%d)\n", ii + 1,
326*8fbed61eSchristos                                    all_tests[i].test_case_name, 1,
32713d40330Schristos                                    all_tests[i].num);
328*8fbed61eSchristos             } else {
329*8fbed61eSchristos                 test_printf_tapout("%d - %s\n", ii + 1,
330*8fbed61eSchristos                                    all_tests[i].test_case_name);
331*8fbed61eSchristos             }
332*8fbed61eSchristos             test_flush_tapout();
333*8fbed61eSchristos         } else if (all_tests[i].num == -1) {
334*8fbed61eSchristos             set_test_title(all_tests[i].test_case_name);
335*8fbed61eSchristos             ERR_clear_error();
336*8fbed61eSchristos             verdict = all_tests[i].test_fn();
337*8fbed61eSchristos             finalize(verdict != 0);
338*8fbed61eSchristos             test_verdict(verdict, "%d - %s", test_case_count + 1, test_title);
339*8fbed61eSchristos             if (verdict == 0)
340*8fbed61eSchristos                 num_failed++;
341*8fbed61eSchristos             test_case_count++;
342*8fbed61eSchristos         } else {
343*8fbed61eSchristos             verdict = TEST_SKIP_CODE;
344*8fbed61eSchristos             set_test_title(all_tests[i].test_case_name);
345*8fbed61eSchristos             if (all_tests[i].subtest) {
346*8fbed61eSchristos                 level += 4;
347*8fbed61eSchristos                 test_adjust_streams_tap_level(level);
348*8fbed61eSchristos                 if (single_iter == -1) {
349*8fbed61eSchristos                     test_printf_stdout("Subtest: %s\n", test_title);
350*8fbed61eSchristos                     test_printf_tapout("%d..%d\n", 1, all_tests[i].num);
35113d40330Schristos                     test_flush_stdout();
352*8fbed61eSchristos                     test_flush_tapout();
353*8fbed61eSchristos                 }
35413d40330Schristos             }
35513d40330Schristos 
35613d40330Schristos             j = -1;
357*8fbed61eSchristos             if (rand_order == 0 || all_tests[i].num < 3)
35813d40330Schristos                 jstep = 1;
35913d40330Schristos             else
36013d40330Schristos                 do
361403eeac4Schristos                     jstep = test_random() % all_tests[i].num;
36213d40330Schristos                 while (jstep == 0 || gcd(all_tests[i].num, jstep) != 1);
36313d40330Schristos 
36413d40330Schristos             for (jj = 0; jj < all_tests[i].num; jj++) {
365*8fbed61eSchristos                 int v;
36613d40330Schristos 
36713d40330Schristos                 j = (j + jstep) % all_tests[i].num;
368*8fbed61eSchristos                 if (single_iter != -1 && ((jj + 1) != single_iter))
369*8fbed61eSchristos                     continue;
370*8fbed61eSchristos                 ERR_clear_error();
371*8fbed61eSchristos                 v = all_tests[i].param_test_fn(j);
37213d40330Schristos 
373*8fbed61eSchristos                 if (v == 0) {
374*8fbed61eSchristos                     verdict = 0;
375*8fbed61eSchristos                 } else if (v != TEST_SKIP_CODE && verdict != 0) {
376*8fbed61eSchristos                     verdict = 1;
377*8fbed61eSchristos                 }
37813d40330Schristos 
379*8fbed61eSchristos                 finalize(v != 0);
380*8fbed61eSchristos 
381*8fbed61eSchristos                 if (all_tests[i].subtest)
382*8fbed61eSchristos                     test_verdict(v, "%d - iteration %d",
383*8fbed61eSchristos                                  subtest_case_count + 1, j + 1);
384*8fbed61eSchristos                 else
385*8fbed61eSchristos                     test_verdict(v, "%d - %s - iteration %d",
386*8fbed61eSchristos                                  test_case_count + subtest_case_count + 1,
387*8fbed61eSchristos                                  test_title, j + 1);
388*8fbed61eSchristos                 subtest_case_count++;
389*8fbed61eSchristos             }
39013d40330Schristos 
39113d40330Schristos             if (all_tests[i].subtest) {
39213d40330Schristos                 level -= 4;
393*8fbed61eSchristos                 test_adjust_streams_tap_level(level);
39413d40330Schristos             }
395*8fbed61eSchristos             if (verdict == 0)
396*8fbed61eSchristos                 ++num_failed;
397*8fbed61eSchristos             if (all_tests[i].num == -1 || all_tests[i].subtest)
398*8fbed61eSchristos                 test_verdict(verdict, "%d - %s", test_case_count + 1,
39913d40330Schristos                              all_tests[i].test_case_name);
400*8fbed61eSchristos             test_case_count++;
40113d40330Schristos         }
40213d40330Schristos     }
40313d40330Schristos     if (num_failed != 0)
40413d40330Schristos         return EXIT_FAILURE;
40513d40330Schristos     return EXIT_SUCCESS;
40613d40330Schristos }
40713d40330Schristos 
40813d40330Schristos /*
40913d40330Schristos  * Glue an array of strings together and return it as an allocated string.
41013d40330Schristos  * Optionally return the whole length of this string in |out_len|
41113d40330Schristos  */
glue_strings(const char * list[],size_t * out_len)41213d40330Schristos char *glue_strings(const char *list[], size_t *out_len)
41313d40330Schristos {
41413d40330Schristos     size_t len = 0;
41513d40330Schristos     char *p, *ret;
41613d40330Schristos     int i;
41713d40330Schristos 
41813d40330Schristos     for (i = 0; list[i] != NULL; i++)
41913d40330Schristos         len += strlen(list[i]);
42013d40330Schristos 
42113d40330Schristos     if (out_len != NULL)
42213d40330Schristos         *out_len = len;
42313d40330Schristos 
42413d40330Schristos     if (!TEST_ptr(ret = p = OPENSSL_malloc(len + 1)))
42513d40330Schristos         return NULL;
42613d40330Schristos 
42713d40330Schristos     for (i = 0; list[i] != NULL; i++)
42813d40330Schristos         p += strlen(strcpy(p, list[i]));
42913d40330Schristos 
43013d40330Schristos     return ret;
43113d40330Schristos }
43213d40330Schristos 
test_mk_file_path(const char * dir,const char * file)433403eeac4Schristos char *test_mk_file_path(const char *dir, const char *file)
434403eeac4Schristos {
435403eeac4Schristos # ifndef OPENSSL_SYS_VMS
436403eeac4Schristos     const char *sep = "/";
437403eeac4Schristos # else
438403eeac4Schristos     const char *sep = "";
439*8fbed61eSchristos     char *dir_end;
440*8fbed61eSchristos     char dir_end_sep;
441403eeac4Schristos # endif
442*8fbed61eSchristos     size_t dirlen = dir != NULL ? strlen(dir) : 0;
443*8fbed61eSchristos     size_t len = dirlen + strlen(sep) + strlen(file) + 1;
444403eeac4Schristos     char *full_file = OPENSSL_zalloc(len);
445403eeac4Schristos 
446403eeac4Schristos     if (full_file != NULL) {
447*8fbed61eSchristos         if (dir != NULL && dirlen > 0) {
448403eeac4Schristos             OPENSSL_strlcpy(full_file, dir, len);
449*8fbed61eSchristos # ifdef OPENSSL_SYS_VMS
450*8fbed61eSchristos             /*
451*8fbed61eSchristos              * If |file| contains a directory spec, we need to do some
452*8fbed61eSchristos              * careful merging.
453*8fbed61eSchristos              * "vol:[dir.dir]" + "[.certs]sm2-root.crt" should become
454*8fbed61eSchristos              * "vol:[dir.dir.certs]sm2-root.crt"
455*8fbed61eSchristos              */
456*8fbed61eSchristos             dir_end = &full_file[strlen(full_file) - 1];
457*8fbed61eSchristos             dir_end_sep = *dir_end;
458*8fbed61eSchristos             if ((dir_end_sep == ']' || dir_end_sep == '>')
459*8fbed61eSchristos                 && (file[0] == '[' || file[0] == '<')) {
460*8fbed61eSchristos                 file++;
461*8fbed61eSchristos                 if (file[0] == '.')
462*8fbed61eSchristos                     *dir_end = '\0';
463*8fbed61eSchristos                 else
464*8fbed61eSchristos                     *dir_end = '.';
465*8fbed61eSchristos             }
466*8fbed61eSchristos #else
467403eeac4Schristos             OPENSSL_strlcat(full_file, sep, len);
468*8fbed61eSchristos #endif
469*8fbed61eSchristos         }
470403eeac4Schristos         OPENSSL_strlcat(full_file, file, len);
471403eeac4Schristos     }
472403eeac4Schristos 
473403eeac4Schristos     return full_file;
474403eeac4Schristos }
475