Commit 0892bd02 authored by Gautier Pelloux-Prayer's avatar Gautier Pelloux-Prayer
Browse files

Fix method linphone_chat_room_get_history which returned one extra message +...

Fix method linphone_chat_room_get_history which returned one extra message + fix memory leaks in message storage tester
parent dee580f5
......@@ -6282,6 +6282,9 @@ static void linphone_core_uninit(LinphoneCore *lc)
if(lc->rec_file!=NULL){
ms_free(lc->rec_file);
}
if (lc->chat_db_file){
ms_free(lc->chat_db_file);
}
if(lc->presence_model){
linphone_presence_model_unref(lc->presence_model);
}
......
......@@ -364,7 +364,7 @@ void linphone_chat_room_delete_history(LinphoneChatRoom *cr){
MSList *linphone_chat_room_get_history_range(LinphoneChatRoom *cr, int startm, int endm){
LinphoneCore *lc=linphone_chat_room_get_lc(cr);
MSList *ret;
char *buf;
char *buf,*buf2;
char *peer;
uint64_t begin,end;
int buf_max_size = 512;
......@@ -378,17 +378,24 @@ MSList *linphone_chat_room_get_history_range(LinphoneChatRoom *cr, int startm, i
buf=ms_malloc(buf_max_size);
buf=sqlite3_snprintf(buf_max_size-1,buf,"SELECT * FROM history WHERE remoteContact = %Q ORDER BY id DESC",peer);
if (startm<0) startm=0;
if (endm>0&&endm>=startm){
buf=sqlite3_snprintf(buf_max_size-1,buf,"%s LIMIT %i ",buf,endm+1-startm);
buf2=ms_strdup_printf("%s LIMIT %i ",buf,endm+1-startm);
ms_free(buf);
buf = buf2;
}else if(startm>0){
ms_message("%s(): end is lower than start (%d < %d). No end assumed.",__FUNCTION__,endm,startm);
buf=sqlite3_snprintf(buf_max_size-1,buf,"%s LIMIT -1",buf);
ms_message("%s(): end is lower than start (%d < %d). Assuming no end limit.",__FUNCTION__,endm,startm);
buf2=ms_strdup_printf("%s LIMIT -1",buf);
ms_free(buf);
buf = buf2;
}
if (startm>0){
buf=sqlite3_snprintf(buf_max_size-1,buf,"%s OFFSET %i ",buf,startm);
buf2=ms_strdup_printf("%s OFFSET %i ",buf,startm);
ms_free(buf);
buf = buf2;
}
begin=ortp_get_cur_time_ms();
......@@ -403,7 +410,7 @@ MSList *linphone_chat_room_get_history_range(LinphoneChatRoom *cr, int startm, i
}
MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){
return linphone_chat_room_get_history_range(cr, 0, nb_message);
return linphone_chat_room_get_history_range(cr, 0, nb_message-1);
}
......
......@@ -1001,25 +1001,37 @@ static void history_messages_count() {
chatroom = linphone_core_get_chat_room(marie->lc, jehan_addr);
CU_ASSERT_PTR_NOT_NULL(chatroom);
if (chatroom){
MSList *history=linphone_chat_room_get_history(chatroom,0);
messages=linphone_chat_room_get_history(chatroom,10);
CU_ASSERT_EQUAL(ms_list_size(messages), 10);
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
messages=linphone_chat_room_get_history(chatroom,0);
CU_ASSERT_EQUAL(linphone_chat_room_get_history_size(chatroom), 1270);
CU_ASSERT_EQUAL(ms_list_size(history), linphone_chat_room_get_history_size(chatroom));
CU_ASSERT_EQUAL(ms_list_size(messages), 1270);
/*check the second most recent message*/
CU_ASSERT_STRING_EQUAL(linphone_chat_message_get_text((LinphoneChatMessage *)history->next->data), "Fore and aft follow each other.");
CU_ASSERT_STRING_EQUAL(linphone_chat_message_get_text((LinphoneChatMessage *)messages->next->data), "Fore and aft follow each other.");
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
/*test offset+limit: retrieve the 42th latest message only and check its content*/
messages=linphone_chat_room_get_history_range(chatroom, 42, 42);
CU_ASSERT_EQUAL(ms_list_size(messages), 1);
CU_ASSERT_STRING_EQUAL(linphone_chat_message_get_text((LinphoneChatMessage *)messages->data), "If you open yourself to the Tao is intangible and evasive, yet prefers to keep us at the mercy of the kingdom, then all of the streams of hundreds of valleys because of its limitless possibilities.");
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
/*test offset without limit*/
CU_ASSERT_EQUAL(ms_list_size(linphone_chat_room_get_history_range(chatroom, 1265, -1)), 1270-1265);
messages = linphone_chat_room_get_history_range(chatroom, 1265, -1);
CU_ASSERT_EQUAL(ms_list_size(messages), 1270-1265);
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
/*test limit without offset*/
CU_ASSERT_EQUAL(ms_list_size(linphone_chat_room_get_history_range(chatroom, 0, 5)), 6);
messages = linphone_chat_room_get_history_range(chatroom, 0, 5);
CU_ASSERT_EQUAL(ms_list_size(messages), 6);
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
/*test invalid start*/
CU_ASSERT_EQUAL(ms_list_size(linphone_chat_room_get_history_range(chatroom, 1265, 1260)), 1270-1265);
messages = linphone_chat_room_get_history_range(chatroom, 1265, 1260);
CU_ASSERT_EQUAL(ms_list_size(messages), 1270-1265);
ms_list_free_with_data(messages, (void (*)(void*))linphone_chat_message_unref);
}
linphone_core_manager_destroy(marie);
linphone_address_destroy(jehan_addr);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment