FileDocCategorySizeDatePackage
GsmSmsTest.javaAPI DocAndroid 5.1 API23399Thu Mar 12 22:22:54 GMT 2015com.android.internal.telephony

GsmSmsTest

public class GsmSmsTest extends android.test.AndroidTestCase

Fields Summary
private static final String[]
sBasicTables
private static final int
GSM_ESCAPE_CHARACTER
private static final String[]
sExtendedTables
private static final int[]
sExtendedTableIndexes
Constructors Summary
Methods Summary
private voiddecodeSingle(int language)

        byte[] septets = new byte[(7 * 128 + 7) / 8];

        int bitOffset = 0;

        for (int i = 0; i < 128; i++) {
            int v;
            if (i == 0x1b) {
                // extended escape char
                v = 0;
            } else {
                v = i;
            }

            int byteOffset = bitOffset / 8;
            int shift = bitOffset % 8;

            septets[byteOffset] |= v << shift;

            if (shift > 1) {
                septets[byteOffset + 1] = (byte) (v >> (8 - shift));
            }

            bitOffset += 7;
        }

        String decoded = GsmAlphabet.gsm7BitPackedToString(septets, 0, 128, 0, language, 0);
        byte[] reEncoded = GsmAlphabet.stringToGsm7BitPacked(decoded, language, 0);

        assertEquals(sBasicTables[language], decoded);

        // reEncoded has the count septets byte at the front
        assertEquals(septets.length + 1, reEncoded.length);

        for (int i = 0; i < septets.length; i++) {
            assertEquals(septets[i], reEncoded[i + 1]);
        }
    
public voidtestAddressing()

        String pdu = "07914151551512f2040B916105551511f100006060605130308A04D4F29C0E";
        SmsMessage sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));
        assertEquals("+14155551212", sms.getServiceCenterAddress());
        assertEquals("+16505551111", sms.getOriginatingAddress());
        assertEquals("Test", sms.getMessageBody());

        pdu = "07914151551512f2040B916105551511f100036060924180008A0DA"
                + "8695DAC2E8FE9296A794E07";
        sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));
        assertEquals("+14155551212", sms.getServiceCenterAddress());
        assertEquals("+16505551111", sms.getOriginatingAddress());
        assertEquals("(Subject)Test", sms.getMessageBody());
    
public voidtestCPHSVoiceMail()

        // "set MWI flag"

        String pdu = "07912160130310F20404D0110041006060627171118A0120";

        SmsMessage sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertTrue(sms.isReplace());
        assertEquals("_@", sms.getOriginatingAddress());
        assertEquals(" ", sms.getMessageBody());
        assertTrue(sms.isMWISetMessage());

        // "clear mwi flag"

        pdu = "07912160130310F20404D0100041006021924193352B0120";

        sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertTrue(sms.isMWIClearMessage());

        // "clear MWI flag"

        pdu = "07912160130310F20404D0100041006060627161058A0120";

        sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertTrue(sms.isReplace());
        assertEquals("\u0394@", sms.getOriginatingAddress());
        assertEquals(" ", sms.getMessageBody());
        assertTrue(sms.isMWIClearMessage());
    
public voidtestCingularVoiceMail()

        // "set MWI flag"

        String pdu = "07912180958750F84401800500C87020026195702B06040102000200";
        SmsMessage sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertTrue(sms.isMWISetMessage());
        assertTrue(sms.isMwiDontStore());

        // "clear mwi flag"

        pdu = "07912180958750F84401800500C07020027160112B06040102000000";
        sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertTrue(sms.isMWIClearMessage());
        assertTrue(sms.isMwiDontStore());
    
public voidtestDecode()

        decodeSingle(0);    // default table
        decodeSingle(1);    // Turkish locking shift table
        decodeSingle(3);    // Portuguese locking shift table
    
