Coverage Report

Created: 2024-02-25 06:29

/src/PcapPlusPlus/Tests/Fuzzers/FuzzWriter.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <functional>
2
#include <Packet.h>
3
#include <PcapFileDevice.h>
4
5
#include "Logger.h"
6
#include "DumpToFile.h"
7
8
static std::string tmpName;
9
static std::string tmpFile;
10
static std::string outPcapFile;
11
static int writes = 0;
12
13
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
14
5.20k
{
15
5.20k
  if (tmpName.empty())
16
2
    tmpName = tmpnam (NULL);
17
18
5.20k
  if (tmpFile.empty())
19
2
    tmpFile = tmpName + FILE_EXT;
20
21
5.20k
  if (dumpDataToPcapFile(data, size, tmpFile.c_str()) != 0)
22
0
  {
23
0
    std::cerr << "Can't Dump buffer to the '" << tmpFile << "' file!!!!\n";
24
0
    return -1;
25
0
  }
26
27
5.20k
  pcpp::Logger::getInstance().suppressLogs();
28
29
5.20k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
30
5.20k
  if (!reader->open())
31
959
  {
32
959
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
33
959
    return -1;
34
959
  }
35
36
4.24k
  if (outPcapFile.empty())
37
#ifdef NG_WRITER
38
    outPcapFile = tmpName + ".pcapng";
39
#else
40
2
    outPcapFile = tmpName + ".pcap";
41
4.24k
#endif
42
43
#ifdef NG_WRITER
44
    pcpp::PcapNgFileWriterDevice pcapWriter(outPcapFile);
45
#else
46
4.24k
    pcpp::PcapFileWriterDevice pcapWriter(outPcapFile, pcpp::LINKTYPE_ETHERNET);
47
4.24k
#endif
48
4.24k
  if (writes++ == 10)
49
424
  {
50
424
    writes = 1;
51
424
    remove(outPcapFile.c_str());
52
424
  }
53
4.24k
  if (!pcapWriter.open(writes != 1))
54
342
  {
55
342
    std::cerr << "Cannot open '" << outPcapFile << "' for writing" << std::endl;
56
342
    return -1;
57
342
  }
58
59
3.90k
  pcpp::RawPacketVector packets;
60
3.90k
  if (reader->getNextPackets(packets, 1) != 1)
61
1.23k
  {
62
1.23k
    std::cerr << "Couldn't read the first packet in the file\n";
63
1.23k
    return 0;
64
1.23k
  }
65
66
2.66k
  pcpp::RawPacket& rawPacket = *packets.front();
67
2.66k
  do
68
26.5k
  {
69
26.5k
    pcapWriter.writePacket(rawPacket);
70
26.5k
  } while (reader->getNextPacket(rawPacket));
71
72
2.66k
  pcpp::IPcapDevice::PcapStats stats;
73
2.66k
  pcapWriter.getStatistics(stats);
74
2.66k
  std::cout << "Written " << stats.packetsRecv << " packets successfully to pcap writer and "
75
2.66k
        << stats.packetsDrop << " packets could not be written" << std::endl;
76
77
2.66k
  pcapWriter.close();
78
2.66k
  return 0;
79
3.90k
}