public void decode(AsnInputStream ais) throws ParseException { try { AsnInputStream localAis = ais.readSequenceStream(); while (true) { if (localAis.available() == 0) return; int tag = localAis.readTag(); if (localAis.isTagPrimitive() || localAis.getTagClass() != Tag.CLASS_APPLICATION) throw new ParseException( PAbortCauseType.IncorrectTxPortion, null, "Error decoding TC-Uni: DialogPortion and Component portion must be constructive and has " + "tag class CLASS_APPLICATION"); switch (tag) { case DialogPortion._TAG: this.dp = TcapFactory.createDialogPortion(localAis); break; case Component._COMPONENT_TAG: AsnInputStream compAis = localAis.readSequenceStream(); List<Component> cps = new ArrayList<Component>(); // its iterator :) while (compAis.available() > 0) { Component c = TcapFactory.createComponent(compAis); if (c == null) { break; } cps.add(c); } this.component = new Component[cps.size()]; this.component = cps.toArray(this.component); break; default: throw new ParseException( PAbortCauseType.IncorrectTxPortion, null, "Error decoding TC-Uni: DialogPortion and Componebt parsing: bad tag - " + tag); } } } catch (IOException e) { throw new ParseException( PAbortCauseType.BadlyFormattedTxPortion, null, "IOException while decoding " + "TC-Uni: " + e.getMessage(), e); } catch (AsnException e) { throw new ParseException( PAbortCauseType.BadlyFormattedTxPortion, null, "AsnException while decoding " + "TC-Uni: " + e.getMessage(), e); } }
public void decode(AsnInputStream aisA) throws ParseException { // TAG has been decoded already, now, lets get LEN try { AsnInputStream ais = aisA.readSequenceStream(); int tag = ais.readTag(); if (tag != Tag.EXTERNAL) throw new ParseException( PAbortCauseType.IncorrectTxPortion, null, "Error decoding DialogPortion: wrong value of tag, expected EXTERNAL, found: " + tag); ext.decode(ais); if (!isAsn() || !isOid()) { throw new ParseException( PAbortCauseType.IncorrectTxPortion, null, "Error decoding DialogPortion: Oid and Asd parts not found"); } // Check Oid if (Arrays.equals(_DIALG_UNI, this.getOidValue())) this.uniDirectional = true; else if (Arrays.equals(_DIALG_STRUCTURED, this.getOidValue())) this.uniDirectional = false; else throw new ParseException( PAbortCauseType.IncorrectTxPortion, null, "Error decoding DialogPortion: bad Oid value"); AsnInputStream loaclAsnIS = new AsnInputStream(ext.getEncodeType()); // now lets get APDU tag = loaclAsnIS.readTag(); this.dialogAPDU = TcapFactory.createDialogAPDU(loaclAsnIS, tag, isUnidirectional()); } catch (IOException e) { throw new ParseException( PAbortCauseType.BadlyFormattedTxPortion, null, "IOException when decoding DialogPortion: " + e.getMessage(), e); } catch (AsnException e) { throw new ParseException( PAbortCauseType.BadlyFormattedTxPortion, null, "AsnException when decoding DialogPortion: " + e.getMessage(), e); } }
@Test(groups = {"functional.encode"}) public void testEncode() throws IOException, EncodeException { byte[] expected = this.getData(); Invoke invoke = TcapFactory.createComponentInvoke(); invoke.setInvokeId(12l); OperationCode oc = TcapFactory.createOperationCode(); oc.setLocalOperationCode(59L); invoke.setOperationCode(oc); Parameter p1 = TcapFactory.createParameter(); p1.setTagClass(Tag.CLASS_UNIVERSAL); p1.setTag(Tag.STRING_OCTET); p1.setData(new byte[] {0x0F}); Parameter p2 = TcapFactory.createParameter(); p2.setTagClass(Tag.CLASS_UNIVERSAL); p2.setTag(Tag.STRING_OCTET); p2.setData( new byte[] { (byte) 0xaa, (byte) 0x98, (byte) 0xac, (byte) 0xa6, 0x5a, (byte) 0xcd, 0x62, 0x36, 0x19, 0x0e, 0x37, (byte) 0xcb, (byte) 0xe5, 0x72, (byte) 0xb9, 0x11 }); Parameter pm = TcapFactory.createParameter(); pm.setTagClass(Tag.CLASS_UNIVERSAL); pm.setTag(Tag.SEQUENCE); pm.setParameters(new Parameter[] {p1, p2}); invoke.setParameter(pm); AsnOutputStream asnos = new AsnOutputStream(); invoke.encode(asnos); byte[] encodedData = asnos.toByteArray(); assertTrue(Arrays.equals(expected, encodedData)); expected = this.getDataFull(); invoke = TcapFactory.createComponentInvoke(); invoke.setInvokeId(-5L); invoke.setLinkedId(2L); oc = TcapFactory.createOperationCode(); oc.setGlobalOperationCode(new long[] {1, 0, 0, 1}); invoke.setOperationCode(oc); pm = TcapFactory.createParameter(); pm.setTagClass(Tag.CLASS_UNIVERSAL); pm.setTag(Tag.STRING_OCTET); pm.setData(new byte[] {11, 22, 33}); invoke.setParameter(pm); asnos = new AsnOutputStream(); invoke.encode(asnos); encodedData = asnos.toByteArray(); assertTrue(Arrays.equals(expected, encodedData)); }
@Test(groups = {"functional.decode"}) public void testDecodeWithParaSequ() throws IOException, ParseException { byte[] b = this.getData(); AsnInputStream asnIs = new AsnInputStream(b); Component comp = TcapFactory.createComponent(asnIs); assertEquals(ComponentType.Invoke, comp.getType()); Invoke invokeComp = (Invoke) comp; assertTrue(12L == invokeComp.getInvokeId()); OperationCode oc = invokeComp.getOperationCode(); assertNotNull(oc); assertTrue(59 == oc.getLocalOperationCode()); assertEquals(OperationCodeType.Local, oc.getOperationType()); Parameter p = invokeComp.getParameter(); assertEquals(Tag.CLASS_UNIVERSAL, p.getTagClass()); assertEquals(false, p.isPrimitive()); assertEquals(Tag.SEQUENCE, p.getTag()); assertTrue( Arrays.equals( new byte[] { 0x04, 0x01, 0x0f, 0x04, 0x10, (byte) 0xaa, (byte) 0x98, (byte) 0xac, (byte) 0xa6, 0x5a, (byte) 0xcd, 0x62, 0x36, 0x19, 0x0e, 0x37, (byte) 0xcb, (byte) 0xe5, 0x72, (byte) 0xb9, 0x11 }, p.getData())); Parameter[] params = invokeComp.getParameter().getParameters(); assertEquals(2, params.length); p = params[0]; assertEquals(Tag.CLASS_UNIVERSAL, p.getTagClass()); assertEquals(true, p.isPrimitive()); assertEquals(Tag.STRING_OCTET, p.getTag()); assertTrue(Arrays.equals(new byte[] {0x0f}, p.getData())); p = params[1]; assertEquals(Tag.CLASS_UNIVERSAL, p.getTagClass()); assertEquals(true, p.isPrimitive()); assertEquals(Tag.STRING_OCTET, p.getTag()); assertTrue( Arrays.equals( new byte[] { (byte) 0xaa, (byte) 0x98, (byte) 0xac, (byte) 0xa6, 0x5a, (byte) 0xcd, 0x62, 0x36, 0x19, 0x0e, 0x37, (byte) 0xcb, (byte) 0xe5, 0x72, (byte) 0xb9, 0x11 }, p.getData())); b = this.getDataFull(); asnIs = new AsnInputStream(b); comp = TcapFactory.createComponent(asnIs); assertEquals(ComponentType.Invoke, comp.getType()); invokeComp = (Invoke) comp; assertEquals(-5L, (long) invokeComp.getInvokeId()); assertEquals(2L, (long) invokeComp.getLinkedId()); oc = invokeComp.getOperationCode(); assertNotNull(oc); assertEquals(OperationCodeType.Global, oc.getOperationType()); assertTrue(Arrays.equals(new long[] {1, 0, 0, 1}, oc.getGlobalOperationCode())); p = invokeComp.getParameter(); assertEquals(Tag.CLASS_UNIVERSAL, p.getTagClass()); assertEquals(true, p.isPrimitive()); assertEquals(Tag.STRING_OCTET, p.getTag()); assertTrue(Arrays.equals(new byte[] {11, 22, 33}, p.getData())); }