upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testmmap.c
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 "test_apr.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 #define TEST_STRING "This is the MMAP data file."APR_EOL_STR
30
31 #if !APR_HAS_MMAP
32 static void not_implemented(CuTest *tc)
33 {
34     CuNotImpl(tc, "User functions");
35 }
36
37 #else
38
39 static apr_mmap_t *themmap = NULL;
40 static apr_file_t *thefile = NULL;
41 static char *file1;
42 static apr_finfo_t finfo;
43 static int fsize;
44
45 static void create_filename(CuTest *tc)
46 {
47     char *oldfileptr;
48
49     apr_filepath_get(&file1, 0, p);
50 #ifndef NETWARE
51 #ifdef WIN32
52     CuAssertTrue(tc, file1[1] == ':');
53 #else
54     CuAssertTrue(tc, file1[0] == '/');
55 #endif
56 #endif
57     CuAssertTrue(tc, file1[strlen(file1) - 1] != '/');
58
59     oldfileptr = file1;
60     file1 = apr_pstrcat(p, file1,"/data/mmap_datafile.txt" ,NULL);
61     CuAssertTrue(tc, oldfileptr != file1);
62 }
63
64 static void test_file_close(CuTest *tc)
65 {
66     apr_status_t rv;
67
68     rv = apr_file_close(thefile);
69     CuAssertIntEquals(tc, rv, APR_SUCCESS);
70 }
71    
72 static void test_file_open(CuTest *tc)
73 {
74     apr_status_t rv;
75
76     rv = apr_file_open(&thefile, file1, APR_READ, APR_UREAD | APR_GREAD, p);
77     CuAssertIntEquals(tc, rv, APR_SUCCESS);
78     CuAssertPtrNotNull(tc, thefile);
79 }
80    
81 static void test_get_filesize(CuTest *tc)
82 {
83     apr_status_t rv;
84
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);
88 }
89
90 static void test_mmap_create(CuTest *tc)
91 {
92     apr_status_t rv;
93
94     rv = apr_mmap_create(&themmap, thefile, 0, (apr_size_t)finfo.size, 
95                                  APR_MMAP_READ, p);
96     CuAssertPtrNotNull(tc, themmap);
97     CuAssertIntEquals(tc, rv, APR_SUCCESS);
98 }
99
100 static void test_mmap_contents(CuTest *tc)
101 {
102     
103     CuAssertPtrNotNull(tc, themmap);
104     CuAssertPtrNotNull(tc, themmap->mm);
105     CuAssertIntEquals(tc, fsize, themmap->size);
106
107     /* Must use nEquals since the string is not guaranteed to be NULL terminated */
108     CuAssertStrNEquals(tc, themmap->mm, TEST_STRING, fsize);
109 }
110
111 static void test_mmap_delete(CuTest *tc)
112 {
113     apr_status_t rv;
114
115     CuAssertPtrNotNull(tc, themmap);
116     rv = apr_mmap_delete(themmap);
117     CuAssertIntEquals(tc, rv, APR_SUCCESS);
118 }
119
120 static void test_mmap_offset(CuTest *tc)
121 {
122     apr_status_t rv;
123     void *addr;
124
125     CuAssertPtrNotNull(tc, themmap);
126     rv = apr_mmap_offset(&addr, themmap, 5);
127
128     /* Must use nEquals since the string is not guaranteed to be NULL terminated */
129     CuAssertStrNEquals(tc, addr, TEST_STRING + 5, fsize-5);
130 }
131 #endif
132
133 CuSuite *testmmap(void)
134 {
135     CuSuite *suite = CuSuiteNew("MMAP");
136
137 #if APR_HAS_MMAP    
138     fsize = strlen(TEST_STRING);
139
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);
148 #else
149     SUITE_ADD_TEST(suite, not_implemented);
150 #endif
151
152     return suite;
153 }
154