DO NOT MERGE: CTS test for bug 33897722

Test: This is the test.

Bug 33897722 resulted in drawing with uninitialized memory. Create a
Movie with a GIF that would result in doing so without the fix, and
verify that the memory is what we expect it to be after drawing.

BUG:33897722
Change-Id: I2f320fe4f6b7deecbbb54e9f92c9fef4d26a3ded
(cherry picked from commit f1ca03c5a6268f3b44270057a518e5dd3306170e)
(cherry picked from commit 473aa82f4e961a36b0e311e0e1359c8ed7388c0d)
diff --git a/tests/tests/security/res/raw/bug_33897722.gif b/tests/tests/security/res/raw/bug_33897722.gif
new file mode 100755
index 0000000..7a563d7
--- /dev/null
+++ b/tests/tests/security/res/raw/bug_33897722.gif
Binary files differ
diff --git a/tests/tests/security/src/android/security/cts/Movie33897722.java b/tests/tests/security/src/android/security/cts/Movie33897722.java
new file mode 100644
index 0000000..0464a6a
--- /dev/null
+++ b/tests/tests/security/src/android/security/cts/Movie33897722.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2017 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 android.security.cts;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Movie;
+import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffXfermode;
+import android.test.AndroidTestCase;
+
+import java.io.InputStream;
+
+import com.android.cts.security.R;
+
+public class Movie33897722 extends AndroidTestCase {
+    /**
+     * Verifies that decoding a particular GIF file does not read out out of bounds.
+     *
+     * The image has a color map of size 2, but states that pixels should come from values
+     * larger than 2. Ensure that we do not attempt to read colors from beyond the end of the
+     * color map, which would be reading memory that we do not control, and may be uninitialized.
+     */
+    public void test_android_bug_33897722() {
+        InputStream exploitImage = mContext.getResources().openRawResource(R.raw.bug_33897722);
+        Movie movie = Movie.decodeStream(exploitImage);
+        assertNotNull(movie);
+        assertEquals(movie.width(), 600);
+        assertEquals(movie.height(), 752);
+
+        // The image has a 10 x 10 frame on top of a transparent background. Only test the
+        // 10 x 10 frame, since the original bug would never have used uninitialized memory
+        // outside of it.
+        Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
+        Canvas canvas = new Canvas(bitmap);
+
+        // Use Src PorterDuff mode, to see exactly what the Movie creates.
+        Paint paint = new Paint();
+        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
+
+        movie.draw(canvas, 0, 0, paint);
+
+        for (int x = 0; x < 10; x++) {
+            for (int y = 0; y < 10; y++) {
+                assertEquals(bitmap.getPixel(x, y), Color.TRANSPARENT);
+            }
+        }
+    }
+}