Coverage Report

Created: 2025-12-31 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp
Line
Count
Source
1
#include <PcapFileDevice.h>
2
#include <Packet.h>
3
#include <Logger.h>
4
#include "DumpToFile.h"
5
#include "ReadParsedPacket.h"
6
7
static std::string tmpName;
8
static std::string tmpFile;
9
10
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
11
10.3k
{
12
10.3k
  if (tmpName.empty())
13
2
    tmpName = tmpnam(nullptr);
14
15
10.3k
  if (tmpFile.empty())
16
2
    tmpFile = tmpName + FILE_EXT;
17
18
10.3k
  if (dumpDataToPcapFile(data, size, tmpFile.c_str()) != 0)
19
0
  {
20
0
    std::cerr << "Can't Dump buffer to the '" << tmpFile << "' file!!!!\n";
21
0
    return -1;
22
0
  }
23
24
10.3k
  pcpp::Logger::getInstance().suppressLogs();
25
26
10.3k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
27
10.3k
  if (!reader->open())
28
55
  {
29
55
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
30
55
    return -1;
31
55
  }
32
33
10.3k
  pcpp::IPcapDevice::PcapStats stats;
34
10.3k
  reader->getStatistics(stats);
35
10.3k
  std::cout << "Read " << stats.packetsRecv << " packets successfully and " << stats.packetsDrop
36
10.3k
            << " packets could not be read" << std::endl;
37
38
10.3k
  if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get()))
39
10.1k
  {
40
10.1k
    std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'"
41
10.1k
              << "'; CaptureApplication is '" << ngReader->getCaptureApplication() << "'; CaptureFileComment is '"
42
10.1k
              << ngReader->getCaptureFileComment() << "'" << std::endl;
43
10.1k
  }
44
45
10.3k
  pcpp::RawPacketVector packets;
46
10.3k
  if (reader->getNextPackets(packets, 1) != 1)
47
743
  {
48
743
    std::cerr << "Couldn't read the first packet in the file\n";
49
743
    return 0;
50
743
  }
51
52
9.59k
  pcpp::RawPacket& rawPacket = *packets.front();
53
9.59k
  do
54
176k
  {
55
    // go deeper only for .pcap and .pcapng format
56
    // for .snoop we are only fuzzing the reader
57
176k
    if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng"))
58
175k
    {
59
175k
      pcpp::Packet parsedPacket(&rawPacket);
60
175k
      parsedPacket.toString();
61
175k
      auto layer = parsedPacket.getFirstLayer();
62
938k
      while (layer != nullptr)
63
762k
      {
64
762k
        std::cout << layer->toString() << std::endl;
65
762k
        layer->getHeaderLen();
66
762k
        readParsedPacket(parsedPacket, layer);
67
762k
        layer = layer->getNextLayer();
68
762k
      }
69
175k
      parsedPacket.computeCalculateFields();
70
175k
    }
71
176k
  } while (reader->getNextPacket(rawPacket));
72
73
9.59k
  reader->close();
74
9.59k
  return 0;
75
10.3k
}