public voidtestDecodeExtended()


    
         
        for (int language = 0; language < 3; language++) {
            int[] tableIndex = sExtendedTableIndexes[language];
            int numSeptets = tableIndex.length * 2;  // two septets per extended char
            byte[] septets = new byte[(7 * numSeptets + 7) / 8];

            int bitOffset = 0;

            for (int v : tableIndex) {
                // escape character
                int byteOffset = bitOffset / 8;
                int shift = bitOffset % 8;

                septets[byteOffset] |= GSM_ESCAPE_CHARACTER << shift;

                if (shift > 1) {
                    septets[byteOffset + 1] = (byte) (GSM_ESCAPE_CHARACTER >> (8 - shift));
                }

                bitOffset += 7;

                // extended table index
                byteOffset = bitOffset / 8;
                shift = bitOffset % 8;

                septets[byteOffset] |= v << shift;

                if (shift > 1) {
                    septets[byteOffset + 1] = (byte) (v >> (8 - shift));
                }

                bitOffset += 7;
            }

            String decoded = GsmAlphabet.gsm7BitPackedToString(septets, 0, numSeptets, 0,
                    0, language);
            byte[] reEncoded = GsmAlphabet.stringToGsm7BitPacked(decoded, 0, language);

            assertEquals(sExtendedTables[language], decoded);

            // reEncoded has the count septets byte at the front
            assertEquals(septets.length + 1, reEncoded.length);

            for (int i = 0; i < septets.length; i++) {
                assertEquals(septets[i], reEncoded[i + 1]);
            }
        }
    
