Exemplo n.º 1
0
  @Test
  public void createFixedLengthStringTlv() throws Exception {
    Tlv tlv0 = null;

    tlv0 = TlvUtil.createFixedLengthStringTlv((short) 0x0001, null, 2, "ISO-8859-1");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("0000"), tlv0.getValue());

    tlv0 = TlvUtil.createFixedLengthStringTlv((short) 0x0001, "", 2);
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("0000"), tlv0.getValue());

    tlv0 = TlvUtil.createFixedLengthStringTlv((short) 0x0001, "1", 2, "ISO-8859-1");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("3100"), tlv0.getValue());

    tlv0 = TlvUtil.createFixedLengthStringTlv((short) 0x0001, "12", 2);
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("3132"), tlv0.getValue());

    try {
      tlv0 = TlvUtil.createFixedLengthStringTlv((short) 0x0001, "12", 1, "ISO-8859-1");
      Assert.fail();
    } catch (TlvConvertException e) {
      // correct behavior
    }
  }
Exemplo n.º 2
0
 @Override
 public void appendBodyToString(StringBuilder buffer) {
   buffer.append("(messageId [");
   buffer.append(StringUtil.toStringWithNullAsEmpty(this.messageId));
   buffer.append("] finalDate [");
   buffer.append(StringUtil.toStringWithNullAsEmpty(this.finalDate));
   buffer.append("] messageState [0x");
   buffer.append(HexUtil.toHexString(this.messageState));
   buffer.append("] errorCode [0x");
   buffer.append(HexUtil.toHexString(this.errorCode));
   buffer.append("])");
 }
