1 /*
2  * Copyright (C) 2020 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.rollback.host;
18 
19 import com.android.tradefed.device.ITestDevice;
20 import com.google.common.truth.FailureMetadata;
21 import com.google.common.truth.Truth;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 public class WatchdogEventLogger {
26     private static final String[] ROLLBACK_EVENT_TYPES = {
27             "ROLLBACK_INITIATE", "ROLLBACK_BOOT_TRIGGERED", "ROLLBACK_SUCCESS"};
28     private static final String[] ROLLBACK_EVENT_ATTRS = {
29             "logPackage", "rollbackReason", "failedPackageName"};
30     private static final String PROP_PREFIX = "persist.sys.rollbacktest.";
31 
32     private ITestDevice mDevice;
33 
resetProperties(boolean enabled)34     private void resetProperties(boolean enabled) throws Exception {
35         try {
36             mDevice.enableAdbRoot();
37             assertThat(mDevice.setProperty(
38                     PROP_PREFIX + "enabled", String.valueOf(enabled))).isTrue();
39             for (String type : ROLLBACK_EVENT_TYPES) {
40                 String key = PROP_PREFIX + type;
41                 assertThat(mDevice.setProperty(key, "")).isTrue();
42                 for (String attr : ROLLBACK_EVENT_ATTRS) {
43                     assertThat(mDevice.setProperty(key + "." + attr, "")).isTrue();
44                 }
45             }
46         } finally {
47             mDevice.disableAdbRoot();
48         }
49     }
50 
start(ITestDevice device)51     public void start(ITestDevice device) throws Exception {
52         mDevice = device;
53         resetProperties(true);
54     }
55 
stop()56     public void stop() throws Exception {
57         if (mDevice != null) {
58             resetProperties(false);
59         }
60     }
61 
matchProperty(String type, String attr, String expectedVal)62     private boolean matchProperty(String type, String attr, String expectedVal) throws Exception {
63         String key = PROP_PREFIX + type + "." + attr;
64         String val = mDevice.getProperty(key);
65         return expectedVal == null || expectedVal.equals(val);
66     }
67 
68     /**
69      * Returns whether a Watchdog event has occurred that matches the given criteria.
70      *
71      * Check the value of all non-null parameters against the list of Watchdog events that have
72      * occurred, and return {@code true} if an event exists which matches all criteria.
73      */
watchdogEventOccurred(String type, String logPackage, String rollbackReason, String failedPackageName)74     public boolean watchdogEventOccurred(String type, String logPackage,
75             String rollbackReason, String failedPackageName) throws Exception {
76         return mDevice.getBooleanProperty(PROP_PREFIX + type, false)
77                 && matchProperty(type, "logPackage", logPackage)
78                 && matchProperty(type, "rollbackReason", rollbackReason)
79                 && matchProperty(type, "failedPackageName", failedPackageName);
80     }
81 
82     static class Subject extends com.google.common.truth.Subject {
83         private final WatchdogEventLogger mActual;
84 
Subject(FailureMetadata failureMetadata, WatchdogEventLogger subject)85         private Subject(FailureMetadata failureMetadata, WatchdogEventLogger subject) {
86             super(failureMetadata, subject);
87             mActual = subject;
88         }
89 
90         private static com.google.common.truth.Subject.Factory<Subject,
loggers()91                 WatchdogEventLogger> loggers() {
92             return Subject::new;
93         }
94 
assertThat(WatchdogEventLogger actual)95         static Subject assertThat(WatchdogEventLogger actual) {
96             return Truth.assertAbout(loggers()).that(actual);
97         }
98 
eventOccurred(String type, String logPackage, String rollbackReason, String failedPackageName)99         void eventOccurred(String type, String logPackage, String rollbackReason,
100                 String failedPackageName) throws Exception {
101             check("watchdogEventOccurred(type=%s, logPackage=%s, rollbackReason=%s, "
102                     + "failedPackageName=%s)", type, logPackage, rollbackReason, failedPackageName)
103                     .that(mActual.watchdogEventOccurred(type, logPackage, rollbackReason,
104                             failedPackageName)).isTrue();
105         }
106     }
107 }
108