1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.cts;
18 
19 import android.service.diskstats.DiskStatsAppSizesProto;
20 import android.service.diskstats.DiskStatsCachedValuesProto;
21 import android.service.diskstats.DiskStatsFreeSpaceProto;
22 import android.service.diskstats.DiskStatsServiceDumpProto;
23 import com.android.tradefed.device.ITestDevice;
24 
25 /**
26  * Test proto dump of diskstats
27  */
28 public class DiskStatsProtoTest extends ProtoDumpTestCase {
29     /**
30      * Test that diskstats dump is reasonable
31      *
32      * @throws Exception
33      */
testDump()34     public void testDump() throws Exception {
35         final DiskStatsServiceDumpProto dump = getDump(DiskStatsServiceDumpProto.parser(),
36                 "dumpsys diskstats --proto");
37 
38         verifyDiskStatsServiceDumpProto(dump, PRIVACY_NONE, getDevice());
39     }
40 
verifyDiskStatsServiceDumpProto(DiskStatsServiceDumpProto dump, final int filterLevel, ITestDevice device)41     static void verifyDiskStatsServiceDumpProto(DiskStatsServiceDumpProto dump, final int filterLevel, ITestDevice device) throws Exception {
42         // At least one partition listed
43         assertTrue(dump.getPartitionsFreeSpaceCount() > 0);
44         // Test latency
45         boolean testError = dump.getHasTestError();
46         if (testError) {
47             assertNotNull(dump.getErrorMessage());
48             if (filterLevel == PRIVACY_AUTO) {
49                 assertTrue(dump.getErrorMessage().isEmpty());
50             }
51         } else {
52             assertTrue(dump.getWrite512BLatencyMillis() < 100); // Less than 100ms
53         }
54         assertTrue(dump.getWrite512BLatencyMillis() >= 0); // Non-negative
55         assertTrue(dump.getBenchmarkedWriteSpeedKbps() >= 0); // Non-negative
56 
57         for (DiskStatsFreeSpaceProto dsfs : dump.getPartitionsFreeSpaceList()) {
58             verifyDiskStatsFreeSpaceProto(dsfs);
59         }
60 
61         DiskStatsServiceDumpProto.EncryptionType encryptionType = dump.getEncryption();
62         if ("file".equals(device.getProperty("ro.crypto.type"))) {
63             assertEquals(DiskStatsServiceDumpProto.EncryptionType.ENCRYPTION_FILE_BASED,
64                     encryptionType);
65         }
66         assertTrue(DiskStatsServiceDumpProto.EncryptionType.getDescriptor().getValues()
67                 .contains(encryptionType.getValueDescriptor()));
68 
69         verifyDiskStatsCachedValuesProto(dump.getCachedFolderSizes());
70     }
71 
verifyDiskStatsAppSizesProto(DiskStatsAppSizesProto dsas)72     private static void verifyDiskStatsAppSizesProto(DiskStatsAppSizesProto dsas) throws Exception {
73         assertTrue(dsas.getAppSizeKb() >= 0);
74         assertTrue(dsas.getCacheSizeKb() >= 0);
75         assertTrue(dsas.getAppDataSizeKb() >= 0);
76     }
77 
verifyDiskStatsCachedValuesProto(DiskStatsCachedValuesProto dscv)78     private static void verifyDiskStatsCachedValuesProto(DiskStatsCachedValuesProto dscv) throws Exception {
79         assertTrue(dscv.getAggAppsSizeKb() >= 0);
80         assertTrue(dscv.getAggAppsCacheSizeKb() >= 0);
81         assertTrue(dscv.getPhotosSizeKb() >= 0);
82         assertTrue(dscv.getVideosSizeKb() >= 0);
83         assertTrue(dscv.getAudioSizeKb() >= 0);
84         assertTrue(dscv.getDownloadsSizeKb() >= 0);
85         assertTrue(dscv.getSystemSizeKb() >= 0);
86         assertTrue(dscv.getOtherSizeKb() >= 0);
87         assertTrue(dscv.getAggAppsDataSizeKb() >= 0);
88 
89         for (DiskStatsAppSizesProto dsas : dscv.getAppSizesList()) {
90             verifyDiskStatsAppSizesProto(dsas);
91         }
92     }
93 
verifyDiskStatsFreeSpaceProto(DiskStatsFreeSpaceProto dsfs)94     private static void verifyDiskStatsFreeSpaceProto(DiskStatsFreeSpaceProto dsfs) throws Exception {
95         assertTrue(DiskStatsFreeSpaceProto.Folder.getDescriptor().getValues()
96                 .contains(dsfs.getFolder().getValueDescriptor()));
97         assertTrue(dsfs.getAvailableSpaceKb() >= 0);
98         assertTrue(dsfs.getTotalSpaceKb() >= 0);
99     }
100 }
101