/protocols/ss7/isup/isup-impl/src/test/java/org/mobicents/protocols/ss7/isup/impl/message/parameter/ParameterHarness.java

http://mobicents.googlecode.com/ · Java · 266 lines · 177 code · 46 blank · 43 comment · 34 complexity · f8efd86fe21780c01ca7073bbe4be12f MD5 · raw file

  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. /**
  23. * Start time:09:16:42 2009-04-22<br>
  24. * Project: mobicents-isup-stack<br>
  25. *
  26. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski
  27. * </a>
  28. *
  29. */
  30. package org.mobicents.protocols.ss7.isup.impl.message.parameter;
  31. import java.io.IOException;
  32. import java.lang.reflect.Method;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.List;
  36. import static org.testng.Assert.*;
  37. import org.testng.*;
  38. import org.testng.annotations.*;
  39. import org.mobicents.protocols.ss7.isup.ParameterException;
  40. /**
  41. * Start time:09:16:42 2009-04-22<br>
  42. * Project: mobicents-isup-stack<br>
  43. *
  44. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski
  45. * </a>
  46. */
  47. public abstract class ParameterHarness {
  48. // 21 10000011 Address....................... 83
  49. // 22 01100000 Address....................... 60
  50. // 23 00111000 Address....................... 38
  51. // NOTE: now see how nice digits swap can come out with conversion, lol
  52. private final static byte[] sixDigits = new byte[] { (byte) 0x83, 0x60, 0x38 };
  53. private final static byte[] fiveDigits = new byte[] { (byte) 0x83, 0x60, 0x08 };
  54. private final static byte[] sevenDigits = new byte[] { (byte) 0x83, 0x60,0x33, 0x08 };
  55. private final static byte[] eightDigits = new byte[] { (byte) 0x83, 0x60,0x33, 0x48 };
  56. private final static byte[] threeDigits = new byte[] { (byte) 0x83, 0x0 };;
  57. private final static String sixDigitsString = "380683";
  58. private final static String fiveDigitsString = "38068";
  59. private final static String sevenDigitsString = "3806338";
  60. private final static String eightDigitsString = "38063384";
  61. private final static String threeDigitsString = "380";
  62. // FIXME: add code to check values :)
  63. protected List<byte[]> goodBodies = new ArrayList<byte[]>();
  64. protected List<byte[]> badBodies = new ArrayList<byte[]>();
  65. protected String makeCompare(byte[] hardcodedBody, byte[] elementEncoded) {
  66. int totalLength = 0;
  67. if (hardcodedBody == null || elementEncoded == null) {
  68. return "One arg is null";
  69. }
  70. if (hardcodedBody.length >= elementEncoded.length) {
  71. totalLength = hardcodedBody.length;
  72. } else {
  73. totalLength = elementEncoded.length;
  74. }
  75. String out = "";
  76. for (int index = 0; index < totalLength; index++) {
  77. if (hardcodedBody.length > index) {
  78. out += "hardcodedBody[" + Integer.toHexString(hardcodedBody[index]) + "]";
  79. } else {
  80. out += "hardcodedBody[NOP]";
  81. }
  82. if (elementEncoded.length > index) {
  83. out += "elementEncoded[" + Integer.toHexString(elementEncoded[index]) + "]";
  84. } else {
  85. out += "elementEncoded[NOP]";
  86. }
  87. out += "\n";
  88. }
  89. return out;
  90. }
  91. public String makeCompare(int[] hardcodedBody, int[] elementEncoded) {
  92. int totalLength = 0;
  93. if (hardcodedBody == null || elementEncoded == null) {
  94. return "One arg is null";
  95. }
  96. if (hardcodedBody.length >= elementEncoded.length) {
  97. totalLength = hardcodedBody.length;
  98. } else {
  99. totalLength = elementEncoded.length;
  100. }
  101. String out = "";
  102. for (int index = 0; index < totalLength; index++) {
  103. if (hardcodedBody.length > index) {
  104. out += "hardcodedBody[" + Integer.toHexString(hardcodedBody[index]) + "]";
  105. } else {
  106. out += "hardcodedBody[NOP]";
  107. }
  108. if (elementEncoded.length > index) {
  109. out += "elementEncoded[" + Integer.toHexString(elementEncoded[index]) + "]";
  110. } else {
  111. out += "elementEncoded[NOP]";
  112. }
  113. out += "\n";
  114. }
  115. return out;
  116. }
  117. @Test(groups = { "functional.encode","functional.decode","parameter"})
  118. public void testDecodeEncode() throws IOException, ParameterException {
  119. for (int index = 0; index < this.goodBodies.size(); index++) {
  120. byte[] goodBody = this.goodBodies.get(index);
  121. AbstractISUPParameter component = this.getTestedComponent();
  122. doTestDecode(goodBody, true, component, index);
  123. byte[] encodedBody = component.encode();
  124. boolean equal = Arrays.equals(goodBody, encodedBody);
  125. assertTrue(equal,"Body index: " + index + "\n" + makeCompare(goodBody, encodedBody));
  126. }
  127. for (int index = 0; index < this.badBodies.size(); index++) {
  128. byte[] badBody = this.badBodies.get(index);
  129. AbstractISUPParameter component = this.getTestedComponent();
  130. doTestDecode(badBody, false, component, index);
  131. byte[] encodedBody = component.encode();
  132. //TODO: make some tests here?
  133. }
  134. }
  135. public abstract AbstractISUPParameter getTestedComponent() throws ParameterException;
  136. protected void doTestDecode(byte[] presumableBody, boolean shouldPass, AbstractISUPParameter component, int index) throws ParameterException {
  137. try {
  138. component.decode(presumableBody);
  139. if (!shouldPass) {
  140. fail("Decoded[" + index + "] parameter[" + component.getClass() + "], should not pass. Passed data: " + dumpData(presumableBody));
  141. }
  142. } catch (IllegalArgumentException iae) {
  143. if (shouldPass) {
  144. fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "], should not happen. " + iae + ".Passed data: " + dumpData(presumableBody));
  145. iae.printStackTrace();
  146. }
  147. } catch (ParameterException iae) {
  148. if (shouldPass) {
  149. fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "], should not happen. " + iae + ".Passed data: " + dumpData(presumableBody));
  150. throw iae;
  151. }
  152. }
  153. catch (Exception e) {
  154. fail("Failed to decode[" + index + "] parameter[" + component.getClass() + "]." + e + ". Passed data: " + dumpData(presumableBody));
  155. e.printStackTrace();
  156. }
  157. }
  158. protected String dumpData(byte[] b) {
  159. String s = "\n";
  160. for (byte bb : b) {
  161. s += Integer.toHexString(bb);
  162. }
  163. return s;
  164. }
  165. public void testValues(AbstractISUPParameter component, String[] getterMethodNames, Object[] expectedValues) {
  166. try {
  167. Class cClass = component.getClass();
  168. for (int index = 0; index < getterMethodNames.length; index++) {
  169. Method m = cClass.getMethod(getterMethodNames[index], null);
  170. // Should not be null by now
  171. Object v = m.invoke(component, null);
  172. if (v == null && expectedValues != null) {
  173. fail("Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index] + " is null, but test values is not.");
  174. }
  175. if(expectedValues[index].getClass().isArray() )
  176. {
  177. assertTrue(Arrays.deepEquals(new Object[]{expectedValues[index]},new Object[]{ v}),"Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index]);
  178. }else
  179. {
  180. assertEquals( v,expectedValues[index],"Failed to validate values in component: " + component.getClass().getName() + ". Value of: " + getterMethodNames[index]);
  181. }
  182. }
  183. } catch (Exception e) {
  184. fail("Failed to check values on component: " + component.getClass().getName() + ", due to: " + e);
  185. e.printStackTrace();
  186. }
  187. }
  188. public static byte[] getSixDigits() {
  189. return sixDigits;
  190. }
  191. public static byte[] getFiveDigits() {
  192. return fiveDigits;
  193. }
  194. public static byte[] getThreeDigits() {
  195. return threeDigits;
  196. }
  197. public static byte[] getSevenDigits()
  198. {
  199. return sevenDigits;
  200. }
  201. public static byte[] getEightDigits()
  202. {
  203. return eightDigits;
  204. }
  205. public static String getSixDigitsString() {
  206. return sixDigitsString;
  207. }
  208. public static String getFiveDigitsString() {
  209. return fiveDigitsString;
  210. }
  211. public static String getThreeDigitsString() {
  212. return threeDigitsString;
  213. }
  214. public static String getSevenDigitsString() {
  215. return sevenDigitsString;
  216. }
  217. public static String getEightDigitsString() {
  218. return eightDigitsString;
  219. }
  220. }