main-db-tester.cpp 6.96 KB
Newer Older
1
/*
Ronan's avatar
Ronan committed
2
 * main-db-tester.cpp
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2017  Belledonne Communications SARL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19
#include "address/address.h"
Ronan's avatar
Ronan committed
20
#include "core/core.h"
21
#include "db/main-db.h"
22
#include "event-log/events.h"
23

Ronan's avatar
Ronan committed
24 25
#include "private.h"

26
#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
27
#include "tester_utils.h"
28 29 30 31 32 33 34 35 36

// =============================================================================

using namespace std;

using namespace LinphonePrivate;

// -----------------------------------------------------------------------------

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static const string getDatabasePath () {
	static const string path = string(bc_tester_get_resource_dir_prefix()) + "db/linphone.db";
	return path;
}

// -----------------------------------------------------------------------------

class MainDbProvider {
public:
	MainDbProvider () {
		mCoreManager = linphone_core_manager_new("marie_rc");
		mMainDb = new MainDb(mCoreManager->lc->cppCore->getSharedFromThis());
		mMainDb->connect(MainDb::Sqlite3, getDatabasePath());
	}

	~MainDbProvider () {
		delete mMainDb;
		linphone_core_manager_destroy(mCoreManager);
	}

	const MainDb &getMainDb () {
		return *mMainDb;
	}

private:
	LinphoneCoreManager *mCoreManager;
	MainDb *mMainDb;
};

// -----------------------------------------------------------------------------

static void open_database () {
	MainDbProvider provider;
	BC_ASSERT_TRUE(provider.getMainDb().isConnected());
}

static void get_events_count () {
	MainDbProvider provider;
	const MainDb &mainDb = provider.getMainDb();
76
	BC_ASSERT_EQUAL(mainDb.getEventsCount(), 5175, int, "%d");
77 78 79
	BC_ASSERT_EQUAL(mainDb.getEventsCount(MainDb::ConferenceCallFilter), 0, int, "%d");
	BC_ASSERT_EQUAL(mainDb.getEventsCount(MainDb::ConferenceInfoFilter), 18, int, "%d");
	BC_ASSERT_EQUAL(mainDb.getEventsCount(MainDb::ConferenceChatMessageFilter), 5157, int, "%d");
80
	BC_ASSERT_EQUAL(mainDb.getEventsCount(MainDb::NoFilter), 5175, int, "%d");
81 82 83 84 85 86 87 88
}

static void get_messages_count () {
	MainDbProvider provider;
	const MainDb &mainDb = provider.getMainDb();
	BC_ASSERT_EQUAL(mainDb.getChatMessagesCount(), 5157, int, "%d");
	BC_ASSERT_EQUAL(
		mainDb.getChatMessagesCount(
89
			ChatRoomId(IdentityAddress("sip:test-3@sip.linphone.org"), IdentityAddress("sip:test-1@sip.linphone.org"))
90
		),
91
		861, int, "%d"
92 93 94 95 96 97 98 99 100
	);
}

static void get_unread_messages_count () {
	MainDbProvider provider;
	const MainDb &mainDb = provider.getMainDb();
	BC_ASSERT_EQUAL(mainDb.getUnreadChatMessagesCount(), 2, int, "%d");
	BC_ASSERT_EQUAL(
		mainDb.getUnreadChatMessagesCount(
101
			ChatRoomId(IdentityAddress("sip:test-3@sip.linphone.org"), IdentityAddress("sip:test-1@sip.linphone.org"))
102 103 104 105 106 107 108 109 110 111
		),
		0, int, "%d"
	);
}

static void get_history () {
	MainDbProvider provider;
	const MainDb &mainDb = provider.getMainDb();
	BC_ASSERT_EQUAL(
		mainDb.getHistoryRange(
112
			ChatRoomId(IdentityAddress("sip:test-4@sip.linphone.org"), IdentityAddress("sip:test-1@sip.linphone.org")),
113 114
			0, -1, MainDb::Filter::ConferenceChatMessageFilter
		).size(),
115
		54,
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		int,
		"%d"
	);
	BC_ASSERT_EQUAL(
		mainDb.getHistoryRange(
			ChatRoomId(IdentityAddress("sip:test-7@sip.linphone.org"), IdentityAddress("sip:test-7@sip.linphone.org")),
			0, -1, MainDb::Filter::ConferenceCallFilter
		).size(),
		0,
		int,
		"%d"
	);
	BC_ASSERT_EQUAL(
		mainDb.getHistoryRange(
			ChatRoomId(IdentityAddress("sip:test-1@sip.linphone.org"), IdentityAddress("sip:test-1@sip.linphone.org")),
			0, -1, MainDb::Filter::ConferenceChatMessageFilter
		).size(),
		862,
		int,
		"%d"
	);
	BC_ASSERT_EQUAL(
		mainDb.getHistory(
			ChatRoomId(IdentityAddress("sip:test-1@sip.linphone.org"), IdentityAddress("sip:test-1@sip.linphone.org")),
			100, MainDb::Filter::ConferenceChatMessageFilter
		).size(),
		100,
		int,
		"%d"
	);
}

static void get_conference_notified_events () {
	MainDbProvider provider;
	const MainDb &mainDb = provider.getMainDb();
	list<shared_ptr<EventLog>> events = mainDb.getConferenceNotifiedEvents(
		ChatRoomId(IdentityAddress("sip:fake-group-2@sip.linphone.org"), IdentityAddress("sip:fake-group-2@sip.linphone.org")),
		1
	);
	BC_ASSERT_EQUAL(events.size(), 3, int, "%d");
	if (events.size() != 3)
		return;

	shared_ptr<EventLog> event;
	auto it = events.cbegin();

	event = *it;
	if (!BC_ASSERT_TRUE(event->getType() == EventLog::Type::ConferenceParticipantRemoved)) return;
	{
		shared_ptr<ConferenceParticipantEvent> participantEvent = static_pointer_cast<ConferenceParticipantEvent>(event);
		BC_ASSERT_TRUE(participantEvent->getChatRoomId().getPeerAddress().asString() == "sip:fake-group-2@sip.linphone.org");
		BC_ASSERT_TRUE(participantEvent->getParticipantAddress().asString() == "sip:test-11@sip.linphone.org");
		BC_ASSERT_TRUE(participantEvent->getNotifyId() == 2);
	}

	event = *++it;
	if (!BC_ASSERT_TRUE(event->getType() == EventLog::Type::ConferenceParticipantDeviceAdded)) return;
	{
		shared_ptr<ConferenceParticipantDeviceEvent> deviceEvent = static_pointer_cast<
			ConferenceParticipantDeviceEvent
		>(event);
		BC_ASSERT_TRUE(deviceEvent->getChatRoomId().getPeerAddress().asString() == "sip:fake-group-2@sip.linphone.org");
		BC_ASSERT_TRUE(deviceEvent->getParticipantAddress().asString() == "sip:test-11@sip.linphone.org");
		BC_ASSERT_TRUE(deviceEvent->getNotifyId() == 3);
180
		BC_ASSERT_TRUE(deviceEvent->getDeviceAddress().asString() == "sip:device-address-1@sip.linphone.org");
181 182 183 184 185 186 187 188 189 190 191
	}

	event = *++it;
	if (!BC_ASSERT_TRUE(event->getType() == EventLog::Type::ConferenceParticipantDeviceRemoved)) return;
	{
		shared_ptr<ConferenceParticipantDeviceEvent> deviceEvent = static_pointer_cast<
			ConferenceParticipantDeviceEvent
		>(event);
		BC_ASSERT_TRUE(deviceEvent->getChatRoomId().getPeerAddress().asString() == "sip:fake-group-2@sip.linphone.org");
		BC_ASSERT_TRUE(deviceEvent->getParticipantAddress().asString() == "sip:test-11@sip.linphone.org");
		BC_ASSERT_TRUE(deviceEvent->getNotifyId() == 4);
192
		BC_ASSERT_TRUE(deviceEvent->getDeviceAddress().asString() == "sip:device-address-1@sip.linphone.org");
193 194
	}
}
195

Ronan's avatar
Ronan committed
196
test_t main_db_tests[] = {
197 198 199 200 201 202
	TEST_NO_TAG("Open database", open_database),
	TEST_NO_TAG("Get events count", get_events_count),
	TEST_NO_TAG("Get messages count", get_messages_count),
	TEST_NO_TAG("Get unread messages count", get_unread_messages_count),
	TEST_NO_TAG("Get history", get_history),
	TEST_NO_TAG("Get conference events", get_conference_notified_events)
203 204
};

Ronan's avatar
Ronan committed
205 206 207
test_suite_t main_db_test_suite = {
	"MainDb", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
	sizeof(main_db_tests) / sizeof(main_db_tests[0]), main_db_tests
208
};