PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/jain-sip-ext/src/test/java/org/mobicents/ext/javax/sip/dns/DNSServerLocatorTest.java

http://mobicents.googlecode.com/
Java | 370 lines | 262 code | 54 blank | 54 comment | 0 complexity | 08a40458a8515a698919e6511d1a2678 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.ext.javax.sip.dns;
  23. import static org.junit.Assert.*;
  24. import gov.nist.javax.sip.address.AddressFactoryImpl;
  25. import gov.nist.javax.sip.stack.HopImpl;
  26. import java.text.ParseException;
  27. import java.util.HashSet;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Queue;
  31. import java.util.Set;
  32. import javax.sip.ListeningPoint;
  33. import javax.sip.address.AddressFactory;
  34. import javax.sip.address.Hop;
  35. import javax.sip.address.SipURI;
  36. import javax.sip.address.URI;
  37. import org.junit.After;
  38. import org.junit.Before;
  39. import org.junit.Test;
  40. import org.xbill.DNS.DClass;
  41. import org.xbill.DNS.NAPTRRecord;
  42. import org.xbill.DNS.Name;
  43. import org.xbill.DNS.Record;
  44. import org.xbill.DNS.SRVRecord;
  45. import org.xbill.DNS.TextParseException;
  46. import static org.mockito.Mockito.*;
  47. /**
  48. * @author jean.deruelle@gmail.com
  49. *
  50. */
  51. public class DNSServerLocatorTest {
  52. AddressFactory addressFactory;
  53. DefaultDNSServerLocator dnsServerLocator;
  54. Set<String> supportedTransports;
  55. SipURI sipURI;
  56. String host = "iptel.org";
  57. public static final String LOCALHOST = "127.0.0.1";
  58. /**
  59. * @throws java.lang.Exception
  60. */
  61. @Before
  62. public void setUp() throws Exception {
  63. addressFactory = new AddressFactoryImpl();
  64. supportedTransports = new HashSet<String>();
  65. supportedTransports.add(ListeningPoint.UDP);
  66. supportedTransports.add(ListeningPoint.TCP);
  67. dnsServerLocator = new DefaultDNSServerLocator(supportedTransports);
  68. sipURI = addressFactory.createSipURI("jean",host);
  69. }
  70. /**
  71. * @throws java.lang.Exception
  72. */
  73. @After
  74. public void tearDown() throws Exception {
  75. }
  76. /**
  77. * Test method for {@link org.mobicents.ext.javax.sip.dns.DefaultDNSServerLocator#resolveHostByDnsSrvLookup(javax.sip.address.SipURI)}.
  78. * @throws ParseException
  79. */
  80. @Test
  81. public void testGetDefaultTransportForSipUri() throws ParseException {
  82. sipURI.setPort(5080);
  83. assertEquals(ListeningPoint.UDP, dnsServerLocator.getDefaultTransportForSipUri(sipURI));
  84. sipURI.setSecure(true);
  85. assertEquals(ListeningPoint.TLS, dnsServerLocator.getDefaultTransportForSipUri(sipURI));
  86. }
  87. /**
  88. * Test method for {@link org.mobicents.ext.javax.sip.dns.DefaultDNSServerLocator#locateHops(javax.sip.address.URI)}.
  89. * @throws ParseException
  90. */
  91. @Test
  92. public void testRealExample() throws ParseException {
  93. Queue<Hop> hops = dnsServerLocator.locateHops(sipURI);
  94. assertNotNull(hops);
  95. assertTrue(hops.size() > 0);
  96. }
  97. @Test
  98. public void testResolveHostByAandAAAALookup() throws ParseException {
  99. String transport = ListeningPoint.UDP;
  100. int port = 5080;
  101. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  102. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  103. LinkedList<Hop> mockedHops = new LinkedList<Hop>();
  104. mockedHops.add(new HopImpl(LOCALHOST, port, transport));
  105. when(dnsLookupPerformer.locateHopsForNonNumericAddressWithPort(host, port, transport.toLowerCase())).thenReturn(mockedHops);
  106. sipURI.setTransportParam(transport);
  107. sipURI.setPort(port);
  108. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  109. assertNotNull(hops);
  110. assertEquals(1, hops.size());
  111. Hop hop = hops.poll();
  112. assertEquals(port, hop.getPort());
  113. assertEquals(transport, hop.getTransport());
  114. assertEquals(LOCALHOST, hop.getHost());
  115. }
  116. @Test
  117. public void testResolveHostByAandAAAALookupCheckEmpty() throws ParseException {
  118. String transport = ListeningPoint.UDP;
  119. int port = 5080;
  120. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  121. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  122. LinkedList<Hop> mockedHops = new LinkedList<Hop>();
  123. when(dnsLookupPerformer.locateHopsForNonNumericAddressWithPort(host, port, transport.toLowerCase())).thenReturn(mockedHops);
  124. sipURI.setTransportParam(transport);
  125. sipURI.setPort(port);
  126. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  127. assertNotNull(hops);
  128. assertEquals(0, hops.size());
  129. }
  130. @Test
  131. public void testResolveHostNoPortButTransportSpecified() throws ParseException, TextParseException {
  132. String transport = ListeningPoint.UDP;
  133. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  134. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  135. LinkedList<Record> mockedHops = new LinkedList<Record>();
  136. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  137. Name name = mock(Name.class);
  138. when(name.isAbsolute()).thenReturn(true);
  139. when(name.toString()).thenReturn("localhost");
  140. mockedHops.add(new SRVRecord(new Name("_sip._" + transport.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 0, 0, 5060, name));
  141. when(dnsLookupPerformer.performSRVLookup("_sip._" + transport.toLowerCase() + "." + host)).thenReturn(mockedHops);
  142. sipURI.setTransportParam(transport);
  143. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  144. assertNotNull(hops);
  145. assertTrue(hops.size() > 0);
  146. Hop hop = hops.poll();
  147. assertEquals(5060, hop.getPort());
  148. assertEquals(transport.toLowerCase(), hop.getTransport());
  149. assertEquals(LOCALHOST, hop.getHost());
  150. }
  151. @Test
  152. public void testResolveHostNoPortButTransportSpecifiedNoSRVFound() throws ParseException, TextParseException {
  153. String transport = ListeningPoint.UDP;
  154. sipURI.setHost("localhost");
  155. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  156. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  157. LinkedList<Hop> mockedHops = new LinkedList<Hop>();
  158. mockedHops.add(new HopImpl(LOCALHOST, 5060, transport));
  159. when(dnsLookupPerformer.locateHopsForNonNumericAddressWithPort("localhost", -1, transport.toLowerCase())).thenReturn(mockedHops);
  160. sipURI.setTransportParam(transport);
  161. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  162. assertNotNull(hops);
  163. assertTrue(hops.size() > 0);
  164. Hop hop = hops.poll();
  165. assertEquals(5060, hop.getPort());
  166. assertEquals(transport, hop.getTransport());
  167. assertEquals(LOCALHOST, hop.getHost());
  168. }
  169. @Test
  170. public void testResolveHostNoPortNoTransportSpecifiedNAPTRAndSRVFound() throws ParseException, TextParseException {
  171. String transport = ListeningPoint.UDP;
  172. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  173. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  174. List<NAPTRRecord> mockedNAPTRRecords = new LinkedList<NAPTRRecord>();
  175. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  176. Name name = mock(Name.class);
  177. when(name.isAbsolute()).thenReturn(true);
  178. when(name.toString()).thenReturn("localhost");
  179. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000, 0, 0, "s", "SIP+D2U", "", new Name("_sip._" + transport.toLowerCase() + "." + host + ".")));
  180. when(dnsLookupPerformer.performNAPTRLookup(host, false, supportedTransports)).thenReturn(mockedNAPTRRecords);
  181. List<Record> mockedSRVRecords = new LinkedList<Record>();
  182. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + transport.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 0, 0, 5060, name));
  183. when(dnsLookupPerformer.performSRVLookup("_sip._" + transport.toLowerCase() + "." + host + ".")).thenReturn(mockedSRVRecords);
  184. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  185. assertNotNull(hops);
  186. assertTrue(hops.size() > 0);
  187. Hop hop = hops.poll();
  188. assertEquals(5060, hop.getPort());
  189. assertEquals(transport.toLowerCase(), hop.getTransport());
  190. assertEquals(LOCALHOST, hop.getHost());
  191. }
  192. @Test
  193. public void testResolveENUM() throws ParseException, TextParseException {
  194. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  195. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  196. List<NAPTRRecord> mockedNAPTRRecords = new LinkedList<NAPTRRecord>();
  197. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  198. Name name = mock(Name.class);
  199. when(name.isAbsolute()).thenReturn(true);
  200. when(name.toString()).thenReturn("!^.*$!sip:jean@localhost!.");
  201. mockedNAPTRRecords.add(new NAPTRRecord(new Name("7.6.5.4.3.2.1.5.5.5.8.5.3.e164.arpa" + "."), DClass.IN, 1000, 0, 0, "s", "E2U+sip", "!^.*$!sip:jean@localhost!", name));
  202. when(dnsLookupPerformer.performNAPTRLookup("7.6.5.4.3.2.1.5.5.5.8.5.3.e164.arpa", false, supportedTransports)).thenReturn(mockedNAPTRRecords);
  203. URI telURI = addressFactory.createTelURL("+358-555-1234567");
  204. SipURI resolvedSipURI = dnsServerLocator.getSipURI(telURI);
  205. assertNotNull(resolvedSipURI);
  206. assertEquals("sip:jean@localhost", resolvedSipURI.toString());
  207. }
  208. /*
  209. * Non regression test for testing regex pattern in NAPTR for ENMU See http://www.ietf.org/mail-archive/web/enum/current/msg05060.html
  210. * and http://www.ietf.org/mail-archive/web/enum/current/msg05059.html
  211. */
  212. @Test
  213. public void testResolveENUMRegex() throws ParseException, TextParseException {
  214. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  215. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  216. List<NAPTRRecord> mockedNAPTRRecords = new LinkedList<NAPTRRecord>();
  217. Name name = mock(Name.class);
  218. when(name.isAbsolute()).thenReturn(true);
  219. mockedNAPTRRecords.add(new NAPTRRecord(new Name("*.6.1.4.6.5.0.5.1.3.4.e164.arpa" + "."), DClass.IN, 1000, 0, 0, "u", "E2U+sip", "!^4315056416(.*)$!sip:\\\\1@enum.at!", name));
  220. when(dnsLookupPerformer.performNAPTRLookup("3.1.6.1.4.6.5.0.5.1.3.4.e164.arpa", false, supportedTransports)).thenReturn(mockedNAPTRRecords);
  221. URI telURI = addressFactory.createTelURL("+431505641613");
  222. SipURI resolvedSipURI = dnsServerLocator.getSipURI(telURI);
  223. assertNotNull(resolvedSipURI);
  224. assertEquals("sip:431505641613@enum.at", resolvedSipURI.toString());
  225. mockedNAPTRRecords.clear();
  226. mockedNAPTRRecords.add(new NAPTRRecord(new Name("*.6.1.4.6.5.0.5.1.3.4.e164.arpa" + "."), DClass.IN, 1000, 0, 0, "u", "E2U+sip", "!^(4315056416)((.*))$!sip:\\\\2-extension-\\\\3@enum.at!", name));
  227. when(dnsLookupPerformer.performNAPTRLookup("3.1.6.1.4.6.5.0.5.1.3.4.e164.arpa", false, supportedTransports)).thenReturn(mockedNAPTRRecords);
  228. telURI = addressFactory.createTelURL("+431505641613");
  229. resolvedSipURI = dnsServerLocator.getSipURI(telURI);
  230. assertNotNull(resolvedSipURI);
  231. assertEquals("sip:4315056416-extension-13@enum.at", resolvedSipURI.toString());
  232. mockedNAPTRRecords.clear();
  233. mockedNAPTRRecords.add(new NAPTRRecord(new Name("7.1.6.8.0.2.3.5.1.2.1.e164.arpa" + "."), DClass.IN, 1000, 0, 0, "u", "E2U+sip", "!^(.*)$!sip:\\\\1@example.net!", name));
  234. when(dnsLookupPerformer.performNAPTRLookup("7.1.6.8.0.2.3.5.1.2.1.e164.arpa", false, supportedTransports)).thenReturn(mockedNAPTRRecords);
  235. telURI = addressFactory.createTelURL("+12153208617");
  236. resolvedSipURI = dnsServerLocator.getSipURI(telURI);
  237. assertNotNull(resolvedSipURI);
  238. assertEquals("sip:12153208617@example.net", resolvedSipURI.toString());
  239. }
  240. @Test
  241. public void testResolveENUMReal() throws ParseException, TextParseException {
  242. URI telURI = addressFactory.createTelURL("+437800047111");
  243. SipURI resolvedSipURI = dnsServerLocator.getSipURI(telURI);
  244. assertNotNull(resolvedSipURI);
  245. assertEquals("sip:enum-echo-test@sip.nemox.net", resolvedSipURI.toString());
  246. }
  247. @Test
  248. public void testResolveHostNoPortNoTransportSpecifiedNoNAPTRFound() throws ParseException, TextParseException {
  249. String transport = ListeningPoint.UDP;
  250. DNSLookupPerformer dnsLookupPerformer = mock(DefaultDNSLookupPerformer.class);
  251. dnsServerLocator.setDnsLookupPerformer(dnsLookupPerformer);
  252. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  253. Name name = mock(Name.class);
  254. when(name.isAbsolute()).thenReturn(true);
  255. when(name.toString()).thenReturn("localhost");
  256. List<Record> mockedSRVRecords = new LinkedList<Record>();
  257. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + transport.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 0, 0, 5060, name));
  258. List<Record> mockedSRVRecordsTCP = new LinkedList<Record>();
  259. mockedSRVRecordsTCP.add(new SRVRecord(new Name("_sip._" + "tcp" + "." + host + "."), DClass.IN, 1000L, 0, 0, 5060, name));
  260. when(dnsLookupPerformer.performSRVLookup("_sip._" + transport.toLowerCase() + "." + host)).thenReturn(mockedSRVRecords);
  261. when(dnsLookupPerformer.performSRVLookup("_sip._" + "tcp" + "." + host)).thenReturn(mockedSRVRecordsTCP);
  262. Queue<Hop> hops = dnsServerLocator.resolveHostByDnsSrvLookup(sipURI);
  263. assertNotNull(hops);
  264. assertTrue(hops.size() > 0);
  265. Hop hop = hops.poll();
  266. assertEquals(5060, hop.getPort());
  267. assertEquals(transport.toLowerCase(), hop.getTransport());
  268. assertEquals(LOCALHOST, hop.getHost());
  269. }
  270. @Test
  271. public void testNAPTRComparator() throws TextParseException {
  272. List<NAPTRRecord> mockedNAPTRRecords = new LinkedList<NAPTRRecord>();
  273. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  274. Name name = mock(Name.class);
  275. when(name.isAbsolute()).thenReturn(true);
  276. when(name.toString()).thenReturn("localhost");
  277. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000L, 90, 50, "s", "SIP+D2T", "", new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + ".")));
  278. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000L, 100, 50, "s", "SIP+D2U", "", new Name("_sip._" + ListeningPoint.UDP.toLowerCase() + "." + host + ".")));
  279. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000L, 50, 50, "s", "SIPS+D2T", "", new Name("_sips._" + ListeningPoint.TLS.toLowerCase() + "." + host + ".")));
  280. // Sorting the records
  281. java.util.Collections.sort(mockedNAPTRRecords, new NAPTRRecordComparator());
  282. assertEquals("SIPS+D2T", mockedNAPTRRecords.get(0).getService());
  283. assertEquals("SIP+D2T", mockedNAPTRRecords.get(1).getService());
  284. assertEquals("SIP+D2U", mockedNAPTRRecords.get(2).getService());
  285. }
  286. @Test
  287. //Issue http://code.google.com/p/mobicents/issues/detail?id=3143
  288. public void testNAPTRPrefComparator() throws TextParseException {
  289. List<NAPTRRecord> mockedNAPTRRecords = new LinkedList<NAPTRRecord>();
  290. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  291. Name name = mock(Name.class);
  292. when(name.isAbsolute()).thenReturn(true);
  293. when(name.toString()).thenReturn("localhost");
  294. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000L, 90, 50, "s", "SIP+D2T", "", new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + ".")));
  295. mockedNAPTRRecords.add(new NAPTRRecord(new Name(host + "."), DClass.IN, 1000L, 90, 40, "s", "SIP+D2U", "", new Name("_sip._" + ListeningPoint.UDP.toLowerCase() + "." + host + ".")));
  296. // Sorting the records
  297. java.util.Collections.sort(mockedNAPTRRecords, new NAPTRRecordComparator());
  298. assertEquals("SIP+D2U", mockedNAPTRRecords.get(0).getService());
  299. assertEquals("SIP+D2T", mockedNAPTRRecords.get(1).getService());
  300. }
  301. @Test
  302. public void testSRVComparator() throws TextParseException {
  303. // mocking the name because localhost is not absolute and localhost. cannot be resolved
  304. List<SRVRecord> mockedSRVRecords = new LinkedList<SRVRecord>();
  305. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 1, 9, 5060, new Name("old-slow-box.example.com.")));
  306. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 1, 10, 5060, new Name("old2-slow-box.example.com.")));
  307. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 3, 9, 5060, new Name("new-fast2-box.example.com.")));
  308. mockedSRVRecords.add(new SRVRecord(new Name("_sip._" + ListeningPoint.TCP.toLowerCase() + "." + host + "."), DClass.IN, 1000L, 3, 9, 5060, new Name( "new-fast-box.example.com.")));
  309. // Sorting the records
  310. java.util.Collections.sort(mockedSRVRecords, new SRVRecordComparator());
  311. assertEquals("old2-slow-box.example.com.", mockedSRVRecords.get(0).getTarget().toString());
  312. assertEquals("old-slow-box.example.com.", mockedSRVRecords.get(1).getTarget().toString());
  313. assertEquals("new-fast-box.example.com.", mockedSRVRecords.get(2).getTarget().toString());
  314. assertEquals("new-fast2-box.example.com.", mockedSRVRecords.get(3).getTarget().toString());
  315. }
  316. }