upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testuser.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_errno.h"
19 #include "apr_general.h"
20 #include "apr_user.h"
21
22 #if APR_HAS_USER
23 static void uid_current(CuTest *tc)
24 {
25     apr_uid_t uid;
26     apr_gid_t gid;
27     apr_status_t rv;
28
29     rv = apr_uid_current(&uid, &gid, p);
30     CuAssertIntEquals(tc, APR_SUCCESS, rv);
31 }
32
33 static void username(CuTest *tc)
34 {
35     apr_uid_t uid;
36     apr_gid_t gid;
37     apr_uid_t retreived_uid;
38     apr_gid_t retreived_gid;
39     apr_status_t rv;
40     char *uname = NULL;
41
42     rv = apr_uid_current(&uid, &gid, p);
43     CuAssertIntEquals(tc, APR_SUCCESS, rv);
44
45     rv = apr_uid_name_get(&uname, uid, p);
46     CuAssertIntEquals(tc, APR_SUCCESS, rv);
47     CuAssertPtrNotNull(tc, uname);
48
49     rv = apr_uid_get(&retreived_uid, &retreived_gid, uname, p);
50     CuAssertIntEquals(tc, APR_SUCCESS, rv);
51
52     CuAssertIntEquals(tc, APR_SUCCESS, apr_uid_compare(uid, retreived_uid));
53 #ifdef WIN32
54     /* ### this fudge was added for Win32 but makes the test return NotImpl
55      * on Unix if run as root, when !gid is also true. */
56     if (!gid || !retreived_gid) {
57         /* The function had no way to recover the gid (this would have been
58          * an ENOTIMPL if apr_uid_ functions didn't try to double-up and
59          * also return apr_gid_t values, which was bogus.
60          */
61         if (!gid) {
62             CuNotImpl(tc, "Groups from apr_uid_current");
63         }
64         else {
65             CuNotImpl(tc, "Groups from apr_uid_get");
66         }        
67     }
68     else {
69 #endif
70         CuAssertIntEquals(tc, APR_SUCCESS, apr_gid_compare(gid, retreived_gid));
71 #ifdef WIN32
72     }
73 #endif
74 }
75
76 static void groupname(CuTest *tc)
77 {
78     apr_uid_t uid;
79     apr_gid_t gid;
80     apr_gid_t retreived_gid;
81     apr_status_t rv;
82     char *gname = NULL;
83
84     rv = apr_uid_current(&uid, &gid, p);
85     CuAssertIntEquals(tc, APR_SUCCESS, rv);
86
87     rv = apr_gid_name_get(&gname, gid, p);
88     CuAssertIntEquals(tc, APR_SUCCESS, rv);
89     CuAssertPtrNotNull(tc, gname);
90
91     rv = apr_gid_get(&retreived_gid, gname, p);
92     CuAssertIntEquals(tc, APR_SUCCESS, rv);
93
94     CuAssertIntEquals(tc, APR_SUCCESS, apr_gid_compare(gid, retreived_gid));
95 }
96
97 #ifndef WIN32
98
99 static void fail_userinfo(CuTest *tc)
100 {
101     apr_uid_t uid;
102     apr_gid_t gid;
103     apr_status_t rv;
104     char *tmp;
105
106     errno = 0;
107     gid = uid = 9999999;
108     tmp = NULL;
109     rv = apr_uid_name_get(&tmp, uid, p);
110     CuAssert(tc, "apr_uid_name_get should fail or "
111                 "return a user name",
112                 rv != APR_SUCCESS || tmp != NULL);
113
114     errno = 0;
115     tmp = NULL;
116     rv = apr_gid_name_get(&tmp, gid, p);
117     CuAssert(tc, "apr_gid_name_get should fail or "
118              "return a group name",
119              rv != APR_SUCCESS || tmp != NULL);
120     
121     gid = 424242;
122     errno = 0;
123     rv = apr_gid_get(&gid, "I_AM_NOT_A_GROUP", p);
124     CuAssert(tc, "apr_gid_get should fail or "
125              "set a group number",
126              rv != APR_SUCCESS || gid == 424242);
127
128     gid = uid = 424242;
129     errno = 0;
130     rv = apr_uid_get(&uid, &gid, "I_AM_NOT_A_USER", p);
131     CuAssert(tc, "apr_gid_get should fail or "
132              "set a user and group number",
133              rv != APR_SUCCESS || uid == 424242 || gid == 4242442);
134
135     errno = 0;
136     tmp = NULL;
137     rv = apr_uid_homepath_get(&tmp, "I_AM_NOT_A_USER", p);
138     CuAssert(tc, "apr_uid_homepath_get should fail or "
139              "set a path name",
140              rv != APR_SUCCESS || tmp != NULL);
141 }
142
143 #else
144 static void fail_userinfo(CuTest *tc)
145 {
146     CuNotImpl(tc, "Intregal uid/gid not present on this platform");
147 }
148 #endif
149
150 #else
151 static void users_not_impl(CuTest *tc)
152 {
153     CuNotImpl(tc, "Users not implemented on this platform");
154 }
155 #endif
156
157 CuSuite *testuser(void)
158 {
159     CuSuite *suite = CuSuiteNew("Users");
160
161 #if !APR_HAS_USER
162     SUITE_ADD_TEST(suite, users_not_impl);
163 #else
164     SUITE_ADD_TEST(suite, uid_current);
165     SUITE_ADD_TEST(suite, username);
166     SUITE_ADD_TEST(suite, groupname);
167     SUITE_ADD_TEST(suite, fail_userinfo);
168 #endif
169
170     return suite;
171 }