From 0be777c0eb93dcbc7486e12bceeb51ceeb3a2cd0 Mon Sep 17 00:00:00 2001 From: Xiao Ma Date: Wed, 22 Jun 2022 19:04:54 +0900 Subject: DeviceInfoUtils: add isKernelVersionAtLeast method. Add a method to check if the current kernel version is at least from a given version. Bug: 236783925 Test: atest com.android.testutils.DeviceInfoUtilsTest --iterations Change-Id: If0509b8ccad895c31d9447fee06789540002f666 --- .../src/com/android/testutils/DeviceInfoUtilsTest.java | 14 ++++++++++++++ .../com/android/testutils/DeviceInfoUtils.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java b/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java index 6f603d50..e46dd599 100644 --- a/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java +++ b/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java @@ -106,4 +106,18 @@ public final class DeviceInfoUtilsTest { assertFalse(v1.isAtLeast(v4)); assertFalse(v1.isAtLeast(v5)); } + + @Test + public void testKernelVersionIsAtLeast() { + // Pick a lower kernel version 4.0 which was released at April 2015, the kernel + // version running on all test devices nowadays should be higher than it. + assertTrue(DeviceInfoUtils.isKernelVersionAtLeast("4.0")); + + // Invalid kernel version. + assertTrue(DeviceInfoUtils.isKernelVersionAtLeast("0.0.0")); + + // Pick a higher kernel version which isn't released yet, comparison should return false. + // Need to update the target version in the future to make sure the test still passes. + assertFalse(DeviceInfoUtils.isKernelVersionAtLeast("20.0.0")); + } } diff --git a/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java b/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java index 1925b559..ea89edaa 100644 --- a/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java +++ b/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java @@ -16,6 +16,7 @@ package com.android.testutils; +import android.os.VintfRuntimeInfo; import android.text.TextUtils; import android.util.Pair; @@ -157,4 +158,18 @@ public class DeviceInfoUtils { return new KVersion(0, 0, 0); } } + + /** + * Check if the current kernel version is at least satisfied with the given version. + * + * @param version the start version to compare + * @return return true if the current version is at least satisfied with the given version. + * otherwise, return false. + */ + public static boolean isKernelVersionAtLeast(final String version) { + final String kernelVersion = VintfRuntimeInfo.getKernelVersion(); + final KVersion current = DeviceInfoUtils.getMajorMinorSubminorVersion(kernelVersion); + final KVersion from = DeviceInfoUtils.getMajorMinorSubminorVersion(version); + return current.isAtLeast(from); + } } -- cgit v1.2.3