test-homedir.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright libuv project 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. #define PATHMAX 4096
  25. #define SMALLPATH 1
  26. TEST_IMPL(homedir) {
  27. char homedir[PATHMAX];
  28. size_t len;
  29. int r;
  30. /* Test the normal case */
  31. len = sizeof homedir;
  32. homedir[0] = '\0';
  33. ASSERT(strlen(homedir) == 0);
  34. r = uv_os_homedir(homedir, &len);
  35. ASSERT(r == 0);
  36. ASSERT(strlen(homedir) == len);
  37. ASSERT(len > 0);
  38. ASSERT(homedir[len] == '\0');
  39. #ifdef _WIN32
  40. if (len == 3 && homedir[1] == ':')
  41. ASSERT(homedir[2] == '\\');
  42. else
  43. ASSERT(homedir[len - 1] != '\\');
  44. #else
  45. if (len == 1)
  46. ASSERT(homedir[0] == '/');
  47. else
  48. ASSERT(homedir[len - 1] != '/');
  49. #endif
  50. /* Test the case where the buffer is too small */
  51. len = SMALLPATH;
  52. r = uv_os_homedir(homedir, &len);
  53. ASSERT(r == UV_ENOBUFS);
  54. ASSERT(len > SMALLPATH);
  55. /* Test invalid inputs */
  56. r = uv_os_homedir(NULL, &len);
  57. ASSERT(r == UV_EINVAL);
  58. r = uv_os_homedir(homedir, NULL);
  59. ASSERT(r == UV_EINVAL);
  60. len = 0;
  61. r = uv_os_homedir(homedir, &len);
  62. ASSERT(r == UV_EINVAL);
  63. return 0;
  64. }