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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "apr_errno.h"
20 #include "apr_general.h"
22 #include "apr_file_io.h"
23 #include "apr_strings.h"
25 /* hmmm, what is a truly portable define for the max path
26 * length on a platform?
29 #define TEST_STRING "This is the MMAP data file."APR_EOL_STR
32 static void not_implemented(CuTest *tc)
34 CuNotImpl(tc, "User functions");
39 static apr_mmap_t *themmap = NULL;
40 static apr_file_t *thefile = NULL;
42 static apr_finfo_t finfo;
45 static void create_filename(CuTest *tc)
49 apr_filepath_get(&file1, 0, p);
52 CuAssertTrue(tc, file1[1] == ':');
54 CuAssertTrue(tc, file1[0] == '/');
57 CuAssertTrue(tc, file1[strlen(file1) - 1] != '/');
60 file1 = apr_pstrcat(p, file1,"/data/mmap_datafile.txt" ,NULL);
61 CuAssertTrue(tc, oldfileptr != file1);
64 static void test_file_close(CuTest *tc)
68 rv = apr_file_close(thefile);
69 CuAssertIntEquals(tc, rv, APR_SUCCESS);
72 static void test_file_open(CuTest *tc)
76 rv = apr_file_open(&thefile, file1, APR_READ, APR_UREAD | APR_GREAD, p);
77 CuAssertIntEquals(tc, rv, APR_SUCCESS);
78 CuAssertPtrNotNull(tc, thefile);
81 static void test_get_filesize(CuTest *tc)
85 rv = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
86 CuAssertIntEquals(tc, rv, APR_SUCCESS);
87 CuAssert(tc, "File size mismatch", fsize == finfo.size);
90 static void test_mmap_create(CuTest *tc)
94 rv = apr_mmap_create(&themmap, thefile, 0, (apr_size_t)finfo.size,
96 CuAssertPtrNotNull(tc, themmap);
97 CuAssertIntEquals(tc, rv, APR_SUCCESS);
100 static void test_mmap_contents(CuTest *tc)
103 CuAssertPtrNotNull(tc, themmap);
104 CuAssertPtrNotNull(tc, themmap->mm);
105 CuAssertIntEquals(tc, fsize, themmap->size);
107 /* Must use nEquals since the string is not guaranteed to be NULL terminated */
108 CuAssertStrNEquals(tc, themmap->mm, TEST_STRING, fsize);
111 static void test_mmap_delete(CuTest *tc)
115 CuAssertPtrNotNull(tc, themmap);
116 rv = apr_mmap_delete(themmap);
117 CuAssertIntEquals(tc, rv, APR_SUCCESS);
120 static void test_mmap_offset(CuTest *tc)
125 CuAssertPtrNotNull(tc, themmap);
126 rv = apr_mmap_offset(&addr, themmap, 5);
128 /* Must use nEquals since the string is not guaranteed to be NULL terminated */
129 CuAssertStrNEquals(tc, addr, TEST_STRING + 5, fsize-5);
133 CuSuite *testmmap(void)
135 CuSuite *suite = CuSuiteNew("MMAP");
138 fsize = strlen(TEST_STRING);
140 SUITE_ADD_TEST(suite, create_filename);
141 SUITE_ADD_TEST(suite, test_file_open);
142 SUITE_ADD_TEST(suite, test_get_filesize);
143 SUITE_ADD_TEST(suite, test_mmap_create);
144 SUITE_ADD_TEST(suite, test_mmap_contents);
145 SUITE_ADD_TEST(suite, test_mmap_offset);
146 SUITE_ADD_TEST(suite, test_mmap_delete);
147 SUITE_ADD_TEST(suite, test_file_close);
149 SUITE_ADD_TEST(suite, not_implemented);