Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 3 | * Copyright (c) 2017-2024, Regents of the University of California. |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 21 | #include "tests/boost-test.hpp" |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 23 | #include <ndn-cxx/util/exception.hpp> |
| 24 | |
| 25 | #include <boost/filesystem/operations.hpp> |
| 26 | #include <boost/filesystem/path.hpp> |
| 27 | |
| 28 | #include <stdexcept> |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 31 | namespace ndncert::tests { |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 32 | |
| 33 | class GlobalConfiguration |
| 34 | { |
| 35 | public: |
| 36 | GlobalConfiguration() |
| 37 | { |
| 38 | const char* envHome = ::getenv("HOME"); |
| 39 | if (envHome) |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 40 | m_home.assign(envHome); |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 41 | |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 42 | // in case an earlier test run crashed without a chance to run the destructor |
| 43 | boost::filesystem::remove_all(TESTDIR); |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 44 | |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 45 | auto testHome = TESTDIR / "test-home"; |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 46 | boost::filesystem::create_directories(testHome); |
| 47 | |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 48 | if (::setenv("HOME", testHome.c_str(), 1) != 0) |
| 49 | NDN_THROW(std::runtime_error("setenv() failed")); |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 52 | ~GlobalConfiguration() noexcept |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 53 | { |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 54 | if (m_home.empty()) |
| 55 | ::unsetenv("HOME"); |
| 56 | else |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 57 | ::setenv("HOME", m_home.data(), 1); |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 58 | |
| 59 | boost::system::error_code ec; |
| 60 | boost::filesystem::remove_all(TESTDIR, ec); // ignore error |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
Davide Pesavento | 842f1f7 | 2024-02-21 21:27:25 -0500 | [diff] [blame] | 64 | static inline const boost::filesystem::path TESTDIR{UNIT_TESTS_TMPDIR}; |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 65 | std::string m_home; |
| 66 | }; |
| 67 | |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 68 | BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration); |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 69 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 70 | } // namespace ndncert::tests |