Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/test/test_common/environment.cc
Line
Count
Source (jump to first uncovered line)
1
#include "test/test_common/environment.h"
2
3
#include <fstream>
4
#include <iostream>
5
#include <regex>
6
#include <sstream>
7
#include <string>
8
#include <vector>
9
10
#include "envoy/common/platform.h"
11
12
#include "source/common/common/assert.h"
13
#include "source/common/common/compiler_requirements.h"
14
#include "source/common/common/logger.h"
15
#include "source/common/common/macros.h"
16
#include "source/common/common/utility.h"
17
#include "source/common/filesystem/directory.h"
18
19
#include "absl/container/node_hash_map.h"
20
21
#ifdef ENVOY_HANDLE_SIGNALS
22
#include "source/common/signal/signal_action.h"
23
#endif
24
25
#include "source/server/options_impl.h"
26
27
#include "test/test_common/file_system_for_test.h"
28
#include "test/test_common/network_utility.h"
29
30
#include "absl/debugging/symbolize.h"
31
#include "absl/strings/match.h"
32
#include "absl/strings/str_format.h"
33
#include "gtest/gtest.h"
34
#include "spdlog/spdlog.h"
35
36
using bazel::tools::cpp::runfiles::Runfiles;
37
using Envoy::Filesystem::Directory;
38
using Envoy::Filesystem::DirectoryEntry;
39
40
namespace Envoy {
41
namespace {
42
43
13
std::string makeTempDir(std::string basename_template) {
44
#ifdef WIN32
45
  std::string name_template{};
46
  if (std::getenv("TMP")) {
47
    name_template = TestEnvironment::getCheckedEnvVar("TMP") + "\\" + basename_template;
48
  } else if (std::getenv("WINDIR")) {
49
    name_template = TestEnvironment::getCheckedEnvVar("WINDIR") + "\\TEMP\\" + basename_template;
50
  } else {
51
    name_template = basename_template;
52
  }
53
  char* dirname = ::_mktemp(&name_template[0]);
54
  RELEASE_ASSERT(dirname != nullptr, fmt::format("failed to create tempdir from template: {} {}",
55
                                                 name_template, errorDetails(errno)));
56
  TestEnvironment::createPath(dirname);
57
#else
58
13
  std::string name_template = "/tmp/" + basename_template;
59
13
  char* dirname = ::mkdtemp(&name_template[0]);
60
13
  RELEASE_ASSERT(dirname != nullptr, fmt::format("failed to create tempdir from template: {} {}",
61
13
                                                 name_template, errorDetails(errno)));
62
13
#endif
63
13
  return {dirname};
64
13
}
65
66
0
std::string getOrCreateUnixDomainSocketDirectory() {
67
0
  const char* path = std::getenv("TEST_UDSDIR");
68
0
  if (path != nullptr) {
69
0
    return {path};
70
0
  }
71
  // Generate temporary path for Unix Domain Sockets only. This is a workaround
72
  // for the sun_path limit on `sockaddr_un`, since TEST_TMPDIR as generated by
73
  // Bazel may be too long.
74
0
  return makeTempDir("envoy_test_uds.XXXXXX");
75
0
}
76
77
13
std::string getTemporaryDirectory() {
78
13
  std::string temp_dir;
79
13
  if (std::getenv("TEST_TMPDIR")) {
80
0
    temp_dir = TestEnvironment::getCheckedEnvVar("TEST_TMPDIR");
81
13
  } else if (std::getenv("TMPDIR")) {
82
0
    temp_dir = TestEnvironment::getCheckedEnvVar("TMPDIR");
83
13
  } else {
84
13
    return makeTempDir("envoy_test_tmp.XXXXXX");
85
13
  }
86
0
  TestEnvironment::createPath(temp_dir);
87
0
  return temp_dir;
88
13
}
89
90
// Allow initializeOptions() to remember CLI args for getOptions().
91
int argc_;
92
char** argv_;
93
94
} // namespace
95
96
5.68k
void TestEnvironment::createPath(const std::string& path) {
97
5.68k
  if (Filesystem::fileSystemForTest().directoryExists(path)) {
98
2.84k
    return;
99
2.84k
  }
100
2.84k
  const Filesystem::PathSplitResult parent =
101
2.84k
      Filesystem::fileSystemForTest().splitPathFromFilename(path).value();
102
2.84k
  if (parent.file_.length() > 0) {
103
2.84k
    TestEnvironment::createPath(std::string(parent.directory_));
104
2.84k
  }
105
2.84k
#ifndef WIN32
106
2.84k
  RELEASE_ASSERT(::mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0,
107
2.84k
                 absl::StrCat("failed to create path: ", path));
108
#else
109
  RELEASE_ASSERT(::CreateDirectory(path.c_str(), NULL),
110
                 absl::StrCat("failed to create path: ", path));
111
#endif
112
2.84k
}
113
114
// On linux, attempt to unlink any file that exists at path,
115
// ignoring the result code, to avoid traversing a symlink,
116
// On windows, also attempt to remove the directory in case
117
// it is actually a symlink/junction, ignoring the result code.
118
// Proceed to iteratively recurse the directory if it still remains
119
2.84k
void TestEnvironment::removePath(const std::string& path) {
120
2.84k
  RELEASE_ASSERT(absl::StartsWith(path, TestEnvironment::temporaryDirectory()),
121
2.84k
                 "cowardly refusing to remove test directory not in temp path");
122
2.84k
#ifndef WIN32
123
2.84k
  (void)::unlink(path.c_str());
124
#else
125
  (void)::DeleteFile(path.c_str());
126
  (void)::RemoveDirectory(path.c_str());
127
#endif
128
2.84k
  if (!Filesystem::fileSystemForTest().directoryExists(path)) {
129
0
    return;
130
0
  }
131
2.84k
  Directory directory(path);
132
2.84k
  std::string entry_name;
133
2.84k
  entry_name.reserve(path.size() + 256);
134
2.84k
  entry_name.append(path);
135
2.84k
  entry_name.append("/");
136
2.84k
  size_t fileidx = entry_name.size();
137
8.52k
  for (const DirectoryEntry& entry : directory) {
138
8.52k
    entry_name.resize(fileidx);
139
8.52k
    entry_name.append(entry.name_);
140
8.52k
    if (entry.type_ == Envoy::Filesystem::FileType::Regular) {
141
2.84k
#ifndef WIN32
142
2.84k
      RELEASE_ASSERT(::unlink(entry_name.c_str()) == 0,
143
2.84k
                     absl::StrCat("failed to remove file: ", entry_name));
144
#else
145
      RELEASE_ASSERT(::DeleteFile(entry_name.c_str()),
146
                     absl::StrCat("failed to remove file: ", entry_name));
147
#endif
148
5.68k
    } else if (entry.type_ == Envoy::Filesystem::FileType::Directory) {
149
5.68k
      if (entry.name_ != "." && entry.name_ != "..") {
150
0
        removePath(entry_name);
151
0
      }
152
5.68k
    }
153
8.52k
  }
154
2.84k
#ifndef WIN32
155
2.84k
  RELEASE_ASSERT(::rmdir(path.c_str()) == 0,
156
2.84k
                 absl::StrCat("failed to remove path: ", path, " (rmdir failed)"));
157
#else
158
  RELEASE_ASSERT(::RemoveDirectory(path.c_str()), absl::StrCat("failed to remove path: ", path));
159
#endif
160
2.84k
}
161
162
0
void TestEnvironment::renameFile(const std::string& old_name, const std::string& new_name) {
163
0
  Filesystem::fileSystemForTest().renameFile(old_name, new_name);
164
0
}
165
166
0
void TestEnvironment::createSymlink(const std::string& target, const std::string& link) {
167
#ifdef WIN32
168
  const DWORD attributes = ::GetFileAttributes(target.c_str());
169
  ASSERT_NE(attributes, INVALID_FILE_ATTRIBUTES);
170
  int flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
171
  if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
172
    flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
173
  }
174
175
  const BOOLEAN rc = ::CreateSymbolicLink(link.c_str(), target.c_str(), flags);
176
  ASSERT_NE(rc, 0);
177
#else
178
0
  const int rc = ::symlink(target.c_str(), link.c_str());
179
0
  ASSERT_EQ(rc, 0);
180
0
#endif
181
0
}
182
183
2.64k
absl::optional<std::string> TestEnvironment::getOptionalEnvVar(const std::string& var) {
184
2.64k
  const char* path = std::getenv(var.c_str());
185
2.64k
  if (path == nullptr) {
186
2.64k
    return {};
187
2.64k
  }
188
0
  return std::string(path);
189
2.64k
}
190
191
0
std::string TestEnvironment::getCheckedEnvVar(const std::string& var) {
192
0
  auto optional = getOptionalEnvVar(var);
193
0
  RELEASE_ASSERT(optional.has_value(), var);
194
0
  return optional.value();
195
0
}
196
197
0
void TestEnvironment::initializeTestMain(char* program_name) {
198
#ifdef WIN32
199
  _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
200
201
  _set_invalid_parameter_handler([](const wchar_t* expression, const wchar_t* function,
202
                                    const wchar_t* file, unsigned int line,
203
                                    uintptr_t pReserved) {});
204
205
  WSADATA wsa_data;
206
  const WORD version_requested = MAKEWORD(2, 2);
207
  RELEASE_ASSERT(WSAStartup(version_requested, &wsa_data) == 0, "");
208
#endif
209
210
0
  absl::InitializeSymbolizer(program_name);
211
212
#ifdef ENVOY_HANDLE_SIGNALS
213
  // Enabled by default. Control with "bazel --define=signal_trace=disabled"
214
  static Envoy::SignalAction handle_sigs;
215
#endif
216
0
}
217
218
126
void TestEnvironment::initializeOptions(int argc, char** argv) {
219
126
  argc_ = argc;
220
126
  argv_ = argv;
221
126
}
222
223
11.3k
bool TestEnvironment::shouldRunTestForIpVersion(Network::Address::IpVersion type) {
224
11.3k
  const char* value = std::getenv("ENVOY_IP_TEST_VERSIONS");
225
11.3k
  std::string option(value ? value : "");
226
11.3k
  if (option.empty()) {
227
11.3k
    return true;
228
11.3k
  }
229
0
  if ((type == Network::Address::IpVersion::v4 && option == "v6only") ||
230
0
      (type == Network::Address::IpVersion::v6 && option == "v4only")) {
231
0
    return false;
232
0
  }
233
0
  return true;
234
0
}
235
236
5.67k
std::vector<Network::Address::IpVersion> TestEnvironment::getIpVersionsForTest() {
237
5.67k
  std::vector<Network::Address::IpVersion> parameters;
238
11.3k
  for (auto version : {Network::Address::IpVersion::v4, Network::Address::IpVersion::v6}) {
239
11.3k
    if (TestEnvironment::shouldRunTestForIpVersion(version)) {
240
11.3k
      parameters.push_back(version);
241
11.3k
      if (!Network::Test::supportsIpVersion(version)) {
242
0
        const auto version_string = Network::Test::addressVersionAsString(version);
243
0
        ENVOY_LOG_TO_LOGGER(
244
0
            Logger::Registry::getLog(Logger::Id::testing), warn,
245
0
            "Testing with IP{} addresses may not be supported on this machine. If "
246
0
            "testing fails, set the environment variable ENVOY_IP_TEST_VERSIONS to 'v{}only'.",
247
0
            version_string, version_string);
248
0
      }
249
11.3k
    }
250
11.3k
  }
251
5.67k
  return parameters;
252
5.67k
}
253
254
0
std::vector<spdlog::logger*> TestEnvironment::getSpdLoggersForTest() {
255
0
  std::vector<spdlog::logger*> logger_parameters;
256
0
  logger_parameters.push_back(&Logger::Registry::loggers()[0].getLogger());
257
0
  std::atomic<spdlog::logger*> flogger{nullptr};
258
0
  getFineGrainLogContext().initFineGrainLogger(__FILE__, flogger);
259
0
  logger_parameters.push_back(flogger.load(std::memory_order_relaxed));
260
0
  return logger_parameters;
261
0
}
262
263
2.90k
Server::Options& TestEnvironment::getOptions() {
264
2.90k
  static OptionsImpl* options = new OptionsImpl(
265
2.90k
      argc_, argv_, [](bool) { return "1"; }, spdlog::level::err);
266
2.90k
  return *options;
267
2.90k
}
268
269
591
std::vector<Grpc::ClientType> TestEnvironment::getsGrpcVersionsForTest() {
270
591
#ifdef ENVOY_GOOGLE_GRPC
271
591
  return {Grpc::ClientType::EnvoyGrpc, Grpc::ClientType::GoogleGrpc};
272
#else
273
  return {Grpc::ClientType::EnvoyGrpc};
274
#endif
275
591
}
276
277
17.6k
const std::string& TestEnvironment::temporaryDirectory() {
278
17.6k
  CONSTRUCT_ON_FIRST_USE(std::string, getTemporaryDirectory());
279
17.6k
}
280
281
0
std::string TestEnvironment::runfilesDirectory(const std::string& workspace) {
282
0
  RELEASE_ASSERT(runfiles_ != nullptr, "");
283
0
  auto path = runfiles_->Rlocation(workspace);
284
#ifdef WIN32
285
  path = std::regex_replace(path, std::regex("\\\\"), "/");
286
#endif
287
0
  return path;
288
0
}
289
290
0
std::string TestEnvironment::runfilesPath(const std::string& path, const std::string& workspace) {
291
0
  RELEASE_ASSERT(runfiles_ != nullptr, "");
292
0
  return runfiles_->Rlocation(absl::StrCat(workspace, "/", path));
293
0
}
294
295
0
const std::string TestEnvironment::unixDomainSocketDirectory() {
296
0
  CONSTRUCT_ON_FIRST_USE(std::string, getOrCreateUnixDomainSocketDirectory());
297
0
}
298
299
std::string TestEnvironment::substitute(const std::string& str,
300
0
                                        Network::Address::IpVersion version) {
301
0
  const absl::node_hash_map<std::string, std::string> path_map = {
302
0
      {"test_tmpdir", TestEnvironment::temporaryDirectory()},
303
0
      {"test_udsdir", TestEnvironment::unixDomainSocketDirectory()},
304
0
      {"test_rundir", runfiles_ != nullptr ? TestEnvironment::runfilesDirectory() : "invalid"},
305
0
  };
306
307
0
  std::string out_json_string = str;
308
0
  for (const auto& it : path_map) {
309
0
    const std::regex port_regex("\\{\\{ " + it.first + " \\}\\}");
310
0
    out_json_string = std::regex_replace(out_json_string, port_regex, it.second);
311
0
  }
312
313
  // Substitute platform specific null device.
314
0
  const std::regex null_device_regex(R"(\{\{ null_device_path \}\})");
315
0
  out_json_string = std::regex_replace(out_json_string, null_device_regex,
316
0
                                       std::string(Platform::null_device_path).c_str());
317
318
  // Substitute IP loopback addresses.
319
0
  const std::regex loopback_address_regex(R"(\{\{ ip_loopback_address \}\})");
320
0
  out_json_string = std::regex_replace(out_json_string, loopback_address_regex,
321
0
                                       Network::Test::getLoopbackAddressString(version));
322
0
  const std::regex ntop_loopback_address_regex(R"(\{\{ ntop_ip_loopback_address \}\})");
323
0
  out_json_string = std::regex_replace(out_json_string, ntop_loopback_address_regex,
324
0
                                       Network::Test::getLoopbackAddressString(version));
325
326
  // Substitute IP any addresses.
327
0
  const std::regex any_address_regex(R"(\{\{ ip_any_address \}\})");
328
0
  out_json_string = std::regex_replace(out_json_string, any_address_regex,
329
0
                                       Network::Test::getAnyAddressString(version));
330
331
  // Substitute dns lookup family.
332
0
  const std::regex lookup_family_regex(R"(\{\{ dns_lookup_family \}\})");
333
0
  switch (version) {
334
0
  case Network::Address::IpVersion::v4:
335
0
    out_json_string = std::regex_replace(out_json_string, lookup_family_regex, "v4_only");
336
0
    break;
337
0
  case Network::Address::IpVersion::v6:
338
0
    out_json_string = std::regex_replace(out_json_string, lookup_family_regex, "v6_only");
339
0
    break;
340
0
  }
341
342
  // Substitute socket options arguments.
343
0
  const std::regex sol_socket_regex(R"(\{\{ sol_socket \}\})");
344
0
  out_json_string =
345
0
      std::regex_replace(out_json_string, sol_socket_regex, std::to_string(SOL_SOCKET));
346
0
  const std::regex so_reuseport_regex(R"(\{\{ so_reuseport \}\})");
347
0
  out_json_string =
348
0
      std::regex_replace(out_json_string, so_reuseport_regex, std::to_string(SO_REUSEPORT));
349
350
0
  return out_json_string;
351
0
}
352
353
std::string TestEnvironment::temporaryFileSubstitute(const std::string& path,
354
                                                     const PortMap& port_map,
355
0
                                                     Network::Address::IpVersion version) {
356
0
  return temporaryFileSubstitute(path, ParamMap(), port_map, version);
357
0
}
358
359
0
std::string TestEnvironment::readFileToStringForTest(const std::string& filename) {
360
0
  return Filesystem::fileSystemForTest().fileReadToEnd(filename).value();
361
0
}
362
363
std::string TestEnvironment::temporaryFileSubstitute(const std::string& path,
364
                                                     const ParamMap& param_map,
365
                                                     const PortMap& port_map,
366
0
                                                     Network::Address::IpVersion version) {
367
0
  RELEASE_ASSERT(!path.empty(), "requested path to substitute in is empty");
368
  // Load the entire file as a string, regex replace one at a time and write it back out. Proper
369
  // templating might be better one day, but this works for now.
370
0
  const std::string json_path = TestEnvironment::runfilesPath(path);
371
0
  std::string out_json_string = readFileToStringForTest(json_path);
372
373
  // Substitute params.
374
0
  for (const auto& it : param_map) {
375
0
    const std::regex param_regex("\\{\\{ " + it.first + " \\}\\}");
376
0
    out_json_string = std::regex_replace(out_json_string, param_regex, it.second);
377
0
  }
378
379
  // Substitute ports.
380
0
  for (const auto& it : port_map) {
381
0
    const std::regex port_regex("\\{\\{ " + it.first + " \\}\\}");
382
0
    out_json_string = std::regex_replace(out_json_string, port_regex, std::to_string(it.second));
383
0
  }
384
385
  // Substitute paths and other common things.
386
0
  out_json_string = substitute(out_json_string, version);
387
388
0
  auto name = Filesystem::fileSystemForTest().splitPathFromFilename(path).value().file_;
389
0
  const std::string extension = std::string(name.substr(name.rfind('.')));
390
0
  const std::string out_json_path =
391
0
      TestEnvironment::temporaryPath(name) + ".with.ports" + extension;
392
0
  {
393
0
    std::ofstream out_json_file(out_json_path, std::ios::binary);
394
0
    out_json_file << out_json_string;
395
0
  }
396
0
  return out_json_path;
397
0
}
398
399
Json::ObjectSharedPtr TestEnvironment::jsonLoadFromString(const std::string& json,
400
0
                                                          Network::Address::IpVersion version) {
401
0
  return Json::Factory::loadFromString(substitute(json, version));
402
0
}
403
404
// This function is not used for Envoy Mobile tests, and ::system() is not supported on iOS.
405
#ifndef TARGET_OS_IOS
406
0
void TestEnvironment::exec(const std::vector<std::string>& args) {
407
0
  std::stringstream cmd;
408
  // Symlinked args[0] can confuse Python when importing module relative, so we let Python know
409
  // where it can find its module relative files.
410
0
  cmd << "bash -c \"PYTHONPATH=$(dirname " << args[0] << ") ";
411
0
  for (auto& arg : args) {
412
0
    cmd << arg << " ";
413
0
  }
414
0
  cmd << "\"";
415
0
  if (::system(cmd.str().c_str()) != 0) {
416
0
    std::cerr << "Failed " << cmd.str() << "\n";
417
0
    RELEASE_ASSERT(false, "");
418
0
  }
419
0
}
420
#endif
421
422
std::string TestEnvironment::writeStringToFileForTest(const std::string& filename,
423
                                                      const std::string& contents,
424
11.9k
                                                      bool fully_qualified_path, bool do_unlink) {
425
11.9k
  const std::string out_path =
426
11.9k
      fully_qualified_path ? filename : TestEnvironment::temporaryPath(filename);
427
11.9k
  if (do_unlink) {
428
11.9k
    unlink(out_path.c_str());
429
11.9k
  }
430
431
11.9k
  Filesystem::FilePathAndType out_file_info{Filesystem::DestinationType::File, out_path};
432
11.9k
  Filesystem::FilePtr file = Filesystem::fileSystemForTest().createFile(out_file_info);
433
11.9k
  const Filesystem::FlagSet flags{1 << Filesystem::File::Operation::Write |
434
11.9k
                                  1 << Filesystem::File::Operation::Create};
435
11.9k
  const Api::IoCallBoolResult open_result = file->open(flags);
436
23.9k
  EXPECT_TRUE(open_result.return_value_) << open_result.err_->getErrorDetails();
437
11.9k
  const Api::IoCallSizeResult result = file->write(contents);
438
11.9k
  EXPECT_EQ(contents.length(), result.return_value_);
439
11.9k
  return out_path;
440
11.9k
}
441
442
0
void TestEnvironment::setEnvVar(const std::string& name, const std::string& value, int overwrite) {
443
#ifdef WIN32
444
  if (!overwrite) {
445
    size_t requiredSize;
446
    const int rc = ::getenv_s(&requiredSize, nullptr, 0, name.c_str());
447
    ASSERT_EQ(0, rc);
448
    if (requiredSize != 0) {
449
      return;
450
    }
451
  }
452
  const int rc = ::_putenv_s(name.c_str(), value.c_str());
453
  ASSERT_EQ(0, rc);
454
#else
455
0
  const int rc = ::setenv(name.c_str(), value.c_str(), overwrite);
456
0
  ASSERT_EQ(0, rc);
457
0
#endif
458
0
}
459
460
0
void TestEnvironment::unsetEnvVar(const std::string& name) {
461
#ifdef WIN32
462
  const int rc = ::_putenv_s(name.c_str(), "");
463
  ASSERT_EQ(0, rc);
464
#else
465
0
  const int rc = ::unsetenv(name.c_str());
466
0
  ASSERT_EQ(0, rc);
467
0
#endif
468
0
}
469
470
0
void TestEnvironment::setRunfiles(Runfiles* runfiles) { runfiles_ = runfiles; }
471
472
Runfiles* TestEnvironment::runfiles_{};
473
474
AtomicFileUpdater::AtomicFileUpdater(const std::string& filename)
475
    : link_(filename), new_link_(absl::StrCat(filename, ".new")),
476
0
      target1_(absl::StrCat(filename, ".target1")), target2_(absl::StrCat(filename, ".target2")) {
477
0
  unlink(link_.c_str());
478
0
  unlink(new_link_.c_str());
479
0
  unlink(target1_.c_str());
480
0
  unlink(target2_.c_str());
481
0
}
482
483
0
void AtomicFileUpdater::update(const std::string& contents) {
484
0
  const std::string target = use_target1_ ? target1_ : target2_;
485
0
  use_target1_ = !use_target1_;
486
0
  {
487
0
    std::ofstream file(target, std::ios_base::binary);
488
0
    file << contents;
489
0
  }
490
0
  TestEnvironment::createSymlink(target, new_link_);
491
0
  TestEnvironment::renameFile(new_link_, link_);
492
0
}
493
494
} // namespace Envoy