1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "testutil.h"
18#include "apr_file_info.h"
19#include "apr_errno.h"
20#include "apr_pools.h"
21#include "apr_tables.h"
22
23#if defined(WIN32) || defined(NETWARE) || defined(OS2)
24#define PSEP ";"
25#define DSEP "\\"
26#else
27#define PSEP ":"
28#define DSEP "/"
29#endif
30
31#define PX ""
32#define P1 "first path"
33#define P2 "second" DSEP "path"
34#define P3 "th ird" DSEP "path"
35#define P4 "fourth" DSEP "pa th"
36#define P5 "fifthpath"
37
38static const char *parts_in[] = { P1, P2, P3, PX, P4, P5 };
39static const char *path_in = P1 PSEP P2 PSEP P3 PSEP PX PSEP P4 PSEP P5;
40static const int  parts_in_count = sizeof(parts_in)/sizeof(*parts_in);
41
42static const char *parts_out[] = { P1, P2, P3, P4, P5 };
43static const char *path_out = P1 PSEP P2 PSEP P3 PSEP P4 PSEP P5;
44static const int  parts_out_count = sizeof(parts_out)/sizeof(*parts_out);
45
46static void list_split_multi(abts_case *tc, void *data)
47{
48    int i;
49    apr_status_t rv;
50    apr_array_header_t *pathelts;
51
52    pathelts = NULL;
53    rv = apr_filepath_list_split(&pathelts, path_in, p);
54    ABTS_PTR_NOTNULL(tc, pathelts);
55    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
56    ABTS_INT_EQUAL(tc, parts_out_count, pathelts->nelts);
57    for (i = 0; i < pathelts->nelts; ++i)
58        ABTS_STR_EQUAL(tc, parts_out[i], ((char**)pathelts->elts)[i]);
59}
60
61static void list_split_single(abts_case *tc, void *data)
62{
63    int i;
64    apr_status_t rv;
65    apr_array_header_t *pathelts;
66
67    for (i = 0; i < parts_in_count; ++i)
68    {
69        pathelts = NULL;
70        rv = apr_filepath_list_split(&pathelts, parts_in[i], p);
71        ABTS_PTR_NOTNULL(tc, pathelts);
72        ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
73        if (parts_in[i][0] == '\0')
74            ABTS_INT_EQUAL(tc, 0, pathelts->nelts);
75        else
76        {
77            ABTS_INT_EQUAL(tc, 1, pathelts->nelts);
78            ABTS_STR_EQUAL(tc, parts_in[i], *(char**)pathelts->elts);
79        }
80    }
81}
82
83static void list_merge_multi(abts_case *tc, void *data)
84{
85    int i;
86    char *liststr;
87    apr_status_t rv;
88    apr_array_header_t *pathelts;
89
90    pathelts = apr_array_make(p, parts_in_count, sizeof(const char*));
91    for (i = 0; i < parts_in_count; ++i)
92        *(const char**)apr_array_push(pathelts) = parts_in[i];
93
94    liststr = NULL;
95    rv = apr_filepath_list_merge(&liststr, pathelts, p);
96    ABTS_PTR_NOTNULL(tc, liststr);
97    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
98    ABTS_STR_EQUAL(tc, liststr, path_out);
99}
100
101static void list_merge_single(abts_case *tc, void *data)
102{
103    int i;
104    char *liststr;
105    apr_status_t rv;
106    apr_array_header_t *pathelts;
107
108    pathelts = apr_array_make(p, 1, sizeof(const char*));
109    apr_array_push(pathelts);
110    for (i = 0; i < parts_in_count; ++i)
111    {
112        *(const char**)pathelts->elts = parts_in[i];
113        liststr = NULL;
114        rv = apr_filepath_list_merge(&liststr, pathelts, p);
115        if (parts_in[i][0] == '\0')
116            ABTS_PTR_EQUAL(tc, NULL, liststr);
117        else
118        {
119            ABTS_PTR_NOTNULL(tc, liststr);
120            ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
121            ABTS_STR_EQUAL(tc, liststr, parts_in[i]);
122        }
123    }
124}
125
126
127abts_suite *testpath(abts_suite *suite)
128{
129    suite = ADD_SUITE(suite)
130
131    abts_run_test(suite, list_split_multi, NULL);
132    abts_run_test(suite, list_split_single, NULL);
133    abts_run_test(suite, list_merge_multi, NULL);
134    abts_run_test(suite, list_merge_single, NULL);
135
136    return suite;
137}
138
139