test-get-currentexe.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright Joyent, Inc. and other Node 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. #ifndef _WIN32
  25. #include <unistd.h>
  26. #endif
  27. #define PATHMAX 4096
  28. extern char executable_path[];
  29. TEST_IMPL(get_currentexe) {
  30. char buffer[PATHMAX];
  31. char path[PATHMAX];
  32. size_t size;
  33. char* match;
  34. int r;
  35. size = sizeof(buffer) / sizeof(buffer[0]);
  36. r = uv_exepath(buffer, &size);
  37. ASSERT(!r);
  38. #ifdef _WIN32
  39. snprintf(path, sizeof(path), "%s", executable_path);
  40. #else
  41. ASSERT(NULL != realpath(executable_path, path));
  42. #endif
  43. match = strstr(buffer, path);
  44. /* Verify that the path returned from uv_exepath is a subdirectory of
  45. * executable_path.
  46. */
  47. ASSERT(match && !strcmp(match, path));
  48. ASSERT(size == strlen(buffer));
  49. /* Negative tests */
  50. size = sizeof(buffer) / sizeof(buffer[0]);
  51. r = uv_exepath(NULL, &size);
  52. ASSERT(r == UV_EINVAL);
  53. r = uv_exepath(buffer, NULL);
  54. ASSERT(r == UV_EINVAL);
  55. size = 0;
  56. r = uv_exepath(buffer, &size);
  57. ASSERT(r == UV_EINVAL);
  58. memset(buffer, -1, sizeof(buffer));
  59. size = 1;
  60. r = uv_exepath(buffer, &size);
  61. ASSERT(r == 0);
  62. ASSERT(size == 0);
  63. ASSERT(buffer[0] == '\0');
  64. memset(buffer, -1, sizeof(buffer));
  65. size = 2;
  66. r = uv_exepath(buffer, &size);
  67. ASSERT(r == 0);
  68. ASSERT(size == 1);
  69. ASSERT(buffer[0] != '\0');
  70. ASSERT(buffer[1] == '\0');
  71. return 0;
  72. }