PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/asn/asn-impl/src/test/java/org/mobicents/protocols/asn/AsnOutputStreamTest.java

http://mobicents.googlecode.com/
Java | 541 lines | 319 code | 69 blank | 153 comment | 2 complexity | 867538cd538baf467f1340890abe9961 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. package org.mobicents.protocols.asn;
  2. import java.io.ByteArrayOutputStream;
  3. import java.util.Arrays;
  4. import junit.framework.TestCase;
  5. import org.junit.After;
  6. import org.junit.AfterClass;
  7. import org.junit.Before;
  8. import org.junit.BeforeClass;
  9. import org.junit.Test;
  10. /**
  11. *
  12. * @author amit bhayani
  13. * @author baranowb
  14. * @author sergey vetyutnev
  15. */
  16. public class AsnOutputStreamTest extends TestCase {
  17. private AsnOutputStream output;
  18. @BeforeClass
  19. public static void setUpClass() throws Exception {
  20. }
  21. @AfterClass
  22. public static void tearDownClass() throws Exception {
  23. }
  24. @Before
  25. public void setUp() throws Exception {
  26. this.output = new AsnOutputStream();
  27. }
  28. @After
  29. public void tearDown() {
  30. this.output = null;
  31. }
  32. private void compareArrays(byte[] expected, byte[] encoded) {
  33. boolean same = Arrays.equals(expected, encoded);
  34. assertTrue("byte[] dont match, expected|encoded \n"
  35. + Arrays.toString(expected) + "\n" + Arrays.toString(encoded),
  36. same);
  37. }
  38. @Test
  39. public void testTag() throws Exception {
  40. byte[] expected = new byte[] { (byte)0xBF, (byte)0x87, (byte)0x68 };
  41. this.output.reset();
  42. this.output.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, 1000);
  43. byte[] encodedData = this.output.toByteArray();
  44. compareArrays(expected, encodedData);
  45. }
  46. @Test
  47. public void testContentLength() throws Exception {
  48. // primitive, contentLength field length = 1 byte
  49. byte[] expected = new byte[] { (byte)0x81, 3, 1, 2, 3 };
  50. this.output.reset();
  51. this.output.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, true, 1);
  52. int i1 = this.output.StartContentDefiniteLength();
  53. this.output.write(1);
  54. this.output.write(2);
  55. this.output.write(3);
  56. this.output.FinalizeContent(i1);
  57. byte[] encodedData = this.output.toByteArray();
  58. compareArrays(expected, encodedData);
  59. // constructed, contentLength field length = 3 byte
  60. byte[] content = new byte[400];
  61. Arrays.fill(content, (byte)22);
  62. content[0] = 33;
  63. content[399] = 33;
  64. expected = new byte[4 + 400];
  65. expected[0] = (byte)0xA1;
  66. expected[1] = (byte)0x82;
  67. expected[2] = (byte)(400 >> 8);
  68. expected[3] = (byte)(400 & 0xFF);
  69. System.arraycopy(content, 0, expected, 4, 400);
  70. this.output.reset();
  71. this.output.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, 1);
  72. i1 = this.output.StartContentDefiniteLength();
  73. this.output.write(content);
  74. this.output.FinalizeContent(i1);
  75. encodedData = this.output.toByteArray();
  76. compareArrays(expected, encodedData);
  77. // constructed, contentLength field in indefinite form
  78. expected = new byte[] { (byte)0xA1, (byte)0x80, 1, 2, 3, 0, 0 };
  79. this.output.reset();
  80. this.output.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, 1);
  81. i1 = this.output.StartContentIndefiniteLength();
  82. this.output.write(1);
  83. this.output.write(2);
  84. this.output.write(3);
  85. this.output.FinalizeContent(i1);
  86. encodedData = this.output.toByteArray();
  87. compareArrays(expected, encodedData);
  88. }
  89. @Test
  90. public void testNULL() throws Exception {
  91. byte[] expected = new byte[] { 0x05, 0 };
  92. this.output.writeNull();
  93. byte[] encodedData = this.output.toByteArray();
  94. compareArrays(expected, encodedData);
  95. }
  96. @Test
  97. public void testBoolean() throws Exception {
  98. // T L V
  99. byte[] expected = new byte[] { 0x01, 0x01, (byte) 0xFF };
  100. this.output.writeBoolean(true);
  101. byte[] encodedData = this.output.toByteArray();
  102. compareArrays(expected, encodedData);
  103. // T L V
  104. this.output.reset();
  105. expected = new byte[] { 0x01, 0x01, 0x00 };
  106. this.output.writeBoolean(false);
  107. encodedData = this.output.toByteArray();
  108. compareArrays(expected, encodedData);
  109. }
  110. @Test
  111. public void testInteger() throws Exception {
  112. byte[] expected = new byte[] { 0x02, 0x01, 0x48 };
  113. this.output.writeInteger(Tag.CLASS_UNIVERSAL, Tag.INTEGER, 72);
  114. byte[] encodedData = this.output.toByteArray();
  115. compareArrays(expected, encodedData);
  116. this.output.reset();
  117. expected = new byte[] { 0x02, 0x01, 0x7F };
  118. this.output.writeTag(Tag.CLASS_UNIVERSAL, true, Tag.INTEGER);
  119. int i1 = this.output.StartContentDefiniteLength();
  120. this.output.writeIntegerData(127);
  121. this.output.FinalizeContent(i1);
  122. encodedData = this.output.toByteArray();
  123. compareArrays(expected, encodedData);
  124. // T L V
  125. this.output.reset();
  126. expected = new byte[] { 0x02, 0x01, (byte) 0x80 };
  127. this.output.writeInteger(-128);
  128. encodedData = this.output.toByteArray();
  129. compareArrays(expected, encodedData);
  130. // T L V -------------
  131. this.output.reset();
  132. expected = new byte[] { 0x02, 0x02, 0x00, (byte) 0x80 };
  133. this.output.writeInteger(128);
  134. encodedData = this.output.toByteArray();
  135. compareArrays(expected, encodedData);
  136. // Test -ve integer -65536
  137. this.output.reset();
  138. byte[] b = this.intToByteArray(-65536);
  139. expected = new byte[] { 0x2, 0x3, b[1], b[2], b[3] };
  140. this.output.writeInteger(-65536);
  141. encodedData = this.output.toByteArray();
  142. compareArrays(expected, encodedData);
  143. // Test +ve integer 797979
  144. this.output.reset();
  145. b = this.intToByteArray(797979);
  146. expected = new byte[] { 0x2, 0x3, b[1], b[2], b[3] };
  147. this.output.writeInteger(797979);
  148. encodedData = this.output.toByteArray();
  149. compareArrays(expected, encodedData);
  150. }
  151. private byte[] intToByteArray(int value) {
  152. System.out.println("binary value = " + Integer.toBinaryString(value));
  153. byte[] b = new byte[4];
  154. for (int i = 0; i < 4; i++) {
  155. int offset = (b.length - 1 - i) * 8;
  156. b[i] = (byte) ((value >>> offset) & 0xFF);
  157. System.out.println("byte for " + i + " is " + b[i]);
  158. }
  159. return b;
  160. }
  161. @Test
  162. public void testRealBinary118_625() throws Exception { // s E M
  163. // 118.625 ---- 0 10000000101 1101 10101000 00000000 00000000 00000000
  164. // 00000000 00000000
  165. // T L V: info bits, exp(2), mantisa(7)
  166. byte[] expected = new byte[] { 0x09, 0x0A, (byte) 0x81, 0x04, 0x05,
  167. 0x0D, (byte) 0xA8, 0x00, 0x00, 0x00, 0x00, 0x00 };
  168. this.output.writeReal(118.625d);
  169. byte[] encoded = this.output.toByteArray();
  170. compareArrays(expected, encoded);
  171. }
  172. @Test
  173. public void testRealBinary_118_625() throws Exception { // s E M
  174. // 118.625 ---- 1 10000000101 1101 10101000 00000000 00000000 00000000
  175. // 00000000 00000000
  176. // T L V: info bits, exp(2), mantisa(7)
  177. byte[] expected = new byte[] { 0x09, 0x0A, (byte) (0x81 | 0x40), 0x04,
  178. 0x05, 0x0D, (byte) 0xA8, 0x00, 0x00, 0x00, 0x00, 0x00 };
  179. this.output.writeReal(-118.625d);
  180. byte[] encoded = this.output.toByteArray();
  181. compareArrays(expected, encoded);
  182. }
  183. @Test
  184. public void testRealBinary0() throws Exception {
  185. // T L V - no V :)
  186. byte[] expected = new byte[] { 0x09, 0x00 };
  187. this.output.writeReal(0);
  188. byte[] encoded = this.output.toByteArray();
  189. compareArrays(expected, encoded);
  190. }
  191. @Test
  192. public void testRealBinary_NEG_INFINITY() throws Exception {
  193. // T L V
  194. byte[] expected = new byte[] { 0x09, 0x01, 0x41 };
  195. this.output.writeReal(Double.NEGATIVE_INFINITY);
  196. byte[] encoded = this.output.toByteArray();
  197. compareArrays(expected, encoded);
  198. }
  199. @Test
  200. public void testRealBinary_POS_INFINITY() throws Exception {
  201. // T L V
  202. byte[] expected = new byte[] { 0x09, 0x01, 0x40 };
  203. this.output.writeReal(Double.POSITIVE_INFINITY);
  204. byte[] encoded = this.output.toByteArray();
  205. compareArrays(expected, encoded);
  206. }
  207. @Test
  208. public void testReal10_Basic() throws Exception {
  209. try {
  210. this.output.writeReal("3", BERStatics.REAL_NR1 - 1);
  211. fail();
  212. } catch (AsnException asne) {
  213. }
  214. try {
  215. this.output.writeReal("3", BERStatics.REAL_NR3 + 1);
  216. fail();
  217. } catch (AsnException asne) {
  218. }
  219. try {
  220. this.output.writeReal("x3", BERStatics.REAL_NR3);
  221. fail();
  222. } catch (NumberFormatException e) {
  223. }
  224. try {
  225. this.output.writeReal("3x", BERStatics.REAL_NR3);
  226. fail();
  227. } catch (NumberFormatException e) {
  228. }
  229. }
  230. @Test
  231. public void testReal10() throws Exception {
  232. // we actually dont check NR, its responsibility of other
  233. // its base10 are encoded as strings... ech, we dont check encoded
  234. // string... should we?
  235. String[] digs = new String[] { " 0004902", " +0004902", " -4902",
  236. "4902.00", "4902.", ".5", " 0.3E-04", "-2.8E+000000",
  237. " 000004.50000E123456789", "+5.6e+03", "+0.56E+4" };
  238. for (int index = 0; index < digs.length; index++) {
  239. double d = Double.parseDouble(digs[index]);
  240. // NR should change, but its responsiblity of user.
  241. this.output.writeReal(digs[index], BERStatics.REAL_NR1);
  242. byte[] encoded = this.output.toByteArray();
  243. // this will "clear" array.
  244. this.output.reset();
  245. AsnInputStream asnIs = new AsnInputStream(encoded);
  246. asnIs.readTag();
  247. double dd = asnIs.readReal();
  248. assertEquals("Decoded value is not proper!!", d, dd);
  249. }
  250. }
  251. @Test
  252. public void testBitString_Short() throws Exception {
  253. // 11110000 11110000 111101xx //0x0F accoring to book...
  254. byte[] expected = new byte[] { 0x03, 0x04, 0x02, (byte) 0xF0,
  255. (byte) 0xF0, (byte) 0xF4 };
  256. BitSetStrictLength bs = new BitSetStrictLength(22);
  257. bs.set(0);
  258. bs.set(1);
  259. bs.set(2);
  260. bs.set(3);
  261. bs.set(8);
  262. bs.set(9);
  263. bs.set(10);
  264. bs.set(11);
  265. bs.set(16);
  266. bs.set(17);
  267. bs.set(18);
  268. bs.set(19);
  269. bs.set(21);
  270. this.output.writeBitString(bs);
  271. byte[] encoded = this.output.toByteArray();
  272. compareArrays(expected, encoded);
  273. }
  274. @Test
  275. public void testBitStringData_Short() throws Exception {
  276. // 11110000 11110000 111101xx //0x0F accoring to book...
  277. byte[] expected = new byte[] { 0x02, (byte) 0xF0,
  278. (byte) 0xF0, (byte) 0xF4 };
  279. BitSetStrictLength bs = new BitSetStrictLength(22);
  280. bs.set(0);
  281. bs.set(1);
  282. bs.set(2);
  283. bs.set(3);
  284. bs.set(8);
  285. bs.set(9);
  286. bs.set(10);
  287. bs.set(11);
  288. bs.set(16);
  289. bs.set(17);
  290. bs.set(18);
  291. bs.set(19);
  292. bs.set(21);
  293. this.output.writeBitStringData(bs);
  294. byte[] encoded = this.output.toByteArray();
  295. compareArrays(expected, encoded);
  296. }
  297. // @Test
  298. // public void testBinaryString_Complex() throws Exception {
  299. //
  300. // ByteArrayOutputStream bos = new ByteArrayOutputStream();
  301. // byte[] expected = new byte[] { 0x03, 0x04, 0x02, (byte) 0xF0,
  302. // (byte) 0xF0, (byte) 0xF4 };
  303. // BitSet bs = new BitSet();
  304. // // complex start
  305. // bos.write(0x03 | (0x01 << 5));
  306. // bos.write(0x80);
  307. //
  308. // // primitive start
  309. // bos.write(0x03);
  310. // bos.write(0x7F);
  311. //
  312. // // extra octet
  313. // bos.write(0x00);
  314. // for (int i = 0; i < 126; i++) {
  315. // if (i % 2 == 0) {
  316. // bos.write(0x0A);
  317. // // 0000 1010
  318. // bs.set(i * 8 + 4);
  319. // bs.set(i * 8 + 6);
  320. // } else {
  321. // bos.write(0x0F);
  322. // bs.set(i * 8 + 4);
  323. // bs.set(i * 8 + 5);
  324. // bs.set(i * 8 + 6);
  325. // bs.set(i * 8 + 7);
  326. // }
  327. // }
  328. //
  329. // // next primitive
  330. // bos.write(expected);
  331. //
  332. // // terminate complex
  333. // bos.write(0x00);
  334. // bos.write(0x00);
  335. //
  336. // bs.set(126 * 8 + 0);
  337. // bs.set(126 * 8 + 1);
  338. // bs.set(126 * 8 + 2);
  339. // bs.set(126 * 8 + 3);
  340. // bs.set(126 * 8 + 8);
  341. // bs.set(126 * 8 + 9);
  342. // bs.set(126 * 8 + 10);
  343. // bs.set(126 * 8 + 11);
  344. // bs.set(126 * 8 + 16);
  345. // bs.set(126 * 8 + 17);
  346. // bs.set(126 * 8 + 18);
  347. // bs.set(126 * 8 + 19);
  348. // bs.set(126 * 8 + 21);
  349. // this.output.writeStringBinary(bs);
  350. // byte[] encoded = this.output.toByteArray();
  351. // compareArrays(bos.toByteArray(), encoded);
  352. // }
  353. @Test
  354. public void testOctetString() throws Exception {
  355. byte[] expected = new byte[] { 0x04, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  356. byte[] bs = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  357. this.output.writeOctetString(bs);
  358. byte[] encoded = this.output.toByteArray();
  359. compareArrays(expected, encoded);
  360. }
  361. @Test
  362. public void testUTF8StringShort() throws Exception {
  363. String dataString = "ACEace$} - S?u?by wiedz?, kto zorganizowa? zamachy w metrze.";
  364. byte[] data = dataString.getBytes(BERStatics.STRING_UTF8_ENCODING);
  365. ByteArrayOutputStream bos = new ByteArrayOutputStream(10);
  366. // write tag
  367. bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  368. | Tag.STRING_UTF8);
  369. bos.write(data.length);
  370. bos.write(data);
  371. byte[] expected = bos.toByteArray();
  372. this.output.writeStringUTF8(dataString);
  373. byte[] encoded = this.output.toByteArray();
  374. compareArrays(expected, encoded);
  375. }
  376. // @Test
  377. // public void testUTF8StringComplex() throws Exception {
  378. // // actual encoding of this is 80bytes, double == 160
  379. // //commenting out, it fails on linux
  380. // String dataString = "ACEace$} - Sluzby wiedza, kto zorganizowal zamachy w metrze.";
  381. // dataString += dataString+dataString;
  382. // dataString = dataString.substring(0,160);
  383. // byte[] data = dataString.getBytes(BERStatics.STRING_UTF8_ENCODING);
  384. // ByteArrayOutputStream bos = new ByteArrayOutputStream(10);
  385. // // write tag
  386. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_CONSTRUCTED << 5)
  387. // | Tag.STRING_UTF8);
  388. // bos.write(0x80);
  389. //
  390. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  391. // | Tag.STRING_UTF8);
  392. // bos.write(127);
  393. // bos.write(data, 0, 127);
  394. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  395. // | Tag.STRING_UTF8);
  396. // bos.write(160 - 127);
  397. // bos.write(data, 127, 160 - 127);
  398. //
  399. // bos.write(0);
  400. // bos.write(0);
  401. //
  402. // byte[] expected = bos.toByteArray();
  403. //
  404. // this.output.writeStringUTF8(dataString);
  405. // byte[] encoded = this.output.toByteArray();
  406. // compareArrays(expected, encoded);
  407. // }
  408. @Test
  409. public void testIA5StringShort() throws Exception {
  410. String dataString = "ACEace$}";
  411. byte[] data = dataString.getBytes(BERStatics.STRING_IA5_ENCODING);
  412. ByteArrayOutputStream bos = new ByteArrayOutputStream(10);
  413. // write tag
  414. bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  415. | Tag.STRING_IA5);
  416. bos.write(data.length);
  417. bos.write(data);
  418. byte[] expected = bos.toByteArray();
  419. this.output.writeStringIA5(dataString);
  420. byte[] encoded = this.output.toByteArray();
  421. compareArrays(expected, encoded);
  422. }
  423. // @Test
  424. // public void testIA5StringComplex() throws Exception {
  425. // // actual encoding of this is 80bytes, double == 160
  426. // String dataString = "ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}ACEace$}";
  427. // dataString += dataString;
  428. //
  429. // byte[] data = dataString.getBytes(BERStatics.STRING_IA5_ENCODING);
  430. //
  431. // ByteArrayOutputStream bos = new ByteArrayOutputStream(10);
  432. // // write tag
  433. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_CONSTRUCTED << 5)
  434. // | Tag.STRING_IA5);
  435. // bos.write(0x80);
  436. //
  437. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  438. // | Tag.STRING_IA5);
  439. // bos.write(127);
  440. // bos.write(data, 0, 127);
  441. // bos.write((Tag.CLASS_UNIVERSAL << 6) | (Tag.PC_PRIMITIVITE << 5)
  442. // | Tag.STRING_IA5);
  443. // bos.write(176 - 127);
  444. // bos.write(data, 127, 176 - 127);
  445. //
  446. // bos.write(0);
  447. // bos.write(0);
  448. //
  449. // byte[] expected = bos.toByteArray();
  450. //
  451. // this.output.writeStringIA5(dataString);
  452. // byte[] encoded = this.output.toByteArray();
  453. // compareArrays(expected, encoded);
  454. // }
  455. @Test
  456. public void testObjectIdentifier() throws Exception {
  457. byte[] expected = new byte[] {Tag.OBJECT_IDENTIFIER, 0x4, 0x28, (byte) 0xC2, (byte) 0x7B, 0x02 };
  458. long[] oids = new long[]{1, 0, 8571, 2};
  459. this.output.writeObjectIdentifier(oids);
  460. byte[] encodedData = this.output.toByteArray();
  461. compareArrays(expected, encodedData);
  462. expected = new byte[] {Tag.OBJECT_IDENTIFIER, 0x2, (byte)180, 1 };
  463. oids = new long[]{2, 100, 1};
  464. this.output.reset();
  465. this.output.write(Tag.OBJECT_IDENTIFIER);
  466. int i1 = this.output.StartContentDefiniteLength();
  467. this.output.writeObjectIdentifierData(oids);
  468. this.output.FinalizeContent(i1);
  469. encodedData = this.output.toByteArray();
  470. compareArrays(expected, encodedData);
  471. }
  472. }