LCOV - code coverage report
Current view: top level - src/test/fuzz - asmap_direct.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 27 27 100.0 %
Date: 2023-10-05 15:40:34 Functions: 4 4 100.0 %
Branches: 33 46 71.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2020-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 <netaddress.h>
       6                 :            : #include <util/asmap.h>
       7                 :            : #include <test/fuzz/fuzz.h>
       8                 :            : 
       9                 :            : #include <cstdint>
      10                 :            : #include <optional>
      11                 :            : #include <vector>
      12                 :            : 
      13                 :            : #include <assert.h>
      14                 :            : 
      15         [ -  + ]:        564 : FUZZ_TARGET(asmap_direct)
      16                 :            : {
      17                 :            :     // Encoding: [asmap using 1 bit / byte] 0xFF [addr using 1 bit / byte]
      18                 :        218 :     std::optional<size_t> sep_pos_opt;
      19         [ +  + ]:      78066 :     for (size_t pos = 0; pos < buffer.size(); ++pos) {
      20                 :      77850 :         uint8_t x = buffer[pos];
      21         [ +  + ]:      77850 :         if ((x & 0xFE) == 0) continue;
      22         [ +  + ]:        218 :         if (x == 0xFF) {
      23         [ +  + ]:        217 :             if (sep_pos_opt) return;
      24                 :        216 :             sep_pos_opt = pos;
      25         [ +  - ]:        389 :         } else {
      26                 :          1 :             return;
      27                 :            :         }
      28                 :        216 :     }
      29         [ +  + ]:        216 :     if (!sep_pos_opt) return; // Needs exactly 1 separator
      30                 :        215 :     const size_t sep_pos{sep_pos_opt.value()};
      31         [ +  + ]:        215 :     if (buffer.size() - sep_pos - 1 > 128) return; // At most 128 bits in IP address
      32                 :            : 
      33                 :            :     // Checks on asmap
      34         [ +  - ]:        214 :     std::vector<bool> asmap(buffer.begin(), buffer.begin() + sep_pos);
      35   [ +  -  +  + ]:        214 :     if (SanityCheckASMap(asmap, buffer.size() - 1 - sep_pos)) {
      36                 :            :         // Verify that for valid asmaps, no prefix (except up to 7 zero padding bits) is valid.
      37         [ +  - ]:        139 :         std::vector<bool> asmap_prefix = asmap;
      38   [ +  -  +  +  :        682 :         while (!asmap_prefix.empty() && asmap_prefix.size() + 7 > asmap.size() && asmap_prefix.back() == false) {
             +  -  +  + ]
      39         [ +  - ]:        543 :             asmap_prefix.pop_back();
      40                 :            :         }
      41         [ +  + ]:      51614 :         while (!asmap_prefix.empty()) {
      42         [ +  - ]:      51475 :             asmap_prefix.pop_back();
      43   [ +  -  -  + ]:      51475 :             assert(!SanityCheckASMap(asmap_prefix, buffer.size() - 1 - sep_pos));
      44                 :            :         }
      45                 :            :         // No address input should trigger assertions in interpreter
      46         [ +  - ]:        139 :         std::vector<bool> addr(buffer.begin() + sep_pos + 1, buffer.end());
      47         [ -  + ]:        139 :         (void)Interpret(asmap, addr);
      48                 :        139 :     }
      49                 :        218 : }

Generated by: LCOV version 1.14