1 /*
2  * Copyright (C) 2018 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.tests.apex;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.service.runtime.DebugEntryProto;
23 import android.service.runtime.RuntimeServiceInfoProto;
24 
25 import com.android.tradefed.device.DeviceNotAvailableException;
26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
27 import com.android.tradefed.util.CommandResult;
28 
29 import com.google.protobuf.InvalidProtocolBufferException;
30 
31 import org.junit.runner.RunWith;
32 
33 import java.nio.charset.StandardCharsets;
34 import java.util.List;
35 import java.util.Optional;
36 
37 /**
38  * Test to check if Apex can be staged, activated and uninstalled successfully.
39  */
40 @RunWith(DeviceJUnit4ClassRunner.class)
41 public class TimezoneDataHostTest extends ApexE2EBaseHostTest {
42 
43     @Override
additionalCheck()44     public void additionalCheck() {
45         // Check that the device reports the expected information for when the test APEX is
46         // installed.
47         RuntimeServiceInfoProto runtimeServiceInfo = getRuntimeServiceInfoProto();
48         List<DebugEntryProto> debugEntries = runtimeServiceInfo.getDebugEntryList();
49 
50         // Check the status of APK update (can't be present) and time zone data module
51         // (must be present).
52         assertEntryValue(debugEntries, "core_library.timezone.source.data_status", "NOT_FOUND");
53         assertEntryValue(debugEntries, "core_library.timezone.source.tzdata_module_status", "OK");
54 
55         // Check the time zone data module has the expected version for test1_com.android.tzdata.
56         String testDataVersion = "2030a";
57         assertEntryValue(debugEntries, "core_library.timezone.source.tzdata_module_rulesVersion",
58                 testDataVersion);
59 
60         // Check all the libraries are reporting the same version.
61         assertEntryValue(debugEntries, "core_library.timezone.lib.icu4j.tzdb_version",
62                 testDataVersion);
63         assertEntryValue(debugEntries, "core_library.timezone.lib.libcore.tzdb_version",
64                 testDataVersion);
65         assertEntryValue(debugEntries, "core_library.timezone.lib.icu4c.tzdb_version",
66                 testDataVersion);
67     }
68 
getRuntimeServiceInfoProto()69     private RuntimeServiceInfoProto getRuntimeServiceInfoProto() {
70         try {
71             CommandResult commandResult =
72                     getDevice().executeShellV2Command("dumpsys runtime --proto");
73             assertEquals(0, (int) commandResult.getExitCode());
74 
75             String outputString = commandResult.getStdout();
76             byte[] outputBytes = outputString.getBytes(StandardCharsets.US_ASCII);
77             return RuntimeServiceInfoProto.parseFrom(outputBytes);
78         } catch (DeviceNotAvailableException e) {
79             throw new AssertionError("Unable to run dumpsys", e);
80         } catch (InvalidProtocolBufferException e) {
81             throw new AssertionError("Unable to parse dumpsys output", e);
82         }
83     }
84 
assertEntryValue( List<DebugEntryProto> entries, String entryKey, String expectedValue)85     private static void assertEntryValue(
86             List<DebugEntryProto> entries, String entryKey, String expectedValue) {
87         Optional<DebugEntryProto> matchingEntry =
88                 entries.stream().filter(x -> x.getKey().equals(entryKey)).findFirst();
89         assertTrue("DebugEntryProto with key " + entryKey + " not found.",
90                 matchingEntry.isPresent());
91         assertEquals("DebugEntryProto with key " + entryKey + " has bad value",
92                 expectedValue, matchingEntry.get().getStringValue());
93     }
94 }
95