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