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