5
5
#include < stdint.h>
6
6
#include < fstream>
7
7
#include < string>
8
+ #include < vector>
8
9
9
10
#ifdef _MSC_VER
10
11
// clang-format off
@@ -31,30 +32,32 @@ TEST(ProcessDetectorUtilsTest, FormFilePath)
31
32
EXPECT_EQ (exe_path, " /proc/1234/exe" );
32
33
}
33
34
34
- TEST (ProcessDetectorUtilsTest, ExtractCommand )
35
+ TEST (ProcessDetectorUtilsTest, ExtractCommandWithArgs )
35
36
{
36
- std::string filename{" test_command .txt" };
37
+ std::string filename{" test_command_args .txt" };
37
38
38
39
{
39
40
std::ofstream outfile (filename, std::ios::binary);
40
41
const char raw_data[] = " test_command\0 arg1\0 arg2\0 arg3\0 " ;
41
42
outfile.write (raw_data, sizeof (raw_data) - 1 );
42
43
}
43
44
44
- std::string command = opentelemetry::resource_detector::detail::ExtractCommand (filename);
45
- EXPECT_EQ (command, std::string{" test_command" });
45
+ std::vector<std::string> args =
46
+ opentelemetry::resource_detector::detail::ExtractCommandWithArgs (filename);
47
+ EXPECT_EQ (args, (std::vector<std::string>{" test_command" , " arg1" , " arg2" , " arg3" }));
46
48
47
49
std::remove (filename.c_str ()); // Cleanup
48
50
}
49
51
50
- TEST (ProcessDetectorUtilsTest, EmptyCommandFile )
52
+ TEST (ProcessDetectorUtilsTest, EmptyCommandWithArgsFile )
51
53
{
52
- std::string filename{" empty_command .txt" };
54
+ std::string filename{" empty_command_args .txt" };
53
55
std::ofstream outfile (filename, std::ios::binary);
54
56
outfile.close ();
55
57
56
- std::string command = opentelemetry::resource_detector::detail::ExtractCommand (filename);
57
- EXPECT_EQ (command, std::string{" " });
58
+ std::vector<std::string> args =
59
+ opentelemetry::resource_detector::detail::ExtractCommandWithArgs (filename);
60
+ EXPECT_TRUE (args.empty ());
58
61
59
62
std::remove (filename.c_str ()); // Cleanup
60
63
}
@@ -109,40 +112,79 @@ TEST(ProcessDetectorUtilsTest, GetExecutablePathTest)
109
112
EXPECT_EQ (path, expected_path);
110
113
}
111
114
112
- TEST (ProcessDetectorUtilsTest, GetCommandTest )
115
+ TEST (ProcessDetectorUtilsTest, CommandTest )
113
116
{
114
117
int32_t pid = getpid ();
115
118
std::string command;
116
119
#ifdef _MSC_VER
117
- // On Windows, GetCommandLineW only works for the CURRENT process,
118
- // so we ignore `pid` and just return the current process's command line.
119
- LPCWSTR wcmd = GetCommandLineW ();
120
- if (!wcmd )
120
+ int argc = 0 ;
121
+ LPWSTR *argvW = CommandLineToArgvW ( GetCommandLineW (), &argc);
122
+
123
+ if (argvW && argc > 0 )
121
124
{
122
- command = std::string ();
125
+ int size_needed = WideCharToMultiByte (CP_UTF8, 0 , argvW[0 ], -1 , NULL , 0 , NULL , NULL );
126
+ if (size_needed > 0 )
127
+ {
128
+ std::string arg (size_needed - 1 , 0 );
129
+ WideCharToMultiByte (CP_UTF8, 0 , argvW[0 ], -1 , &arg[0 ], size_needed, NULL , NULL );
130
+ command = arg;
131
+ }
132
+
133
+ LocalFree (argvW);
123
134
}
124
135
else
125
136
{
137
+ command = std::string ();
138
+ }
139
+ #else
140
+ std::string command_line_path =
141
+ opentelemetry::resource_detector::detail::FormFilePath (pid, " cmdline" );
142
+ std::ifstream command_line_file (command_line_path, std::ios::in | std::ios::binary);
143
+ std::getline (command_line_file, command, ' \0 ' );
144
+ #endif
145
+ std::vector<std::string> expected_command_with_args =
146
+ opentelemetry::resource_detector::detail::GetCommandWithArgs (pid);
147
+ std::string expected_command;
148
+ if (!expected_command_with_args.empty ())
149
+ {
150
+ expected_command = expected_command_with_args[0 ];
151
+ }
152
+ EXPECT_EQ (command, expected_command);
153
+ }
126
154
127
- // Convert UTF-16 to UTF-8
128
- int size_needed = WideCharToMultiByte (CP_UTF8, 0 , wcmd, -1 , NULL , 0 , NULL , NULL );
129
- if (size_needed <= 0 )
130
- {
131
- command = std::string ();
132
- }
133
- else
155
+ TEST (ProcessDetectorUtilsTest, GetCommandWithArgsTest)
156
+ {
157
+ int32_t pid = getpid ();
158
+ std::vector<std::string> args;
159
+ #ifdef _MSC_VER
160
+ int argc = 0 ;
161
+ LPWSTR *argvW = CommandLineToArgvW (GetCommandLineW (), &argc);
162
+ if (!argvW)
163
+ {
164
+ args = {};
165
+ }
166
+ else
167
+ {
168
+ for (int i = 0 ; i < argc; i++)
134
169
{
135
- std::string utf8_command (size_needed - 1 , 0 ); // exclude null terminator
136
- WideCharToMultiByte (CP_UTF8, 0 , wcmd, -1 , &utf8_command[0 ], size_needed, NULL , NULL );
137
- command = utf8_command;
170
+ // Convert UTF-16 to UTF-8
171
+ int size_needed = WideCharToMultiByte (CP_UTF8, 0 , argvW[i], -1 , NULL , 0 , NULL , NULL );
172
+ if (size_needed > 0 )
173
+ {
174
+ std::string arg (size_needed - 1 , 0 );
175
+ WideCharToMultiByte (CP_UTF8, 0 , argvW[i], -1 , &arg[0 ], size_needed, NULL , NULL );
176
+ args.push_back (arg);
177
+ }
138
178
}
139
179
}
180
+
181
+ LocalFree (argvW);
140
182
#else
141
- // This is the path to get the command that was used to start the process
142
183
std::string command_line_path =
143
184
opentelemetry::resource_detector::detail::FormFilePath (pid, " cmdline" );
144
- command = opentelemetry::resource_detector::detail::ExtractCommand (command_line_path);
185
+ args = opentelemetry::resource_detector::detail::ExtractCommandWithArgs (command_line_path);
145
186
#endif
146
- std::string expected_command = opentelemetry::resource_detector::detail::GetCommand (pid);
147
- EXPECT_EQ (command, expected_command);
187
+ std::vector<std::string> expected_args =
188
+ opentelemetry::resource_detector::detail::GetCommandWithArgs (pid);
189
+ EXPECT_EQ (args, expected_args);
148
190
}
0 commit comments