LCOV - code coverage report
Current view: top level - src/test/fuzz - minisketch.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 41 41 100.0 %
Date: 2023-10-05 15:40:34 Functions: 8 8 100.0 %
Branches: 34 56 60.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2021 The Bitcoin Core developers
       2                 :            : // Distributed under the MIT software license, see the accompanying
       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :            : 
       5                 :            : #include <minisketch.h>
       6                 :            : #include <node/minisketchwrapper.h>
       7                 :            : #include <test/fuzz/FuzzedDataProvider.h>
       8                 :            : #include <test/fuzz/fuzz.h>
       9                 :            : #include <test/fuzz/util.h>
      10                 :            : #include <util/check.h>
      11                 :            : 
      12                 :            : #include <map>
      13                 :            : #include <numeric>
      14                 :            : 
      15                 :            : using node::MakeMinisketch32;
      16                 :            : 
      17         [ -  + ]:        546 : FUZZ_TARGET(minisketch)
      18                 :            : {
      19                 :        200 :     FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
      20                 :        200 :     const auto capacity{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 200)};
      21         [ +  - ]:        200 :     Minisketch sketch_a{Assert(MakeMinisketch32(capacity))};
      22   [ +  -  +  - ]:        200 :     Minisketch sketch_b{Assert(MakeMinisketch32(capacity))};
      23                 :            : 
      24                 :            :     // Fill two sets and keep the difference in a map
      25         [ +  - ]:        373 :     std::map<uint32_t, bool> diff;
      26   [ +  -  +  +  :      16712 :     LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
                   +  + ]
      27                 :            :     {
      28         [ +  - ]:      16512 :         const auto entry{fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, std::numeric_limits<uint32_t>::max() - 1)};
      29                 :      32437 :         const auto KeepDiff{[&] {
      30                 :      15925 :             bool& mut{diff[entry]};
      31                 :      15925 :             mut = !mut;
      32                 :      15925 :         }};
      33         [ +  - ]:      16512 :         CallOneOf(
      34                 :            :             fuzzed_data_provider,
      35                 :      25387 :             [&] {
      36                 :       8875 :                 sketch_a.Add(entry);
      37                 :       8875 :                 KeepDiff();
      38                 :       8875 :             },
      39                 :      23562 :             [&] {
      40                 :       7050 :                 sketch_b.Add(entry);
      41                 :       7050 :                 KeepDiff();
      42                 :       7050 :             },
      43                 :      17099 :             [&] {
      44                 :        587 :                 sketch_a.Add(entry);
      45                 :        587 :                 sketch_b.Add(entry);
      46                 :        587 :             });
      47                 :      16512 :     }
      48         [ +  - ]:       9636 :     const auto num_diff{std::accumulate(diff.begin(), diff.end(), size_t{0}, [](auto n, const auto& e) { return n + e.second; })};
      49                 :            : 
      50         [ +  - ]:        200 :     Minisketch sketch_ar{MakeMinisketch32(capacity)};
      51         [ +  - ]:        200 :     Minisketch sketch_br{MakeMinisketch32(capacity)};
      52         [ +  - ]:        200 :     sketch_ar.Deserialize(sketch_a.Serialize());
      53         [ +  - ]:        200 :     sketch_br.Deserialize(sketch_b.Serialize());
      54                 :            : 
      55   [ +  -  +  + ]:        200 :     Minisketch sketch_diff{std::move(fuzzed_data_provider.ConsumeBool() ? sketch_a : sketch_ar)};
      56   [ +  -  +  + ]:        200 :     sketch_diff.Merge(fuzzed_data_provider.ConsumeBool() ? sketch_b : sketch_br);
      57                 :            : 
      58         [ +  + ]:        200 :     if (capacity >= num_diff) {
      59         [ +  - ]:        198 :         const auto max_elements{fuzzed_data_provider.ConsumeIntegralInRange<size_t>(num_diff, capacity)};
      60   [ +  -  +  -  :        198 :         const auto dec{*Assert(sketch_diff.Decode(max_elements))};
                   +  - ]
      61         [ +  - ]:        198 :         Assert(dec.size() == num_diff);
      62         [ +  + ]:       8439 :         for (auto d : dec) {
      63   [ +  -  +  - ]:       8241 :             Assert(diff.at(d));
      64                 :            :         }
      65                 :        198 :     }
      66                 :        200 : }

Generated by: LCOV version 1.14