Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 20 | #include "core/common.hpp" |
| 21 | |
| 22 | #include "tests/boost-test.hpp" |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 23 | |
| 24 | #include <boost/filesystem.hpp> |
| 25 | #include <fstream> |
| 26 | #include <stdlib.h> |
| 27 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 28 | namespace ndn::tests { |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 29 | |
| 30 | class GlobalConfiguration |
| 31 | { |
| 32 | public: |
| 33 | GlobalConfiguration() |
| 34 | { |
| 35 | const char* envHome = ::getenv("HOME"); |
| 36 | if (envHome) |
| 37 | m_home = envHome; |
| 38 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 39 | auto testHome = boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-home"; |
| 40 | if (::setenv("HOME", testHome.c_str(), 1) != 0) |
| 41 | NDN_THROW(std::runtime_error("setenv() failed")); |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 42 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 43 | boost::filesystem::create_directories(testHome); |
| 44 | |
| 45 | std::ofstream clientConf((testHome / ".ndn" / "client.conf").c_str()); |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 46 | clientConf << "pib=pib-sqlite3" << std::endl |
| 47 | << "tpm=tpm-file" << std::endl; |
| 48 | } |
| 49 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 50 | ~GlobalConfiguration() noexcept |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 51 | { |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 52 | if (m_home.empty()) |
| 53 | ::unsetenv("HOME"); |
| 54 | else |
Davide Pesavento | cc2c717 | 2018-01-21 21:41:03 -0500 | [diff] [blame] | 55 | ::setenv("HOME", m_home.data(), 1); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | std::string m_home; |
| 60 | }; |
| 61 | |
| 62 | #if BOOST_VERSION >= 106500 |
| 63 | BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration); |
| 64 | #elif BOOST_VERSION >= 105900 |
| 65 | BOOST_GLOBAL_FIXTURE(GlobalConfiguration); |
| 66 | #else |
| 67 | BOOST_GLOBAL_FIXTURE(GlobalConfiguration) |
| 68 | #endif |
| 69 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 70 | } // namespace ndn::tests |