summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2021-09-29 17:59:46 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2021-09-29 17:59:46 +0000
commit9957258452251b5e231e732bdabf473f59965018 (patch)
tree1a5fcb5e733bf4a6c5c525414890e38babb1483e
parent91cb7474d600cf8b1c357c7ff01113cc1f49682b (diff)
parent453098f1c81e0d503ec7cb7eec100dd6c49f8894 (diff)
downloadplatform_testing-9957258452251b5e231e732bdabf473f59965018.tar.gz
Merge changes I9073def2,I922e4eae into sc-dev
* changes: Convert STS to Extra Business Logic Add Extra Business Logic support
-rw-r--r--libraries/compatibility-common-util/Android.bp1
-rw-r--r--libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicExecutor.java6
-rw-r--r--libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicMapStore.java58
-rw-r--r--libraries/compatibility-common-util/src/com/android/compatibility/common/util/DescriptionProvider.java34
-rw-r--r--libraries/compatibility-common-util/src/com/android/compatibility/common/util/MultiLog.java54
-rw-r--r--libraries/compatibility-common-util/src/com/android/sts/OWNERS2
-rw-r--r--libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java40
-rw-r--r--libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java165
8 files changed, 359 insertions, 1 deletions
diff --git a/libraries/compatibility-common-util/Android.bp b/libraries/compatibility-common-util/Android.bp
index bc40510fc..714a681c7 100644
--- a/libraries/compatibility-common-util/Android.bp
+++ b/libraries/compatibility-common-util/Android.bp
@@ -26,6 +26,7 @@ java_library_static {
static_libs: [
"guava",
"junit",
+ "platform-test-annotations",
],
}
diff --git a/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicExecutor.java b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicExecutor.java
index b137801d6..8905fce3a 100644
--- a/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicExecutor.java
+++ b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicExecutor.java
@@ -140,10 +140,14 @@ public abstract class BusinessLogicExecutor {
}
String className = method.substring(0, index);
Class cls = Class.forName(className);
- Object obj = cls.getDeclaredConstructor().newInstance();
+ Object obj = null;
if (getTestObject() != null && cls.isAssignableFrom(getTestObject().getClass())) {
// The given method is a member of the test class, use the known test class instance
obj = getTestObject();
+ } else {
+ // Only instantiate a new object if we don't already have one.
+ // Otherwise the class could have been an interface which isn't instantiatable.
+ obj = cls.getDeclaredConstructor().newInstance();
}
ResolvedMethod rm = getResolvedMethod(cls, method.substring(index + 1), args);
return rm.invoke(obj);
diff --git a/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicMapStore.java b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicMapStore.java
new file mode 100644
index 000000000..d208d392d
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/BusinessLogicMapStore.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.compatibility.common.util;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Business-Logic GCL-accessible utility for key-value stores. */
+public class BusinessLogicMapStore {
+
+ private static Map<String, Map<String, String>> maps = new HashMap<>();
+
+ public boolean hasMap(String mapName) {
+ return maps.containsKey(mapName);
+ }
+
+ public void putMap(String mapName, String separator, String... keyValuePairs) {
+ Map<String, String> map = maps.get(mapName);
+ if (map == null) {
+ map = new HashMap<>();
+ maps.put(mapName, map);
+ }
+
+ for (String keyValuePair : keyValuePairs) {
+ String[] tmp = keyValuePair.split(separator, 2);
+ if (tmp.length != 2) {
+ throw new IllegalArgumentException(
+ "Can't split key-value pair for \"" + keyValuePair + "\"");
+ }
+ String key = tmp[0];
+ String value = tmp[1];
+ map.put(key, value);
+ }
+ }
+
+ public static Map<String, String> getMap(String mapName) {
+ Map<String, String> map = maps.get(mapName);
+ if (map == null) {
+ return null;
+ }
+ return Collections.unmodifiableMap(map);
+ }
+}
diff --git a/libraries/compatibility-common-util/src/com/android/compatibility/common/util/DescriptionProvider.java b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/DescriptionProvider.java
new file mode 100644
index 000000000..2c58fd7a3
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/DescriptionProvider.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.compatibility.common.util;
+
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/** Provide a way for tests to get their description while running. */
+public class DescriptionProvider extends TestWatcher {
+ private volatile Description description;
+
+ @Override
+ protected void starting(Description description) {
+ this.description = description;
+ }
+
+ public Description getDescription() {
+ return description;
+ }
+}
diff --git a/libraries/compatibility-common-util/src/com/android/compatibility/common/util/MultiLog.java b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/MultiLog.java
new file mode 100644
index 000000000..3a63bd4a4
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/compatibility/common/util/MultiLog.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.compatibility.common.util;
+
+/** Provide an interface for logging on host+device-common code. */
+public interface MultiLog {
+
+ /**
+ * Log information with whichever logging mechanism is available to the instance. This varies
+ * from host-side to device-side, so implementations are left to subclasses.
+ * Host: MultiLogHost
+ * Device: MultiLogDevice
+ * See {@link String.format(String, Object...)} for parameter information.
+ */
+ public void logInfo(String logTag, String format, Object... args);
+
+ /**
+ * Log debugging information to the host or device logs (depending on implementation).
+ * See {@link String.format(String, Object...)} for parameter information.
+ * Host: MultiLogHost
+ * Device: MultiLogDevice
+ */
+ public void logDebug(String logTag, String format, Object... args);
+
+ /**
+ * Log warnings to the host or device logs (depending on implementation).
+ * See {@link String.format(String, Object...)} for parameter information.
+ * Host: MultiLogHost
+ * Device: MultiLogDevice
+ */
+ public void logWarn(String logTag, String format, Object... args);
+
+ /**
+ * Log errors to the host or device logs (depending on implementation).
+ * See {@link String.format(String, Object...)} for parameter information.
+ * Host: MultiLogHost
+ * Device: MultiLogDevice
+ */
+ public void logError(String logTag, String format, Object... args);
+}
diff --git a/libraries/compatibility-common-util/src/com/android/sts/OWNERS b/libraries/compatibility-common-util/src/com/android/sts/OWNERS
new file mode 100644
index 000000000..d029d2098
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/sts/OWNERS
@@ -0,0 +1,2 @@
+# STS Owners
+cdombroski@google.com
diff --git a/libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java b/libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java
new file mode 100644
index 000000000..4144fff76
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/sts/common/util/SplUtils.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.sts.common.util;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+
+/** Tools for Security Patch Levels and LocalDates representing them. */
+public final class SplUtils {
+ private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC");
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+ public static LocalDate localDateFromMillis(long millis) {
+ return Instant.ofEpochMilli(millis).atZone(UTC_ZONE_ID).toLocalDate();
+ }
+
+ public static LocalDate localDateFromSplString(String spl) {
+ return LocalDate.parse(spl, formatter);
+ }
+
+ public static String format(LocalDate date) {
+ return date.format(formatter);
+ }
+}
diff --git a/libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java b/libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java
new file mode 100644
index 000000000..1dec855c2
--- /dev/null
+++ b/libraries/compatibility-common-util/src/com/android/sts/common/util/StsLogic.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.sts.common.util;
+
+import static org.junit.Assume.*;
+
+import android.platform.test.annotations.AsbSecurityTest;
+
+import com.android.compatibility.common.util.BusinessLogicMapStore;
+import com.android.compatibility.common.util.MultiLog;
+
+import org.junit.runner.Description;
+
+import java.time.LocalDate;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/** Common STS extra business logic for host-side and device-side to implement. */
+public interface StsLogic extends MultiLog {
+
+ static final String LOG_TAG = StsLogic.class.getSimpleName();
+
+ // keep in sync with google3:
+ // //wireless/android/partner/apbs/*/config/xtsbgusinesslogic/sts_business_logic.gcl
+ List<String> STS_EXTRA_BUSINESS_LOGIC_FULL = Arrays.asList(new String[]{
+ "uploadSpl",
+ "uploadModificationTime",
+ });
+ List<String> STS_EXTRA_BUSINESS_LOGIC_INCREMENTAL = Arrays.asList(new String[]{
+ "uploadSpl",
+ "uploadModificationTime",
+ "incremental",
+ });
+
+ Description getTestDescription();
+
+ LocalDate getDeviceSpl();
+
+ default long[] getCveBugIds() {
+ AsbSecurityTest annotation = getTestDescription().getAnnotation(AsbSecurityTest.class);
+ if (annotation == null) {
+ return null;
+ }
+ return annotation.cveBugId();
+ }
+
+ default LocalDate getMinTestSpl() {
+ Map<String, String> map = BusinessLogicMapStore.getMap("security_bulletins");
+ if (map == null) {
+ throw new IllegalArgumentException("Could not find the security bulletin map");
+ }
+ LocalDate minSpl = null;
+ for (long cveBugId : getCveBugIds()) {
+ String splString = map.get(Long.toString(cveBugId));
+ if (splString == null) {
+ // This bug id wasn't found in the map.
+ // This is a new test or the bug was removed from the bulletin and this is an old
+ // binary. Neither is a critical issue and the test will run in these cases.
+ // New test: developer should be able to write the test without getting blocked.
+ // Removed bug + old binary: test will run.
+ logInfo(LOG_TAG, "could not find the CVE bug %d in the spl map", cveBugId);
+ continue;
+ }
+ LocalDate spl = SplUtils.localDateFromSplString(splString);
+ if (minSpl == null) {
+ minSpl = spl;
+ } else if (spl.isBefore(minSpl)) {
+ minSpl = spl;
+ }
+ }
+ return minSpl;
+ }
+
+ default LocalDate getMinModificationDate() {
+ Map<String, String> map = BusinessLogicMapStore.getMap("sts_modification_times");
+ if (map == null) {
+ throw new IllegalArgumentException("Could not find the modification date map");
+ }
+ LocalDate minModificationDate = null;
+ for (long cveBugId : getCveBugIds()) {
+ String modificationMillisString = map.get(Long.toString(cveBugId));
+ if (modificationMillisString == null) {
+ logInfo(LOG_TAG,
+ "Could not find the CVE bug %d in the modification date map", cveBugId);
+ continue;
+ }
+ LocalDate modificationDate =
+ SplUtils.localDateFromMillis(Long.parseLong(modificationMillisString));
+ if (minModificationDate == null) {
+ minModificationDate = modificationDate;
+ } else if (modificationDate.isBefore(minModificationDate)) {
+ minModificationDate = modificationDate;
+ }
+ }
+ return minModificationDate;
+ }
+
+ default boolean shouldSkipIncremental() {
+ logDebug(LOG_TAG, "filtering by incremental");
+
+ long[] bugIds = getCveBugIds();
+ if (bugIds == null) {
+ // There were no @AsbSecurityTest annotations
+ logInfo(LOG_TAG, "not an ASB test");
+ return false;
+ }
+
+ // check if test spl is older than the past 6 months from the device spl
+ LocalDate deviceSpl = getDeviceSpl();
+ LocalDate incrementalCutoffSpl = deviceSpl.plusMonths(-6);
+
+ LocalDate minTestModifiedDate = getMinModificationDate();
+ if (minTestModifiedDate == null) {
+ // could not get the modification date - run the test
+ if (Arrays.stream(bugIds).min().getAsLong() < 157905780) {
+ // skip if the bug id is older than ~ June 2020
+ // otherwise the test will run due to missing data
+ logDebug(LOG_TAG, "no data for this old test");
+ return true;
+ }
+ return false;
+ }
+ if (minTestModifiedDate.isAfter(incrementalCutoffSpl)) {
+ logDebug(LOG_TAG, "the test was recently modified");
+ return false;
+ }
+
+ LocalDate minTestSpl = getMinTestSpl();
+ if (minTestSpl == null) {
+ // could not get the test spl - run the test
+ logWarn(LOG_TAG, "could not get the test SPL");
+ return false;
+ }
+ if (minTestSpl.isAfter(incrementalCutoffSpl)) {
+ logDebug(LOG_TAG, "the test has a recent SPL");
+ return false;
+ }
+
+ logDebug(LOG_TAG, "test should skip");
+ return true;
+ }
+
+ default boolean shouldSkipSpl() {
+ return true;
+ }
+
+ default void skip(String message) {
+ assumeTrue(message, false);
+ }
+}