blob: f56959a7bb4de61de07efab078375c3ce5c420a4 [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
Davide Pesavento401d1a42024-12-19 21:10:22 -050030#include <filesystem>
Davide Pesavento152874a2024-02-20 22:07:07 -050031#include <stdexcept>
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050032#include <stdlib.h>
Davide Pesavento401d1a42024-12-19 21:10:22 -050033#include <system_error>
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060034
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035namespace nfd::tests {
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060036
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050037class GlobalConfiguration
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060038{
39public:
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050040 GlobalConfiguration()
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060041 {
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050042 const char* envHome = ::getenv("HOME");
43 if (envHome)
Davide Pesavento152874a2024-02-20 22:07:07 -050044 m_home.assign(envHome);
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050045
Davide Pesavento152874a2024-02-20 22:07:07 -050046 // in case an earlier test run crashed without a chance to run the destructor
Davide Pesavento401d1a42024-12-19 21:10:22 -050047 std::filesystem::remove_all(TESTDIR);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060048
Davide Pesavento152874a2024-02-20 22:07:07 -050049 auto testHome = TESTDIR / "test-home";
Davide Pesavento401d1a42024-12-19 21:10:22 -050050 std::filesystem::create_directories(testHome);
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080051
Davide Pesavento152874a2024-02-20 22:07:07 -050052 if (::setenv("HOME", testHome.c_str(), 1) != 0)
53 NDN_THROW_NO_STACK(std::runtime_error("setenv() failed"));
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060054 }
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080055
Davide Pesavento2840dbc2019-04-16 23:14:21 -040056 ~GlobalConfiguration() noexcept
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080057 {
Davide Pesavento2840dbc2019-04-16 23:14:21 -040058 if (m_home.empty())
59 ::unsetenv("HOME");
60 else
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050061 ::setenv("HOME", m_home.data(), 1);
Davide Pesavento152874a2024-02-20 22:07:07 -050062
Davide Pesavento401d1a42024-12-19 21:10:22 -050063 std::error_code ec;
64 std::filesystem::remove_all(TESTDIR, ec); // ignore error
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080065 }
66
67private:
Davide Pesavento401d1a42024-12-19 21:10:22 -050068 static inline const std::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR};
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080069 std::string m_home;
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060070};
71
Davide Pesaventoa6f2d242018-01-20 19:05:05 -050072BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration);
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060073
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040074} // namespace nfd::tests