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_mmap.h"
19 #include "apr_errno.h"
20 #include "apr_general.h"
21 #include "apr_lib.h"
22 #include "apr_file_io.h"
23 #include "apr_strings.h"
24
25 /* hmmm, what is a truly portable define for the max path
26 * length on a platform?
27 */
28 #define PATH_LEN 255
29
30 #if !APR_HAS_MMAP
not_implemented(abts_case * tc,void * data)31 static void not_implemented(abts_case *tc, void *data)
32 {
33 ABTS_NOT_IMPL(tc, "MMAP functions");
34 }
35
36 #else
37
38 static char test_string[256]; /* read from the datafile */
39 static apr_mmap_t *themmap = NULL;
40 static apr_file_t *thefile = NULL;
41 static char *file1;
42 static apr_finfo_t thisfinfo;
43 static apr_size_t thisfsize;
44
create_filename(abts_case * tc,void * data)45 static void create_filename(abts_case *tc, void *data)
46 {
47 char *oldfileptr;
48
49 apr_filepath_get(&file1, 0, p);
50 #ifndef NETWARE
51 #ifdef WIN32
52 ABTS_TRUE(tc, file1[1] == ':');
53 #else
54 ABTS_TRUE(tc, file1[0] == '/');
55 #endif
56 #endif
57 ABTS_TRUE(tc, file1[strlen(file1) - 1] != '/');
58
59 oldfileptr = file1;
60 file1 = apr_pstrcat(p, file1,"/data/mmap_datafile.txt" ,NULL);
61 ABTS_TRUE(tc, oldfileptr != file1);
62 }
63
test_file_close(abts_case * tc,void * data)64 static void test_file_close(abts_case *tc, void *data)
65 {
66 apr_status_t rv;
67
68 rv = apr_file_close(thefile);
69 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
70 }
71
read_expected_contents(abts_case * tc,void * data)72 static void read_expected_contents(abts_case *tc, void *data)
73 {
74 apr_status_t rv;
75 apr_size_t nbytes = sizeof(test_string) - 1;
76
77 rv = apr_file_read(thefile, test_string, &nbytes);
78 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
79 test_string[nbytes] = '\0';
80 thisfsize = strlen(test_string);
81 }
82
test_file_open(abts_case * tc,void * data)83 static void test_file_open(abts_case *tc, void *data)
84 {
85 apr_status_t rv;
86
87 rv = apr_file_open(&thefile, file1, APR_FOPEN_READ, APR_UREAD | APR_GREAD, p);
88 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
89 ABTS_PTR_NOTNULL(tc, thefile);
90 }
91
test_get_filesize(abts_case * tc,void * data)92 static void test_get_filesize(abts_case *tc, void *data)
93 {
94 apr_status_t rv;
95
96 rv = apr_file_info_get(&thisfinfo, APR_FINFO_NORM, thefile);
97 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
98 ABTS_ASSERT(tc, "File size mismatch", thisfsize == thisfinfo.size);
99 }
100
test_mmap_create(abts_case * tc,void * data)101 static void test_mmap_create(abts_case *tc, void *data)
102 {
103 apr_status_t rv;
104
105 rv = apr_mmap_create(&themmap, thefile, 0, (apr_size_t) thisfinfo.size,
106 APR_MMAP_READ, p);
107 ABTS_PTR_NOTNULL(tc, themmap);
108 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
109 }
110
test_mmap_contents(abts_case * tc,void * data)111 static void test_mmap_contents(abts_case *tc, void *data)
112 {
113
114 ABTS_PTR_NOTNULL(tc, themmap);
115 ABTS_PTR_NOTNULL(tc, themmap->mm);
116 ABTS_SIZE_EQUAL(tc, thisfsize, themmap->size);
117
118 /* Must use nEquals since the string is not guaranteed to be NULL terminated */
119 ABTS_STR_NEQUAL(tc, themmap->mm, test_string, thisfsize);
120 }
121
test_mmap_delete(abts_case * tc,void * data)122 static void test_mmap_delete(abts_case *tc, void *data)
123 {
124 apr_status_t rv;
125
126 ABTS_PTR_NOTNULL(tc, themmap);
127 rv = apr_mmap_delete(themmap);
128 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
129 }
130
test_mmap_offset(abts_case * tc,void * data)131 static void test_mmap_offset(abts_case *tc, void *data)
132 {
133 apr_status_t rv;
134 void *addr;
135
136 ABTS_PTR_NOTNULL(tc, themmap);
137 rv = apr_mmap_offset(&addr, themmap, 5);
138
139 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
140 /* Must use nEquals since the string is not guaranteed to be NULL terminated */
141 ABTS_STR_NEQUAL(tc, addr, test_string + 5, thisfsize-5);
142 }
143 #endif
144
testmmap(abts_suite * suite)145 abts_suite *testmmap(abts_suite *suite)
146 {
147 suite = ADD_SUITE(suite)
148
149 #if APR_HAS_MMAP
150 abts_run_test(suite, create_filename, NULL);
151 abts_run_test(suite, test_file_open, NULL);
152 abts_run_test(suite, read_expected_contents, NULL);
153 abts_run_test(suite, test_get_filesize, NULL);
154 abts_run_test(suite, test_mmap_create, NULL);
155 abts_run_test(suite, test_mmap_contents, NULL);
156 abts_run_test(suite, test_mmap_offset, NULL);
157 abts_run_test(suite, test_mmap_delete, NULL);
158 abts_run_test(suite, test_file_close, NULL);
159 #else
160 abts_run_test(suite, not_implemented, NULL);
161 #endif
162
163 return suite;
164 }
165
166