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.
18 #include "apr_errno.h"
19 #include "apr_general.h"
23 static void uid_current(CuTest *tc)
29 rv = apr_uid_current(&uid, &gid, p);
30 CuAssertIntEquals(tc, APR_SUCCESS, rv);
33 static void username(CuTest *tc)
37 apr_uid_t retreived_uid;
38 apr_gid_t retreived_gid;
42 rv = apr_uid_current(&uid, &gid, p);
43 CuAssertIntEquals(tc, APR_SUCCESS, rv);
45 rv = apr_uid_name_get(&uname, uid, p);
46 CuAssertIntEquals(tc, APR_SUCCESS, rv);
47 CuAssertPtrNotNull(tc, uname);
49 rv = apr_uid_get(&retreived_uid, &retreived_gid, uname, p);
50 CuAssertIntEquals(tc, APR_SUCCESS, rv);
52 CuAssertIntEquals(tc, APR_SUCCESS, apr_uid_compare(uid, retreived_uid));
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.
62 CuNotImpl(tc, "Groups from apr_uid_current");
65 CuNotImpl(tc, "Groups from apr_uid_get");
70 CuAssertIntEquals(tc, APR_SUCCESS, apr_gid_compare(gid, retreived_gid));
76 static void groupname(CuTest *tc)
80 apr_gid_t retreived_gid;
84 rv = apr_uid_current(&uid, &gid, p);
85 CuAssertIntEquals(tc, APR_SUCCESS, rv);
87 rv = apr_gid_name_get(&gname, gid, p);
88 CuAssertIntEquals(tc, APR_SUCCESS, rv);
89 CuAssertPtrNotNull(tc, gname);
91 rv = apr_gid_get(&retreived_gid, gname, p);
92 CuAssertIntEquals(tc, APR_SUCCESS, rv);
94 CuAssertIntEquals(tc, APR_SUCCESS, apr_gid_compare(gid, retreived_gid));
99 static void fail_userinfo(CuTest *tc)
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);
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);
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);
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);
137 rv = apr_uid_homepath_get(&tmp, "I_AM_NOT_A_USER", p);
138 CuAssert(tc, "apr_uid_homepath_get should fail or "
140 rv != APR_SUCCESS || tmp != NULL);
144 static void fail_userinfo(CuTest *tc)
146 CuNotImpl(tc, "Intregal uid/gid not present on this platform");
151 static void users_not_impl(CuTest *tc)
153 CuNotImpl(tc, "Users not implemented on this platform");
157 CuSuite *testuser(void)
159 CuSuite *suite = CuSuiteNew("Users");
162 SUITE_ADD_TEST(suite, users_not_impl);
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);