LCOV - code coverage report
Current view: top level - test/unitTest/account_factory - testAccount_factory.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 90 92 97.8 %
Date: 2024-11-15 09:04:49 Functions: 21 21 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 <condition_variable>
      23             : 
      24             : #include "account_factory.h"
      25             : #include "../../test_runner.h"
      26             : #include "jami.h"
      27             : #include "account_const.h"
      28             : #include "manager.h"
      29             : #include "account_const.h"
      30             : #include "account_schema.h"
      31             : #include "common.h"
      32             : #include "jamidht/jamiaccount.h"
      33             : 
      34             : using namespace std::literals::chrono_literals;
      35             : 
      36             : namespace jami { namespace test {
      37             : 
      38             : class Account_factoryTest : public CppUnit::TestFixture {
      39             : public:
      40           3 :     Account_factoryTest()
      41           3 :     {
      42             :         // Init daemon
      43           3 :         libjami::init(libjami::InitFlag(libjami::LIBJAMI_FLAG_DEBUG | libjami::LIBJAMI_FLAG_CONSOLE_LOG));
      44           3 :         if (not Manager::instance().initialized)
      45           1 :             CPPUNIT_ASSERT(libjami::start("dring-sample.yml"));
      46           3 :     }
      47           6 :     ~Account_factoryTest() { libjami::fini(); }
      48           2 :     static std::string name() { return "Account_factory"; }
      49             :     void setUp();
      50             :     void tearDown();
      51             : 
      52             : private:
      53             :     void testAddRemoveSIPAccount();
      54             :     void testAddRemoveRINGAccount();
      55             :     void testClear();
      56             : 
      57           2 :     CPPUNIT_TEST_SUITE(Account_factoryTest);
      58           1 :     CPPUNIT_TEST(testAddRemoveSIPAccount);
      59           1 :     CPPUNIT_TEST(testAddRemoveRINGAccount);
      60           1 :     CPPUNIT_TEST(testClear);
      61           4 :     CPPUNIT_TEST_SUITE_END();
      62             : 
      63             :     const std::string SIP_ID = "SIP_ID";
      64             :     const std::string JAMI_ID = "JAMI_ID";
      65             :     bool sipReady, ringReady, accountsRemoved, knownDevicesChanged;
      66             :     size_t initialAccounts;
      67             :     std::map<std::string, std::shared_ptr<libjami::CallbackWrapperBase>> confHandlers;
      68             :     std::mutex mtx;
      69             :     std::unique_lock<std::mutex> lk {mtx};
      70             :     std::condition_variable cv;
      71             : };
      72             : 
      73             : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Account_factoryTest, Account_factoryTest::name());
      74             : 
      75             : void
      76           3 : Account_factoryTest::setUp()
      77             : {
      78           3 :     sipReady = false;
      79           3 :     ringReady = false;
      80           3 :     accountsRemoved = false;
      81           3 :     initialAccounts = Manager::instance().accountCount();
      82             : 
      83           3 :     confHandlers.insert(
      84           6 :         libjami::exportable_callback<libjami::ConfigurationSignal::VolatileDetailsChanged>(
      85          12 :             [&](const std::string& accountID,
      86             :                 const std::map<std::string, std::string>& details) {
      87          12 :                 if (accountID != JAMI_ID && accountID != SIP_ID) {
      88           0 :                     return;
      89             :                 }
      90             :                 try {
      91          24 :                     ringReady |= accountID == JAMI_ID
      92          21 :                                 && details.at(jami::Conf::CONFIG_ACCOUNT_REGISTRATION_STATUS) == "REGISTERED"
      93          21 :                                 && details.at(libjami::Account::VolatileProperties::DEVICE_ANNOUNCED) == "true";
      94          24 :                     sipReady |= accountID == SIP_ID
      95          12 :                                 && details.at(jami::Conf::CONFIG_ACCOUNT_REGISTRATION_STATUS) == "READY";
      96           0 :                 } catch (const std::out_of_range&) {}
      97             :             }));
      98           3 :     confHandlers.insert(
      99           6 :         libjami::exportable_callback<libjami::ConfigurationSignal::AccountsChanged>([&]() {
     100           4 :             if (jami::Manager::instance().getAccountList().size() <= initialAccounts) {
     101           2 :                 accountsRemoved = true;
     102             :             }
     103           4 :         }));
     104           3 :     confHandlers.insert(
     105           6 :         libjami::exportable_callback<libjami::ConfigurationSignal::KnownDevicesChanged>([&](auto, auto) {
     106           3 :             knownDevicesChanged = true;
     107           3 :         }));
     108           3 :     libjami::registerSignalHandlers(confHandlers);
     109           3 : }
     110             : 
     111             : void
     112           3 : Account_factoryTest::tearDown()
     113             : {
     114           3 :     libjami::unregisterSignalHandlers();
     115           3 : }
     116             : 
     117             : void
     118           1 : Account_factoryTest::testAddRemoveSIPAccount()
     119             : {
     120           1 :     AccountFactory* accountFactory = &Manager::instance().accountFactory;
     121             : 
     122           2 :     auto accDetails = libjami::getAccountTemplate("SIP");
     123           1 :     auto newAccount = Manager::instance().addAccount(accDetails, SIP_ID);
     124             : 
     125           2 :     CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
     126             :         return sipReady;
     127             :     }));
     128             : 
     129           1 :     CPPUNIT_ASSERT(accountFactory->hasAccount(SIP_ID));
     130           1 :     CPPUNIT_ASSERT(!accountFactory->hasAccount(JAMI_ID));
     131           1 :     CPPUNIT_ASSERT(!accountFactory->empty());
     132           1 :     CPPUNIT_ASSERT(accountFactory->accountCount() == 1 + initialAccounts);
     133             : 
     134           1 :     auto details = Manager::instance().getVolatileAccountDetails(SIP_ID);
     135           1 :     CPPUNIT_ASSERT(details.find(libjami::Account::ConfProperties::DEVICE_ID) == details.end());
     136             : 
     137           1 :     Manager::instance().removeAccount(SIP_ID, true);
     138             : 
     139           2 :     CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return accountsRemoved; }));
     140           1 : }
     141             : 
     142             : void
     143           1 : Account_factoryTest::testAddRemoveRINGAccount()
     144             : {
     145           1 :     AccountFactory* accountFactory = &Manager::instance().accountFactory;
     146             : 
     147           2 :     auto accDetails = libjami::getAccountTemplate("RING");
     148           1 :     auto newAccount = Manager::instance().addAccount(accDetails, JAMI_ID);
     149           3 :     CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] {
     150             :         return ringReady;
     151             :     }));
     152             : 
     153           1 :     CPPUNIT_ASSERT(accountFactory->hasAccount(JAMI_ID));
     154           1 :     CPPUNIT_ASSERT(!accountFactory->hasAccount(SIP_ID));
     155           1 :     CPPUNIT_ASSERT(!accountFactory->empty());
     156           1 :     CPPUNIT_ASSERT(accountFactory->accountCount() == 1 + initialAccounts);
     157             : 
     158           1 :     auto details = Manager::instance().getVolatileAccountDetails(JAMI_ID);
     159           1 :     CPPUNIT_ASSERT(details.find(libjami::Account::ConfProperties::DEVICE_ID) != details.end());
     160             : 
     161           1 :     std::map<std::string, std::string> newDetails;
     162           1 :     newDetails[libjami::Account::ConfProperties::DEVICE_NAME] = "foo";
     163           1 :     knownDevicesChanged = false;
     164           1 :     libjami::setAccountDetails(JAMI_ID, newDetails);
     165           2 :     CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return knownDevicesChanged; }));
     166           1 :     details = Manager::instance().getAccountDetails(JAMI_ID);
     167           1 :     CPPUNIT_ASSERT(details[libjami::Account::ConfProperties::DEVICE_NAME] == "foo");
     168             : 
     169             : 
     170           1 :     Manager::instance().removeAccount(JAMI_ID, true);
     171           2 :     CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return accountsRemoved; }));
     172           1 : }
     173             : 
     174             : void
     175           1 : Account_factoryTest::testClear()
     176             : {
     177           1 :     AccountFactory accountFactory;
     178             :     // verify if there is no account at the beginning
     179           1 :     CPPUNIT_ASSERT(accountFactory.empty());
     180           1 :     CPPUNIT_ASSERT(accountFactory.accountCount() == 0);
     181             : 
     182           1 :     const int nbrAccount = 5;
     183             : 
     184           6 :     for(int i = 0; i < nbrAccount ; ++i) {
     185           5 :         accountFactory.createAccount(libjami::Account::ProtocolNames::RING, JAMI_ID+std::to_string(i));
     186             :     }
     187             : 
     188           1 :     CPPUNIT_ASSERT(accountFactory.accountCount()==nbrAccount);
     189           1 :     CPPUNIT_ASSERT(!accountFactory.empty());
     190             : 
     191           1 :     accountFactory.clear();
     192             : 
     193           1 :     CPPUNIT_ASSERT(accountFactory.empty());
     194           1 :     CPPUNIT_ASSERT(accountFactory.accountCount()==0);
     195           1 : }
     196             : 
     197             : }} // namespace jami::test
     198             : 
     199           1 : RING_TEST_RUNNER(jami::test::Account_factoryTest::name())

Generated by: LCOV version 1.14