Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 25 | |
Davide Pesavento | 2840dbc | 2019-04-16 23:14:21 -0400 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/exception.hpp> |
| 29 | |
| 30 | #include <boost/filesystem/operations.hpp> |
| 31 | #include <boost/filesystem/path.hpp> |
| 32 | |
| 33 | #include <stdexcept> |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 34 | #include <stdlib.h> |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 37 | |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 38 | class GlobalConfiguration |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 39 | { |
| 40 | public: |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 41 | GlobalConfiguration() |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 42 | { |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 43 | const char* envHome = ::getenv("HOME"); |
| 44 | if (envHome) |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 45 | m_home.assign(envHome); |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 46 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 47 | // in case an earlier test run crashed without a chance to run the destructor |
| 48 | boost::filesystem::remove_all(TESTDIR); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 49 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 50 | auto testHome = TESTDIR / "test-home"; |
Davide Pesavento | 2840dbc | 2019-04-16 23:14:21 -0400 | [diff] [blame] | 51 | boost::filesystem::create_directories(testHome); |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 52 | |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 53 | if (::setenv("HOME", testHome.c_str(), 1) != 0) |
| 54 | NDN_THROW_NO_STACK(std::runtime_error("setenv() failed")); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 55 | } |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 56 | |
Davide Pesavento | 2840dbc | 2019-04-16 23:14:21 -0400 | [diff] [blame] | 57 | ~GlobalConfiguration() noexcept |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 58 | { |
Davide Pesavento | 2840dbc | 2019-04-16 23:14:21 -0400 | [diff] [blame] | 59 | if (m_home.empty()) |
| 60 | ::unsetenv("HOME"); |
| 61 | else |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 62 | ::setenv("HOME", m_home.data(), 1); |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 63 | |
| 64 | boost::system::error_code ec; |
| 65 | boost::filesystem::remove_all(TESTDIR, ec); // ignore error |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 69 | static inline const boost::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR}; |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 70 | std::string m_home; |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 71 | }; |
| 72 | |
Davide Pesavento | a6f2d24 | 2018-01-20 19:05:05 -0500 | [diff] [blame] | 73 | BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration); |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 74 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 75 | } // namespace nfd::tests |