public voidtestDecodeExtendedFallback()

        // verify that unmapped characters in extension table fall back to locking shift table
        for (int language = 0; language < 3; language++) {
            int[] tableIndex = sExtendedTableIndexes[language];
            int numChars = 128 - tableIndex.length;
            int numSeptets = numChars * 2;  // two septets per extended char
            byte[] septets = new byte[(7 * numSeptets + 7) / 8];

            int tableOffset = 0;
            int bitOffset = 0;

            StringBuilder defaultTable = new StringBuilder(128);
            StringBuilder turkishTable = new StringBuilder(128);
            StringBuilder portugueseTable = new StringBuilder(128);

            for (char c = 0; c < 128; c++) {
                // skip characters that are present in the current extension table
                if (tableOffset < tableIndex.length && tableIndex[tableOffset] == c) {
                    tableOffset++;
                    continue;
                }

                // escape character
                int byteOffset = bitOffset / 8;
                int shift = bitOffset % 8;

                septets[byteOffset] |= GSM_ESCAPE_CHARACTER << shift;

                if (shift > 1) {
                    septets[byteOffset + 1] = (byte) (GSM_ESCAPE_CHARACTER >> (8 - shift));
                }

                bitOffset += 7;

                // extended table index
                byteOffset = bitOffset / 8;
                shift = bitOffset % 8;

                septets[byteOffset] |= c << shift;

                if (shift > 1) {
                    septets[byteOffset + 1] = (byte) (c >> (8 - shift));
                }

                bitOffset += 7;

                if (c == GsmAlphabet.GSM_EXTENDED_ESCAPE) {
                    // double Escape maps to space character
                    defaultTable.append(' ");
                    turkishTable.append(' ");
                    portugueseTable.append(' ");
                } else {
                    // other unmapped chars map to the default or locking shift table
                    defaultTable.append(sBasicTables[0].charAt(c));
                    turkishTable.append(sBasicTables[1].charAt(c));
                    portugueseTable.append(sBasicTables[3].charAt(c));
                }
            }

            String decoded = GsmAlphabet.gsm7BitPackedToString(septets, 0, numSeptets, 0,
                    0, language);

            assertEquals(defaultTable.toString(), decoded);

            decoded = GsmAlphabet.gsm7BitPackedToString(septets, 0, numSeptets, 0, 1, language);
            assertEquals(turkishTable.toString(), decoded);

            decoded = GsmAlphabet.gsm7BitPackedToString(septets, 0, numSeptets, 0, 3, language);
            assertEquals(portugueseTable.toString(), decoded);
        }
    
public voidtestEmailGateway()

        String pdu = "07914151551512f204038105f300007011103164638a28e6f71b50c687db" +
                "7076d9357eb7412f7a794e07cdeb6275794c07bde8e5391d247e93f3";

        SmsMessage sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertEquals("+14155551212", sms.getServiceCenterAddress());
        assertTrue(sms.isEmail());
        assertEquals("foo@example.com", sms.getEmailFrom());
        assertEquals("foo@example.com", sms.getDisplayOriginatingAddress());
        // As of https://android-git.corp.google.com/g/#change,9324
        // getPseudoSubject will always be empty, and any subject is not extracted.
        assertEquals("", sms.getPseudoSubject());
        assertEquals("test subject /test body", sms.getDisplayMessageBody());
        assertEquals("test subject /test body", sms.getEmailBody());

        // email gateway sms test, including gsm extended character set.
        pdu = "07914151551512f204038105f400007011103105458a29e6f71b50c687db" +
                "7076d9357eb741af0d0a442fcfe9c23739bfe16d289bdee6b5f1813629";

        sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertEquals("+14155551212", sms.getServiceCenterAddress());
        assertTrue(sms.isEmail());
        assertEquals("foo@example.com", sms.getDisplayOriginatingAddress());
        assertEquals("foo@example.com", sms.getEmailFrom());
        assertEquals("{ testBody[^~\\] }", sms.getDisplayMessageBody());
        assertEquals("{ testBody[^~\\] }", sms.getEmailBody());
    
public voidtestExtendedCharacterTable()

        String pdu = "07914151551512f2040B916105551511f100006080615131728A44D4F29C0E2" +
                "AE3E96537B94C068DD16179784C2FCB41F4B0985D06B958ADD00FB0E94536AF9749" +
                "74DA6D281BA00E95E26D509B946FC3DBF87A25D56A04";

        SmsMessage sms = SmsMessage.createFromPdu(HexDump.hexStringToByteArray(pdu));

        assertEquals("+14155551212", sms.getServiceCenterAddress());
        assertEquals("+16505551111", sms.getOriginatingAddress());
        assertEquals("Test extended character table .,-!?@~_\\/&\"';^|:()<{}>[]=%*+#",
                sms.getMessageBody());
    
public voidtestFragmentText()


    
         
        boolean isGsmPhone = (TelephonyManager.getDefault().getPhoneType() ==
                TelephonyManager.PHONE_TYPE_GSM);

        // Valid 160 character 7-bit text.
        String text = "123456789012345678901234567890123456789012345678901234567890" +
                "1234567890123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890";
        GsmAlphabet.TextEncodingDetails ted = SmsMessage.calculateLength(text, false);
        assertEquals(1, ted.msgCount);
        assertEquals(160, ted.codeUnitCount);
        assertEquals(1, ted.codeUnitSize);
        assertEquals(0, ted.languageTable);
        assertEquals(0, ted.languageShiftTable);
        if (isGsmPhone) {
            ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text);
            assertEquals(1, fragments.size());
        }

        // Valid 161 character 7-bit text.
        text = "123456789012345678901234567890123456789012345678901234567890" +
                "1234567890123456789012345678901234567890123456789012345678901234567890" +
                "1234567890123456789012345678901";
        ted = SmsMessage.calculateLength(text, false);
        assertEquals(2, ted.msgCount);
        assertEquals(161, ted.codeUnitCount);
        assertEquals(1, ted.codeUnitSize);
        assertEquals(0, ted.languageTable);
        assertEquals(0, ted.languageShiftTable);
        if (isGsmPhone) {
            ArrayList<String> fragments = android.telephony.SmsMessage.fragmentText(text);
            assertEquals(2, fragments.size());
            assertEquals(text, fragments.get(0) + fragments.get(1));
            assertEquals(153, fragments.get(0).length());
            assertEquals(8, fragments.get(1).length());
        }
    
public voidtestFragmentTurkishText()

        boolean isGsmPhone = (TelephonyManager.getDefault().getPhoneType() ==
                TelephonyManager.PHONE_TYPE_GSM);

        int[] oldTables = GsmAlphabet.getEnabledSingleShiftTables();
        int[] turkishTable = { 1 };
        GsmAlphabet.setEnabledSingleShiftTables(turkishTable);

        // Valid 77 character text with Turkish characters.
        String text = "Ä