RIC:1060: Change in PTL
[ric-plt/sdl.git] / tst / main.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include "config.h"
23 #include <vector>
24 #include <string>
25 #include <sstream>
26 #include <algorithm>
27 #include <iterator>
28 #include <cstdlib>
29 #include <cstring>
30 #include <syslog.h>
31 #include <unistd.h>
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <gtest/gtest.h>
35
36 namespace
37 {
38     void preventCoreDumps()
39     {
40         static const struct rlimit rlim { 0, 0 };
41         setrlimit(RLIMIT_CORE, &rlim);
42     }
43
44     void preventCoreDumpsIfNeeded(int argc, char** argv)
45     {
46         static const std::string option("--gtest_internal_run_death_test");
47         for (int i = 1; i < argc; ++i)
48             if (!strncmp(argv[i], option.c_str(), option.size()))
49             {
50                 preventCoreDumps();
51                 break;
52             }
53     }
54
55     int privateMain(int argc, char** argv)
56     {
57         openlog(PACKAGE_NAME "-testrunner", LOG_PERROR | LOG_PID, LOG_USER);
58         preventCoreDumpsIfNeeded(argc, argv);
59         testing::InitGoogleTest(&argc, argv);
60         const int ret(RUN_ALL_TESTS());
61         closelog();
62         return ret;
63     }
64 }
65
66 int main(int argc, char** argv)
67 {
68     const char* valgrind(getenv("VALGRIND"));
69     if (!valgrind)
70         return privateMain(argc, argv);
71
72     unsetenv("VALGRIND");
73
74     std::vector<const char*> newArgv { valgrind, "--error-exitcode=255", "--leak-check=full", "--show-reachable=yes" };
75
76     const char* suppressions(getenv("VALGRIND_SUPPRESSIONS"));
77     std::string suppressionsArg("--suppressions=");
78     if (suppressions)
79     {
80         suppressionsArg += suppressions;
81         newArgv.push_back(suppressionsArg.c_str());
82     }
83
84     const char* extraArgs(getenv("VALGRIND_EXTRA_ARGS"));
85     std::vector<std::string> valgrindExtraArgs;
86     if (extraArgs)
87     {
88         std::istringstream is(extraArgs);
89         std::copy(std::istream_iterator<std::string>(is),
90                   std::istream_iterator<std::string>(),
91                   std::back_inserter(valgrindExtraArgs));
92         for (const auto& i : valgrindExtraArgs)
93             newArgv.push_back(i.c_str());
94     }
95
96     for (int i = 0; i < argc; ++i)
97         newArgv.push_back(argv[i]);
98
99     newArgv.push_back(nullptr);
100
101     return execvp(valgrind, const_cast<char**>(newArgv.data()));
102 }