diff options
| author | 2024-05-21 07:58:43 +0000 | |
|---|---|---|
| committer | 2024-07-01 15:25:37 +0000 | |
| commit | ec923ae4732b0344efe76a8dedde3a76c824e03c (patch) | |
| tree | 2fcf0bf3986db3bbefe73d5e7c30933b318377e3 | |
| parent | 724e36e30a06207816a9a9e65959feb5d22f3542 (diff) | |
Add unit test to test data overflow when using BinaryXmlSerializer
Add the unit tests to test data overflow when calling:
1.BinaryXmlSerializer#attributeBytesHex
2.BinaryXmlSerializer#attributeBytesBase64
Bug: 307288067
Test: atest BinaryXmlTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:eebe3b8baf112082c3178ba7d17b5318c53b3b5f)
Merged-In: I4e3f4881742f0e865eaefabb8ee134c67c6b53d9
Change-Id: I4e3f4881742f0e865eaefabb8ee134c67c6b53d9
24D1-dev is based on 24Q2-release. Therefore, we merged this CL to 24D1-dev.
| -rw-r--r-- | core/tests/coretests/src/android/util/BinaryXmlTest.java | 50 | 
1 files changed, 50 insertions, 0 deletions
diff --git a/core/tests/coretests/src/android/util/BinaryXmlTest.java b/core/tests/coretests/src/android/util/BinaryXmlTest.java index 025e8314f5ed..da29828383b6 100644 --- a/core/tests/coretests/src/android/util/BinaryXmlTest.java +++ b/core/tests/coretests/src/android/util/BinaryXmlTest.java @@ -24,6 +24,8 @@ import static android.util.XmlTest.doVerifyRead;  import static android.util.XmlTest.doVerifyWrite;  import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.fail;  import static org.xmlpull.v1.XmlPullParser.START_TAG;  import android.os.PersistableBundle; @@ -41,12 +43,15 @@ import java.io.ByteArrayOutputStream;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream; +import java.io.IOException;  import java.io.InputStream;  import java.io.OutputStream;  import java.nio.charset.StandardCharsets;  @RunWith(AndroidJUnit4.class)  public class BinaryXmlTest { +    private static final int MAX_UNSIGNED_SHORT = 65_535; +      /**       * Verify that we can write and read large numbers of interned       * {@link String} values. @@ -170,4 +175,49 @@ public class BinaryXmlTest {              }          }      } + +    @Test +    public void testAttributeBytes_BinaryDataOverflow() throws Exception { +        final TypedXmlSerializer out = Xml.newBinarySerializer(); +        final ByteArrayOutputStream os = new ByteArrayOutputStream(); +        out.setOutput(os, StandardCharsets.UTF_8.name()); + +        final byte[] testBytes = new byte[MAX_UNSIGNED_SHORT + 1]; +        assertThrows(IOException.class, +                () -> out.attributeBytesHex(/* namespace */ null, /* name */ "attributeBytesHex", +                        testBytes)); + +        assertThrows(IOException.class, +                () -> out.attributeBytesBase64(/* namespace */ null, /* name */ +                        "attributeBytesBase64", testBytes)); +    } + +    @Test +    public void testAttributeBytesHex_MaximumBinaryData() throws Exception { +        final TypedXmlSerializer out = Xml.newBinarySerializer(); +        final ByteArrayOutputStream os = new ByteArrayOutputStream(); +        out.setOutput(os, StandardCharsets.UTF_8.name()); + +        final byte[] testBytes = new byte[MAX_UNSIGNED_SHORT]; +        try { +            out.attributeBytesHex(/* namespace */ null, /* name */ "attributeBytesHex", testBytes); +        } catch (Exception e) { +            fail("testAttributeBytesHex fails with exception: " + e.toString()); +        } +    } + +    @Test +    public void testAttributeBytesBase64_MaximumBinaryData() throws Exception { +        final TypedXmlSerializer out = Xml.newBinarySerializer(); +        final ByteArrayOutputStream os = new ByteArrayOutputStream(); +        out.setOutput(os, StandardCharsets.UTF_8.name()); + +        final byte[] testBytes = new byte[MAX_UNSIGNED_SHORT]; +        try { +            out.attributeBytesBase64(/* namespace */ null, /* name */ "attributeBytesBase64", +                    testBytes); +        } catch (Exception e) { +            fail("testAttributeBytesBase64 fails with exception: " + e.toString()); +        } +    }  }  |