Don't allow writable files in /sys

Change-Id: Idce7a0eefc8ce8271bf58693ea39820bbcc6117e
diff --git a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
index 7b3a78e..3cbb362 100644
--- a/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/FileSystemPermissionTest.java
@@ -200,6 +200,47 @@
         }
     }
 
+    public void testAllFilesInSysAreNotWritable() throws Exception {
+        assertAllFilesInDirAndSubDirAreNotWritable(new File("/sys"));
+    }
+
+    private static void
+    assertAllFilesInDirAndSubDirAreNotWritable(File dir) throws Exception {
+        assertTrue(dir.isDirectory());
+
+        if (isSymbolicLink(dir)) {
+            // don't examine symbolic links.
+            return;
+        }
+
+        File[] subDirectories = dir.listFiles(new FileFilter() {
+            @Override public boolean accept(File pathname) {
+                return pathname.isDirectory();
+            }
+        });
+
+
+        /* recurse into subdirectories */
+        if (subDirectories != null) {
+            for (File f : subDirectories) {
+                assertAllFilesInDirAndSubDirAreNotWritable(f);
+            }
+        }
+
+        File[] filesInThisDirectory = dir.listFiles(new FileFilter() {
+            @Override public boolean accept(File pathname) {
+                return pathname.isFile();
+            }
+        });
+        if (filesInThisDirectory == null) {
+            return;
+        }
+
+        for (File f: filesInThisDirectory) {
+            assertFalse(f.getCanonicalPath(), f.canWrite());
+        }
+    }
+
     public void testAllBlockDevicesAreNotReadableWritable() throws Exception {
         assertBlockDevicesInDirAndSubDirAreNotWritable(new File("/dev"));
     }
@@ -242,8 +283,8 @@
             return;
         }
 
-        if (!dir.getAbsolutePath().equals(dir.getCanonicalPath())) {
-            // don't follow symbolic links.
+        if (isSymbolicLink(dir)) {
+            // don't examine symbolic links.
             return;
         }
 
@@ -266,4 +307,8 @@
             assertDirectoryAndSubdirectoriesNotWritable(f);
         }
     }
+
+    private static boolean isSymbolicLink(File f) throws IOException {
+        return !f.getAbsolutePath().equals(f.getCanonicalPath());
+    }
 }