Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 3 : * Author: Olivier Gregoire <olivier.gregoire@savoirfairelinux.com> 4 : * 5 : * This program is free software; you can redistribute it and/or modify 6 : * it under the terms of the GNU General Public License as published by 7 : * the Free Software Foundation; either version 3 of the License, or 8 : * (at your option) any later version. 9 : * 10 : * This program is distributed in the hope that it will be useful, 11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : * GNU General Public License for more details. 14 : * 15 : * You should have received a copy of the GNU General Public License 16 : * along with this program; if not, write to the Free Software 17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 : */ 19 : 20 : #include <cppunit/TestAssert.h> 21 : #include <cppunit/TestFixture.h> 22 : #include <cppunit/extensions/HelperMacros.h> 23 : 24 : #include <condition_variable> 25 : 26 : #include "account_factory.h" 27 : #include "../../test_runner.h" 28 : #include "jami.h" 29 : #include "account_const.h" 30 : #include "manager.h" 31 : #include "account_const.h" 32 : #include "account_schema.h" 33 : #include "common.h" 34 : #include "jamidht/jamiaccount.h" 35 : 36 : using namespace std::literals::chrono_literals; 37 : 38 : namespace jami { namespace test { 39 : 40 : class Account_factoryTest : public CppUnit::TestFixture { 41 : public: 42 3 : Account_factoryTest() 43 3 : { 44 : // Init daemon 45 3 : libjami::init(libjami::InitFlag(libjami::LIBJAMI_FLAG_DEBUG | libjami::LIBJAMI_FLAG_CONSOLE_LOG)); 46 3 : if (not Manager::instance().initialized) 47 1 : CPPUNIT_ASSERT(libjami::start("dring-sample.yml")); 48 3 : } 49 6 : ~Account_factoryTest() { libjami::fini(); } 50 2 : static std::string name() { return "Account_factory"; } 51 : void setUp(); 52 : void tearDown(); 53 : 54 : private: 55 : void testAddRemoveSIPAccount(); 56 : void testAddRemoveRINGAccount(); 57 : void testClear(); 58 : 59 2 : CPPUNIT_TEST_SUITE(Account_factoryTest); 60 1 : CPPUNIT_TEST(testAddRemoveSIPAccount); 61 1 : CPPUNIT_TEST(testAddRemoveRINGAccount); 62 1 : CPPUNIT_TEST(testClear); 63 4 : CPPUNIT_TEST_SUITE_END(); 64 : 65 : const std::string SIP_ID = "SIP_ID"; 66 : const std::string JAMI_ID = "JAMI_ID"; 67 : bool sipReady, ringReady, accountsRemoved, knownDevicesChanged; 68 : size_t initialAccounts; 69 : std::map<std::string, std::shared_ptr<libjami::CallbackWrapperBase>> confHandlers; 70 : std::mutex mtx; 71 : std::unique_lock<std::mutex> lk {mtx}; 72 : std::condition_variable cv; 73 : }; 74 : 75 : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Account_factoryTest, Account_factoryTest::name()); 76 : 77 : void 78 3 : Account_factoryTest::setUp() 79 : { 80 3 : sipReady = false; 81 3 : ringReady = false; 82 3 : accountsRemoved = false; 83 3 : initialAccounts = Manager::instance().accountCount(); 84 : 85 3 : confHandlers.insert( 86 6 : libjami::exportable_callback<libjami::ConfigurationSignal::VolatileDetailsChanged>( 87 12 : [&](const std::string& accountID, 88 : const std::map<std::string, std::string>& details) { 89 12 : if (accountID != JAMI_ID && accountID != SIP_ID) { 90 0 : return; 91 : } 92 : try { 93 24 : ringReady |= accountID == JAMI_ID 94 21 : && details.at(jami::Conf::CONFIG_ACCOUNT_REGISTRATION_STATUS) == "REGISTERED" 95 21 : && details.at(libjami::Account::VolatileProperties::DEVICE_ANNOUNCED) == "true"; 96 24 : sipReady |= accountID == SIP_ID 97 12 : && details.at(jami::Conf::CONFIG_ACCOUNT_REGISTRATION_STATUS) == "READY"; 98 0 : } catch (const std::out_of_range&) {} 99 : })); 100 3 : confHandlers.insert( 101 6 : libjami::exportable_callback<libjami::ConfigurationSignal::AccountsChanged>([&]() { 102 4 : if (jami::Manager::instance().getAccountList().size() <= initialAccounts) { 103 2 : accountsRemoved = true; 104 : } 105 4 : })); 106 3 : confHandlers.insert( 107 6 : libjami::exportable_callback<libjami::ConfigurationSignal::KnownDevicesChanged>([&](auto, auto) { 108 3 : knownDevicesChanged = true; 109 3 : })); 110 3 : libjami::registerSignalHandlers(confHandlers); 111 3 : } 112 : 113 : void 114 3 : Account_factoryTest::tearDown() 115 : { 116 3 : libjami::unregisterSignalHandlers(); 117 3 : } 118 : 119 : void 120 1 : Account_factoryTest::testAddRemoveSIPAccount() 121 : { 122 1 : AccountFactory* accountFactory = &Manager::instance().accountFactory; 123 : 124 2 : auto accDetails = libjami::getAccountTemplate("SIP"); 125 1 : auto newAccount = Manager::instance().addAccount(accDetails, SIP_ID); 126 : 127 2 : CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { 128 : return sipReady; 129 : })); 130 : 131 1 : CPPUNIT_ASSERT(accountFactory->hasAccount(SIP_ID)); 132 1 : CPPUNIT_ASSERT(!accountFactory->hasAccount(JAMI_ID)); 133 1 : CPPUNIT_ASSERT(!accountFactory->empty()); 134 1 : CPPUNIT_ASSERT(accountFactory->accountCount() == 1 + initialAccounts); 135 : 136 1 : auto details = Manager::instance().getVolatileAccountDetails(SIP_ID); 137 1 : CPPUNIT_ASSERT(details.find(libjami::Account::ConfProperties::DEVICE_ID) == details.end()); 138 : 139 1 : Manager::instance().removeAccount(SIP_ID, true); 140 : 141 2 : CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return accountsRemoved; })); 142 1 : } 143 : 144 : void 145 1 : Account_factoryTest::testAddRemoveRINGAccount() 146 : { 147 1 : AccountFactory* accountFactory = &Manager::instance().accountFactory; 148 : 149 2 : auto accDetails = libjami::getAccountTemplate("RING"); 150 1 : auto newAccount = Manager::instance().addAccount(accDetails, JAMI_ID); 151 3 : CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { 152 : return ringReady; 153 : })); 154 : 155 1 : CPPUNIT_ASSERT(accountFactory->hasAccount(JAMI_ID)); 156 1 : CPPUNIT_ASSERT(!accountFactory->hasAccount(SIP_ID)); 157 1 : CPPUNIT_ASSERT(!accountFactory->empty()); 158 1 : CPPUNIT_ASSERT(accountFactory->accountCount() == 1 + initialAccounts); 159 : 160 1 : auto details = Manager::instance().getVolatileAccountDetails(JAMI_ID); 161 1 : CPPUNIT_ASSERT(details.find(libjami::Account::ConfProperties::DEVICE_ID) != details.end()); 162 : 163 1 : std::map<std::string, std::string> newDetails; 164 1 : newDetails[libjami::Account::ConfProperties::DEVICE_NAME] = "foo"; 165 1 : knownDevicesChanged = false; 166 1 : libjami::setAccountDetails(JAMI_ID, newDetails); 167 2 : CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return knownDevicesChanged; })); 168 1 : details = Manager::instance().getAccountDetails(JAMI_ID); 169 1 : CPPUNIT_ASSERT(details[libjami::Account::ConfProperties::DEVICE_NAME] == "foo"); 170 : 171 : 172 1 : Manager::instance().removeAccount(JAMI_ID, true); 173 2 : CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&] { return accountsRemoved; })); 174 1 : } 175 : 176 : void 177 1 : Account_factoryTest::testClear() 178 : { 179 1 : AccountFactory accountFactory; 180 : // verify if there is no account at the beginning 181 1 : CPPUNIT_ASSERT(accountFactory.empty()); 182 1 : CPPUNIT_ASSERT(accountFactory.accountCount() == 0); 183 : 184 1 : const int nbrAccount = 5; 185 : 186 6 : for(int i = 0; i < nbrAccount ; ++i) { 187 5 : accountFactory.createAccount(libjami::Account::ProtocolNames::RING, JAMI_ID+std::to_string(i)); 188 : } 189 : 190 1 : CPPUNIT_ASSERT(accountFactory.accountCount()==nbrAccount); 191 1 : CPPUNIT_ASSERT(!accountFactory.empty()); 192 : 193 1 : accountFactory.clear(); 194 : 195 1 : CPPUNIT_ASSERT(accountFactory.empty()); 196 1 : CPPUNIT_ASSERT(accountFactory.accountCount()==0); 197 1 : } 198 : 199 : }} // namespace jami::test 200 : 201 1 : RING_TEST_RUNNER(jami::test::Account_factoryTest::name())