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 #ifndef _WIN32
27 #include <unistd.h>
28 #endif
29
30 #define PATHMAX 4096
31 extern char executable_path[];
32
TEST_IMPL(get_currentexe)33 TEST_IMPL(get_currentexe) {
34 /* TODO(gengjiawen): Fix test on QEMU. */
35 #if defined(__QEMU__)
36 RETURN_SKIP("Test does not currently work in QEMU");
37 #endif
38
39 char buffer[PATHMAX];
40 char path[PATHMAX];
41 size_t size;
42 char* match;
43 int r;
44
45 size = sizeof(buffer) / sizeof(buffer[0]);
46 r = uv_exepath(buffer, &size);
47 ASSERT(!r);
48
49 #ifdef _WIN32
50 snprintf(path, sizeof(path), "%s", executable_path);
51 #else
52 ASSERT_NOT_NULL(realpath(executable_path, path));
53 #endif
54
55 match = strstr(buffer, path);
56 /* Verify that the path returned from uv_exepath is a subdirectory of
57 * executable_path.
58 */
59 ASSERT(match && !strcmp(match, path));
60 ASSERT(size == strlen(buffer));
61
62 /* Negative tests */
63 size = sizeof(buffer) / sizeof(buffer[0]);
64 r = uv_exepath(NULL, &size);
65 ASSERT(r == UV_EINVAL);
66
67 r = uv_exepath(buffer, NULL);
68 ASSERT(r == UV_EINVAL);
69
70 size = 0;
71 r = uv_exepath(buffer, &size);
72 ASSERT(r == UV_EINVAL);
73
74 memset(buffer, -1, sizeof(buffer));
75
76 size = 1;
77 r = uv_exepath(buffer, &size);
78 ASSERT(r == 0);
79 ASSERT(size == 0);
80 ASSERT(buffer[0] == '\0');
81
82 memset(buffer, -1, sizeof(buffer));
83
84 size = 2;
85 r = uv_exepath(buffer, &size);
86 ASSERT(r == 0);
87 ASSERT(size == 1);
88 ASSERT(buffer[0] != '\0');
89 ASSERT(buffer[1] == '\0');
90
91 /* Verify uv_exepath is not affected by uv_set_process_title(). */
92 r = uv_set_process_title("foobar");
93 ASSERT_EQ(r, 0);
94 size = sizeof(buffer);
95 r = uv_exepath(buffer, &size);
96 ASSERT_EQ(r, 0);
97
98 match = strstr(buffer, path);
99 /* Verify that the path returned from uv_exepath is a subdirectory of
100 * executable_path.
101 */
102 ASSERT_NOT_NULL(match);
103 ASSERT_STR_EQ(match, path);
104 ASSERT_EQ(size, strlen(buffer));
105 return 0;
106 }
107