stun_tester.c 3.66 KB
Newer Older
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	belle-sip - SIP (RFC3261) library.
	Copyright (C) 2014  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

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
20 21 22
#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"
jehan's avatar
jehan committed
23
#include "mediastreamer2/stun.h"
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "ortp/port.h"


static const char* stun_address = "stun.linphone.org";


static int test_stun_encode( char*buffer, size_t len, bool_t expect_fail )
{
	StunAtrString username;
	StunAtrString password;
	StunMessage req;
	memset(&req, 0, sizeof(StunMessage));
	memset(&username,0,sizeof(username));
	memset(&password,0,sizeof(password));
	stunBuildReqSimple( &req, &username, TRUE , TRUE , 11);
	len = stunEncodeMessage( &req, buffer, len, &password);
	if (len<=0){
		if( expect_fail )
			ms_message("Fail to encode stun message (EXPECTED).\n");
		else
			ms_error("Fail to encode stun message.\n");
		return -1;
	}
	return len;
}


static void linphone_stun_test_encode()
{
	char smallBuff[12];
	size_t smallLen = 12;
	char bigBuff[STUN_MAX_MESSAGE_SIZE];
	size_t bigLen = STUN_MAX_MESSAGE_SIZE;

	size_t len = test_stun_encode(smallBuff, smallLen, TRUE);
59
	BC_ASSERT(len == -1);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
60 61

	len = test_stun_encode(bigBuff, bigLen, TRUE);
62
	BC_ASSERT(len > 0);
63
	ms_message("STUN message encoded in %i bytes", (int)len);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
64 65 66 67 68 69 70 71 72 73 74
}


static void linphone_stun_test_grab_ip()
{
	LinphoneCoreManager* lc_stun = linphone_core_manager_new2( "stun_rc", FALSE);
	LinphoneCall dummy_call;
	int ping_time;
	int tmp=0;

	memset(&dummy_call, 0, sizeof(LinphoneCall));
Sylvain Berfini's avatar
Sylvain Berfini committed
75 76 77 78 79 80
	dummy_call.main_audio_stream_index = 0;
	dummy_call.main_video_stream_index = 1;
	dummy_call.main_text_stream_index = 2;
	dummy_call.media_ports[dummy_call.main_audio_stream_index].rtp_port = 7078;
	dummy_call.media_ports[dummy_call.main_video_stream_index].rtp_port = 9078;
	dummy_call.media_ports[dummy_call.main_text_stream_index].rtp_port = 11078;
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
81 82

	linphone_core_set_stun_server(lc_stun->lc, stun_address);
83
	BC_ASSERT_STRING_EQUAL(stun_address, linphone_core_get_stun_server(lc_stun->lc));
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
84 85 86 87

	wait_for(lc_stun->lc,lc_stun->lc,&tmp,1);

	ping_time = linphone_core_run_stun_tests(lc_stun->lc, &dummy_call);
88
	BC_ASSERT(ping_time != -1);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
89 90 91

	ms_message("Round trip to STUN: %d ms", ping_time);

92 93
	BC_ASSERT( dummy_call.ac.addr[0] != '\0');
	BC_ASSERT( dummy_call.ac.port != 0);
94
#ifdef VIDEO_ENABLED
95 96
	BC_ASSERT( dummy_call.vc.addr[0] != '\0');
	BC_ASSERT( dummy_call.vc.port != 0);
97
#endif
Sylvain Berfini's avatar
Sylvain Berfini committed
98 99
	BC_ASSERT( dummy_call.tc.addr[0] != '\0');
	BC_ASSERT( dummy_call.tc.port != 0);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
100 101 102 103

	ms_message("STUN test result: local audio port maps to %s:%i",
			dummy_call.ac.addr,
			dummy_call.ac.port);
104
#ifdef VIDEO_ENABLED
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
105 106 107
	ms_message("STUN test result: local video port maps to %s:%i",
			dummy_call.vc.addr,
			dummy_call.vc.port);
108
#endif
Sylvain Berfini's avatar
Sylvain Berfini committed
109 110 111
	ms_message("STUN test result: local text port maps to %s:%i",
			dummy_call.tc.addr,
			dummy_call.tc.port);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
112 113 114 115 116 117 118 119 120 121 122 123

	linphone_core_manager_destroy(lc_stun);
}


test_t stun_tests[] = {
	{ "Basic Stun test (Ping/public IP)", linphone_stun_test_grab_ip },
	{ "STUN encode buffer protection", linphone_stun_test_encode },
};

test_suite_t stun_test_suite = {
	"Stun",
124
	liblinphone_tester_setup,
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
125 126 127 128
	NULL,
	sizeof(stun_tests) / sizeof(stun_tests[0]),
	stun_tests
};