Exemplo n.º 3
0
  @Test
  public void createNullTerminatedStringTlv() throws Exception {
    Tlv tlv0 = null;

    // null string should just be 0x00
    tlv0 = TlvUtil.createNullTerminatedStringTlv((short) 0x0001, null, "ISO-8859-1");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("00"), tlv0.getValue());

    tlv0 = TlvUtil.createNullTerminatedStringTlv((short) 0x0001, "", "ISO-8859-1");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("00"), tlv0.getValue());

    tlv0 = TlvUtil.createNullTerminatedStringTlv((short) 0x0001, "a");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("6100"), tlv0.getValue());

    tlv0 = TlvUtil.createNullTerminatedStringTlv((short) 0x0001, "c1net");
    Assert.assertEquals((short) 0x0001, tlv0.getTag());
    Assert.assertArrayEquals(HexUtil.toByteArray("63316e657400"), tlv0.getValue());
  }
  @Test
  public void encode() throws Exception {
    // euro currency symbol
    String str0 = "\u20ac";
    byte[] bytes = null;

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B65"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_PACKED_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("9B32"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UCS_2);
    Assert.assertArrayEquals(HexUtil.toByteArray("20AC"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-16BE"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UTF_8);
    Assert.assertArrayEquals(HexUtil.toByteArray("E282AC"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-8"), bytes);

    // latin-1 doesn't contain the euro symbol - replace with '?'
    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertArrayEquals(HexUtil.toByteArray("3F"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-1"), bytes);

    // latin-9 does contain the euro symbol
    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertArrayEquals(HexUtil.toByteArray("A4"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-15"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B65"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B65"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B65"), bytes);

    // longer string with @ symbol in-between
    str0 = "Hello @ World";

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F200020576F726C64"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_PACKED_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("C8329BFD060140D7B79C4D06"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UCS_2);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(
        HexUtil.toByteArray("00480065006C006C006F0020004000200057006F0072006C0064"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-16BE"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UTF_8);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F204020576F726C64"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-8"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F204020576F726C64"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-1"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F204020576F726C64"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-15"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F200020576F726C64"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFD2_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F204020576F726C64"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFTR_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("48656C6C6F204020576F726C64"), bytes);

    // longer string with @ symbol in-between
    str0 = "JoeyBlue";

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_PACKED_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("CA77392F64D7CB"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UCS_2);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("004A006F006500790042006C00750065"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-16BE"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UTF_8);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-8"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-1"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-15"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("4A6F6579426C7565"), bytes);

    // longer string with @ symbol in-between
    str0 = "{}[]$";

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("1B281B291B3C1B3E02"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_PACKED_GSM);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("1BD426B5E16D7C02"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UCS_2);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("007B007D005B005D0024"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-16BE"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_UTF_8);
    // logger.debug(HexUtil.toHexString(bytes));
    Assert.assertArrayEquals(HexUtil.toByteArray("7B7D5B5D24"), bytes);
    Assert.assertArrayEquals(str0.getBytes("UTF-8"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertArrayEquals(HexUtil.toByteArray("7B7D5B5D24"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-1"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertArrayEquals(HexUtil.toByteArray("7B7D5B5D24"), bytes);
    Assert.assertArrayEquals(str0.getBytes("ISO-8859-15"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B281B291B3C1B3E02"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B281B291B3C1B3E24"), bytes);

    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("1B281B291B3C1B3E24"), bytes);

    // chars specifically to vodafone-turkey
    // str0 = "[email protected]£¤¥§ÄÅßñΓΔΘΩ€";
    str0 = "[email protected]\u00a3\u00a4\u00a5\u00a7\u00c4\u00c5\u00df\u00f1\u0393\u0394\u0398\u03a9\u20ac";
    bytes = CharsetUtil.encode(str0, CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertArrayEquals(HexUtil.toByteArray("2440A3A4A5A7C4C5DFF1137F19151B65"), bytes);
  }
  @Test
  public void decode() throws Exception {
    // euro currency symbol
    String str0 = "\u20ac";
    String str1 = null;

    str1 = CharsetUtil.decode(HexUtil.toByteArray("1B65"), CharsetUtil.CHARSET_GSM);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("9B32"), CharsetUtil.CHARSET_PACKED_GSM);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("20AC"), CharsetUtil.CHARSET_UCS_2);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("E282AC"), CharsetUtil.CHARSET_UTF_8);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("1B65"), CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertEquals(str0, str1);

    // latin-1 doesn't contain the euro symbol - replace with '?'
    // str1 = CharsetUtil.decode(HexUtil.toByteArray("3F"), CharsetUtil.CHARSET_ISO_8859_1);
    // Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("A4"), CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("1B65"), CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("1B65"), CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertEquals(str0, str1);

    // longer string with @ symbol in-between
    str0 = "Hello @ World";

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F200020576F726C64"), CharsetUtil.CHARSET_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("C8329BFD060140D7B79C4D06"), CharsetUtil.CHARSET_PACKED_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("00480065006C006C006F0020004000200057006F0072006C0064"),
            CharsetUtil.CHARSET_UCS_2);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_UTF_8);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("48656C6C6F204020576F726C64"), CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertEquals(str0, str1);

    // longer string with @ symbol in-between
    str0 = "JoeyBlue";

    str1 = CharsetUtil.decode(HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("CA77392F64D7CB"), CharsetUtil.CHARSET_PACKED_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("004A006F006500790042006C00750065"), CharsetUtil.CHARSET_UCS_2);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_UTF_8);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("4A6F6579426C7565"), CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertEquals(str0, str1);

    // longer string with @ symbol in-between
    str0 = "{}[]$";

    str1 = CharsetUtil.decode(HexUtil.toByteArray("1B281B291B3C1B3E02"), CharsetUtil.CHARSET_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("1BD426B5E16D7C02"), CharsetUtil.CHARSET_PACKED_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("007B007D005B005D0024"), CharsetUtil.CHARSET_UCS_2);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("7B7D5B5D24"), CharsetUtil.CHARSET_UTF_8);
    Assert.assertEquals(str0, str1);

    // airwide is close to GSM, $ is 0x24 rather than 0x02 though
    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("1B281B291B3C1B3E24"), CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("7B7D5B5D24"), CharsetUtil.CHARSET_ISO_8859_1);
    Assert.assertEquals(str0, str1);

    str1 = CharsetUtil.decode(HexUtil.toByteArray("7B7D5B5D24"), CharsetUtil.CHARSET_ISO_8859_15);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("1B281B291B3C1B3E24"), CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertEquals(str0, str1);

    str1 =
        CharsetUtil.decode(HexUtil.toByteArray("1B281B291B3C1B3E24"), CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertEquals(str0, str1);

    // had problem passing these tests on linux vs. mac os x -- issue with
    // byte encoding on differnet platforms, replaced tests with source strings
    // that use Java unicode escapes
    // helpful URL: http://www.greywyvern.com/code/php/utf8_html
    // decode a string with every char in VFD2-GSM
    // str0 = "@$ߤ¡Ñܧñü_";
    str0 = "@$\u00df\u00a4\u00a1\u00d1\u00dc\u00a7\u00f1\u00fc_";
    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("40247E02A15F5D5E1E7D11"), CharsetUtil.CHARSET_VFD2_GSM);
    Assert.assertEquals(str0, str1);

    // str0 = "@$ß[email protected]ÑÜ_ñü_";
    str0 = "@$\[email protected]\u00d1\u00dc_\u00f1\u00fc_";
    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("40241E24405D5E5F7D7E5F"), CharsetUtil.CHARSET_AIRWIDE_IA5);
    Assert.assertEquals(str0, str1);

    // chars specifically to vodafone-turkey
    // str0 = "[email protected]£¤¥§ÄÅßñΓΔΘΩ€";
    str0 = "[email protected]\u00a3\u00a4\u00a5\u00a7\u00c4\u00c5\u00df\u00f1\u0393\u0394\u0398\u03a9\u20ac";
    str1 =
        CharsetUtil.decode(
            HexUtil.toByteArray("2440A3A4A5A7C4C5DFF1137F19151B65"), CharsetUtil.CHARSET_VFTR_GSM);
    Assert.assertEquals(str0, str1);
  }