From d8eb370a89689cc349a6168f773ec4fa36203b7f Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Fri, 13 Jul 2018 20:04:45 +0100 Subject: [PATCH] Extend Uri tests The internal UriCodec class is being removed from libcore/ and the associated CTS tests are being removed. This change adds equivalent URI decoding tests to the UriTest CTS test class. Test: make droid && make cts Test: run cts-dev -m CtsNetTestCases -t android.net.cts.UriTest Bug: 111055375 Change-Id: Ided15d5abf8478064d193034e84c4dbe0689c6f0 --- .../cts/net/src/android/net/cts/UriTest.java | 61 +++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/UriTest.java b/tests/cts/net/src/android/net/cts/UriTest.java index 05e826a2c4..611eae2590 100644 --- a/tests/cts/net/src/android/net/cts/UriTest.java +++ b/tests/cts/net/src/android/net/cts/UriTest.java @@ -158,22 +158,61 @@ public class UriTest extends AndroidTestCase { String encoded = Uri.encode("Bob:/", "/"); assertEquals(-1, encoded.indexOf(':')); assertTrue(encoded.indexOf('/') > -1); - assertDecode(null); - assertDecode(""); - assertDecode("Bob"); - assertDecode(":Bob"); - assertDecode("::Bob"); - assertDecode("Bob::Lee"); - assertDecode("Bob:Lee"); - assertDecode("Bob::"); - assertDecode("Bob:"); - assertDecode("::Bob::"); + assertEncodeDecodeRoundtripExact(null); + assertEncodeDecodeRoundtripExact(""); + assertEncodeDecodeRoundtripExact("Bob"); + assertEncodeDecodeRoundtripExact(":Bob"); + assertEncodeDecodeRoundtripExact("::Bob"); + assertEncodeDecodeRoundtripExact("Bob::Lee"); + assertEncodeDecodeRoundtripExact("Bob:Lee"); + assertEncodeDecodeRoundtripExact("Bob::"); + assertEncodeDecodeRoundtripExact("Bob:"); + assertEncodeDecodeRoundtripExact("::Bob::"); } - private void assertDecode(String s) { + private static void assertEncodeDecodeRoundtripExact(String s) { assertEquals(s, Uri.decode(Uri.encode(s, null))); } + public void testDecode_emptyString_returnsEmptyString() { + assertEquals("", Uri.decode("")); + } + + public void testDecode_null_returnsNull() { + assertNull(Uri.decode(null)); + } + + public void testDecode_wrongHexDigit() { + // %p in the end. + assertEquals("ab/$\u0102%\u0840\uFFFD\u0000", Uri.decode("ab%2f$%C4%82%25%e0%a1%80%p")); + } + + public void testDecode_secondHexDigitWrong() { + // %1p in the end. + assertEquals("ab/$\u0102%\u0840\uFFFD\u0001", Uri.decode("ab%2f$%c4%82%25%e0%a1%80%1p")); + } + + public void testDecode_endsWithPercent_appendsUnknownCharacter() { + // % in the end. + assertEquals("ab/$\u0102%\u0840\uFFFD", Uri.decode("ab%2f$%c4%82%25%e0%a1%80%")); + } + + public void testDecode_plusNotConverted() { + assertEquals("ab/$\u0102%+\u0840", Uri.decode("ab%2f$%c4%82%25+%e0%a1%80")); + } + + // Last character needs decoding (make sure we are flushing the buffer with chars to decode). + public void testDecode_lastCharacter() { + assertEquals("ab/$\u0102%\u0840", Uri.decode("ab%2f$%c4%82%25%e0%a1%80")); + } + + // Check that a second row of encoded characters is decoded properly (internal buffers are + // reset properly). + public void testDecode_secondRowOfEncoded() { + assertEquals("ab/$\u0102%\u0840aa\u0840", + Uri.decode("ab%2f$%c4%82%25%e0%a1%80aa%e0%a1%80")); + } + public void testFromFile() { File f = new File("/tmp/bob"); Uri uri = Uri.fromFile(f);