2
0

testuser.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "testutil.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_user.h"
  20. #if APR_HAS_USER
  21. static void uid_current(abts_case *tc, void *data)
  22. {
  23. fspr_uid_t uid;
  24. fspr_gid_t gid;
  25. APR_ASSERT_SUCCESS(tc, "fspr_uid_current failed",
  26. fspr_uid_current(&uid, &gid, p));
  27. }
  28. static void username(abts_case *tc, void *data)
  29. {
  30. fspr_uid_t uid;
  31. fspr_gid_t gid;
  32. fspr_uid_t retreived_uid;
  33. fspr_gid_t retreived_gid;
  34. char *uname = NULL;
  35. APR_ASSERT_SUCCESS(tc, "fspr_uid_current failed",
  36. fspr_uid_current(&uid, &gid, p));
  37. APR_ASSERT_SUCCESS(tc, "fspr_uid_name_get failed",
  38. fspr_uid_name_get(&uname, uid, p));
  39. ABTS_PTR_NOTNULL(tc, uname);
  40. APR_ASSERT_SUCCESS(tc, "fspr_uid_get failed",
  41. fspr_uid_get(&retreived_uid, &retreived_gid, uname, p));
  42. APR_ASSERT_SUCCESS(tc, "fspr_uid_compare failed",
  43. fspr_uid_compare(uid, retreived_uid));
  44. #ifdef WIN32
  45. /* ### this fudge was added for Win32 but makes the test return NotImpl
  46. * on Unix if run as root, when !gid is also true. */
  47. if (!gid || !retreived_gid) {
  48. /* The function had no way to recover the gid (this would have been
  49. * an ENOTIMPL if fspr_uid_ functions didn't try to double-up and
  50. * also return fspr_gid_t values, which was bogus.
  51. */
  52. if (!gid) {
  53. ABTS_NOT_IMPL(tc, "Groups from fspr_uid_current");
  54. }
  55. else {
  56. ABTS_NOT_IMPL(tc, "Groups from fspr_uid_get");
  57. }
  58. }
  59. else {
  60. #endif
  61. APR_ASSERT_SUCCESS(tc, "fspr_gid_compare failed",
  62. fspr_gid_compare(gid, retreived_gid));
  63. #ifdef WIN32
  64. }
  65. #endif
  66. }
  67. static void groupname(abts_case *tc, void *data)
  68. {
  69. fspr_uid_t uid;
  70. fspr_gid_t gid;
  71. fspr_gid_t retreived_gid;
  72. char *gname = NULL;
  73. APR_ASSERT_SUCCESS(tc, "fspr_uid_current failed",
  74. fspr_uid_current(&uid, &gid, p));
  75. APR_ASSERT_SUCCESS(tc, "fspr_gid_name_get failed",
  76. fspr_gid_name_get(&gname, gid, p));
  77. ABTS_PTR_NOTNULL(tc, gname);
  78. APR_ASSERT_SUCCESS(tc, "fspr_gid_get failed",
  79. fspr_gid_get(&retreived_gid, gname, p));
  80. APR_ASSERT_SUCCESS(tc, "fspr_gid_compare failed",
  81. fspr_gid_compare(gid, retreived_gid));
  82. }
  83. #ifndef WIN32
  84. static void fail_userinfo(abts_case *tc, void *data)
  85. {
  86. fspr_uid_t uid;
  87. fspr_gid_t gid;
  88. fspr_status_t rv;
  89. char *tmp;
  90. errno = 0;
  91. gid = uid = 9999999;
  92. tmp = NULL;
  93. rv = fspr_uid_name_get(&tmp, uid, p);
  94. ABTS_ASSERT(tc, "fspr_uid_name_get should fail or "
  95. "return a user name",
  96. rv != APR_SUCCESS || tmp != NULL);
  97. errno = 0;
  98. tmp = NULL;
  99. rv = fspr_gid_name_get(&tmp, gid, p);
  100. ABTS_ASSERT(tc, "fspr_gid_name_get should fail or "
  101. "return a group name",
  102. rv != APR_SUCCESS || tmp != NULL);
  103. gid = 424242;
  104. errno = 0;
  105. rv = fspr_gid_get(&gid, "I_AM_NOT_A_GROUP", p);
  106. ABTS_ASSERT(tc, "fspr_gid_get should fail or "
  107. "set a group number",
  108. rv != APR_SUCCESS || gid == 424242);
  109. gid = uid = 424242;
  110. errno = 0;
  111. rv = fspr_uid_get(&uid, &gid, "I_AM_NOT_A_USER", p);
  112. ABTS_ASSERT(tc, "fspr_gid_get should fail or "
  113. "set a user and group number",
  114. rv != APR_SUCCESS || uid == 424242 || gid == 4242442);
  115. errno = 0;
  116. tmp = NULL;
  117. rv = fspr_uid_homepath_get(&tmp, "I_AM_NOT_A_USER", p);
  118. ABTS_ASSERT(tc, "fspr_uid_homepath_get should fail or "
  119. "set a path name",
  120. rv != APR_SUCCESS || tmp != NULL);
  121. }
  122. #else
  123. static void fail_userinfo(abts_case *tc, void *data)
  124. {
  125. ABTS_NOT_IMPL(tc, "Users are not opaque integers on this platform");
  126. }
  127. #endif
  128. #else
  129. static void users_not_impl(abts_case *tc, void *data)
  130. {
  131. ABTS_NOT_IMPL(tc, "Users not implemented on this platform");
  132. }
  133. #endif
  134. abts_suite *testuser(abts_suite *suite)
  135. {
  136. suite = ADD_SUITE(suite)
  137. #if !APR_HAS_USER
  138. abts_run_test(suite, users_not_impl, NULL);
  139. #else
  140. abts_run_test(suite, uid_current, NULL);
  141. abts_run_test(suite, username, NULL);
  142. abts_run_test(suite, groupname, NULL);
  143. abts_run_test(suite, fail_userinfo, NULL);
  144. #endif
  145. return suite;
  146. }