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