Fix VisualVoicemailSmsFilter NPE on invalid SmsMessage

Certain PDU will cause createFromPdu() to return a SmsMessage with null
mWrappedSmsMessage, throwing NPE on any method called. In this case, just
ignore the message.

Bug:29123941
Change-Id: Id649af4b7a7a9f5d7b7a08d007b619a4c98f81cb
diff --git a/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java b/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
index 005e969..e7b0a6e 100644
--- a/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
+++ b/src/java/com/android/internal/telephony/VisualVoicemailSmsFilter.java
@@ -60,7 +60,9 @@
         // TODO: filter base on originating number and destination port.
 
         String messageBody = getFullMessage(pdus, format);
-
+        if(messageBody == null){
+            return false;
+        }
         String clientPrefix = settings.clientPrefix;
 
         WrappedMessageData messageData = VisualVoicemailSmsParser.parse(clientPrefix, messageBody);
@@ -81,7 +83,15 @@
     private static String getFullMessage(byte[][] pdus, String format) {
         StringBuilder builder = new StringBuilder();
         for (byte pdu[] : pdus) {
-            String body = SmsMessage.createFromPdu(pdu, format).getMessageBody();
+            SmsMessage message =SmsMessage.createFromPdu(pdu, format);
+
+            if(message == null || message.mWrappedSmsMessage == null) {
+                // b/29123941 Certain PDU will cause createFromPdu() to return a SmsMessage with
+                // null mWrappedSmsMessage, throwing NPE on any method called. In this case, just
+                // ignore the message.
+                return null;
+            }
+            String body = message.getMessageBody();
             if (body != null) {
                 builder.append(body);
             }
diff --git a/tests/telephonytests/src/com/android/internal/telephony/VisualVoicemailSmsFilterTest.java b/tests/telephonytests/src/com/android/internal/telephony/VisualVoicemailSmsFilterTest.java
new file mode 100644
index 0000000..f7a6ac2
--- /dev/null
+++ b/tests/telephonytests/src/com/android/internal/telephony/VisualVoicemailSmsFilterTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.internal.telephony;
+
+import android.content.Context;
+import android.telephony.TelephonyManager;
+import android.telephony.VisualVoicemailSmsFilterSettings;
+
+import junit.framework.TestCase;
+
+import org.mockito.Mockito;
+
+public class VisualVoicemailSmsFilterTest extends TestCase {
+
+    public void testUnsupportedPdu() {
+        Context context = Mockito.mock(Context.class);
+        TelephonyManager telephonyManager = Mockito.mock(TelephonyManager.class);
+        Mockito.when(context.getSystemServiceName(TelephonyManager.class))
+                .thenReturn(Context.TELEPHONY_SERVICE);
+        Mockito.when(context.getSystemService(Mockito.anyString())).thenReturn(telephonyManager);
+
+        VisualVoicemailSmsFilterSettings settings = new VisualVoicemailSmsFilterSettings.Builder()
+                .build();
+
+        Mockito.when(telephonyManager
+                .getVisualVoicemailSmsFilterSettings(Mockito.anyString(), Mockito.anyInt()))
+                .thenReturn(settings);
+
+        byte[][] pdus = {
+                ("MBOXUPDATE?m=11;server=example.com;"
+                        + "port=143;name=1234567890@example.com;pw=CphQJKnYS4jEiDO").getBytes()};
+        VisualVoicemailSmsFilter.filter(context, pdus, SmsConstants.FORMAT_3GPP2, 0, 0);
+    }
+
+}