test-tmpdir.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(tmpdir) {
  27. char tmpdir[PATHMAX];
  28. size_t len;
  29. char last;
  30. int r;
  31. /* Test the normal case */
  32. len = sizeof tmpdir;
  33. tmpdir[0] = '\0';
  34. ASSERT(strlen(tmpdir) == 0);
  35. r = uv_os_tmpdir(tmpdir, &len);
  36. ASSERT(r == 0);
  37. ASSERT(strlen(tmpdir) == len);
  38. ASSERT(len > 0);
  39. ASSERT(tmpdir[len] == '\0');
  40. if (len > 1) {
  41. last = tmpdir[len - 1];
  42. #ifdef _WIN32
  43. ASSERT(last != '\\');
  44. #else
  45. ASSERT(last != '/');
  46. #endif
  47. }
  48. /* Test the case where the buffer is too small */
  49. len = SMALLPATH;
  50. r = uv_os_tmpdir(tmpdir, &len);
  51. ASSERT(r == UV_ENOBUFS);
  52. ASSERT(len > SMALLPATH);
  53. /* Test invalid inputs */
  54. r = uv_os_tmpdir(NULL, &len);
  55. ASSERT(r == UV_EINVAL);
  56. r = uv_os_tmpdir(tmpdir, NULL);
  57. ASSERT(r == UV_EINVAL);
  58. len = 0;
  59. r = uv_os_tmpdir(tmpdir, &len);
  60. ASSERT(r == UV_EINVAL);
  61. #ifdef _WIN32
  62. const char *name = "TMP";
  63. char tmpdir_win[] = "C:\\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  64. r = uv_os_setenv(name, tmpdir_win);
  65. ASSERT(r == 0);
  66. char tmpdirx[PATHMAX];
  67. size_t lenx = sizeof tmpdirx;
  68. r = uv_os_tmpdir(tmpdirx, &lenx);
  69. ASSERT(r == 0);
  70. #endif
  71. return 0;
  72. }