| Kea 3.0.2-git

sandbox.h
Go to the documentation of this file.
1// Copyright (C) 2019-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef SANDBOX_H
8#define SANDBOX_H
9
10#include <exceptions/exceptions.h>
11
12#include <iostream>
13#include <string>
14#include <cstdlib>
15#include <cstdio>
16#include <unistd.h>
17#include <ftw.h>
18
19 namespace isc {
20 namespace test {
21
28 class Sandbox {
29 private:
31 std::string path_;
32
36 static int rmFile(const char *fpath, const struct stat *, int , struct FTW *) {
37 return(remove(fpath));
38 }
39
40 public:
42 Sandbox() {
43 char tmpl[] = {P_tmpdir "/kea-XXXXXX"};
44 path_ = mkdtemp(tmpl);
45 }
46
48 ~Sandbox() {
49 // Delete content of path_ recursively.
50 if (nftw(path_.c_str(), Sandbox::rmFile, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0) {
51 auto msg = "Some error occurred while deleting unit test sandbox " + path_;
52 std::perror(msg.c_str());
53 exit(1);
54 }
55 }
56
60 std::string join(std::string file) {
61 return (path_ + "/" + file);
62 }
63};
64
65
66} // end of isc::test namespace
67} // end of isc namespace
68
69#endif // SANDBOX_H
isc::test::Sandbox::~Sandbox
~Sandbox()
Destructor, it deletes temporary folder with its content.
Definition sandbox.h:48
isc::test::Sandbox::Sandbox
Sandbox()
Sandbox constructor.
Definition sandbox.h:42
isc::test::Sandbox::join
std::string join(std::string file)
Join sandbox path with indicated file subpath.
Definition sandbox.h:60
exceptions.h
isc::test
Definition env_var_wrapper.cc:12
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148