2 p2pproxy Copyright (C) 2007 Jehan Monnier ()
4 P2pProxyTester.java - junit test for p2pproxy
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package org.linphone.p2pproxy.test;
22 import java.net.DatagramSocket;
23 import java.util.Enumeration;
24 import java.util.List;
25 import java.util.concurrent.Semaphore;
26 import java.util.concurrent.TimeUnit;
30 import net.jxta.discovery.DiscoveryEvent;
31 import net.jxta.discovery.DiscoveryListener;
32 import net.jxta.discovery.DiscoveryService;
33 import net.jxta.document.Advertisement;
34 import net.jxta.protocol.DiscoveryResponseMsg;
36 import org.apache.log4j.Logger;
37 import org.apache.log4j.PropertyConfigurator;
38 import org.linphone.p2pproxy.api.P2pProxyInstance;
39 import org.linphone.p2pproxy.api.P2pProxyInstance.Mode;
40 import org.linphone.p2pproxy.core.JxtaNetworkManager;
41 import org.linphone.p2pproxy.core.P2pProxyAccountManagement;
42 import org.linphone.p2pproxy.core.P2pProxyAccountManagementMBean;
43 import org.linphone.p2pproxy.core.P2pProxyInstanceImpl;
44 import org.linphone.p2pproxy.core.P2pProxyMain;
45 import org.linphone.p2pproxy.core.sipproxy.NetworkResourceAdvertisement;
46 import org.linphone.p2pproxy.core.sipproxy.SipProxyRegistrar;
47 import org.linphone.p2pproxy.core.stun.AddressInfo;
48 import org.linphone.p2pproxy.core.stun.StunClient;
49 import org.linphone.p2pproxy.test.utils.DefaultCallListener;
50 import org.linphone.p2pproxy.test.utils.SipClient;
51 import org.zoolu.sip.address.NameAddress;
52 import org.zoolu.sip.call.Call;
53 import org.zoolu.sip.call.CallListener;
54 import org.zoolu.sip.message.Message;
55 import org.zoolu.sip.provider.SipProvider;
57 import junit.framework.Assert;
58 import junit.framework.TestCase;
60 public class P2pProxyTester extends TestCase {
63 private final static Logger mLog = Logger.getLogger(P2pProxyTester.class);
65 static private SipProvider mProvider;
66 static final String mDefaultSipIdentity = "sip:p2pTester@linphone.org";
67 static private SipClient mSipClient;
68 static private P2pProxyInstance mP2pProxyInstance;
69 final private String mCallerUri = "sip:caller@linphone.org";
70 final private String mCalleeUri = "sip:callee@linphone.org";
71 final private int RDV_DISCOVERY_TIMEOUT = 5000;
72 static private P2pProxyAccountManagementMBean mP2pProxyAccountManagement;
74 public P2pProxyTester() {
77 protected void setUp() throws Exception {
79 if (mP2pProxyInstance == null) {
81 System.setProperty("org.linphone.p2pproxy.home", ".");
82 P2pProxyMain.staticLoadTraceConfigFile();
83 //PropertyConfigurator.configure("log4j.properties");
85 mProvider = mP2pProxyInstance.getSipClientProvider() ;
86 mSipClient = new SipClient(mProvider, mDefaultSipIdentity,RDV_DISCOVERY_TIMEOUT*2);
87 // create account for user mDefaultSipIdentity, mCallerUri, mCalleeUri if not exist
88 mP2pProxyAccountManagement = new P2pProxyAccountManagement((JxtaNetworkManager)mP2pProxyInstance.getOpaqueNetworkManager());
91 mP2pProxyAccountManagement.createAccount(mDefaultSipIdentity);
92 } catch (Exception e) {
96 mP2pProxyAccountManagement.createAccount(mCallerUri);
97 } catch (Exception e) {
102 mP2pProxyAccountManagement.createAccount(mCalleeUri);
103 } catch (Exception e) {
110 protected void tearDown() throws Exception {
113 public void testStunClient() {
115 // if (mP2pProxyInstance2 == null) {
118 // } catch (Exception e) {
119 // mLog.error("cannot start peer2");
122 DatagramSocket lDatagramSocket = new DatagramSocket();
123 StunClient lStunClient = new StunClient((JxtaNetworkManager)mP2pProxyInstance.getOpaqueNetworkManager());
124 AddressInfo lAddressInfo = lStunClient.computeAddressInfo(lDatagramSocket);
125 mLog.info("AddressInfo ["+lAddressInfo+"]");
126 }catch (Exception e) {
127 mLog.error("testStunClient ko",e);
128 Assert.fail(e.getMessage());
132 public void testGetRegistrarAdress() {
134 NetworkResourceAdvertisement lSipProxyRegistrarAdvertisement = (NetworkResourceAdvertisement) (((JxtaNetworkManager)mP2pProxyInstance.getOpaqueNetworkManager()).getAdvertisement(null,SipProxyRegistrar.ADV_NAME, true));
135 mLog.info("testGetRegistrarAdress ok ["+lSipProxyRegistrarAdvertisement.getAddress()+"]");
136 } catch (Exception e) {
137 mLog.error("testGetRegistrarAdress ko",e);
138 Assert.fail(e.getMessage());
142 public void testSipRegisterUnregister() {
145 mSipClient.register();
147 mSipClient.unRegister();
148 mLog.info("testSipRegisterUnregister ok");
149 } catch (Exception e) {
150 mLog.error("testSipRegisterUnregister ko",e);
151 Assert.fail(e.getMessage());
155 public void testRegisterUnknownUser() {
158 Assert.assertFalse(mSipClient.register(900,"sip:toto@linphone.org",404));
159 mLog.info("testRegisterUnknownUser ok");
160 } catch (Exception e) {
161 mLog.error("testRegisterUnknownUser ko",e);
162 Assert.fail(e.getMessage());
166 // public void testPipeDiscovery() {
168 // long lDiscoveryTimout = 10000;
170 // mSipClient.register(900,mDefaultSipIdentity);
171 // DiscoveryService lDiscoveryService = ((JxtaNetworkManager)mP2pProxyInstance.getOpaqueNetworkManager()).getPeerGroup().getDiscoveryService();
172 // final Semaphore lSemaphore = new Semaphore(0);
173 // DiscoveryListener lDiscoveryListener = new DiscoveryListener() {
175 // public void discoveryEvent(DiscoveryEvent event) {
176 // DiscoveryResponseMsg LRes = event.getResponse();
177 // Enumeration lAdvertisementLists = LRes.getAdvertisements();
178 // Advertisement lAdvertisement = (Advertisement) lAdvertisementLists.nextElement();
179 // //Assert.assertEquals("not the good adv name", lContact, lAdvertisement.)
180 // mLog.info(lAdvertisement.toString());
181 // lSemaphore.release();
185 // lDiscoveryService.getRemoteAdvertisements(null, DiscoveryService.ADV, "Name",mDefaultSipIdentity, 1,lDiscoveryListener);
186 // Assert.assertTrue("pipe not found until ["+lDiscoveryTimout+"]", lSemaphore.tryAcquire(lDiscoveryTimout,TimeUnit.MILLISECONDS));
187 // lSemaphore.release();
190 // mSipClient.register(0,mDefaultSipIdentity);
191 // mLog.info("testPipeDiscovery ok");
192 // } catch (Exception e) {
193 // mLog.error("testPipeDiscovery ko",e);
194 // Assert.fail(e.getMessage());
198 public void testCall() {
201 } catch (Exception e) {
202 mLog.error("testCall ko",e);
203 Assert.fail(e.getMessage());
207 public void testCallWithSdp() {
210 } catch (Exception e) {
211 mLog.error("testCallWithSdp ko",e);
212 Assert.fail(e.getMessage());
217 private void Call(boolean useSdp) throws Exception {
219 mSipClient.register(900,mCallerUri);
220 mSipClient.register(900,mCalleeUri);
221 call(mCallerUri,mCalleeUri,useSdp);
223 mSipClient.register(0,mCallerUri);
224 mSipClient.register(0,mCalleeUri);
226 mLog.info("testCall ok");
229 public void testUserNotFound() {
231 long lTimout = (long) (RDV_DISCOVERY_TIMEOUT * 2);
233 mSipClient.register(900,mCallerUri);
234 final Semaphore lCallerSemaphoreRefused = new Semaphore(0);
235 CallListener lCallerListener = new DefaultCallListener() {
236 public void onCallRefused(Call call, String reason, Message resp) {
237 lCallerSemaphoreRefused.release();
238 Assert.assertEquals("bad reason, must be user not found", 404, resp.getStatusLine().getCode());
241 Call lCaller = new Call(mProvider, mCallerUri, mSipClient.getContact(mProvider), lCallerListener);
242 lCaller.call(mCalleeUri);
243 Assert.assertTrue("caller call not refused until ["+lTimout+"]", lCallerSemaphoreRefused.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
245 mSipClient.register(0,mCallerUri);
247 mLog.info("testUserNotFound ok");
248 } catch (Exception e) {
249 mLog.error("testUserNotFound ko",e);
250 Assert.fail(e.getMessage());
253 public void testUserUnregisterred() {
255 long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
257 mSipClient.register(900,mCallerUri);
258 mSipClient.register(900,mCalleeUri);
260 mSipClient.register(0,mCalleeUri);
261 final Semaphore lCallerSemaphoreRefused = new Semaphore(0);
262 CallListener lCallerListener = new DefaultCallListener() {
263 public void onCallRefused(Call call, String reason, Message resp) {
264 lCallerSemaphoreRefused.release();
265 Assert.assertEquals("bad reason, must be user not found", 404, resp.getStatusLine().getCode());
268 Call lCaller = new Call(mProvider, mCallerUri, mSipClient.getContact(mProvider), lCallerListener);
269 lCaller.call(mCalleeUri);
270 Assert.assertTrue("caller call not refused until ["+lTimout+"]", lCallerSemaphoreRefused.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
272 mSipClient.register(0,mCallerUri);
274 mLog.info("testUserNotFound ok");
275 } catch (Exception e) {
276 mLog.error("testUserNotFound ko",e);
277 Assert.fail(e.getMessage());
280 public void xxxCallCancelledBeforeDialogEstablishement() {
282 //Assert.fail("not debugged yet");
283 long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
285 mSipClient.register(900,mCallerUri);
286 //mSipClient.register(900,mCalleeUri);
287 final Semaphore lCallerSemaphoreCancel = new Semaphore(0);
288 CallListener lCallerListener = new DefaultCallListener() {
289 public void onCallRefused(Call call, String reason, Message resp) {
290 Assert.assertEquals("bad reason, must be Request Terminated", 487, resp.getStatusLine().getCode());
291 lCallerSemaphoreCancel.release();
295 Call lCaller = new Call(mProvider, mCallerUri, mSipClient.getContact(mProvider), lCallerListener);
296 lCaller.call(mCalleeUri);
299 Assert.assertTrue("caller call not canceled until ["+lTimout+"]", lCallerSemaphoreCancel.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
301 mSipClient.register(0,mCallerUri);
303 mLog.info("testCallCancelledBeforeDialogEstablishement ok");
304 } catch (Exception e) {
305 mLog.error("testCallCancelledBeforeDialogEstablishement ko",e);
306 Assert.fail(e.getMessage());
310 public void testCallCancelledAfterRinging() {
312 long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
314 mSipClient.register(900,mCallerUri);
315 mSipClient.register(900,mCalleeUri);
316 final Semaphore lCalleeSemaphoreCanceling = new Semaphore(0);
317 final Semaphore lCallerSemaphoreRinging = new Semaphore(0);
318 final Semaphore lCallerSemaphoreCanceled = new Semaphore(0);
319 CallListener lCallerListener = new DefaultCallListener() {
321 public void onCallRinging(Call call, Message resp) {
322 lCallerSemaphoreRinging.release();
324 public void onCallRefused(Call call, String reason, Message resp) {
325 Assert.assertEquals("bad reason, must be Request Terminated", 487, resp.getStatusLine().getCode());
326 lCallerSemaphoreCanceled.release();
330 Call lCaller = new Call(mProvider, mCallerUri, mSipClient.getContact(mProvider), lCallerListener);
331 final Semaphore lCalleeSemaphoreIncoming = new Semaphore(0);
332 CallListener lCalleeListener = new DefaultCallListener() {
333 public void onCallIncoming(Call call, NameAddress callee, NameAddress caller, String sdp, Message invite) {
334 lCalleeSemaphoreIncoming.release();
337 public void onCallCanceling(Call call, Message cancel) {
338 lCalleeSemaphoreCanceling.release();
342 Call lCallee = new Call(mProvider, mCalleeUri, mSipClient.getContact(mProvider), lCalleeListener);
344 lCaller.call(mCalleeUri);
346 Assert.assertTrue("callee not alerted until ["+lTimout+"]", lCalleeSemaphoreIncoming.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
347 Assert.assertTrue("callee call not ringing until ["+lTimout+"]", lCallerSemaphoreRinging.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
349 Assert.assertTrue("caller call not canceling until ["+lTimout+"]", lCalleeSemaphoreCanceling.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
350 Assert.assertTrue("caller call not refused until ["+lTimout+"]", lCallerSemaphoreCanceled.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
353 mSipClient.register(0,mCallerUri);
354 mSipClient.register(0,mCalleeUri);
356 mLog.info("testCallCancelledAfterRinging ok");
357 } catch (Exception e) {
358 mLog.error("testCallCancelledAfterRinging ko",e);
359 Assert.fail(e.getMessage());
363 public void testAlreadyRegister() {
364 Assert.fail("not implemented yet");
366 public void testReRegisterAfterExpired() {
369 mSipClient.register(10,mCallerUri);
370 mSipClient.register(10,mCalleeUri);
371 call(mCallerUri,mCalleeUri);
374 mSipClient.register(20,mCallerUri);
375 mSipClient.register(20,mCalleeUri);
377 call(mCallerUri,mCalleeUri);
380 mSipClient.register(0,mCallerUri);
381 mSipClient.register(0,mCalleeUri);
383 mLog.info("testReRegisterAfterExpired ok");
384 } catch (Exception e) {
385 mLog.error("testtestReRegisterAfterExpired ko",e);
386 Assert.fail(e.getMessage());
391 public void testReRegisterBeforeExpired() {
394 mSipClient.register(900,mCallerUri);
395 mSipClient.register(900,mCalleeUri);
397 call(mCallerUri,mCalleeUri);
399 mSipClient.register(900,mCallerUri);
400 mSipClient.register(900,mCalleeUri);
402 call(mCallerUri,mCalleeUri);
405 mSipClient.register(0,mCallerUri);
406 mSipClient.register(0,mCalleeUri);
408 mLog.info("testReRegisterBeforeExpired ok");
409 } catch (Exception e) {
410 mLog.error("testReRegisterBeforeExpired ko",e);
411 Assert.fail(e.getMessage());
417 private void setupJxta() throws Exception {
418 mP2pProxyInstance = new P2pProxyInstanceImpl();
419 mP2pProxyInstance.setMode(Mode.seeding_server);
420 mP2pProxyInstance.setIndex(1);
421 mP2pProxyInstance.setProperty(JxtaNetworkManager.ADV_DISCOVERY_TIMEOUT, String.valueOf(RDV_DISCOVERY_TIMEOUT));
422 mP2pProxyInstance.start();
423 while (mP2pProxyInstance.isStarted() == false) Thread.sleep(500);
426 private void call(String aCaller,String aCallee) throws Exception {
427 call(aCaller,aCallee,false);
430 private void call(String aCaller,String aCallee,boolean useSdp) throws Exception {
432 final Semaphore lCallerSemaphoreAccepted = new Semaphore(0);
433 final Semaphore lCalleeSemaphoreClosed = new Semaphore(0);
434 final Semaphore lCallerSemaphoreRinging = new Semaphore(0);
435 CallListener lCallerListener = new DefaultCallListener() {
436 public void onCallAccepted(Call call, String sdp, Message resp) {
437 lCallerSemaphoreAccepted.release();
438 call.ackWithAnswer(sdp);
440 public void onCallClosing(Call call, Message bye) {
443 public void onCallRinging(Call call, Message resp) {
444 lCallerSemaphoreRinging.release();
447 Call lCaller = new Call(mProvider, aCaller, mSipClient.getContact(mProvider), lCallerListener);
449 lCaller.setLocalSessionDescriptor(SipClient.sdp_offer);
451 final Semaphore lCalleeSemaphoreConfirmed = new Semaphore(0);
452 final Semaphore lCalleeSemaphoreIncoming = new Semaphore(0);
453 CallListener lCalleeListener = new DefaultCallListener() {
454 public void onCallConfirmed(Call call, String sdp, Message ack) {
455 lCalleeSemaphoreConfirmed.release();
458 public void onCallIncoming(Call call, NameAddress callee, NameAddress caller, String sdp, Message invite) {
459 lCalleeSemaphoreIncoming.release();
462 public void onCallClosed(Call call, Message resp) {
463 lCalleeSemaphoreClosed.release();
466 Call lCallee = new Call(mProvider, aCallee, mSipClient.getContact(mProvider), lCalleeListener);
468 lCallee.setLocalSessionDescriptor(SipClient.sdp_offer);
471 lCaller.call(aCallee);
473 Assert.assertTrue("callee not alerted until ["+lTimout+"]", lCalleeSemaphoreIncoming.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
474 Assert.assertTrue("caller call not accepted until ["+lTimout+"]", lCallerSemaphoreAccepted.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
475 Assert.assertTrue("callee call not confirmed until ["+lTimout+"]", lCalleeSemaphoreConfirmed.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
476 Assert.assertTrue("caller call not closed until ["+lTimout+"]", lCalleeSemaphoreClosed.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
479 public void testBunchOfCall() {
480 for (int i=0;i<20;i++) {
481 //testCallCancelledBeforeDialogEstablishement();
483 testCallCancelledAfterRinging();