LCOV - code coverage report
Current view: top level - src/test - base64_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 24 24
Test Date: 2024-11-04 05:10:19 Functions: 100.0 % 2 2
Branches: 49.4 % 156 77

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

Generated by: LCOV version 2.0-1