Coverage Report

Created: 2025-12-08 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea-fuzzer/helper_func.cc
Line
Count
Source
1
// Copyright (C) 2025 Ada Logcis Ltd.
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
#include "helper_func.h"
8
9
#include <cstdlib>
10
#include <filesystem>
11
#include <fstream>
12
#include <unistd.h>
13
14
namespace fs = std::filesystem;
15
16
using namespace isc::data;
17
18
namespace fuzz {
19
30.1k
    std::string writeTempConfig(bool isV4) {
20
30.1k
        return writeTempFile(isV4? JSON_CONFIG4 : JSON_CONFIG6);
21
30.1k
    }
22
23
24.7k
    std::string writeTempLease(bool isV4) {
24
24.7k
        if (isV4) {
25
15.0k
            return writeTempFile(LEASE4, "", "/tmp/kea-leases4.csv");
26
15.0k
        } else {
27
9.63k
            return writeTempFile(LEASE6, "", "/tmp/kea-leases6.csv");
28
9.63k
        }
29
24.7k
    }
30
31
5.33k
    std::string writeTempUserFile() {
32
5.33k
        return writeTempFile(USER, "", "/tmp/users.txt");
33
5.33k
    }
34
35
70.1k
    std::string writeTempFile(const std::string& payload, const char* suffix, const std::string& explicit_path) {
36
70.1k
        std::string path = explicit_path;
37
70.1k
        if (explicit_path.empty()) {
38
40.1k
            const long r = std::rand();
39
40.1k
            const pid_t pid = ::getpid();
40
40.1k
            path = std::string("/tmp/kea_fuzz_") + std::to_string(pid) +
41
40.1k
                   "_" + std::to_string(r) + "." + (suffix ? suffix : "tmp");
42
40.1k
        }
43
44
70.1k
        std::ofstream ofs(path.c_str(), std::ios::binary);
45
70.1k
        if (ofs.good()) {
46
70.1k
            ofs.write(payload.data(), static_cast<std::streamsize>(payload.size()));
47
70.1k
            ofs.close();
48
70.1k
            return path;
49
70.1k
        }
50
0
        return std::string();
51
70.1k
    }
52
53
33.7k
    void deleteTempFile(std::string file_path) {
54
33.7k
        if (fs::exists(file_path)) {
55
33.2k
            try {
56
33.2k
                fs::remove(file_path);
57
33.2k
            } catch (...) {
58
                // Slient exceptions
59
0
            }
60
33.2k
        }
61
33.7k
    }
62
63
94.6k
    isc::data::ElementPtr parseJSON(const std::string& s) {
64
94.6k
        try {
65
94.6k
            return Element::fromJSON(s);
66
94.6k
        } catch (...) {
67
55.8k
            return Element::createMap();
68
55.8k
        }
69
94.6k
    }
70
}