|
25 | 25 |
|
26 | 26 | import java.io.File; |
27 | 27 | import java.lang.reflect.Constructor; |
28 | | -import java.lang.reflect.InvocationTargetException; |
29 | | -import java.lang.reflect.Method; |
30 | 28 | import java.util.Arrays; |
31 | 29 | import java.util.Iterator; |
32 | 30 | import java.util.List; |
@@ -869,84 +867,90 @@ private static Uri makeHierarchicalUri(Object authority, Object path) throws Exc |
869 | 867 | return (Uri) hierarchicalUriConstructor.newInstance("https", authority, path, null, null); |
870 | 868 | } |
871 | 869 |
|
872 | | - /** Attempting to unparcel a legacy parcel format of Uri.{,Path}Part should fail. */ |
873 | | - public void testUnparcelLegacyPart_fails() throws Exception { |
874 | | - assertUnparcelLegacyPart_fails(Class.forName("android.net.Uri$Part")); |
875 | | - assertUnparcelLegacyPart_fails(Class.forName("android.net.Uri$PathPart")); |
876 | | - } |
877 | | - |
878 | | - private static void assertUnparcelLegacyPart_fails(Class partClass) throws Exception { |
879 | | - Parcel parcel = Parcel.obtain(); |
880 | | - parcel.writeInt(0 /* BOTH */); |
881 | | - parcel.writeString("encoded"); |
882 | | - parcel.writeString("decoded"); |
883 | | - parcel.setDataPosition(0); |
884 | | - |
885 | | - Method readFromMethod = partClass.getDeclaredMethod("readFrom", Parcel.class); |
886 | | - readFromMethod.setAccessible(true); |
887 | | - try { |
888 | | - readFromMethod.invoke(null, parcel); |
889 | | - fail(); |
890 | | - } catch (InvocationTargetException expected) { |
891 | | - Throwable targetException = expected.getTargetException(); |
892 | | - // Check that the exception was thrown for the correct reason. |
893 | | - assertEquals("Unknown representation: 0", targetException.getMessage()); |
894 | | - } finally { |
895 | | - parcel.recycle(); |
896 | | - } |
897 | | - } |
898 | | - |
899 | | - private Uri buildUriFromRawParcel(boolean argumentsEncoded, |
| 870 | + private Uri buildUriFromParts(boolean argumentsEncoded, |
900 | 871 | String scheme, |
901 | 872 | String authority, |
902 | 873 | String path, |
903 | 874 | String query, |
904 | 875 | String fragment) { |
905 | | - // Representation value (from AbstractPart.REPRESENTATION_{ENCODED,DECODED}). |
906 | | - final int representation = argumentsEncoded ? 1 : 2; |
907 | | - Parcel parcel = Parcel.obtain(); |
908 | | - try { |
909 | | - parcel.writeInt(3); // hierarchical |
910 | | - parcel.writeString8(scheme); |
911 | | - parcel.writeInt(representation); |
912 | | - parcel.writeString8(authority); |
913 | | - parcel.writeInt(representation); |
914 | | - parcel.writeString8(path); |
915 | | - parcel.writeInt(representation); |
916 | | - parcel.writeString8(query); |
917 | | - parcel.writeInt(representation); |
918 | | - parcel.writeString8(fragment); |
919 | | - parcel.setDataPosition(0); |
920 | | - return Uri.CREATOR.createFromParcel(parcel); |
921 | | - } finally { |
922 | | - parcel.recycle(); |
| 876 | + final Uri.Builder builder = new Uri.Builder(); |
| 877 | + builder.scheme(scheme); |
| 878 | + if (argumentsEncoded) { |
| 879 | + builder.encodedAuthority(authority); |
| 880 | + builder.encodedPath(path); |
| 881 | + builder.encodedQuery(query); |
| 882 | + builder.encodedFragment(fragment); |
| 883 | + } else { |
| 884 | + builder.authority(authority); |
| 885 | + builder.path(path); |
| 886 | + builder.query(query); |
| 887 | + builder.fragment(fragment); |
923 | 888 | } |
| 889 | + return builder.build(); |
924 | 890 | } |
925 | 891 |
|
926 | 892 | public void testUnparcelMalformedPath() { |
927 | 893 | // Regression tests for b/171966843. |
928 | 894 |
|
929 | 895 | // Test cases with arguments encoded (covering testing `scheme` * `authority` options). |
930 | | - Uri uri0 = buildUriFromRawParcel(true, "https", "google.com", "@evil.com", null, null); |
| 896 | + Uri uri0 = buildUriFromParts(true, "https", "google.com", "@evil.com", null, null); |
931 | 897 | assertEquals("https://google.com/@evil.com", uri0.toString()); |
932 | | - Uri uri1 = buildUriFromRawParcel(true, null, "google.com", "@evil.com", "name=spark", "x"); |
| 898 | + Uri uri1 = buildUriFromParts(true, null, "google.com", "@evil.com", "name=spark", "x"); |
933 | 899 | assertEquals("//google.com/@evil.com?name=spark#x", uri1.toString()); |
934 | | - Uri uri2 = buildUriFromRawParcel(true, "http:", null, "@evil.com", null, null); |
| 900 | + Uri uri2 = buildUriFromParts(true, "http:", null, "@evil.com", null, null); |
935 | 901 | assertEquals("http::/@evil.com", uri2.toString()); |
936 | | - Uri uri3 = buildUriFromRawParcel(true, null, null, "@evil.com", null, null); |
| 902 | + Uri uri3 = buildUriFromParts(true, null, null, "@evil.com", null, null); |
937 | 903 | assertEquals("@evil.com", uri3.toString()); |
938 | 904 |
|
939 | 905 | // Test cases with arguments not encoded (covering testing `scheme` * `authority` options). |
940 | | - Uri uriA = buildUriFromRawParcel(false, "https", "google.com", "@evil.com", null, null); |
| 906 | + Uri uriA = buildUriFromParts(false, "https", "google.com", "@evil.com", null, null); |
941 | 907 | assertEquals("https://google.com/%40evil.com", uriA.toString()); |
942 | | - Uri uriB = buildUriFromRawParcel(false, null, "google.com", "@evil.com", null, null); |
| 908 | + Uri uriB = buildUriFromParts(false, null, "google.com", "@evil.com", null, null); |
943 | 909 | assertEquals("//google.com/%40evil.com", uriB.toString()); |
944 | | - Uri uriC = buildUriFromRawParcel(false, "http:", null, "@evil.com", null, null); |
| 910 | + Uri uriC = buildUriFromParts(false, "http:", null, "@evil.com", null, null); |
945 | 911 | assertEquals("http::/%40evil.com", uriC.toString()); |
946 | | - Uri uriD = buildUriFromRawParcel(false, null, null, "@evil.com", "name=spark", "y"); |
| 912 | + Uri uriD = buildUriFromParts(false, null, null, "@evil.com", "name=spark", "y"); |
947 | 913 | assertEquals("%40evil.com?name%3Dspark#y", uriD.toString()); |
948 | 914 | } |
949 | 915 |
|
| 916 | + public void testParsedUriFromStringEquality() { |
| 917 | + Uri uri = buildUriFromParts( |
| 918 | + true, "https", "google.com", "@evil.com", null, null); |
| 919 | + assertEquals(uri, Uri.parse(uri.toString())); |
| 920 | + Uri uri2 = buildUriFromParts( |
| 921 | + true, "content://evil.authority?foo=", "safe.authority", "@evil.com", null, null); |
| 922 | + assertEquals(uri2, Uri.parse(uri2.toString())); |
| 923 | + Uri uri3 = buildUriFromParts( |
| 924 | + false, "content://evil.authority?foo=", "safe.authority", "@evil.com", null, null); |
| 925 | + assertEquals(uri3, Uri.parse(uri3.toString())); |
| 926 | + } |
| 927 | + |
| 928 | + public void testParceledUrisAreEqual() { |
| 929 | + Uri opaqueUri = Uri.fromParts("fake://uri#", "ssp", "fragment"); |
| 930 | + Parcel parcel = Parcel.obtain(); |
| 931 | + try { |
| 932 | + opaqueUri.writeToParcel(parcel, 0); |
| 933 | + parcel.setDataPosition(0); |
| 934 | + Uri postParcelUri = Uri.CREATOR.createFromParcel(parcel); |
| 935 | + Uri parsedUri = Uri.parse(postParcelUri.toString()); |
| 936 | + assertEquals(parsedUri.getScheme(), postParcelUri.getScheme()); |
| 937 | + } finally { |
| 938 | + parcel.recycle(); |
| 939 | + } |
| 940 | + |
| 941 | + Uri hierarchicalUri = new Uri.Builder().scheme("fake://uri#").authority("auth").build(); |
| 942 | + parcel = Parcel.obtain(); |
| 943 | + try { |
| 944 | + hierarchicalUri.writeToParcel(parcel, 0); |
| 945 | + parcel.setDataPosition(0); |
| 946 | + Uri postParcelUri = Uri.CREATOR.createFromParcel(parcel); |
| 947 | + Uri parsedUri = Uri.parse(postParcelUri.toString()); |
| 948 | + assertEquals(parsedUri.getScheme(), postParcelUri.getScheme()); |
| 949 | + } finally { |
| 950 | + parcel.recycle(); |
| 951 | + } |
| 952 | + } |
| 953 | + |
950 | 954 | public void testToSafeString() { |
951 | 955 | checkToSafeString("tel:xxxxxx", "tel:Google"); |
952 | 956 | checkToSafeString("tel:xxxxxxxxxx", "tel:1234567890"); |
|
0 commit comments