diff options
| author | Nucca Chen <nuccachen@google.com> | 2022-06-02 02:09:34 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-06-02 02:09:34 +0000 |
| commit | d831f1665e003b0ed8d5d1011f0f645e4d3ba3b3 (patch) | |
| tree | 1271c2ee23e865267089217914b5bf957f7954af | |
| parent | 52bf8f540318a42315e973aabd3501b0a976d157 (diff) | |
| parent | 4e277a4555d87adaa2e3f929d645d76f0c3de596 (diff) | |
Merge changes from topic "cherrypicker-L41500000954817786:N47600001269435138" into tm-dev
* changes:
DeviceInfoUtils: add class and helper for three-part version number
Move DeviceInfoUtilsTest from NetworkStack to frameworks/lib/net
| -rw-r--r-- | common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java | 109 | ||||
| -rw-r--r-- | common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java | 103 |
2 files changed, 209 insertions, 3 deletions
diff --git a/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java b/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java new file mode 100644 index 0000000..6f603d5 --- /dev/null +++ b/common/tests/unit/src/com/android/testutils/DeviceInfoUtilsTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2022 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.testutils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +@SmallTest +public final class DeviceInfoUtilsTest { + /** + * Verifies that version string compare logic returns expected result for various cases. + * Note that only major and minor number are compared. + */ + @Test + public void testMajorMinorVersionCompare() { + assertEquals(0, DeviceInfoUtils.compareMajorMinorVersion("4.8.1", "4.8")); + assertEquals(1, DeviceInfoUtils.compareMajorMinorVersion("4.9", "4.8.1")); + assertEquals(1, DeviceInfoUtils.compareMajorMinorVersion("5.0", "4.8")); + assertEquals(1, DeviceInfoUtils.compareMajorMinorVersion("5", "4.8")); + assertEquals(0, DeviceInfoUtils.compareMajorMinorVersion("5", "5.0")); + assertEquals(1, DeviceInfoUtils.compareMajorMinorVersion("5-beta1", "4.8")); + assertEquals(0, DeviceInfoUtils.compareMajorMinorVersion("4.8.0.0", "4.8")); + assertEquals(0, DeviceInfoUtils.compareMajorMinorVersion("4.8-RC1", "4.8")); + assertEquals(0, DeviceInfoUtils.compareMajorMinorVersion("4.8", "4.8")); + assertEquals(-1, DeviceInfoUtils.compareMajorMinorVersion("3.10", "4.8.0")); + assertEquals(-1, DeviceInfoUtils.compareMajorMinorVersion("4.7.10.10", "4.8")); + } + + @Test + public void testGetMajorMinorSubminorVersion() throws Exception { + final DeviceInfoUtils.KVersion expected = new DeviceInfoUtils.KVersion(4, 19, 220); + assertEquals(expected, DeviceInfoUtils.getMajorMinorSubminorVersion("4.19.220")); + assertEquals(expected, DeviceInfoUtils.getMajorMinorSubminorVersion("4.19.220.50")); + assertEquals(expected, DeviceInfoUtils.getMajorMinorSubminorVersion( + "4.19.220-g500ede0aed22-ab8272303")); + + final DeviceInfoUtils.KVersion expected2 = new DeviceInfoUtils.KVersion(5, 17, 0); + assertEquals(expected2, DeviceInfoUtils.getMajorMinorSubminorVersion("5.17")); + assertEquals(expected2, DeviceInfoUtils.getMajorMinorSubminorVersion("5.17.")); + assertEquals(expected2, DeviceInfoUtils.getMajorMinorSubminorVersion("5.17.beta")); + assertEquals(expected2, DeviceInfoUtils.getMajorMinorSubminorVersion( + "5.17-rc6-g52099515ca00-ab8032400")); + + final DeviceInfoUtils.KVersion invalid = new DeviceInfoUtils.KVersion(0, 0, 0); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("")); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("4")); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("4.")); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("4-beta")); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("1.x.1")); + assertEquals(invalid, DeviceInfoUtils.getMajorMinorSubminorVersion("x.1.1")); + } + + @Test + public void testVersion() throws Exception { + final DeviceInfoUtils.KVersion v1 = new DeviceInfoUtils.KVersion(4, 8, 1); + final DeviceInfoUtils.KVersion v2 = new DeviceInfoUtils.KVersion(4, 8, 1); + final DeviceInfoUtils.KVersion v3 = new DeviceInfoUtils.KVersion(4, 8, 2); + final DeviceInfoUtils.KVersion v4 = new DeviceInfoUtils.KVersion(4, 9, 1); + final DeviceInfoUtils.KVersion v5 = new DeviceInfoUtils.KVersion(5, 8, 1); + + assertEquals(v1, v2); + assertNotEquals(v1, v3); + assertNotEquals(v1, v4); + assertNotEquals(v1, v5); + + assertEquals(0, v1.compareTo(v2)); + assertEquals(-1, v1.compareTo(v3)); + assertEquals(1, v3.compareTo(v1)); + assertEquals(-1, v1.compareTo(v4)); + assertEquals(1, v4.compareTo(v1)); + assertEquals(-1, v1.compareTo(v5)); + assertEquals(1, v5.compareTo(v1)); + + assertTrue(v2.isInRange(v1, v5)); + assertTrue(v3.isInRange(v1, v5)); + assertTrue(v4.isInRange(v1, v5)); + assertFalse(v5.isInRange(v1, v5)); + assertFalse(v1.isInRange(v3, v5)); + assertFalse(v5.isInRange(v2, v4)); + + assertTrue(v2.isAtLeast(v1)); + assertTrue(v3.isAtLeast(v1)); + assertTrue(v4.isAtLeast(v1)); + assertTrue(v5.isAtLeast(v1)); + assertFalse(v1.isAtLeast(v3)); + assertFalse(v1.isAtLeast(v4)); + assertFalse(v1.isAtLeast(v5)); + } +} diff --git a/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java b/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java index 69cb5f8..1925b55 100644 --- a/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java +++ b/common/testutils/devicetests/com/android/testutils/DeviceInfoUtils.java @@ -26,7 +26,78 @@ import java.util.regex.Pattern; * Utilities for device information. */ public class DeviceInfoUtils { - private static Pair<Integer, Integer> getVersionFromString(String version) { + /** + * Class for a three-part kernel version number. + */ + public static class KVersion { + public final int major; + public final int minor; + public final int sub; + + public KVersion(int major, int minor, int sub) { + this.major = major; + this.minor = minor; + this.sub = sub; + } + + /** + * Compares with other version numerically. + * + * @param other the other version to compare + * @return the value 0 if this == other; + * a value less than 0 if this < other and + * a value greater than 0 if this > other. + */ + public int compareTo(final KVersion other) { + int res = Integer.compare(this.major, other.major); + if (res == 0) { + res = Integer.compare(this.minor, other.minor); + } + if (res == 0) { + res = Integer.compare(this.sub, other.sub); + } + return res; + } + + /** + * At least satisfied with the given version. + * + * @param from the start version to compare + * @return return true if this version is at least satisfied with the given version. + * otherwise, return false. + */ + public boolean isAtLeast(final KVersion from) { + return compareTo(from) >= 0; + } + + /** + * Falls within the given range [from, to). + * + * @param from the start version to compare + * @param to the end version to compare + * @return return true if this version falls within the given range. + * otherwise, return false. + */ + public boolean isInRange(final KVersion from, final KVersion to) { + return isAtLeast(from) && !isAtLeast(to); + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof KVersion)) return false; + KVersion that = (KVersion) o; + return this.major == that.major + && this.minor == that.minor + && this.sub == that.sub; + } + }; + + /** + * Get a two-part kernel version number (major and minor) from a given string. + * + * TODO: use class KVersion. + */ + private static Pair<Integer, Integer> getMajorMinorVersion(String version) { // Only gets major and minor number of the version string. final Pattern versionPattern = Pattern.compile("^(\\d+)(\\.(\\d+))?.*"); final Matcher m = versionPattern.matcher(version); @@ -49,10 +120,12 @@ public class DeviceInfoUtils { * @return the value 0 if s1 == s2; * a value less than 0 if s1 < s2 and * a value greater than 0 if s1 > s2. + * + * TODO: use class KVersion. */ public static int compareMajorMinorVersion(final String s1, final String s2) { - final Pair<Integer, Integer> v1 = getVersionFromString(s1); - final Pair<Integer, Integer> v2 = getVersionFromString(s2); + final Pair<Integer, Integer> v1 = getMajorMinorVersion(s1); + final Pair<Integer, Integer> v2 = getMajorMinorVersion(s2); if (v1.first == v2.first) { return Integer.compare(v1.second, v2.second); @@ -60,4 +133,28 @@ public class DeviceInfoUtils { return Integer.compare(v1.first, v2.first); } } + + /** + * Get a three-part kernel version number (major, minor and subminor) from a given string. + * Any version string must at least have major and minor number. If the subminor number can't + * be parsed from string. Assign zero as subminor number. Invalid version is treated as + * version 0.0.0. + */ + public static KVersion getMajorMinorSubminorVersion(final String version) { + // The kernel version is a three-part version number (major, minor and subminor). Get + // the three-part version numbers and discard the remaining stuff if any. + // For example: + // 4.19.220-g500ede0aed22-ab8272303 --> 4.19.220 + // 5.17-rc6-g52099515ca00-ab8032400 --> 5.17.0 + final Pattern versionPattern = Pattern.compile("^(\\d+)\\.(\\d+)(\\.(\\d+))?.*"); + final Matcher m = versionPattern.matcher(version); + if (m.matches()) { + final int major = Integer.parseInt(m.group(1)); + final int minor = Integer.parseInt(m.group(2)); + final int sub = TextUtils.isEmpty(m.group(4)) ? 0 : Integer.parseInt(m.group(4)); + return new KVersion(major, minor, sub); + } else { + return new KVersion(0, 0, 0); + } + } } |
