More greylisted MediaStore columns.

Based on observed bugreports, add more MediaStore hidden API usage
to the greylist, along with tests.

Bug: 115845887, 116699470, 116531759
Bug: 116489751, 116135586, 116117120, 116084561, 116074030, 116062802
Test: atest MediaProviderTests
Change-Id: I0795fdbd78318889d351db02cfbe94780bf5a8c2
(cherry picked from commit 7e18f50476a8cfeed924bed61416f9e016561d4b)
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 6e2a33a..b0917dc 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -5128,7 +5128,9 @@
 
     {
         sGreylist.add(Pattern.compile(
-                "(?i)count\\(\\*\\)"));
+                "(?i)[_a-z0-9]+ as [_a-z0-9]+"));
+        sGreylist.add(Pattern.compile(
+                "(?i)(min|max|sum|avg|total|count)\\(([_a-z0-9]+|\\*)\\)( as [_a-z0-9]+)?"));
         sGreylist.add(Pattern.compile(
                 "case when case when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added \\* \\d+ when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added / \\d+ else \\d+ end > case when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified \\* \\d+ when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified / \\d+ else \\d+ end then case when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added \\* \\d+ when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added / \\d+ else \\d+ end else case when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified \\* \\d+ when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified / \\d+ else \\d+ end end as corrected_added_modified"));
         sGreylist.add(Pattern.compile(
@@ -5136,7 +5138,7 @@
         sGreylist.add(Pattern.compile(
                 "MAX\\(case when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added \\* \\d+ when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added when \\(date_added >= \\d+ and date_added < \\d+\\) then date_added / \\d+ else \\d+ end\\)"));
         sGreylist.add(Pattern.compile(
-                "(?i)\\d+ as orientation"));
+                "MAX\\(case when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified \\* \\d+ when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified when \\(date_modified >= \\d+ and date_modified < \\d+\\) then date_modified / \\d+ else \\d+ end\\)"));
         sGreylist.add(Pattern.compile(
                 "\"content://media/[a-z]+/audio/media\""));
     }
diff --git a/tests/src/com/android/providers/media/MediaProviderTest.java b/tests/src/com/android/providers/media/MediaProviderTest.java
index be1478e..7568767 100644
--- a/tests/src/com/android/providers/media/MediaProviderTest.java
+++ b/tests/src/com/android/providers/media/MediaProviderTest.java
@@ -143,24 +143,66 @@
                 "SELECT secret FROM other_table"));
 
         assertTrue(isGreylistMatch(
-                "COUNT(*)"));
-        assertTrue(isGreylistMatch(
-                "count(*)"));
-        assertFalse(isGreylistMatch(
-                "xCOUNT(*)"));
-
-        assertTrue(isGreylistMatch(
                 "case when case when (date_added >= 157680000 and date_added < 1892160000) then date_added * 1000 when (date_added >= 157680000000 and date_added < 1892160000000) then date_added when (date_added >= 157680000000000 and date_added < 1892160000000000) then date_added / 1000 else 0 end > case when (date_modified >= 157680000 and date_modified < 1892160000) then date_modified * 1000 when (date_modified >= 157680000000 and date_modified < 1892160000000) then date_modified when (date_modified >= 157680000000000 and date_modified < 1892160000000000) then date_modified / 1000 else 0 end then case when (date_added >= 157680000 and date_added < 1892160000) then date_added * 1000 when (date_added >= 157680000000 and date_added < 1892160000000) then date_added when (date_added >= 157680000000000 and date_added < 1892160000000000) then date_added / 1000 else 0 end else case when (date_modified >= 157680000 and date_modified < 1892160000) then date_modified * 1000 when (date_modified >= 157680000000 and date_modified < 1892160000000) then date_modified when (date_modified >= 157680000000000 and date_modified < 1892160000000000) then date_modified / 1000 else 0 end end as corrected_added_modified"));
         assertTrue(isGreylistMatch(
                 "MAX(case when (datetaken >= 157680000 and datetaken < 1892160000) then datetaken * 1000 when (datetaken >= 157680000000 and datetaken < 1892160000000) then datetaken when (datetaken >= 157680000000000 and datetaken < 1892160000000000) then datetaken / 1000 else 0 end)"));
         assertTrue(isGreylistMatch(
-                "MAX(case when (date_added >= 157680000 and date_added < 1892160000) then date_added * 1000 when (date_added >= 157680000000 and date_added < 1892160000000) then date_added when (date_added >= 157680000000000 and date_added < 1892160000000000) then date_added / 1000 else 0 end)"));
-        assertTrue(isGreylistMatch(
                 "0 as orientation"));
         assertTrue(isGreylistMatch(
                 "\"content://media/internal/audio/media\""));
     }
 
+    @Test
+    public void testGreylist_115845887() {
+        assertTrue(isGreylistMatch(
+                "MAX(*)"));
+        assertTrue(isGreylistMatch(
+                "MAX(_id)"));
+
+        assertTrue(isGreylistMatch(
+                "sum(column_name)"));
+        assertFalse(isGreylistMatch(
+                "SUM(foo+bar)"));
+
+        assertTrue(isGreylistMatch(
+                "count(column_name)"));
+        assertFalse(isGreylistMatch(
+                "count(other_table.column_name)"));
+    }
+
+    @Test
+    public void testGreylist_116489751_116135586_116117120_116084561_116074030_116062802() {
+        assertTrue(isGreylistMatch(
+                "MAX(case when (date_added >= 157680000 and date_added < 1892160000) then date_added * 1000 when (date_added >= 157680000000 and date_added < 1892160000000) then date_added when (date_added >= 157680000000000 and date_added < 1892160000000000) then date_added / 1000 else 0 end)"));
+    }
+
+    @Test
+    public void testGreylist_116699470() {
+        assertTrue(isGreylistMatch(
+                "MAX(case when (date_modified >= 157680000 and date_modified < 1892160000) then date_modified * 1000 when (date_modified >= 157680000000 and date_modified < 1892160000000) then date_modified when (date_modified >= 157680000000000 and date_modified < 1892160000000000) then date_modified / 1000 else 0 end)"));
+    }
+
+    @Test
+    public void testGreylist_116531759() {
+        assertTrue(isGreylistMatch(
+                "count(*)"));
+        assertTrue(isGreylistMatch(
+                "COUNT(*)"));
+        assertFalse(isGreylistMatch(
+                "xCOUNT(*)"));
+        assertTrue(isGreylistMatch(
+                "count(*) AS image_count"));
+        assertTrue(isGreylistMatch(
+                "count(_id)"));
+        assertTrue(isGreylistMatch(
+                "count(_id) AS image_count"));
+
+        assertTrue(isGreylistMatch(
+                "column_a AS column_b"));
+        assertFalse(isGreylistMatch(
+                "other_table.column_a AS column_b"));
+    }
+
     private static boolean isGreylistMatch(String raw) {
         for (Pattern p : MediaProvider.sGreylist) {
             if (p.matcher(raw).matches()) {