am c5c30ba1: NPE check before finalizeDestinationFile().

* commit 'c5c30ba14d411b3e23c983912e7fea4f449c9f54':
  NPE check before finalizeDestinationFile().
diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java
index 6b4420f..02e5d58 100644
--- a/src/com/android/providers/downloads/DownloadProvider.java
+++ b/src/com/android/providers/downloads/DownloadProvider.java
@@ -44,6 +44,7 @@
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -673,13 +674,18 @@
         if (scheme == null || !scheme.equals("file")) {
             throw new IllegalArgumentException("Not a file URI: " + uri);
         }
-        String path = uri.getPath();
+        final String path = uri.getPath();
         if (path == null) {
             throw new IllegalArgumentException("Invalid file URI: " + uri);
         }
-        String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
-        if (!path.startsWith(externalPath)) {
-            throw new SecurityException("Destination must be on external storage: " + uri);
+        try {
+            final String canonicalPath = new File(path).getCanonicalPath();
+            final String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
+            if (!canonicalPath.startsWith(externalPath)) {
+                throw new SecurityException("Destination must be on external storage: " + uri);
+            }
+        } catch (IOException e) {
+            throw new SecurityException("Problem resolving path: " + uri);
         }
     }
 
diff --git a/src/com/android/providers/downloads/DownloadService.java b/src/com/android/providers/downloads/DownloadService.java
index bdbd388..5fb46fb 100644
--- a/src/com/android/providers/downloads/DownloadService.java
+++ b/src/com/android/providers/downloads/DownloadService.java
@@ -550,6 +550,7 @@
     private void deleteFileIfExists(String path) {
         try {
             if (!TextUtils.isEmpty(path)) {
+                Log.i(Constants.TAG, "deleting " + path);
                 File file = new File(path);
                 file.delete();
             }
diff --git a/tests/src/com/android/providers/downloads/HelpersTest.java b/tests/src/com/android/providers/downloads/HelpersTest.java
index fdd0334..50f4c44 100644
--- a/tests/src/com/android/providers/downloads/HelpersTest.java
+++ b/tests/src/com/android/providers/downloads/HelpersTest.java
@@ -32,22 +32,13 @@
     public void testGetFullPath() throws Exception {
       String hint = "file:///com.android.providers.downloads/test";
 
-      // Test that an extension derived from the specified mime type is appended to a filename that
-      // does not itself have an extension.
+      // Test that we never change requested filename.
       String fileName = Helpers.getFullPath(
           hint,
           "video/mp4", // MIME type corresponding to file extension .mp4
           Downloads.Impl.DESTINATION_FILE_URI,
           null);
-      assertEquals(hint + ".mp4", fileName);
-
-      // Test that the filename extension is replaced by one derived from the specified mime type.
-      fileName = Helpers.getFullPath(
-          hint + ".shouldbereplaced",
-          "video/mp4", // MIME type corresponding to file extension .mp4
-          Downloads.Impl.DESTINATION_FILE_URI,
-          null);
-      assertEquals(hint + ".mp4", fileName);
+      assertEquals(hint, fileName);
     }
 
 }