1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2019,2021 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef SANDBOX_H
#define SANDBOX_H

#include <exceptions/exceptions.h>

#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdlib><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstdio><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ftw.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace test {

/// @brief A Sandbox class that provides access to unit test unique
///    temporary folder.
///
/// The sandbox's temporary folder is created in constructor ie.
/// in unit test setup phase, and then it is deleted with its content
/// in destructor ie. in unit test tear down phase.
class Sandbox {
private:
    /// Path to temporary folder
    std::string path_;

    /// @brief Method for deleting files and folders, used in nftw traversal function.
    ///
    /// @param fpath path to the file to be removed.
    static int rmFile(const char *fpath, const struct stat *, int , struct FTW *) {
        return(remove(fpath));
    }

public:
    /// @brief Sandbox constructor.
    Sandbox() {
        char tmpl[] = {P_tmpdir "/kea-XXXXXX"};
        path_ = mkdtemp(tmpl);
    }

    /// @brief Destructor, it deletes temporary folder with its content.
    ~Sandbox() {
        // Delete content of path_ recursively.
        if (nftw(path_.c_str(), Sandbox::rmFile, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) < 0) {
            auto msg = "Some error occurred while deleting unit test sandbox " + path_;
            std::perror(msg.c_str());
            exit(1);
        }
    }

    /// @brief Join sandbox path with indicated file subpath.
    ///
    /// @param file path to file that should be joined to base path of sandbox.
    std::string join(std::string file) {<--- Function parameter 'file' should be passed by const reference.
        return (path_ + "/" + file);
    }
};


}; // end of isc::test namespace
}; // end of isc namespace

#endif // SANDBOX_H