blob: d328d71295f2a389fe837de37a3bf7e5438b58df [file] [log] [blame]
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento842f1f72024-02-21 21:27:25 -05003 * Copyright (c) 2017-2024, Regents of the University of California.
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -07004 *
5 * This file is part of ndncert, a certificate management system based on NDN.
6 *
7 * ndncert is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ndncert authors and contributors.
19 */
20
Davide Pesavento829aff62022-05-15 20:30:34 -040021#include "tests/boost-test.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050022
Davide Pesavento842f1f72024-02-21 21:27:25 -050023#include <ndn-cxx/util/exception.hpp>
24
Davide Pesaventocde062e2024-12-14 16:50:10 -050025#include <filesystem>
Davide Pesavento842f1f72024-02-21 21:27:25 -050026#include <stdexcept>
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070027#include <stdlib.h>
Davide Pesaventocde062e2024-12-14 16:50:10 -050028#include <system_error>
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070029
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040030namespace ndncert::tests {
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070031
32class GlobalConfiguration
33{
34public:
35 GlobalConfiguration()
36 {
37 const char* envHome = ::getenv("HOME");
38 if (envHome)
Davide Pesavento842f1f72024-02-21 21:27:25 -050039 m_home.assign(envHome);
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070040
Davide Pesavento842f1f72024-02-21 21:27:25 -050041 // in case an earlier test run crashed without a chance to run the destructor
Davide Pesaventocde062e2024-12-14 16:50:10 -050042 std::filesystem::remove_all(TESTDIR);
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070043
Davide Pesavento842f1f72024-02-21 21:27:25 -050044 auto testHome = TESTDIR / "test-home";
Davide Pesaventocde062e2024-12-14 16:50:10 -050045 std::filesystem::create_directories(testHome);
Davide Pesavento0dc02012021-11-23 22:55:03 -050046
Davide Pesavento842f1f72024-02-21 21:27:25 -050047 if (::setenv("HOME", testHome.c_str(), 1) != 0)
48 NDN_THROW(std::runtime_error("setenv() failed"));
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070049 }
50
Davide Pesavento0dc02012021-11-23 22:55:03 -050051 ~GlobalConfiguration() noexcept
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070052 {
Davide Pesavento0dc02012021-11-23 22:55:03 -050053 if (m_home.empty())
54 ::unsetenv("HOME");
55 else
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070056 ::setenv("HOME", m_home.data(), 1);
Davide Pesavento842f1f72024-02-21 21:27:25 -050057
Davide Pesaventocde062e2024-12-14 16:50:10 -050058 std::error_code ec;
59 std::filesystem::remove_all(TESTDIR, ec); // ignore error
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070060 }
61
62private:
Davide Pesaventocde062e2024-12-14 16:50:10 -050063 static inline const std::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR};
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070064 std::string m_home;
65};
66
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070067BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration);
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070068
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040069} // namespace ndncert::tests