Snap for 11685116 from 48aa9b3d2302bf33eca416a776a3fa42fffaf2d7 to mainline-adservices-release

Change-Id: I1caa9f536e1aeef8429e0c406921f62df4efd66d
diff --git a/tests/kernel_proc_file_api_test/vts_kernel_proc_file_api_test.py b/tests/kernel_proc_file_api_test/vts_kernel_proc_file_api_test.py
index 5cd1686..7e77164 100644
--- a/tests/kernel_proc_file_api_test/vts_kernel_proc_file_api_test.py
+++ b/tests/kernel_proc_file_api_test/vts_kernel_proc_file_api_test.py
@@ -171,13 +171,43 @@
                 uid, uid number.
 
             Returns:
-                list of I/O numbers.
+                list of I/O numbers, can be blank if uid not found.
             """
             stats_path = "/proc/uid_io/stats"
             out, err, r_code = self.dut.shell.Execute(
                     "cat %s | grep '^%d'" % (stats_path, uid))
             return out.split()
 
+        def GetWcharCount(uid, state):
+            """Returns the wchar count (bytes written) for a given uid.
+
+            Args:
+                uid, uid number.
+                state, boolean. Use False for foreground,
+                and True for background.
+
+            Returns:
+                wchar, the number of bytes written by a uid in the given state..
+            """
+            # fg write chars are at index 2, and bg write chars are at 6.
+            wchar_index = 6 if state else 2
+
+            stats = UidIOStats(uid)
+            # UidIOStats() can return a blank line if the entries are not found
+            # so we need to check the length of the return to prevent a list
+            # index out of range exception.
+            arr_len = len(stats)
+
+            # On a properly running system, the output of
+            # 'cat /proc/uid_io/stats | grep ^0'
+            # (which is what UidIOStats() does) results in something that has 11
+            # fields and looks like this:
+            # "0 9006642940 84253078 9751207936 1064480768 0 0 0 0 1048 0"
+            self.assertTrue(arr_len == 11,
+                            "Array len returned by UidIOStats() unexpected: %d" %
+                            arr_len)
+            return int(stats[wchar_index])
+
         def CheckStatsInState(state):
             """Sets VTS (root uid) into a given state and checks the stats.
 
@@ -189,15 +219,14 @@
             filepath = "/proc/uid_procstat/set"
             root_uid = 0
 
-            # fg write chars are at index 2, and bg write chars are at 6.
-            wchar_index = 6 if state else 2
-            old_wchar = UidIOStats(root_uid)[wchar_index]
+            old_wchar = GetWcharCount(root_uid, state)
             self.dut.shell.Execute("echo %d %s > %s" % (root_uid, state, filepath))
             # This should increase the number of write syscalls.
             self.dut.shell.Execute("echo foo")
+            new_wchar = GetWcharCount(root_uid, state)
             self.assertLess(
-                int(old_wchar),
-                int(UidIOStats(root_uid)[wchar_index]),
+                old_wchar,
+                new_wchar,
                 "Number of write syscalls has not increased.")
 
         CheckStatsInState(False)