Coverage Report

Created: 2025-11-16 07:29

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