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