Coverage Report

Created: 2026-02-14 07:22

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
32.3k
    std::string writeTempConfig(bool isV4) {
20
32.3k
        return writeTempFile(isV4? JSON_CONFIG4 : JSON_CONFIG6);
21
32.3k
    }
22
23
25.2k
    std::string writeTempLease(bool isV4) {
24
25.2k
        if (isV4) {
25
16.5k
            return writeTempFile(LEASE4, "", "/tmp/kea-leases4.csv");
26
16.5k
        } else {
27
8.71k
            return writeTempFile(LEASE6, "", "/tmp/kea-leases6.csv");
28
8.71k
        }
29
25.2k
    }
30
31
5.18k
    std::string writeTempUserFile() {
32
5.18k
        return writeTempFile(USER, "", "/tmp/users.txt");
33
5.18k
    }
34
35
82.7k
    std::string writeTempFile(const std::string& payload, const char* suffix, const std::string& explicit_path) {
36
82.7k
        std::string path = explicit_path;
37
82.7k
        if (explicit_path.empty()) {
38
52.3k
            const long r = std::rand();
39
52.3k
            const pid_t pid = ::getpid();
40
52.3k
            path = std::string("/tmp/kea_fuzz_") + std::to_string(pid) +
41
52.3k
                   "_" + std::to_string(r) + "." + (suffix ? suffix : "tmp");
42
52.3k
        }
43
44
82.7k
        std::ofstream ofs(path.c_str(), std::ios::binary);
45
82.7k
        if (ofs.good()) {
46
82.7k
            ofs.write(payload.data(), static_cast<std::streamsize>(payload.size()));
47
82.7k
            ofs.close();
48
82.7k
            return path;
49
82.7k
        }
50
0
        return std::string();
51
82.7k
    }
52
53
36.7k
    void deleteTempFile(std::string file_path) {
54
36.7k
        if (fs::exists(file_path)) {
55
36.0k
            try {
56
36.0k
                fs::remove(file_path);
57
36.0k
            } catch (...) {
58
                // Slient exceptions
59
0
            }
60
36.0k
        }
61
36.7k
    }
62
63
191k
    isc::data::ElementPtr parseJSON(const std::string& s) {
64
191k
        try {
65
191k
            return Element::fromJSON(s);
66
191k
        } catch (...) {
67
109k
            return Element::createMap();
68
109k
        }
69
191k
    }
70
}