test-get-passwd.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright libuv contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. TEST_IMPL(get_passwd) {
  25. uv_passwd_t pwd;
  26. size_t len;
  27. int r;
  28. /* Test the normal case */
  29. r = uv_os_get_passwd(&pwd);
  30. ASSERT(r == 0);
  31. len = strlen(pwd.username);
  32. ASSERT(len > 0);
  33. #ifdef _WIN32
  34. ASSERT(pwd.shell == NULL);
  35. #else
  36. len = strlen(pwd.shell);
  37. # ifndef __PASE__
  38. ASSERT(len > 0);
  39. # endif
  40. #endif
  41. len = strlen(pwd.homedir);
  42. ASSERT(len > 0);
  43. #ifdef _WIN32
  44. if (len == 3 && pwd.homedir[1] == ':')
  45. ASSERT(pwd.homedir[2] == '\\');
  46. else
  47. ASSERT(pwd.homedir[len - 1] != '\\');
  48. #else
  49. if (len == 1)
  50. ASSERT(pwd.homedir[0] == '/');
  51. else
  52. ASSERT(pwd.homedir[len - 1] != '/');
  53. #endif
  54. #ifdef _WIN32
  55. ASSERT(pwd.uid == -1);
  56. ASSERT(pwd.gid == -1);
  57. #else
  58. ASSERT(pwd.uid >= 0);
  59. ASSERT(pwd.gid >= 0);
  60. #endif
  61. /* Test uv_os_free_passwd() */
  62. uv_os_free_passwd(&pwd);
  63. ASSERT(pwd.username == NULL);
  64. ASSERT(pwd.shell == NULL);
  65. ASSERT(pwd.homedir == NULL);
  66. /* Test a double free */
  67. uv_os_free_passwd(&pwd);
  68. ASSERT(pwd.username == NULL);
  69. ASSERT(pwd.shell == NULL);
  70. ASSERT(pwd.homedir == NULL);
  71. /* Test invalid input */
  72. r = uv_os_get_passwd(NULL);
  73. ASSERT(r == UV_EINVAL);
  74. return 0;
  75. }