test-process-title.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static void set_title(const char* title) {
  25. char buffer[512];
  26. int err;
  27. err = uv_get_process_title(buffer, sizeof(buffer));
  28. ASSERT(err == 0);
  29. err = uv_set_process_title(title);
  30. ASSERT(err == 0);
  31. err = uv_get_process_title(buffer, sizeof(buffer));
  32. ASSERT(err == 0);
  33. ASSERT(strcmp(buffer, title) == 0);
  34. }
  35. static void uv_get_process_title_edge_cases(void) {
  36. char buffer[512];
  37. int r;
  38. /* Test a NULL buffer */
  39. r = uv_get_process_title(NULL, 100);
  40. ASSERT(r == UV_EINVAL);
  41. /* Test size of zero */
  42. r = uv_get_process_title(buffer, 0);
  43. ASSERT(r == UV_EINVAL);
  44. /* Test for insufficient buffer size */
  45. r = uv_get_process_title(buffer, 1);
  46. ASSERT(r == UV_ENOBUFS);
  47. }
  48. TEST_IMPL(process_title) {
  49. #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
  50. defined(__PASE__)
  51. RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
  52. #endif
  53. /* Check for format string vulnerabilities. */
  54. set_title("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s");
  55. set_title("new title");
  56. /* Check uv_get_process_title() edge cases */
  57. uv_get_process_title_edge_cases();
  58. return 0;
  59. }
  60. static void exit_cb(uv_process_t* process, int64_t status, int signo) {
  61. ASSERT(status == 0);
  62. ASSERT(signo == 0);
  63. uv_close((uv_handle_t*) process, NULL);
  64. }
  65. TEST_IMPL(process_title_big_argv) {
  66. uv_process_options_t options;
  67. uv_process_t process;
  68. size_t exepath_size;
  69. char exepath[1024];
  70. char jumbo[512];
  71. char* args[5];
  72. #ifdef _WIN32
  73. /* Remove once https://github.com/libuv/libuv/issues/2667 is fixed. */
  74. uv_set_process_title("run-tests");
  75. #endif
  76. exepath_size = sizeof(exepath) - 1;
  77. ASSERT(0 == uv_exepath(exepath, &exepath_size));
  78. exepath[exepath_size] = '\0';
  79. memset(jumbo, 'x', sizeof(jumbo) - 1);
  80. jumbo[sizeof(jumbo) - 1] = '\0';
  81. /* Note: need to pass three arguments, not two, otherwise
  82. * run-tests.c thinks it's the name of a test to run.
  83. */
  84. args[0] = exepath;
  85. args[1] = "process_title_big_argv_helper";
  86. args[2] = jumbo;
  87. args[3] = jumbo;
  88. args[4] = NULL;
  89. memset(&options, 0, sizeof(options));
  90. options.file = exepath;
  91. options.args = args;
  92. options.exit_cb = exit_cb;
  93. ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
  94. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  95. MAKE_VALGRIND_HAPPY();
  96. return 0;
  97. }
  98. /* Called by process_title_big_argv_helper. */
  99. void process_title_big_argv(void) {
  100. char buf[256] = "fail";
  101. /* Return value deliberately ignored. */
  102. uv_get_process_title(buf, sizeof(buf));
  103. ASSERT(0 != strcmp(buf, "fail"));
  104. }