PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/servers/diameter/testsuite/tests/src/test/java/org/mobicents/slee/resources/diameter/tests/framework/validator/AvpUtilitiesTest.java

http://mobicents.googlecode.com/
Java | 709 lines | 308 code | 54 blank | 347 comment | 46 complexity | e0f2c76b1d19a691cb1a1ae7aca3840e 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.slee.resources.diameter.tests.framework.validator;
  23. import static org.jdiameter.client.impl.helpers.Parameters.*;
  24. import static org.jdiameter.server.impl.helpers.Parameters.*;
  25. import static org.junit.Assert.fail;
  26. import java.io.InputStream;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.concurrent.TimeUnit;
  30. import net.java.slee.resource.diameter.base.events.avp.AvpNotAllowedException;
  31. import net.java.slee.resource.diameter.base.events.avp.AvpUtilities;
  32. import net.java.slee.resource.diameter.base.events.avp.DiameterAvpType;
  33. import org.jdiameter.api.Avp;
  34. import org.jdiameter.api.AvpSet;
  35. import org.jdiameter.api.Mode;
  36. import org.jdiameter.api.Session;
  37. import org.jdiameter.api.Stack;
  38. import org.jdiameter.api.validation.Dictionary;
  39. import org.jdiameter.client.impl.DictionarySingleton;
  40. import org.jdiameter.client.impl.helpers.EmptyConfiguration;
  41. import org.junit.After;
  42. import org.junit.Before;
  43. import org.junit.Test;
  44. import org.mobicents.diameter.dictionary.AvpDictionary;
  45. import org.mobicents.slee.resource.diameter.base.DiameterMessageFactoryImpl;
  46. import org.mobicents.slee.resource.diameter.base.events.AccountingRequestImpl;
  47. import org.mobicents.slee.resource.diameter.base.events.DiameterMessageImpl;
  48. import org.mobicents.slee.resource.diameter.base.events.avp.DiameterAvpImpl;
  49. /**
  50. *
  51. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  52. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  53. */
  54. public class AvpUtilitiesTest {
  55. private static String clientHost = "127.0.0.1";
  56. private static String clientPort = "23868";
  57. private static String clientURI = "aaa://" + clientHost + ":" + clientPort;
  58. private static String serverHost = "127.0.0.1";
  59. private static String serverPort = "33868";
  60. private static String serverURI = "aaa://" + serverHost + ":" + serverPort;
  61. private static String realmName = "mobicentsXYZ.org";
  62. private static DiameterMessageFactoryImpl baseFactory;
  63. private Dictionary instance = null;
  64. private static Stack stack = null;
  65. private static Stack serverStack = null;
  66. private final static String validatorOnFile = "dictionary.xml";
  67. //private final static String validatorOffFile = "validatorOff.xml";
  68. static {
  69. stack = new org.jdiameter.client.impl.StackImpl();
  70. serverStack = new org.jdiameter.client.impl.StackImpl();
  71. InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(validatorOnFile);
  72. try {
  73. MyConfigurationClient clientConf = new MyConfigurationClient();
  74. MyConfigurationServer serverConf = new MyConfigurationServer();
  75. System.out.println("[SERVER] Configured. Starting ...");
  76. serverStack.init(serverConf);
  77. serverStack.start();
  78. Thread.sleep(2000);
  79. System.out.println("[SERVER] Started!");
  80. System.out.println("[CLIENT] Configured. Starting ...");
  81. stack.init(clientConf);
  82. // conf dict, after stack.
  83. DictionarySingleton.getDictionary().configure(is);
  84. stack.start(Mode.ANY_PEER, 5000, TimeUnit.MILLISECONDS);
  85. System.out.println("[CLIENT] Started");
  86. }
  87. catch (Exception e) {
  88. throw new RuntimeException("Failed to initialize the stack.", e);
  89. }
  90. finally {
  91. if (is != null) {
  92. try {
  93. is.close();
  94. }
  95. catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. }
  100. baseFactory = new DiameterMessageFactoryImpl(stack);
  101. // DiameterAvpFactoryImpl baseAvpFactory = new DiameterAvpFactoryImpl();
  102. try {
  103. AvpDictionary.INSTANCE.parseDictionary(AvpUtilitiesTest.class.getClassLoader().getResourceAsStream(validatorOnFile));
  104. }
  105. catch (Exception e) {
  106. throw new RuntimeException("Failed to parse dictionary file.");
  107. }
  108. }
  109. @Before
  110. public void setUp() {
  111. this.instance = DictionarySingleton.getDictionary();
  112. }
  113. @After
  114. public void tearDown() {
  115. this.instance = null;
  116. }
  117. /* ammendonca: removal allowed is gone...
  118. @Test
  119. public void testOperationsAddWithValidatorOnAndRemovalAllowed() {
  120. instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
  121. instance.setEnabled(true);
  122. // It has session id
  123. AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest();
  124. // <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
  125. AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
  126. // <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
  127. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
  128. // <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
  129. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
  130. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
  131. // <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
  132. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
  133. // <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
  134. // AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
  135. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverHost);
  136. // <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
  137. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
  138. String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
  139. Session localSession = null;
  140. try {
  141. localSession = stack.getSessionFactory().getNewSession(sessionId);
  142. sendMessage(localSession, request);
  143. System.out.println(request);
  144. fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
  145. }
  146. catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
  147. if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
  148. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  149. + ").");
  150. }
  151. }
  152. catch (Exception e) {
  153. Throwable cause = e;
  154. boolean wasAvpNotAllowed = false;
  155. while ((cause = cause.getCause()) != null) {
  156. if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
  157. wasAvpNotAllowed = true;
  158. org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
  159. if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
  160. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":"
  161. + exc.getVendorId() + ").");
  162. }
  163. }
  164. }
  165. if (!wasAvpNotAllowed) {
  166. fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
  167. }
  168. }
  169. // <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
  170. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
  171. // In here we send the message actually. It is OK!
  172. try {
  173. sendMessage(localSession, request);
  174. }
  175. catch (Exception e) {
  176. fail("Failed to send message when it should succeed! Exception: " + e);
  177. }
  178. // <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
  179. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
  180. // Again, in here we send the message actually. It is OK!
  181. try {
  182. sendMessage(localSession, request);
  183. }
  184. catch (Exception e) {
  185. fail("Failed to send message when it should succeed! Exception: " + e);
  186. }
  187. // <!-- FORBBIDEN -->
  188. // <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
  189. try {
  190. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
  191. // Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
  192. sendMessage(localSession, request);
  193. fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
  194. }
  195. catch (AvpNotAllowedException e) {
  196. if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
  197. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  198. + ").");
  199. }
  200. }
  201. catch (Exception e) {
  202. fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
  203. }
  204. // This is just in case
  205. Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
  206. ExpectedAvp a = new ExpectedAvp();
  207. a.code = 263;
  208. a.count = 1;
  209. expectedAvps.put(a, a);
  210. a = new ExpectedAvp();
  211. a.code = 264;
  212. a.count = 1;
  213. expectedAvps.put(a, a);
  214. a = new ExpectedAvp();
  215. a.code = 296;
  216. a.count = 1;
  217. expectedAvps.put(a, a);
  218. a = new ExpectedAvp();
  219. a.code = 283;
  220. a.count = 1;
  221. expectedAvps.put(a, a);
  222. a = new ExpectedAvp();
  223. a.code = 480;
  224. a.count = 1;
  225. expectedAvps.put(a, a);
  226. a = new ExpectedAvp();
  227. a.code = 485;
  228. a.count = 1;
  229. expectedAvps.put(a, a);
  230. a = new ExpectedAvp();
  231. a.code = 259;
  232. a.count = 1;
  233. expectedAvps.put(a, a);
  234. a = new ExpectedAvp();
  235. a.code = 293;
  236. a.count = 1;
  237. expectedAvps.put(a, a);
  238. testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
  239. }
  240. */
  241. @Test
  242. public void testOperationsAddWithValidatorOnAndRemovalNotAllowed() {
  243. instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOnFile));
  244. instance.setEnabled(true);
  245. // It has session id
  246. AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest(new DiameterAvpImpl[] { new DiameterAvpImpl(263, 0L, 0, 1,
  247. "xxx".getBytes(), DiameterAvpType.UTF8_STRING) });
  248. // <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
  249. try {
  250. AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
  251. fail("Session-Id can not be set twice.");
  252. }
  253. catch (AvpNotAllowedException e) {
  254. if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
  255. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  256. + ").");
  257. }
  258. }
  259. catch (Exception e) {
  260. fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
  261. }
  262. // <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
  263. if (!request.hasOriginHost())
  264. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
  265. // <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
  266. if (!request.hasOriginRealm())
  267. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
  268. // <avp name="Destination-Host" code="293" vendor="0" multiplicity="0-1" index="-1"/>
  269. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
  270. // <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
  271. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
  272. // <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
  273. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
  274. String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
  275. Session localSession = null;
  276. try {
  277. localSession = stack.getSessionFactory().getNewSession(sessionId);
  278. sendMessage(localSession, request);
  279. fail("Should not send this message. Message MUST contain Accounting-Record-Number AVP (485), and it is not present.");
  280. }
  281. catch (org.jdiameter.api.validation.AvpNotAllowedException e) {
  282. if (e.getAvpCode() != 485 && e.getVendorId() != 0) {
  283. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  284. + ").");
  285. }
  286. }
  287. catch (Exception e) {
  288. Throwable cause = e;
  289. boolean wasAvpNotAllowed = false;
  290. while ((cause = cause.getCause()) != null) {
  291. if (cause instanceof org.jdiameter.api.validation.AvpNotAllowedException) {
  292. wasAvpNotAllowed = true;
  293. org.jdiameter.api.validation.AvpNotAllowedException exc = (org.jdiameter.api.validation.AvpNotAllowedException) cause;
  294. if (exc.getAvpCode() != 485 && exc.getVendorId() != 0) {
  295. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (485:0), Received (" + exc.getAvpCode() + ":"
  296. + exc.getVendorId() + ").");
  297. }
  298. }
  299. }
  300. if (!wasAvpNotAllowed) {
  301. fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
  302. }
  303. }
  304. // <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
  305. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
  306. // In here we send the message actually. It is OK!
  307. try {
  308. sendMessage(localSession, request);
  309. }
  310. catch (Exception e) {
  311. fail("Failed to send message when it should succeed! Exception: " + e);
  312. }
  313. // Message should already come with Acct-Application-Id.. but still..
  314. try {
  315. // <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
  316. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
  317. }
  318. catch (AvpNotAllowedException e) {
  319. if (e.getAvpCode() != 259 && e.getVendorId() != 0) {
  320. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (259:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  321. + ").");
  322. }
  323. }
  324. // Again, in here we send the message actually. It is OK!
  325. try {
  326. sendMessage(localSession, request);
  327. }
  328. catch (Exception e) {
  329. fail("Failed to send message when it should succeed! Exception: " + e);
  330. }
  331. // <!-- FORBBIDEN -->
  332. // <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
  333. try {
  334. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
  335. // Now send should fail. Message has AVP "Auth-Application-Id", not valid in ACR.
  336. sendMessage(localSession, request);
  337. fail("Should not send this message. Message MUST NOT contain Auth-Application-Id AVP (258), and it is present.");
  338. }
  339. catch (AvpNotAllowedException e) {
  340. if (e.getAvpCode() != 258 && e.getVendorId() != 0) {
  341. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (258:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  342. + ").");
  343. }
  344. }
  345. catch (Exception e) {
  346. fail("Message failed to be sent for wrong reason. Expected AvpNotAllowedException, Received " + e);
  347. }
  348. // Just in case.
  349. Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
  350. ExpectedAvp a = new ExpectedAvp();
  351. a.code = 263;
  352. a.count = 1;
  353. expectedAvps.put(a, a);
  354. a = new ExpectedAvp();
  355. a.code = 264;
  356. a.count = 1;
  357. expectedAvps.put(a, a);
  358. a = new ExpectedAvp();
  359. a.code = 296;
  360. a.count = 1;
  361. expectedAvps.put(a, a);
  362. a = new ExpectedAvp();
  363. a.code = 283;
  364. a.count = 1;
  365. expectedAvps.put(a, a);
  366. a = new ExpectedAvp();
  367. a.code = 480;
  368. a.count = 1;
  369. expectedAvps.put(a, a);
  370. a = new ExpectedAvp();
  371. a.code = 485;
  372. a.count = 1;
  373. expectedAvps.put(a, a);
  374. a = new ExpectedAvp();
  375. a.code = 259;
  376. a.count = 1;
  377. expectedAvps.put(a, a);
  378. a = new ExpectedAvp();
  379. a.code = 293;
  380. a.count = 1;
  381. expectedAvps.put(a, a);
  382. testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
  383. }
  384. /* ammendonca: removal allowed is gone...
  385. @Test
  386. public void testOperationsAddWithValidatorOffAndRemovalAllowed() {
  387. instance.configure(this.getClass().getClassLoader().getResourceAsStream(validatorOffFile));
  388. instance.setEnabled(false);
  389. // It has session id
  390. AccountingRequestImpl request = (AccountingRequestImpl) baseFactory.createAccountingRequest(new DiameterAvpImpl[] { new DiameterAvpImpl(263, 0L, 0, 1,
  391. "xxx".getBytes(), DiameterAvpType.UTF8_STRING) });
  392. // <avp name="Session-Id" code="263" vendor="0" multiplicity="1" index="0"/>
  393. AvpUtilities.setAvpAsUTF8String(request.getGenericData(), 263, request.getGenericData().getAvps(), "1346ferg5y");
  394. // <avp name="Origin-Host" code="264" vendor="0" multiplicity="1" index="-1"/>
  395. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 264, request.getGenericData().getAvps(), clientURI);
  396. // <avp name="Origin-Realm" code="296" vendor="0" multiplicity="1" index="-1"/>
  397. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
  398. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 296, request.getGenericData().getAvps(), realmName);
  399. // <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
  400. // We don't add this one, make it fail
  401. // <avp name="Destination-Host" code="293" vendor="0" multiplicity="0" index="-1" />
  402. // AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverURI);
  403. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 293, request.getGenericData().getAvps(), serverHost);
  404. // <avp name="Accounting-Record-Type" code="480" vendor="0" multiplicity="1" index="-1"/>
  405. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 480, request.getGenericData().getAvps(), 1);
  406. String sessionId = AvpUtilities.getAvpAsUTF8String(263, request.getGenericData().getAvps());
  407. Session localSession = null;
  408. try {
  409. localSession = stack.getSessionFactory().getNewSession(sessionId);
  410. sendMessage(localSession, request);
  411. // this should fail eve so, but just in case
  412. fail("Should not send this message. Message MUST contain Destination-Realm AVP (283), and it is not present.");
  413. }
  414. catch (RouteException e) {
  415. // We want to come here, since we lack Destination-Realm AVP (283).
  416. }
  417. catch (Exception e) {
  418. e.printStackTrace();
  419. fail("Message failed to be sent for wrong reason. Expected RouteException due to lack of Destination-Realm AVP (283), Received " + e);
  420. }
  421. // <avp name="Destination-Realm" code="283" vendor="0" multiplicity="1" index="-1"/>
  422. AvpUtilities.setAvpAsOctetString(request.getGenericData(), 283, request.getGenericData().getAvps(), realmName);
  423. // <avp name="Accounting-Record-Number" code="485" vendor="0" multiplicity="1" index="-1"/>
  424. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 485, request.getGenericData().getAvps(), 1);
  425. // In here we send the message actually. It is OK!
  426. try {
  427. sendMessage(localSession, request);
  428. }
  429. catch (Exception e) {
  430. fail("Failed to send message when it should succeed! Exception: " + e);
  431. }
  432. int expectedAcctApplicationIdAvps = request.hasAcctApplicationId() ? 1 : 0;
  433. // Message should already come with Acct-Application-Id.. but still..
  434. try {
  435. // <avp name="Acct-Application-Id" code="259" vendor="0" multiplicity="0-1" index="-1"/>
  436. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 259, request.getGenericData().getAvps(), 1);
  437. expectedAcctApplicationIdAvps++;
  438. }
  439. catch (AvpNotAllowedException e) {
  440. if (e.getAvpCode() != 259 && e.getVendorId() != 0) {
  441. fail("Message Validation failed with wrong AVP Code/Vendor-Id in Exception. Expected (259:0), Received (" + e.getAvpCode() + ":" + e.getVendorId()
  442. + ").");
  443. }
  444. else {
  445. fail("Wrong AVP not allowed to be added, with Validator in OFF state. It's smart, but not what we want.");
  446. }
  447. }
  448. // Again, in here we send the message actually. It is OK!
  449. try {
  450. sendMessage(localSession, request);
  451. }
  452. catch (Exception e) {
  453. fail("Failed to send message when it should succeed! Exception: " + e);
  454. }
  455. // <!-- FORBBIDEN -->
  456. // <avp name="Auth-Application-Id" code="258" vendor="0" multiplicity="0" index="-1"/>
  457. try {
  458. AvpUtilities.setAvpAsUnsigned32(request.getGenericData(), 258, request.getGenericData().getAvps(), 1);
  459. // this should fail eve so, but just in case
  460. }
  461. catch (AvpNotAllowedException e) {
  462. fail("Wrong AVP not allowed to be added, with Validator in OFF state. It's smart, but not what we want.");
  463. }
  464. catch (Exception e) {
  465. fail("Failed to add AVP, when it should succeed, even being a not allowed AVP.");
  466. }
  467. // This is just in case
  468. Map<ExpectedAvp, ExpectedAvp> expectedAvps = new HashMap<ExpectedAvp, ExpectedAvp>();
  469. ExpectedAvp a = new ExpectedAvp();
  470. a.code = 263;
  471. a.count = 2;
  472. expectedAvps.put(a, a);
  473. a = new ExpectedAvp();
  474. a.code = 264;
  475. a.count = 2; // was 1 but request comes with one already...
  476. expectedAvps.put(a, a);
  477. a = new ExpectedAvp();
  478. a.code = 296;
  479. // cause its legal in this case.
  480. a.count = 3; // was 2 but request comes with one already...
  481. expectedAvps.put(a, a);
  482. a = new ExpectedAvp();
  483. a.code = 283;
  484. a.count = 1;
  485. expectedAvps.put(a, a);
  486. a = new ExpectedAvp();
  487. a.code = 480;
  488. a.count = 1;
  489. expectedAvps.put(a, a);
  490. a = new ExpectedAvp();
  491. a.code = 485;
  492. a.count = 1;
  493. expectedAvps.put(a, a);
  494. a = new ExpectedAvp();
  495. a.code = 259;
  496. a.count = expectedAcctApplicationIdAvps;
  497. expectedAvps.put(a, a);
  498. // yes, its legal also. we dont check
  499. a = new ExpectedAvp();
  500. a.code = 258;
  501. a.count = 1;
  502. expectedAvps.put(a, a);
  503. a = new ExpectedAvp();
  504. a.code = 293;
  505. a.count = 1;
  506. expectedAvps.put(a, a);
  507. testPresentAvps(request.getGenericData().getAvps(), expectedAvps);
  508. }
  509. */
  510. private void testPresentAvps(AvpSet set, Map<ExpectedAvp, ExpectedAvp> expected) {
  511. for (ExpectedAvp e : expected.values()) {
  512. AvpSet innerSet = set.removeAvp(e.code);
  513. if (innerSet.size() != e.count) {
  514. fail("Wrong count of avps, code: " + e.code + ", vendor:" + e.vendor + ". Expected: " + e.count + ", present: " + innerSet.size());
  515. }
  516. if (e.count > 0) {
  517. Avp avp = innerSet.getAvpByIndex(0);
  518. if (avp.getVendorId() != e.vendor) {
  519. fail("Wrong vendor of avp, code: " + e.code + ". Expected: " + e.vendor + ", present: " + avp.getVendorId());
  520. }
  521. }
  522. }
  523. if (set.size() > 0) {
  524. StringBuffer buf = new StringBuffer();
  525. for (Avp a : set) {
  526. buf.append("Code[").append(a.getCode()).append("] Vendor[").append("], ");
  527. }
  528. fail("Wrong count of avps, removed all expected, left overs: " + set.size() + " -- " + buf.toString());
  529. }
  530. }
  531. private void sendMessage(Session localSession, DiameterMessageImpl message) throws Exception {
  532. localSession.send(message.getGenericData());
  533. Thread.sleep(1000);
  534. }
  535. /**
  536. * Class representing the Diameter Configuration
  537. */
  538. public static class MyConfigurationClient extends EmptyConfiguration {
  539. public MyConfigurationClient() {
  540. super();
  541. add(Assembler, Assembler.defValue());
  542. add(OwnDiameterURI, clientURI);
  543. add(OwnRealm, realmName);
  544. add(OwnVendorID, 193L);
  545. // Set Ericsson SDK feature
  546. // add(UseUriAsFqdn, true);
  547. // Set Common Applications
  548. add(ApplicationId,
  549. // AppId 1
  550. getInstance().add(VendorId, 0L).add(AuthApplId, 0L).add(AcctApplId, 3L));
  551. // Set peer table
  552. add(PeerTable,
  553. // Peer 1
  554. getInstance().add(PeerRating, 1).add(PeerName, serverURI).add(PeerAttemptConnection, true));
  555. // Set realm table
  556. add(RealmTable,
  557. // Realm 1
  558. getInstance()
  559. .add(
  560. RealmEntry,
  561. getInstance().add(RealmName, realmName).add(ApplicationId, getInstance().add(VendorId, 0L).add(AuthApplId, 0L).add(AcctApplId, 3L))
  562. .add(RealmHosts, clientHost + ", " + serverHost).add(RealmLocalAction, "LOCAL").add(RealmEntryIsDynamic, false)
  563. .add(RealmEntryExpTime, 1000L)));
  564. }
  565. }
  566. /**
  567. * Class representing the Diameter Configuration
  568. */
  569. public static class MyConfigurationServer extends org.jdiameter.server.impl.helpers.EmptyConfiguration {
  570. public MyConfigurationServer() {
  571. super();
  572. add(Assembler, Assembler.defValue());
  573. add(OwnDiameterURI, serverURI);
  574. add(OwnRealm, realmName);
  575. add(OwnVendorID, 193L);
  576. // Set Ericsson SDK feature
  577. // add(UseUriAsFqdn, true);
  578. // Set Common Applications
  579. add(ApplicationId,
  580. // AppId 1
  581. getInstance().add(VendorId, 0L).add(AuthApplId, 0L).add(AcctApplId, 3L));
  582. // Set peer table
  583. add(PeerTable,
  584. // Peer 1
  585. getInstance().add(PeerRating, 1).add(PeerAttemptConnection, false).add(PeerName, clientURI));
  586. // Set realm table
  587. add(RealmTable,
  588. // Realm 1
  589. getInstance()
  590. .add(
  591. RealmEntry,
  592. getInstance().add(RealmName, realmName).add(ApplicationId, getInstance().add(VendorId, 0L).add(AuthApplId, 0L).add(AcctApplId, 3L))
  593. .add(RealmHosts, clientHost + ", " + serverHost).add(RealmLocalAction, "LOCAL").add(RealmEntryIsDynamic, false)
  594. .add(RealmEntryExpTime, 1000L)));
  595. }
  596. }
  597. }
  598. class ExpectedAvp {
  599. int code = 0;
  600. long vendor = 0;
  601. int count = 0;
  602. @Override
  603. public int hashCode() {
  604. final int prime = 31;
  605. int result = 1;
  606. result = prime * result + code;
  607. result = prime * result + (int) (vendor ^ (vendor >>> 32));
  608. return result;
  609. }
  610. @Override
  611. public boolean equals(Object obj) {
  612. if (this == obj)
  613. return true;
  614. if (obj == null)
  615. return false;
  616. if (getClass() != obj.getClass())
  617. return false;
  618. ExpectedAvp other = (ExpectedAvp) obj;
  619. if (code != other.code)
  620. return false;
  621. if (vendor != other.vendor)
  622. return false;
  623. return true;
  624. }
  625. }