LCOV - code coverage report
Current view: top level - test/unitTest/base64 - base64.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 19 19 100.0 %
Date: 2024-11-15 09:04:49 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include <cppunit/TestAssert.h>
      19             : #include <cppunit/TestFixture.h>
      20             : #include <cppunit/extensions/HelperMacros.h>
      21             : 
      22             : #include "test_runner.h"
      23             : 
      24             : #include "base64.h"
      25             : 
      26             : namespace jami { namespace test {
      27             : 
      28             : class Base64Test : public CppUnit::TestFixture {
      29             : public:
      30           2 :     static std::string name() { return "base64"; }
      31             : 
      32             : private:
      33             :     void encodingTest();
      34             :     void decodingTestSuccess();
      35             :     void decodingTestFail();
      36             : 
      37           2 :     CPPUNIT_TEST_SUITE(Base64Test);
      38           1 :     CPPUNIT_TEST(encodingTest);
      39           1 :     CPPUNIT_TEST(decodingTestSuccess);
      40           1 :     CPPUNIT_TEST(decodingTestFail);
      41           4 :     CPPUNIT_TEST_SUITE_END();
      42             : 
      43             :     const std::vector<uint8_t> test_bytes = { 23, 45, 67, 87, 89, 34, 2, 45, 9, 10 };
      44             :     const std::string test_base64 = "Fy1DV1kiAi0JCg==";
      45             :     const std::string test_invalid_base64 = "ERSAÄÖöädt4-++asd==";
      46             : };
      47             : 
      48             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Base64Test, Base64Test::name());
      49             : 
      50             : void
      51           1 : Base64Test::encodingTest()
      52             : {
      53           1 :     const std::string output = jami::base64::encode(test_bytes);
      54           1 :     CPPUNIT_ASSERT(test_base64.compare(output) == 0);
      55           1 : }
      56             : 
      57             : void
      58           1 : Base64Test::decodingTestSuccess()
      59             : {
      60           1 :     const std::vector<uint8_t> output = jami::base64::decode(test_base64);
      61           1 :     CPPUNIT_ASSERT(std::equal(test_bytes.begin(), test_bytes.end(), output.begin()));
      62           1 : }
      63             : 
      64             : void
      65           1 : Base64Test::decodingTestFail()
      66             : {
      67             :     // Currently, the input is not validated, i.e. the function most not throw an
      68             :     // exception if decoding fails to make sure calling code not expecting any
      69             :     // is no broken. (Some validation should be implemented sometimes later, though.
      70           1 :     jami::base64::decode(test_invalid_base64);
      71           1 :     CPPUNIT_ASSERT(true);
      72           1 : }
      73             : 
      74             : }} // namespace jami::test
      75             : 
      76           1 : RING_TEST_RUNNER(jami::test::Base64Test::name());

Generated by: LCOV version 1.14