LCOV - code coverage report
Current view: top level - src/test - base64_tests.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 27 0.0 %
Date: 2023-10-05 12:38:51 Functions: 0 7 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2011-2022 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 <util/strencodings.h>
       6                 :            : 
       7                 :            : #include <boost/test/unit_test.hpp>
       8                 :            : #include <string>
       9                 :            : 
      10                 :            : using namespace std::literals;
      11                 :            : 
      12                 :          0 : BOOST_AUTO_TEST_SUITE(base64_tests)
      13                 :            : 
      14                 :          0 : BOOST_AUTO_TEST_CASE(base64_testvectors)
      15                 :            : {
      16                 :          0 :     static const std::string vstrIn[]  = {"","f","fo","foo","foob","fooba","foobar"};
      17                 :          0 :     static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
      18                 :          0 :     for (unsigned int i=0; i<std::size(vstrIn); i++)
      19                 :            :     {
      20                 :          0 :         std::string strEnc = EncodeBase64(vstrIn[i]);
      21                 :          0 :         BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
      22                 :          0 :         auto dec = DecodeBase64(strEnc);
      23                 :          0 :         BOOST_REQUIRE(dec);
      24                 :          0 :         BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
      25                 :          0 :     }
      26                 :            : 
      27                 :            :     {
      28                 :          0 :         const std::vector<uint8_t> in_u{0xff, 0x01, 0xff};
      29                 :          0 :         const std::vector<std::byte> in_b{std::byte{0xff}, std::byte{0x01}, std::byte{0xff}};
      30                 :          0 :         const std::string in_s{"\xff\x01\xff"};
      31                 :          0 :         const std::string out_exp{"/wH/"};
      32                 :          0 :         BOOST_CHECK_EQUAL(EncodeBase64(in_u), out_exp);
      33                 :          0 :         BOOST_CHECK_EQUAL(EncodeBase64(in_b), out_exp);
      34                 :          0 :         BOOST_CHECK_EQUAL(EncodeBase64(in_s), out_exp);
      35                 :          0 :     }
      36                 :            : 
      37                 :            :     // Decoding strings with embedded NUL characters should fail
      38                 :          0 :     BOOST_CHECK(!DecodeBase64("invalid\0"s));
      39                 :          0 :     BOOST_CHECK(DecodeBase64("nQB/pZw="s));
      40                 :          0 :     BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s));
      41                 :          0 :     BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s));
      42                 :          0 : }
      43                 :            : 
      44                 :          0 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14