blob: 402dcf8f0d393c40b60c055e798133ceb20a76e1 [file] [log] [blame]
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa6f2d242018-01-20 19:05:05 -05002/*
Davide Pesavento152874a2024-02-20 22:07:07 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -08004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyevbc9ed492016-01-26 11:38:11 -080024 */
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060025
Davide Pesavento2840dbc2019-04-16 23:14:21 -040026#include "tests/boost-test.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027
Davide Pesavento152874a2024-02-20 22:07:07 -050028#include <ndn-cxx/util/exception.hpp>
29
30#include <boost/filesystem/operations.hpp>
31#include <boost/filesystem/path.hpp>
32
33#include <stdexcept>
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050034#include <stdlib.h>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060037
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050038class GlobalConfiguration
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060039{
40public:
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050041 GlobalConfiguration()
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060042 {
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050043 const char* envHome = ::getenv("HOME");
44 if (envHome)
Davide Pesavento152874a2024-02-20 22:07:07 -050045 m_home.assign(envHome);
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050046
Davide Pesavento152874a2024-02-20 22:07:07 -050047 // in case an earlier test run crashed without a chance to run the destructor
48 boost::filesystem::remove_all(TESTDIR);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060049
Davide Pesavento152874a2024-02-20 22:07:07 -050050 auto testHome = TESTDIR / "test-home";
Davide Pesavento2840dbc2019-04-16 23:14:21 -040051 boost::filesystem::create_directories(testHome);
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080052
Davide Pesavento152874a2024-02-20 22:07:07 -050053 if (::setenv("HOME", testHome.c_str(), 1) != 0)
54 NDN_THROW_NO_STACK(std::runtime_error("setenv() failed"));
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060055 }
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080056
Davide Pesavento2840dbc2019-04-16 23:14:21 -040057 ~GlobalConfiguration() noexcept
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080058 {
Davide Pesavento2840dbc2019-04-16 23:14:21 -040059 if (m_home.empty())
60 ::unsetenv("HOME");
61 else
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050062 ::setenv("HOME", m_home.data(), 1);
Davide Pesavento152874a2024-02-20 22:07:07 -050063
64 boost::system::error_code ec;
65 boost::filesystem::remove_all(TESTDIR, ec); // ignore error
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080066 }
67
68private:
Davide Pesavento152874a2024-02-20 22:07:07 -050069 static inline const boost::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR};
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080070 std::string m_home;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060071};
72
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050073BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060074
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040075} // namespace nfd::tests