xref: /libuv/test/test-get-currentexe.c (revision 011a1ac1)
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 #if defined(__OpenBSD__)
39   RETURN_SKIP("Test does not currently work in OpenBSD");
40 #endif
41 
42   char buffer[PATHMAX];
43   char path[PATHMAX];
44   size_t size;
45   char* match;
46   int r;
47 
48   size = sizeof(buffer) / sizeof(buffer[0]);
49   r = uv_exepath(buffer, &size);
50   ASSERT(!r);
51 
52 #ifdef _WIN32
53   snprintf(path, sizeof(path), "%s", executable_path);
54 #else
55   ASSERT_NOT_NULL(realpath(executable_path, path));
56 #endif
57 
58   match = strstr(buffer, path);
59   /* Verify that the path returned from uv_exepath is a subdirectory of
60    * executable_path.
61    */
62   ASSERT(match && !strcmp(match, path));
63   ASSERT_EQ(size, strlen(buffer));
64 
65   /* Negative tests */
66   size = sizeof(buffer) / sizeof(buffer[0]);
67   r = uv_exepath(NULL, &size);
68   ASSERT_EQ(r, UV_EINVAL);
69 
70   r = uv_exepath(buffer, NULL);
71   ASSERT_EQ(r, UV_EINVAL);
72 
73   size = 0;
74   r = uv_exepath(buffer, &size);
75   ASSERT_EQ(r, UV_EINVAL);
76 
77   memset(buffer, -1, sizeof(buffer));
78 
79   size = 1;
80   r = uv_exepath(buffer, &size);
81   ASSERT_OK(r);
82   ASSERT_OK(size);
83   ASSERT_EQ(buffer[0], '\0');
84 
85   memset(buffer, -1, sizeof(buffer));
86 
87   size = 2;
88   r = uv_exepath(buffer, &size);
89   ASSERT_OK(r);
90   ASSERT_EQ(1, size);
91   ASSERT_NE(buffer[0], '\0');
92   ASSERT_EQ(buffer[1], '\0');
93 
94   /* Verify uv_exepath is not affected by uv_set_process_title(). */
95   r = uv_set_process_title("foobar");
96   ASSERT_OK(r);
97   size = sizeof(buffer);
98   r = uv_exepath(buffer, &size);
99   ASSERT_OK(r);
100 
101   match = strstr(buffer, path);
102   /* Verify that the path returned from uv_exepath is a subdirectory of
103    * executable_path.
104    */
105   ASSERT_NOT_NULL(match);
106   ASSERT_STR_EQ(match, path);
107   ASSERT_EQ(size, strlen(buffer));
108   return 0;
109 }
110