Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp
Line
Count
Source (jump to first uncovered line)
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
1.37k
{
12
1.37k
  if (tmpName.empty())
13
2
    tmpName = tmpnam(nullptr);
14
15
1.37k
  if (tmpFile.empty())
16
2
    tmpFile = tmpName + FILE_EXT;
17
18
1.37k
  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
1.37k
  pcpp::Logger::getInstance().suppressLogs();
25
26
1.37k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
27
1.37k
  if (!reader->open())
28
45
  {
29
45
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
30
45
    return -1;
31
45
  }
32
33
1.33k
  pcpp::IPcapDevice::PcapStats stats;
34
1.33k
  reader->getStatistics(stats);
35
1.33k
  std::cout << "Read " << stats.packetsRecv << " packets successfully and " << stats.packetsDrop
36
1.33k
            << " packets could not be read" << std::endl;
37
38
1.33k
  if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get()))
39
1.18k
  {
40
1.18k
    std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'"
41
1.18k
              << "'; CaptureApplication is '" << ngReader->getCaptureApplication() << "'; CaptureFileComment is '"
42
1.18k
              << ngReader->getCaptureFileComment() << "'" << std::endl;
43
1.18k
  }
44
45
1.33k
  pcpp::RawPacketVector packets;
46
1.33k
  if (reader->getNextPackets(packets, 1) != 1)
47
141
  {
48
141
    std::cerr << "Couldn't read the first packet in the file\n";
49
141
    return 0;
50
141
  }
51
52
1.19k
  pcpp::RawPacket& rawPacket = *packets.front();
53
1.19k
  do
54
66.0k
  {
55
    // go deeper only for .pcap and .pcapng format
56
    // for .snoop we are only fuzzing the reader
57
66.0k
    if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng"))
58
65.4k
    {
59
65.4k
      pcpp::Packet parsedPacket(&rawPacket);
60
65.4k
      parsedPacket.toString();
61
65.4k
      auto layer = parsedPacket.getFirstLayer();
62
342k
      while (layer != nullptr)
63
277k
      {
64
277k
        std::cout << layer->toString() << std::endl;
65
277k
        layer->getHeaderLen();
66
277k
        readParsedPacket(parsedPacket, layer);
67
277k
        layer = layer->getNextLayer();
68
277k
      }
69
65.4k
      parsedPacket.computeCalculateFields();
70
65.4k
    }
71
66.0k
  } while (reader->getNextPacket(rawPacket));
72
73
1.19k
  reader->close();
74
1.19k
  return 0;
75
1.33k
}