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-03-29 09:30:40 Functions: 8 8 100.0 %

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

Generated by: LCOV version 1.14