File: process_detector_test.cc

package info (click to toggle)
opentelemetry-cpp 1.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,372 kB
  • sloc: cpp: 96,239; sh: 1,766; makefile: 36; python: 31
file content (190 lines) | stat: -rw-r--r-- 5,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <stdint.h>
#include <fstream>
#include <string>
#include <vector>

#ifdef _MSC_VER
// clang-format off
#  include <process.h>
#  include <windows.h>
#  include <psapi.h>
#  define getpid _getpid
// clang-format on
#else
#  include <sys/types.h>
#  include <unistd.h>
#  include <cstdio>
#endif

#include "opentelemetry/resource_detectors/detail/process_detector_utils.h"

TEST(ProcessDetectorUtilsTest, FormFilePath)
{
  int32_t pid              = 1234;
  std::string cmdline_path = opentelemetry::resource_detector::detail::FormFilePath(pid, "cmdline");
  std::string exe_path     = opentelemetry::resource_detector::detail::FormFilePath(pid, "exe");

  EXPECT_EQ(cmdline_path, "/proc/1234/cmdline");
  EXPECT_EQ(exe_path, "/proc/1234/exe");
}

TEST(ProcessDetectorUtilsTest, ExtractCommandWithArgs)
{
  std::string filename{"test_command_args.txt"};

  {
    std::ofstream outfile(filename, std::ios::binary);
    const char raw_data[] = "test_command\0arg1\0arg2\0arg3\0";
    outfile.write(raw_data, sizeof(raw_data) - 1);
  }

  std::vector<std::string> args =
      opentelemetry::resource_detector::detail::ExtractCommandWithArgs(filename);
  EXPECT_EQ(args, (std::vector<std::string>{"test_command", "arg1", "arg2", "arg3"}));

  std::remove(filename.c_str());  // Cleanup
}

TEST(ProcessDetectorUtilsTest, EmptyCommandWithArgsFile)
{
  std::string filename{"empty_command_args.txt"};
  std::ofstream outfile(filename, std::ios::binary);
  outfile.close();

  std::vector<std::string> args =
      opentelemetry::resource_detector::detail::ExtractCommandWithArgs(filename);
  EXPECT_TRUE(args.empty());

  std::remove(filename.c_str());  // Cleanup
}

TEST(ProcessDetectorUtilsTest, GetExecutablePathTest)
{
  int32_t pid = getpid();
  std::string path;
#ifdef _MSC_VER
  HANDLE hProcess =
      OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid));
  if (!hProcess)
  {
    path = std::string();
  }
  else
  {

    WCHAR wbuffer[MAX_PATH];
    DWORD len = GetProcessImageFileNameW(hProcess, wbuffer, MAX_PATH);
    CloseHandle(hProcess);

    if (len == 0)
    {
      path = std::string();
    }
    else
    {
      int size_needed = WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, NULL, 0, NULL, NULL);
      std::string utf8_path(size_needed, 0);
      WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, &utf8_path[0], size_needed, NULL, NULL);

      path = utf8_path;
    }
  }
#else
  std::string exe_path = opentelemetry::resource_detector::detail::FormFilePath(pid, "exe");
  char buffer[4096];

  ssize_t len = readlink(exe_path.c_str(), buffer, sizeof(buffer) - 1);
  if (len != -1)
  {
    buffer[len] = '\0';
    path        = std::string(buffer);
  }
  else
  {
    path = std::string();
  }
#endif
  std::string expected_path = opentelemetry::resource_detector::detail::GetExecutablePath(pid);
  EXPECT_EQ(path, expected_path);
}

TEST(ProcessDetectorUtilsTest, CommandTest)
{
  int32_t pid = getpid();
  std::string command;
#ifdef _MSC_VER
  int argc      = 0;
  LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc);

  if (argvW && argc > 0)
  {
    int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, NULL, 0, NULL, NULL);
    if (size_needed > 0)
    {
      std::string arg(size_needed - 1, 0);
      WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, &arg[0], size_needed, NULL, NULL);
      command = arg;
    }

    LocalFree(argvW);
  }
  else
  {
    command = std::string();
  }
#else
  std::string command_line_path =
      opentelemetry::resource_detector::detail::FormFilePath(pid, "cmdline");
  std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary);
  std::getline(command_line_file, command, '\0');
#endif
  std::vector<std::string> expected_command_with_args =
      opentelemetry::resource_detector::detail::GetCommandWithArgs(pid);
  std::string expected_command;
  if (!expected_command_with_args.empty())
  {
    expected_command = expected_command_with_args[0];
  }
  EXPECT_EQ(command, expected_command);
}

TEST(ProcessDetectorUtilsTest, GetCommandWithArgsTest)
{
  int32_t pid = getpid();
  std::vector<std::string> args;
#ifdef _MSC_VER
  int argc      = 0;
  LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc);
  if (!argvW)
  {
    args = {};
  }
  else
  {
    for (int i = 0; i < argc; i++)
    {
      // Convert UTF-16 to UTF-8
      int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL);
      if (size_needed > 0)
      {
        std::string arg(size_needed - 1, 0);
        WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, &arg[0], size_needed, NULL, NULL);
        args.push_back(arg);
      }
    }
  }

  LocalFree(argvW);
#else
  std::string command_line_path =
      opentelemetry::resource_detector::detail::FormFilePath(pid, "cmdline");
  args = opentelemetry::resource_detector::detail::ExtractCommandWithArgs(command_line_path);
#endif
  std::vector<std::string> expected_args =
      opentelemetry::resource_detector::detail::GetCommandWithArgs(pid);
  EXPECT_EQ(args, expected_args);
}