]> sjero.net Git - linphone/blob - p2pproxy/test-src/org/linphone/p2pproxy/test/P2pProxyTester.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / test-src / org / linphone / p2pproxy / test / P2pProxyTester.java
1 /*
2 p2pproxy Copyright (C) 2007  Jehan Monnier ()
3
4 P2pProxyTester.java - junit test for p2pproxy
5
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.
10
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.
15
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.
19 */
20 package org.linphone.p2pproxy.test;
21
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;
27
28
29
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;
35
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;
56
57 import junit.framework.Assert;
58 import junit.framework.TestCase;
59
60 public class P2pProxyTester extends TestCase {
61
62
63    private final static Logger mLog = Logger.getLogger(P2pProxyTester.class);
64
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;
73
74     public P2pProxyTester() {
75        
76        }   
77         protected void setUp() throws Exception {
78         
79                 if (mP2pProxyInstance == null) {
80                   // setup logging
81                System.setProperty("org.linphone.p2pproxy.home", ".");
82                P2pProxyMain.staticLoadTraceConfigFile(); 
83                //PropertyConfigurator.configure("log4j.properties");
84            setupJxta(); 
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());
89
90            try {
91               mP2pProxyAccountManagement.createAccount(mDefaultSipIdentity);
92             } catch (Exception e) {
93               mLog.warn(e);
94            }           
95             try {
96                mP2pProxyAccountManagement.createAccount(mCallerUri);
97              } catch (Exception e) {
98                mLog.warn(e);
99             }
100
101              try {
102                 mP2pProxyAccountManagement.createAccount(mCalleeUri);
103               } catch (Exception e) {
104                 mLog.warn(e);
105              }
106                 }
107                 
108         }
109
110         protected void tearDown() throws Exception {
111         }
112
113         public void testStunClient() {
114            try {
115 //            if (mP2pProxyInstance2 == null) {
116 //               try {
117 //                  setupJxta2();
118 //               } catch (Exception e) {
119 //                  mLog.error("cannot start peer2");
120 //               }
121 //            }
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());
129            }
130
131         }
132         public void testGetRegistrarAdress() {
133            try {
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());
139            }
140
141         }
142         public void testSipRegisterUnregister() {
143                 try {
144                         //register
145            mSipClient.register();
146                         //unregister
147            mSipClient.unRegister();
148                         mLog.info("testSipRegisterUnregister ok");
149                 } catch (Exception e) {
150                         mLog.error("testSipRegisterUnregister ko",e);
151                         Assert.fail(e.getMessage());
152                 }
153                 
154         }
155     public void testRegisterUnknownUser() {
156         try {
157             //register
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());
163         }
164         
165     }    
166 //      public void testPipeDiscovery() {
167 //              try {
168 //           long lDiscoveryTimout = 10000;
169 //                      //register
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() {
174 //
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();
182 //                              }
183 //                              
184 //                      };
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();
188 //                      
189 //                      //unregister
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());
195 //              }
196 //              
197 //      }
198     public void testCall() {
199        try {
200           Call(false);
201        } catch (Exception e) {
202           mLog.error("testCall ko",e);
203           Assert.fail(e.getMessage());
204       }       
205        
206     }
207     public void testCallWithSdp() {
208        try {
209           Call(true);
210        } catch (Exception e) {
211           mLog.error("testCallWithSdp ko",e);
212           Assert.fail(e.getMessage());
213       }       
214        
215     }
216
217     private void Call(boolean useSdp) throws Exception {
218                         //register
219             mSipClient.register(900,mCallerUri);
220             mSipClient.register(900,mCalleeUri);
221             call(mCallerUri,mCalleeUri,useSdp);
222                         // unregister
223             mSipClient.register(0,mCallerUri);
224             mSipClient.register(0,mCalleeUri);
225
226                         mLog.info("testCall ok");
227
228         }
229     public void testUserNotFound() {
230         try {
231             long lTimout = (long) (RDV_DISCOVERY_TIMEOUT * 2);
232             //register
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());
239             }
240             };
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));
244             // unregister
245             mSipClient.register(0,mCallerUri);
246
247             mLog.info("testUserNotFound ok");
248         } catch (Exception e) {
249             mLog.error("testUserNotFound ko",e);
250             Assert.fail(e.getMessage());
251         }       
252     }
253     public void testUserUnregisterred() {
254        try {
255            long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
256            //register
257            mSipClient.register(900,mCallerUri);
258            mSipClient.register(900,mCalleeUri);
259            //unregister callee
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());
266            }
267            };
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));
271            // unregister caller
272            mSipClient.register(0,mCallerUri);
273
274            mLog.info("testUserNotFound ok");
275        } catch (Exception e) {
276            mLog.error("testUserNotFound ko",e);
277            Assert.fail(e.getMessage());
278        }       
279    }
280     public void xxxCallCancelledBeforeDialogEstablishement() {
281        try {
282           //Assert.fail("not debugged yet");
283            long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
284           //register
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();         
292                 }                
293
294           };
295           Call  lCaller = new Call(mProvider, mCallerUri, mSipClient.getContact(mProvider), lCallerListener);
296           lCaller.call(mCalleeUri);
297           Thread.sleep(1000);
298           lCaller.cancel();
299           Assert.assertTrue("caller  call not canceled until ["+lTimout+"]", lCallerSemaphoreCancel.tryAcquire(lTimout,TimeUnit.MILLISECONDS));
300           // unregister
301           mSipClient.register(0,mCallerUri);
302           
303           mLog.info("testCallCancelledBeforeDialogEstablishement ok");
304        } catch (Exception e) {
305           mLog.error("testCallCancelledBeforeDialogEstablishement ko",e);
306           Assert.fail(e.getMessage());
307        }       
308     }
309     
310     public void testCallCancelledAfterRinging() {
311         try {
312             long lTimout = RDV_DISCOVERY_TIMEOUT * 2;
313             //register
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() {
320
321                 public void onCallRinging(Call call, Message resp) {
322                     lCallerSemaphoreRinging.release();
323                 }
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();         
327                    }                
328  
329             };
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();
335                     call.ring();
336                 }
337                 public void onCallCanceling(Call call, Message cancel) {
338                    lCalleeSemaphoreCanceling.release();
339                }
340
341             };
342             Call  lCallee = new Call(mProvider, mCalleeUri, mSipClient.getContact(mProvider), lCalleeListener);
343             lCallee.listen();
344             lCaller.call(mCalleeUri);
345
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));
348             lCaller.cancel();
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));
351             
352             // unregister
353            mSipClient.register(0,mCallerUri);
354            mSipClient.register(0,mCalleeUri);
355
356             mLog.info("testCallCancelledAfterRinging ok");
357         } catch (Exception e) {
358             mLog.error("testCallCancelledAfterRinging ko",e);
359             Assert.fail(e.getMessage());
360         }       
361
362    }
363     public void testAlreadyRegister() {
364         Assert.fail("not implemented yet");
365     }
366     public void testReRegisterAfterExpired() {
367         try {
368              //register
369            mSipClient.register(10,mCallerUri);
370            mSipClient.register(10,mCalleeUri);
371            call(mCallerUri,mCalleeUri);
372            // wait
373            Thread.sleep(10000);
374            mSipClient.register(20,mCallerUri);
375            mSipClient.register(20,mCalleeUri);
376            
377            call(mCallerUri,mCalleeUri);
378
379            // unregister
380            mSipClient.register(0,mCallerUri);
381            mSipClient.register(0,mCalleeUri);
382
383             mLog.info("testReRegisterAfterExpired ok");
384         } catch (Exception e) {
385             mLog.error("testtestReRegisterAfterExpired ko",e);
386             Assert.fail(e.getMessage());
387         }       
388
389     
390     }
391     public void testReRegisterBeforeExpired() {
392        try {
393            //register
394           mSipClient.register(900,mCallerUri);
395           mSipClient.register(900,mCalleeUri);
396           
397           call(mCallerUri,mCalleeUri);
398           //re-register
399           mSipClient.register(900,mCallerUri);
400           mSipClient.register(900,mCalleeUri);
401           
402           call(mCallerUri,mCalleeUri);
403
404           // unregister
405           mSipClient.register(0,mCallerUri);
406           mSipClient.register(0,mCalleeUri);
407
408            mLog.info("testReRegisterBeforeExpired ok");
409        } catch (Exception e) {
410            mLog.error("testReRegisterBeforeExpired ko",e);
411            Assert.fail(e.getMessage());
412        }       
413
414    
415    }
416
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);
424         }
425
426         private void call(String aCaller,String aCallee) throws Exception {
427            call(aCaller,aCallee,false);
428         }
429         
430         private void call(String aCaller,String aCallee,boolean useSdp) throws Exception {
431            long lTimout = 1000;
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);
439               }
440               public void onCallClosing(Call call, Message bye) {
441                  //nop
442               }
443               public void onCallRinging(Call call, Message resp) {
444                  lCallerSemaphoreRinging.release();
445               }
446            };
447            Call  lCaller = new Call(mProvider, aCaller, mSipClient.getContact(mProvider), lCallerListener);
448        if (useSdp) {
449           lCaller.setLocalSessionDescriptor(SipClient.sdp_offer);
450        }
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();
456                  call.bye();
457               }
458               public void onCallIncoming(Call call, NameAddress callee, NameAddress caller, String sdp, Message invite) {
459                  lCalleeSemaphoreIncoming.release();
460                  call.accept(sdp);
461               }
462               public void onCallClosed(Call call, Message resp) {
463                  lCalleeSemaphoreClosed.release();
464               }
465            };
466            Call  lCallee = new Call(mProvider, aCallee, mSipClient.getContact(mProvider), lCalleeListener);
467        if (useSdp) {
468           lCallee.setLocalSessionDescriptor(SipClient.sdp_offer);
469        }
470            lCallee.listen();
471            lCaller.call(aCallee);
472            
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));
477            
478         }
479         public void testBunchOfCall() {
480                 for (int i=0;i<20;i++) {
481                         //testCallCancelledBeforeDialogEstablishement();
482                         testCall();
483                         testCallCancelledAfterRinging();
484                 }
485         }
486
487 }