1*fbb2e0a3Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*fbb2e0a3Schristos  *
3*fbb2e0a3Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
4*fbb2e0a3Schristos  * of this software and associated documentation files (the "Software"), to
5*fbb2e0a3Schristos  * deal in the Software without restriction, including without limitation the
6*fbb2e0a3Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*fbb2e0a3Schristos  * sell copies of the Software, and to permit persons to whom the Software is
8*fbb2e0a3Schristos  * furnished to do so, subject to the following conditions:
9*fbb2e0a3Schristos  *
10*fbb2e0a3Schristos  * The above copyright notice and this permission notice shall be included in
11*fbb2e0a3Schristos  * all copies or substantial portions of the Software.
12*fbb2e0a3Schristos  *
13*fbb2e0a3Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*fbb2e0a3Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*fbb2e0a3Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*fbb2e0a3Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*fbb2e0a3Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*fbb2e0a3Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*fbb2e0a3Schristos  * IN THE SOFTWARE.
20*fbb2e0a3Schristos  */
21*fbb2e0a3Schristos 
22*fbb2e0a3Schristos #include "uv.h"
23*fbb2e0a3Schristos #include "task.h"
24*fbb2e0a3Schristos #include <string.h>
25*fbb2e0a3Schristos 
26*fbb2e0a3Schristos 
set_title(const char * title)27*fbb2e0a3Schristos static void set_title(const char* title) {
28*fbb2e0a3Schristos   char buffer[512];
29*fbb2e0a3Schristos   int err;
30*fbb2e0a3Schristos 
31*fbb2e0a3Schristos   err = uv_get_process_title(buffer, sizeof(buffer));
32*fbb2e0a3Schristos   ASSERT(err == 0);
33*fbb2e0a3Schristos 
34*fbb2e0a3Schristos   err = uv_set_process_title(title);
35*fbb2e0a3Schristos   ASSERT(err == 0);
36*fbb2e0a3Schristos 
37*fbb2e0a3Schristos   err = uv_get_process_title(buffer, sizeof(buffer));
38*fbb2e0a3Schristos   ASSERT(err == 0);
39*fbb2e0a3Schristos 
40*fbb2e0a3Schristos   ASSERT(strcmp(buffer, title) == 0);
41*fbb2e0a3Schristos }
42*fbb2e0a3Schristos 
43*fbb2e0a3Schristos 
uv_get_process_title_edge_cases(void)44*fbb2e0a3Schristos static void uv_get_process_title_edge_cases(void) {
45*fbb2e0a3Schristos   char buffer[512];
46*fbb2e0a3Schristos   int r;
47*fbb2e0a3Schristos 
48*fbb2e0a3Schristos   /* Test a NULL buffer */
49*fbb2e0a3Schristos   r = uv_get_process_title(NULL, 100);
50*fbb2e0a3Schristos   ASSERT(r == UV_EINVAL);
51*fbb2e0a3Schristos 
52*fbb2e0a3Schristos   /* Test size of zero */
53*fbb2e0a3Schristos   r = uv_get_process_title(buffer, 0);
54*fbb2e0a3Schristos   ASSERT(r == UV_EINVAL);
55*fbb2e0a3Schristos 
56*fbb2e0a3Schristos   /* Test for insufficient buffer size */
57*fbb2e0a3Schristos   r = uv_get_process_title(buffer, 1);
58*fbb2e0a3Schristos   ASSERT(r == UV_ENOBUFS);
59*fbb2e0a3Schristos }
60*fbb2e0a3Schristos 
61*fbb2e0a3Schristos 
TEST_IMPL(process_title)62*fbb2e0a3Schristos TEST_IMPL(process_title) {
63*fbb2e0a3Schristos #if defined(__sun) || defined(__CYGWIN__) || defined(__MSYS__) || \
64*fbb2e0a3Schristos     defined(__PASE__)
65*fbb2e0a3Schristos   RETURN_SKIP("uv_(get|set)_process_title is not implemented.");
66*fbb2e0a3Schristos #endif
67*fbb2e0a3Schristos 
68*fbb2e0a3Schristos   /* Check for format string vulnerabilities. */
69*fbb2e0a3Schristos   set_title("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s");
70*fbb2e0a3Schristos   set_title("new title");
71*fbb2e0a3Schristos 
72*fbb2e0a3Schristos   /* Check uv_get_process_title() edge cases */
73*fbb2e0a3Schristos   uv_get_process_title_edge_cases();
74*fbb2e0a3Schristos 
75*fbb2e0a3Schristos   return 0;
76*fbb2e0a3Schristos }
77*fbb2e0a3Schristos 
78*fbb2e0a3Schristos 
exit_cb(uv_process_t * process,int64_t status,int signo)79*fbb2e0a3Schristos static void exit_cb(uv_process_t* process, int64_t status, int signo) {
80*fbb2e0a3Schristos   ASSERT(status == 0);
81*fbb2e0a3Schristos   ASSERT(signo == 0);
82*fbb2e0a3Schristos   uv_close((uv_handle_t*) process, NULL);
83*fbb2e0a3Schristos }
84*fbb2e0a3Schristos 
85*fbb2e0a3Schristos 
TEST_IMPL(process_title_big_argv)86*fbb2e0a3Schristos TEST_IMPL(process_title_big_argv) {
87*fbb2e0a3Schristos   uv_process_options_t options;
88*fbb2e0a3Schristos   uv_process_t process;
89*fbb2e0a3Schristos   size_t exepath_size;
90*fbb2e0a3Schristos   char exepath[1024];
91*fbb2e0a3Schristos   char jumbo[512];
92*fbb2e0a3Schristos   char* args[5];
93*fbb2e0a3Schristos 
94*fbb2e0a3Schristos #ifdef _WIN32
95*fbb2e0a3Schristos   /* Remove once https://github.com/libuv/libuv/issues/2667 is fixed. */
96*fbb2e0a3Schristos   uv_set_process_title("run-tests");
97*fbb2e0a3Schristos #endif
98*fbb2e0a3Schristos 
99*fbb2e0a3Schristos   exepath_size = sizeof(exepath) - 1;
100*fbb2e0a3Schristos   ASSERT(0 == uv_exepath(exepath, &exepath_size));
101*fbb2e0a3Schristos   exepath[exepath_size] = '\0';
102*fbb2e0a3Schristos 
103*fbb2e0a3Schristos   memset(jumbo, 'x', sizeof(jumbo) - 1);
104*fbb2e0a3Schristos   jumbo[sizeof(jumbo) - 1] = '\0';
105*fbb2e0a3Schristos 
106*fbb2e0a3Schristos   /* Note: need to pass three arguments, not two, otherwise
107*fbb2e0a3Schristos    * run-tests.c thinks it's the name of a test to run.
108*fbb2e0a3Schristos    */
109*fbb2e0a3Schristos   args[0] = exepath;
110*fbb2e0a3Schristos   args[1] = "process_title_big_argv_helper";
111*fbb2e0a3Schristos   args[2] = jumbo;
112*fbb2e0a3Schristos   args[3] = jumbo;
113*fbb2e0a3Schristos   args[4] = NULL;
114*fbb2e0a3Schristos 
115*fbb2e0a3Schristos   memset(&options, 0, sizeof(options));
116*fbb2e0a3Schristos   options.file = exepath;
117*fbb2e0a3Schristos   options.args = args;
118*fbb2e0a3Schristos   options.exit_cb = exit_cb;
119*fbb2e0a3Schristos 
120*fbb2e0a3Schristos   ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
121*fbb2e0a3Schristos   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
122*fbb2e0a3Schristos 
123*fbb2e0a3Schristos   MAKE_VALGRIND_HAPPY();
124*fbb2e0a3Schristos   return 0;
125*fbb2e0a3Schristos }
126*fbb2e0a3Schristos 
127*fbb2e0a3Schristos 
128*fbb2e0a3Schristos /* Called by process_title_big_argv_helper. */
process_title_big_argv(void)129*fbb2e0a3Schristos void process_title_big_argv(void) {
130*fbb2e0a3Schristos   char buf[256] = "fail";
131*fbb2e0a3Schristos 
132*fbb2e0a3Schristos   /* Return value deliberately ignored. */
133*fbb2e0a3Schristos   uv_get_process_title(buf, sizeof(buf));
134*fbb2e0a3Schristos   ASSERT(0 != strcmp(buf, "fail"));
135*fbb2e0a3Schristos }
136